freedombone-backup-local 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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-2018 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. if [ -d "/home/$USERNAME/.mutt" ]; then
  154. echo $"Backing up Mutt configurations for $USERNAME"
  155. backup_directory_to_usb "/home/$USERNAME/.mutt" "mutt/${USERNAME}configs"
  156. fi
  157. # Backup email
  158. if [ -d "/home/$USERNAME/Maildir" ]; then
  159. echo $"Stopping mail server"
  160. systemctl stop exim4
  161. echo $"Creating an email archive for $USERNAME"
  162. if [ ! -d "/root/tempbackupemail/$USERNAME" ]; then
  163. mkdir -p "/root/tempbackupemail/$USERNAME"
  164. fi
  165. tar -czvf "/root/tempbackupemail/$USERNAME/maildir.tar.gz" "/home/$USERNAME/Maildir"
  166. echo $"Restarting mail server"
  167. systemctl start exim4
  168. echo $"Backing up emails for $USERNAME"
  169. backup_directory_to_usb "/root/tempbackupemail/$USERNAME" "mail/$USERNAME"
  170. fi
  171. # Backup spamassassin
  172. if [ -d "/home/$USERNAME/.spamassassin" ]; then
  173. echo $"Backing up spamassassin settings for $USERNAME"
  174. backup_directory_to_usb "/home/$USERNAME/.spamassassin" "spamassassin/$USERNAME"
  175. fi
  176. # Backup procmail
  177. if [ -f "/home/$USERNAME/.procmailrc" ]; then
  178. echo $"Backing up procmail settings for $USERNAME"
  179. if [ ! -d "/home/$USERNAME/tempbackup" ]; then
  180. mkdir -p "/home/$USERNAME/tempbackup"
  181. fi
  182. cp "/home/$USERNAME/.procmailrc" "/home/$USERNAME/tempbackup"
  183. backup_directory_to_usb "/home/$USERNAME/tempbackup" "procmail/$USERNAME"
  184. fi
  185. gpg_agent_enable "$USERNAME"
  186. fi
  187. done
  188. }
  189. function backup_directories {
  190. # directories to be backed up (source,dest)
  191. backup_dirs=(
  192. "/etc/letsencrypt, letsencrypt"
  193. "/etc/ssl, ssl"
  194. "/var/spool/mlmmj, mailinglist"
  195. "/etc/nginx/sites-available, web"
  196. "/var/lib/tor, tor"
  197. "/root/.passwords, passwordstore"
  198. )
  199. # shellcheck disable=SC2068
  200. for dr in ${backup_dirs[@]}
  201. do
  202. # if this directory exists then back it up to the given destination
  203. source_directory=$(echo "$dr" | awk -F ',' '{print $1}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  204. if [ -d "$source_directory" ]; then
  205. dest_directory=$(echo "$dr" | awk -F ',' '{print $2}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  206. echo $"Backing up $source_directory to $dest_directory"
  207. backup_directory_to_usb "$source_directory" "$dest_directory"
  208. fi
  209. restart_site
  210. done
  211. }
  212. function remove_backup_directory {
  213. if [ "$1" ]; then
  214. if [[ "$1" == "remove" ]]; then
  215. if [ -d $USB_MOUNT/backup ]; then
  216. rm -rf $USB_MOUNT/backup
  217. echo $'Existing backup directory removed'
  218. backup_unmount_drive
  219. exit 0
  220. fi
  221. fi
  222. fi
  223. }
  224. function prepare_directories {
  225. # Some miscellaneous preparation for backing up directories
  226. if [ -d /var/lib/tox-bootstrapd ]; then
  227. cp /etc/tox-bootstrapd.conf /var/lib/tox-bootstrapd
  228. if [ -d /var/lib/tox-bootstrapd/Maildir ]; then
  229. rm -rf /var/lib/tox-bootstrapd/Maildir
  230. fi
  231. fi
  232. }
  233. function backup_blocklist {
  234. if [ ! -f /root/${PROJECT_NAME}-firewall-domains.cfg ]; then
  235. return
  236. fi
  237. echo $"Backing up ${PROJECT_NAME} blocklist"
  238. temp_backup_dir=/root/tempbackupblocklist
  239. if [ ! -d $temp_backup_dir ]; then
  240. mkdir -p $temp_backup_dir
  241. fi
  242. if [ -f "$NODEJS_INSTALLED_APPS_FILE" ]; then
  243. cp -f /root/${PROJECT_NAME}-firewall-domains.cfg $temp_backup_dir
  244. fi
  245. backup_directory_to_usb $temp_backup_dir blocklist
  246. rm -rf $temp_backup_dir
  247. }
  248. function backup_configfiles {
  249. echo $"Backing up ${PROJECT_NAME} configuration files"
  250. temp_backup_dir=/root/tempbackupconfig
  251. if [ ! -d $temp_backup_dir ]; then
  252. mkdir -p $temp_backup_dir
  253. fi
  254. if [ -f "$NODEJS_INSTALLED_APPS_FILE" ]; then
  255. cp -f "$NODEJS_INSTALLED_APPS_FILE" $temp_backup_dir
  256. fi
  257. if [ -f /root/.nostore ]; then
  258. cp -f /root/.nostore $temp_backup_dir
  259. else
  260. if [ -f $temp_backup_dir/.nostore ]; then
  261. rm $temp_backup_dir/.nostore
  262. fi
  263. fi
  264. cp -f "$CONFIGURATION_FILE" $temp_backup_dir
  265. cp -f "$COMPLETION_FILE" $temp_backup_dir
  266. if [ -f $BACKUP_EXTRA_DIRECTORIES ]; then
  267. cp -f $BACKUP_EXTRA_DIRECTORIES $temp_backup_dir
  268. fi
  269. if [ -f "$MONGODB_APPS_FILE" ]; then
  270. cp -f "$MONGODB_APPS_FILE" $temp_backup_dir
  271. fi
  272. # nginx password hashes
  273. if [ -f /etc/nginx/.htpasswd ]; then
  274. cp -f /etc/nginx/.htpasswd $temp_backup_dir/htpasswd
  275. fi
  276. backup_directory_to_usb $temp_backup_dir configfiles
  277. rm -rf $temp_backup_dir
  278. }
  279. function backup_admin_readme {
  280. if [ -f /home/$ADMIN_USERNAME/README ]; then
  281. echo $"Backing up README"
  282. temp_backup_dir=/home/$ADMIN_USERNAME/tempbackup
  283. if [ ! -d $temp_backup_dir ]; then
  284. mkdir -p $temp_backup_dir
  285. fi
  286. cp -f /home/$ADMIN_USERNAME/README $temp_backup_dir
  287. backup_directory_to_usb $temp_backup_dir readme
  288. fi
  289. }
  290. function backup_mariadb {
  291. if [ ${#DATABASE_PASSWORD} -gt 1 ]; then
  292. temp_backup_dir=/root/tempmariadb
  293. if [ ! -d $temp_backup_dir ]; then
  294. mkdir $temp_backup_dir
  295. fi
  296. keep_database_running
  297. mysqldump --lock-tables --password="$DATABASE_PASSWORD" mysql user > $temp_backup_dir/mysql.sql
  298. if [ ! -s $temp_backup_dir/mysql.sql ]; then
  299. echo $"Unable to backup mysql settings"
  300. rm -rf $temp_backup_dir
  301. umount $USB_MOUNT
  302. rm -rf $USB_MOUNT
  303. exit 8
  304. fi
  305. echo "$DATABASE_PASSWORD" > $temp_backup_dir/db
  306. chmod 400 $temp_backup_dir/db
  307. backup_directory_to_usb $temp_backup_dir mariadb
  308. fi
  309. }
  310. function backup_postgresql {
  311. if [ ! -d /etc/postgresql ]; then
  312. return
  313. fi
  314. temp_backup_dir=/root/temppostgresql
  315. if [ ! -d $temp_backup_dir ]; then
  316. mkdir $temp_backup_dir
  317. fi
  318. # shellcheck disable=SC2024
  319. sudo -u postgres pg_dumpall --roles-only > "$temp_backup_dir/postgresql.sql"
  320. if [ ! -s $temp_backup_dir/postgresql.sql ]; then
  321. echo $"Unable to backup postgresql settings"
  322. rm -rf $temp_backup_dir
  323. umount $USB_MOUNT
  324. rm -rf $USB_MOUNT
  325. exit 684365
  326. fi
  327. echo "$DATABASE_PASSWORD" > $temp_backup_dir/db
  328. chmod 400 $temp_backup_dir/db
  329. backup_directory_to_usb $temp_backup_dir postgresql
  330. }
  331. # has the remove option been set ?
  332. remove_option=$2
  333. if [[ $1 == "remove" ]]; then
  334. remove_option=$1
  335. fi
  336. gpg_agent_setup root
  337. backup_mount_drive "$1" "$2"
  338. remove_backup_directory "$remove_option"
  339. make_backup_directory
  340. check_storage_space_remaining
  341. backup_users
  342. prepare_directories
  343. backup_directories
  344. backup_apps local
  345. backup_configfiles
  346. backup_blocklist
  347. backup_admin_readme
  348. backup_mariadb
  349. backup_postgresql
  350. backup_extra_directories local
  351. backup_unmount_drive $USB_DRIVE $USB_MOUNT
  352. echo $"Backup to USB drive is complete. You can now unplug it."
  353. exit 0