freedombone-backup-local 11KB

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