freedombone-backup-local 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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-2016 Bob Mottram <bob@freedombone.net>
  16. #
  17. # This program is free software: you can redistribute it and/or modify
  18. # it under the terms of the GNU Affero 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 Affero General Public License for more details.
  26. #
  27. # You should have received a copy of the GNU Affero 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. CONFIGURATION_FILE=$HOME/${PROJECT_NAME}.cfg
  32. BACKUP_EXTRA_DIRECTORIES=/root/backup-extra-dirs.csv
  33. ENABLE_BACKUP_VERIFICATION="no"
  34. export TEXTDOMAIN=${PROJECT_NAME}-backup-local
  35. export TEXTDOMAINDIR="/usr/share/locale"
  36. PROJECT_INSTALL_DIR=/usr/local/bin
  37. if [ -f /usr/bin/${PROJECT_NAME} ]; then
  38. PROJECT_INSTALL_DIR=/usr/bin
  39. fi
  40. source $PROJECT_INSTALL_DIR/${PROJECT_NAME}-vars
  41. # include utils which allow function_check and drive mount
  42. UTILS_FILES=/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*
  43. for f in $UTILS_FILES
  44. do
  45. source $f
  46. done
  47. USB_DRIVE=/dev/sdb1
  48. USB_MOUNT=/mnt/usb
  49. read_config_param USB_DRIVE
  50. ADMIN_USERNAME=
  51. ADMIN_NAME=
  52. # The name of a currently suspended site
  53. # Sites are suspended so that verification should work
  54. SUSPENDED_SITE=
  55. DATABASE_PASSWORD=''
  56. if [ -f /root/dbpass ]; then
  57. DATABASE_PASSWORD=$(cat /root/dbpass)
  58. fi
  59. function make_backup_directory {
  60. # make a backup directory on the drive
  61. if [ ! -d $USB_MOUNT/backup ]; then
  62. mkdir $USB_MOUNT/backup
  63. fi
  64. if [ ! -d $USB_MOUNT/backup ]; then
  65. echo $"There was a problem making the directory $USB_MOUNT/backup."
  66. umount $USB_MOUNT
  67. rm -rf $USB_MOUNT
  68. exit 3
  69. fi
  70. }
  71. function check_storage_space_remaining {
  72. # Check space remaining on the usb drive
  73. used_percent=$(df -k $USB_MOUNT | tail -n 1 | awk -F ' ' '{print $5}' | awk -F '%' '{print $1}')
  74. if [ $used_percent -gt 95 ]; then
  75. echo $"Less than 5% of space remaining on backup drive"
  76. umount $USB_MOUNT
  77. rm -rf $USB_MOUNT
  78. exit 4
  79. fi
  80. }
  81. function backup_users {
  82. # Backup user files
  83. for d in /home/*/ ; do
  84. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  85. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  86. # Backup any gpg keys
  87. if [ -d /home/$USERNAME/.gnupg ]; then
  88. echo $"Backing up gpg keys for $USERNAME"
  89. backup_directory_to_usb /home/$USERNAME/.gnupg gnupg/$USERNAME
  90. fi
  91. # Backup any personal settings
  92. if [ -d /home/$USERNAME/personal ]; then
  93. echo $"Backing up personal settings for $USERNAME"
  94. backup_directory_to_usb /home/$USERNAME/personal personal/$USERNAME
  95. fi
  96. # Backup ssh keys
  97. if [ -d /home/$USERNAME/.ssh ]; then
  98. echo $"Backing up ssh keys for $USERNAME"
  99. backup_directory_to_usb /home/$USERNAME/.ssh ssh/$USERNAME
  100. fi
  101. # Backup fin database if it exists
  102. if [ -d /home/$USERNAME/.fin ]; then
  103. echo $"Backing up fin files for $USERNAME"
  104. backup_directory_to_usb /home/$USERNAME/.fin fin/$USERNAME
  105. fi
  106. # Backup emacs
  107. if [ -d /home/$USERNAME/.emacs.d ]; then
  108. echo $"Backing up Emacs config for $USERNAME"
  109. if [ -f /home/$USERNAME/.emacs ]; then
  110. cp /home/$USERNAME/.emacs /home/$USERNAME/.emacs.d/dotemacs
  111. fi
  112. backup_directory_to_usb /home/$USERNAME/.emacs.d config/$USERNAME
  113. fi
  114. # Backup user configs
  115. if [ -d /home/$USERNAME/.config ]; then
  116. echo $"Backing up config files for $USERNAME"
  117. backup_directory_to_usb /home/$USERNAME/.config config/$USERNAME
  118. fi
  119. # Backup monkeysphere
  120. if [ -d /home/$USERNAME/.monkeysphere ]; then
  121. echo $"Backing up monkeysphere files for $USERNAME"
  122. backup_directory_to_usb /home/$USERNAME/.monkeysphere monkeysphere/$USERNAME
  123. fi
  124. # Backup user local
  125. if [ -d /home/$USERNAME/.local ]; then
  126. echo $"Backing up local files for $USERNAME"
  127. backup_directory_to_usb /home/$USERNAME/.local local/$USERNAME
  128. fi
  129. # Backup mutt
  130. if [ -f /home/$USERNAME/.muttrc ]; then
  131. echo $"Backing up Mutt settings for $USERNAME"
  132. if [ ! -d /home/$USERNAME/tempbackup ]; then
  133. mkdir -p /home/$USERNAME/tempbackup
  134. fi
  135. cp /home/$USERNAME/.muttrc /home/$USERNAME/tempbackup
  136. if [ -f /etc/Muttrc ]; then
  137. cp /etc/Muttrc /home/$USERNAME/tempbackup
  138. fi
  139. backup_directory_to_usb /home/$USERNAME/tempbackup mutt/$USERNAME
  140. fi
  141. # Backup email
  142. if [ -d /home/$USERNAME/Maildir ]; then
  143. echo $"Stopping mail server"
  144. systemctl stop exim4
  145. echo $"Creating an email archive for $USERNAME"
  146. if [ ! -d /root/tempbackupemail/$USERNAME ]; then
  147. mkdir -p /root/tempbackupemail/$USERNAME
  148. fi
  149. tar -czvf /root/tempbackupemail/$USERNAME/maildir.tar.gz /home/$USERNAME/Maildir
  150. echo $"Restarting mail server"
  151. systemctl start exim4
  152. echo $"Backing up emails for $USERNAME"
  153. backup_directory_to_usb /root/tempbackupemail/$USERNAME mail/$USERNAME
  154. fi
  155. # Backup spamassassin
  156. if [ -d /home/$USERNAME/.spamassassin ]; then
  157. echo $"Backing up spamassassin settings for $USERNAME"
  158. backup_directory_to_usb /home/$USERNAME/.spamassassin spamassassin/$USERNAME
  159. fi
  160. # Backup procmail
  161. if [ -f /home/$USERNAME/.procmailrc ]; then
  162. echo $"Backing up procmail settings for $USERNAME"
  163. if [ ! -d /home/$USERNAME/tempbackup ]; then
  164. mkdir -p /home/$USERNAME/tempbackup
  165. fi
  166. cp /home/$USERNAME/.procmailrc /home/$USERNAME/tempbackup
  167. backup_directory_to_usb /home/$USERNAME/tempbackup procmail/$USERNAME
  168. fi
  169. fi
  170. done
  171. }
  172. function backup_directories {
  173. # directories to be backed up (source,dest)
  174. backup_dirs=(
  175. "/etc/letsencrypt, letsencrypt"
  176. "/etc/ssl, ssl"
  177. "/var/spool/mlmmj, mailinglist"
  178. "/etc/nginx/sites-available, web"
  179. "/var/lib/tor, tor"
  180. )
  181. for dr in "${backup_dirs[@]}"
  182. do
  183. # if this directory exists then back it up to the given destination
  184. source_directory=$(echo $dr | awk -F ',' '{print $1}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  185. if [ -d $source_directory ]; then
  186. dest_directory=$(echo $dr | awk -F ',' '{print $2}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  187. echo $"Backing up $source_directory to $dest_directory"
  188. backup_directory_to_usb $source_directory $dest_directory
  189. fi
  190. restart_site
  191. done
  192. }
  193. function remove_backup_directory {
  194. if [ $1 ]; then
  195. if [[ $1 == "remove" ]]; then
  196. if [ -d $USB_MOUNT/backup ]; then
  197. rm -rf $USB_MOUNT/backup
  198. echo $'Existing backup directory removed'
  199. backup_unmount_drive
  200. exit 0
  201. fi
  202. fi
  203. fi
  204. }
  205. function prepare_directories {
  206. # Some miscellaneous preparation for backing up directories
  207. if [ -d /var/lib/tox-bootstrapd ]; then
  208. cp /etc/tox-bootstrapd.conf /var/lib/tox-bootstrapd
  209. if [ -d /var/lib/tox-bootstrapd/Maildir ]; then
  210. rm -rf /var/lib/tox-bootstrapd/Maildir
  211. fi
  212. fi
  213. }
  214. function backup_configfiles {
  215. echo $"Backing up ${PROJECT_NAME} configuration files"
  216. temp_backup_dir=/root/tempbackupconfig
  217. if [ ! -d $temp_backup_dir ]; then
  218. mkdir -p $temp_backup_dir
  219. fi
  220. if [ -f $NODEJS_INSTALLED_APPS_FILE ]; then
  221. cp -f $NODEJS_INSTALLED_APPS_FILE $temp_backup_dir
  222. fi
  223. cp -f $CONFIGURATION_FILE $temp_backup_dir
  224. cp -f $COMPLETION_FILE $temp_backup_dir
  225. if [ -f $BACKUP_EXTRA_DIRECTORIES ]; then
  226. cp -f $BACKUP_EXTRA_DIRECTORIES $temp_backup_dir
  227. fi
  228. # nginx password hashes
  229. if [ -f /etc/nginx/.htpasswd ]; then
  230. cp -f /etc/nginx/.htpasswd $temp_backup_dir/htpasswd
  231. fi
  232. backup_directory_to_usb $temp_backup_dir configfiles
  233. }
  234. function backup_admin_readme {
  235. if [ -f /home/$ADMIN_USERNAME/README ]; then
  236. echo $"Backing up README"
  237. temp_backup_dir=/home/$ADMIN_USERNAME/tempbackup
  238. if [ ! -d $temp_backup_dir ]; then
  239. mkdir -p $temp_backup_dir
  240. fi
  241. cp -f /home/$ADMIN_USERNAME/README $temp_backup_dir
  242. backup_directory_to_usb $temp_backup_dir readme
  243. fi
  244. }
  245. function backup_mariadb {
  246. if [ ${#DATABASE_PASSWORD} -gt 1 ]; then
  247. temp_backup_dir=/root/tempmariadb
  248. if [ ! -d $temp_backup_dir ]; then
  249. mkdir $temp_backup_dir
  250. fi
  251. mysqldump --lock-tables --password="$DATABASE_PASSWORD" mysql user > $temp_backup_dir/mysql.sql
  252. if [ ! -s $temp_backup_dir/mysql.sql ]; then
  253. echo $"Unable to backup mysql settings"
  254. rm -rf $temp_backup_dir
  255. umount $USB_MOUNT
  256. rm -rf $USB_MOUNT
  257. exit 8
  258. fi
  259. echo "$DATABASE_PASSWORD" > $temp_backup_dir/db
  260. chmod 400 $temp_backup_dir/db
  261. backup_directory_to_usb $temp_backup_dir mariadb
  262. fi
  263. }
  264. # has the remove option been set ?
  265. remove_option=$2
  266. if [[ $1 == "remove" ]]; then
  267. remove_option=$1
  268. fi
  269. backup_mount_drive $1 $2
  270. remove_backup_directory $remove_option
  271. make_backup_directory
  272. check_storage_space_remaining
  273. backup_users
  274. prepare_directories
  275. backup_directories
  276. backup_apps local
  277. backup_configfiles
  278. backup_admin_readme
  279. backup_mariadb
  280. backup_extra_directories local
  281. backup_unmount_drive $USB_DRIVE $USB_MOUNT
  282. echo $"Backup to USB drive is complete. You can now unplug it."
  283. exit 0