freedombone-backup-local 15KB

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