freedombone-backup-local 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Backup to local storage - typically a USB drive
  12. # License
  13. # =======
  14. #
  15. # Copyright (C) 2015 Bob Mottram <bob@robotics.uk.to>
  16. #
  17. # This program is free software: you can redistribute it and/or modify
  18. # it under the terms of the GNU General Public License as published by
  19. # the Free Software Foundation, either version 3 of the License, or
  20. # (at your option) any later version.
  21. #
  22. # This program is distributed in the hope that it will be useful,
  23. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. # GNU General Public License for more details.
  26. #
  27. # You should have received a copy of the GNU General Public License
  28. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. PROJECT_NAME='freedombone'
  30. COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
  31. export TEXTDOMAIN=${PROJECT_NAME}-backup-local
  32. export TEXTDOMAINDIR="/usr/share/locale"
  33. USB_DRIVE=/dev/sdb1
  34. USB_MOUNT=/mnt/usb
  35. ADMIN_USERNAME=
  36. ADMIN_NAME=
  37. DATABASE_PASSWORD=''
  38. if [ -f /root/dbpass ]; then
  39. DATABASE_PASSWORD=$(cat /root/dbpass)
  40. fi
  41. MICROBLOG_DOMAIN_NAME='microblog'
  42. if grep -q "GNU Social domain" $COMPLETION_FILE; then
  43. MICROBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "GNU Social domain" | awk -F ':' '{print $2}')
  44. fi
  45. HUBZILLA_DOMAIN_NAME='hubzilla'
  46. if grep -q "Hubzilla domain" $COMPLETION_FILE; then
  47. HUBZILLA_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Hubzilla domain" | awk -F ':' '{print $2}')
  48. fi
  49. FULLBLOG_DOMAIN_NAME='blog'
  50. if grep -q "Blog domain" $COMPLETION_FILE; then
  51. FULLBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Blog domain" | awk -F ':' '{print $2}')
  52. fi
  53. function mount_drive {
  54. if [ $1 ]; then
  55. USB_DRIVE=/dev/${1}1
  56. fi
  57. # get the admin user
  58. ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
  59. if [ $2 ]; then
  60. ADMIN_USERNAME=$2
  61. fi
  62. ADMIN_NAME=$(getent passwd $ADMIN_USERNAME | cut -d: -f5 | cut -d, -f1)
  63. # check that the backup destination is available
  64. if [ ! -b $USB_DRIVE ]; then
  65. echo $"Please attach a USB drive"
  66. exit 1
  67. fi
  68. # unmount if already mounted
  69. umount -f $USB_MOUNT
  70. if [ ! -d $USB_MOUNT ]; then
  71. mkdir $USB_MOUNT
  72. fi
  73. if [ -f /dev/mapper/encrypted_usb ]; then
  74. rm -rf /dev/mapper/encrypted_usb
  75. fi
  76. cryptsetup luksClose encrypted_usb
  77. # mount the encrypted backup drive
  78. cryptsetup luksOpen $USB_DRIVE encrypted_usb
  79. if [ "$?" = "0" ]; then
  80. USB_DRIVE=/dev/mapper/encrypted_usb
  81. fi
  82. mount $USB_DRIVE $USB_MOUNT
  83. if [ ! "$?" = "0" ]; then
  84. echo $"There was a problem mounting the USB drive to $USB_MOUNT"
  85. rm -rf $USB_MOUNT
  86. exit 2
  87. fi
  88. }
  89. function unmount_drive {
  90. sync
  91. umount $USB_MOUNT
  92. if [ ! "$?" = "0" ]; then
  93. echo $"Unable to unmount the drive. This means that the backup did not work"
  94. rm -rf $USB_MOUNT
  95. exit 9
  96. fi
  97. rm -rf $USB_MOUNT
  98. if [[ $USB_DRIVE == /dev/mapper/encrypted_usb ]]; then
  99. echo $"Unmount encrypted USB"
  100. cryptsetup luksClose encrypted_usb
  101. fi
  102. if [ -f /dev/mapper/encrypted_usb ]; then
  103. rm -rf /dev/mapper/encrypted_usb
  104. fi
  105. echo $"Backup to USB drive is complete. You can now unplug it."
  106. }
  107. function backup_database {
  108. if [ ${#DATABASE_PASSWORD} -lt 2 ]; then
  109. echo $"No MariaDB password was given"
  110. exit 10
  111. fi
  112. if [ ! -d $USB_MOUNT/backup/${1} ]; then
  113. mkdir -p $USB_MOUNT/backup/${1}
  114. fi
  115. if [ ! -d $USB_MOUNT/backup/${1}data ]; then
  116. mkdir -p $USB_MOUNT/backup/${1}data
  117. fi
  118. if [ ! -d /root/temp${1}data ]; then
  119. mkdir -p /root/temp${1}data
  120. fi
  121. echo $"Obtaining ${1} database backup"
  122. mysqldump --password="$DATABASE_PASSWORD" ${1} > /root/temp${1}data/${1}.sql
  123. if [ ! -s /root/temp${1}data/${1}.sql ]; then
  124. echo $"${1} database could not be saved"
  125. shred -zu /root/temp${1}data/*
  126. rm -rf /root/temp${1}data
  127. umount $USB_MOUNT
  128. rm -rf $USB_MOUNT
  129. exit 5
  130. fi
  131. }
  132. function backup_directory_to_usb {
  133. if [ ! -d ${1} ]; then
  134. echo $"WARNING: directory does not exist: ${1}"
  135. else
  136. BACKUP_KEY_EXISTS=$(gpg --list-keys "$ADMIN_NAME (backup key)")
  137. if [ ! "$?" = "0" ]; then
  138. echo $"Backup key could not be found"
  139. exit 6
  140. fi
  141. MY_BACKUP_KEY_ID=$(gpg --list-keys "$ADMIN_NAME (backup key)" | grep 'pub ' | awk -F ' ' '{print $2}' | awk -F '/' '{print $2}')
  142. if [ ! -d $USB_MOUNT/backup/${2} ]; then
  143. mkdir -p $USB_MOUNT/backup/${2}
  144. fi
  145. obnam force-lock -r $USB_MOUNT/backup/${2} --encrypt-with $MY_BACKUP_KEY_ID ${1}
  146. obnam backup -r $USB_MOUNT/backup/${2} --encrypt-with $MY_BACKUP_KEY_ID ${1}
  147. obnam forget --keep=30d -r $USB_MOUNT/backup/${2} --encrypt-with $MY_BACKUP_KEY_ID
  148. if [ ! "$?" = "0" ]; then
  149. umount $USB_MOUNT
  150. rm -rf $USB_MOUNT
  151. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  152. shred -zu ${1}/*
  153. rm -rf ${1}
  154. fi
  155. exit 7
  156. fi
  157. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  158. shred -zu ${1}/*
  159. rm -rf ${1}
  160. fi
  161. fi
  162. }
  163. function make_backup_directory {
  164. # make a backup directory on the drive
  165. if [ ! -d $USB_MOUNT/backup ]; then
  166. mkdir $USB_MOUNT/backup
  167. fi
  168. if [ ! -d $USB_MOUNT/backup ]; then
  169. echo $"There was a problem making the directory $USB_MOUNT/backup."
  170. umount $USB_MOUNT
  171. rm -rf $USB_MOUNT
  172. exit 3
  173. fi
  174. }
  175. function check_storage_space_remaining {
  176. # Check space remaining on the usb drive
  177. used_percent=$(df -k $USB_MOUNT | tail -n 1 | awk -F ' ' '{print $5}' | awk -F '%' '{print $1}')
  178. if [ $used_percent -gt 95 ]; then
  179. echo $"Less than 5% of space remaining on backup drive"
  180. umount $USB_MOUNT
  181. rm -rf $USB_MOUNT
  182. exit 4
  183. fi
  184. }
  185. function backup_users {
  186. # Backup user files
  187. for d in /home/*/ ; do
  188. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  189. if [[ $USERNAME != "git" ]]; then
  190. # Backup any gpg keys
  191. if [ -d /home/$USERNAME/.gnupg ]; then
  192. echo $"Backing up gpg keys for $USERNAME"
  193. backup_directory_to_usb /home/$USERNAME/.gnupg gnupg/$USERNAME
  194. fi
  195. # Backup any personal settings
  196. if [ -d /home/$USERNAME/personal ]; then
  197. echo $"Backing up personal settings for $USERNAME"
  198. backup_directory_to_usb /home/$USERNAME/personal personal/$USERNAME
  199. fi
  200. # Backup ssh keys
  201. if [ -d /home/$USERNAME/.ssh ]; then
  202. echo $"Backing up ssh keys for $USERNAME"
  203. backup_directory_to_usb /home/$USERNAME/.ssh ssh/$USERNAME
  204. fi
  205. # Backup user configs
  206. if [ -d /home/$USERNAME/.config ]; then
  207. echo $"Backing up config files for $USERNAME"
  208. backup_directory_to_usb /home/$USERNAME/.config config/$USERNAME
  209. fi
  210. # Backup mutt
  211. if [ -f /home/$USERNAME/.muttrc ]; then
  212. echo $"Backing up Mutt settings for $USERNAME"
  213. if [ ! -d /home/$USERNAME/tempbackup ]; then
  214. mkdir -p /home/$USERNAME/tempbackup
  215. fi
  216. cp /home/$USERNAME/.muttrc /home/$USERNAME/tempbackup
  217. if [ -f /etc/Muttrc ]; then
  218. cp /etc/Muttrc /home/$USERNAME/tempbackup
  219. fi
  220. backup_directory_to_usb /home/$USERNAME/tempbackup mutt/$USERNAME
  221. fi
  222. # Backup email
  223. if [ -d /home/$USERNAME/Maildir ]; then
  224. echo $"Creating an email archive for $USERNAME"
  225. if [ ! -d /root/tempbackupemail/$USERNAME ]; then
  226. mkdir -p /root/tempbackupemail/$USERNAME
  227. fi
  228. tar -czvf /root/tempbackupemail/$USERNAME/maildir.tar.gz /home/$USERNAME/Maildir
  229. echo $"Backing up emails for $USERNAME"
  230. backup_directory_to_usb /root/tempbackupemail/$USERNAME mail/$USERNAME
  231. fi
  232. # Backup spamassassin
  233. if [ -d /home/$USERNAME/.spamassassin ]; then
  234. echo $"Backing up spamassassin settings for $USERNAME"
  235. backup_directory_to_usb /home/$USERNAME/.spamassassin spamassassin/$USERNAME
  236. fi
  237. # Backup procmail
  238. if [ -f /home/$USERNAME/.procmailrc ]; then
  239. echo $"Backing up procmail settings for $USERNAME"
  240. if [ ! -d /home/$USERNAME/tempbackup ]; then
  241. mkdir -p /home/$USERNAME/tempbackup
  242. fi
  243. cp /home/$USERNAME/.procmailrc /home/$USERNAME/tempbackup
  244. backup_directory_to_usb /home/$USERNAME/tempbackup procmail/$USERNAME
  245. fi
  246. fi
  247. done
  248. }
  249. function backup_directories {
  250. # directories to be backed up (source,dest)
  251. backup_dirs=(
  252. "none, none, /etc/letsencrypt, letsencrypt"
  253. "none, none, /var/lib/dokuwiki, wiki"
  254. "none, none, /etc/dokuwiki, wiki2"
  255. "none, none, /etc/ssl, ssl"
  256. "none, none, /var/spool/mlmmj, mailinglist"
  257. "none, none, /var/lib/prosody, xmpp"
  258. "none, none, /etc/nginx/sites-available, web"
  259. "none, none, /home/$ADMIN_USERNAME/.ipfs, ipfs"
  260. "none, none, /var/cache/minidlna, dlna"
  261. "/etc/owncloud, owncloud, /root/tempownclouddata, ownclouddata"
  262. "none, none, /var/lib/owncloud, owncloud"
  263. "none, none, /etc/owncloud, owncloud2"
  264. "/home/git/go/src/github.com/gogits, gogs, /root/tempgogsdata, gogsdata"
  265. "none, none, /home/git/go/src/github.com/gogits/gogs/custom, gogs"
  266. "none, none, /home/git/gogs-repositories, gogsrepos"
  267. "none, none, /home/git/.ssh, gogsssh"
  268. "none, none, /var/lib/tox-bootstrapd, tox"
  269. "/var/www/${MICROBLOG_DOMAIN_NAME}, gnusocial, /root/tempgnusocialdata, gnusocialdata"
  270. "none, none, /var/www/${MICROBLOG_DOMAIN_NAME}/htdocs, gnusocial"
  271. "/var/www/${HUBZILLA_DOMAIN_NAME}, hubzilla, /root/temphubzilladata, hubzilladata"
  272. "none, none, /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs, hubzilla"
  273. "none, none, /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs, blog"
  274. )
  275. for dr in "${backup_dirs[@]}"
  276. do
  277. # if this directory exists then backup the given database
  278. required_directory=$(echo $dr | awk -F ',' '{print $1}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  279. if [[ $required_directory != "none" ]]; then
  280. if [ -d $required_directory ]; then
  281. database_name=$(echo $dr | awk -F ',' '{print $2}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  282. if [[ $database_name != "none" ]]; then
  283. backup_database $database_name
  284. fi
  285. fi
  286. fi
  287. # if this directory exists then back it up to the given destination
  288. source_directory=$(echo $dr | awk -F ',' '{print $3}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  289. if [ -d $source_directory ]; then
  290. dest_directory=$(echo $dr | awk -F ',' '{print $4}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  291. echo $"Backing up $source_directory to $dest_directory"
  292. backup_directory_to_usb $source_directory $dest_directory
  293. fi
  294. done
  295. }
  296. mount_drive $1 $2
  297. make_backup_directory
  298. check_storage_space_remaining
  299. backup_users
  300. if [ -d /home/git/go/src/github.com/gogits ]; then
  301. mv /home/git/gogs-repositories/*.git /home/git/gogs-repositories/$ADMIN_USERNAME
  302. fi
  303. if [ -d /var/lib/tox-bootstrapd ]; then
  304. cp /etc/tox-bootstrapd.conf /var/lib/tox-bootstrapd
  305. if [ -d /var/lib/tox-bootstrapd/Maildir ]; then
  306. rm -rf /var/lib/tox-bootstrapd/Maildir
  307. fi
  308. fi
  309. backup_directories
  310. # Backup admin user README file
  311. if [ -f /home/$ADMIN_USERNAME/README ]; then
  312. echo $"Backing up README"
  313. if [ ! -d /home/$ADMIN_USERNAME/tempbackup ]; then
  314. mkdir -p /home/$ADMIN_USERNAME/tempbackup
  315. fi
  316. cp -f /home/$ADMIN_USERNAME/README /home/$ADMIN_USERNAME/tempbackup
  317. backup_directory_to_usb /home/$ADMIN_USERNAME/tempbackup readme
  318. fi
  319. # Backup VoIP settings
  320. if [ -f /etc/mumble-server.ini ]; then
  321. echo $"Backing up VoIP settings"
  322. if [ ! -d /root/tempvoipbackup ]; then
  323. mkdir -p /root/tempvoipbackup
  324. fi
  325. cp -f /etc/mumble-server.ini /root/tempvoipbackup
  326. cp -f /var/lib/mumble-server/mumble-server.sqlite /root/tempvoipbackup
  327. cp -f /etc/sipwitch.conf /root/tempvoipbackup
  328. backup_directory_to_usb /root/tempvoipbackup voip
  329. fi
  330. # MariaDB settings
  331. if [ ${#DATABASE_PASSWORD} -gt 1 ]; then
  332. if [ ! -d /root/tempmariadb ]; then
  333. mkdir /root/tempmariadb
  334. fi
  335. mysqldump --password="$DATABASE_PASSWORD" mysql user > /root/tempmariadb/mysql.sql
  336. if [ ! -s /root/tempmariadb/mysql.sql ]; then
  337. echo $"Unable to backup mysql settings"
  338. rm -rf /root/tempmariadb
  339. umount $USB_MOUNT
  340. rm -rf $USB_MOUNT
  341. exit 8
  342. fi
  343. echo "$DATABASE_PASSWORD" > /root/tempmariadb/db
  344. chmod 400 /root/tempmariadb/db
  345. backup_directory_to_usb /root/tempmariadb mariadb
  346. fi
  347. unmount_drive
  348. exit 0