freedombone-backup-local 13KB

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