freedombone-backup-local 11KB

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