freedombone-backup-local 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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@robotics.uk.to>
  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. BACKUP_EXTRA_DIRECTORIES=/root/backup-extra-dirs.csv
  32. ENABLE_BACKUP_VERIFICATION="no"
  33. export TEXTDOMAIN=${PROJECT_NAME}-backup-local
  34. export TEXTDOMAINDIR="/usr/share/locale"
  35. # include utils which allow function_check and drive mount
  36. UTILS_FILES=/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*
  37. for f in $UTILS_FILES
  38. do
  39. source $f
  40. done
  41. USB_DRIVE=/dev/sdb1
  42. USB_MOUNT=/mnt/usb
  43. # get default USB from config file
  44. CONFIG_FILE=$HOME/${PROJECT_NAME}.cfg
  45. if [ -f $CONFIG_FILE ]; then
  46. if grep -q "USB_DRIVE=" $CONFIG_FILE; then
  47. USB_DRIVE=$(cat $CONFIG_FILE | grep "USB_DRIVE=" | awk -F '=' '{print $2}')
  48. fi
  49. fi
  50. # get the version of Go being used
  51. GO_VERSION=$(cat /usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-go | grep 'GO_VERSION=' | head -n 1 | awk -F '=' '{print $2}')
  52. GVM_HOME=$(cat /usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-go | grep 'GVM_HOME=' | head -n 1 | awk -F '=' '{print $2}')
  53. ADMIN_USERNAME=
  54. ADMIN_NAME=
  55. # The name of a currently suspended site
  56. # Sites are suspended so that verification should work
  57. SUSPENDED_SITE=
  58. DATABASE_PASSWORD=''
  59. if [ -f /root/dbpass ]; then
  60. DATABASE_PASSWORD=$(cat /root/dbpass)
  61. fi
  62. function make_backup_directory {
  63. # make a backup directory on the drive
  64. if [ ! -d $USB_MOUNT/backup ]; then
  65. mkdir $USB_MOUNT/backup
  66. fi
  67. if [ ! -d $USB_MOUNT/backup ]; then
  68. echo $"There was a problem making the directory $USB_MOUNT/backup."
  69. umount $USB_MOUNT
  70. rm -rf $USB_MOUNT
  71. exit 3
  72. fi
  73. }
  74. function check_storage_space_remaining {
  75. # Check space remaining on the usb drive
  76. used_percent=$(df -k $USB_MOUNT | tail -n 1 | awk -F ' ' '{print $5}' | awk -F '%' '{print $1}')
  77. if [ $used_percent -gt 95 ]; then
  78. echo $"Less than 5% of space remaining on backup drive"
  79. umount $USB_MOUNT
  80. rm -rf $USB_MOUNT
  81. exit 4
  82. fi
  83. }
  84. function backup_users {
  85. # Backup user files
  86. for d in /home/*/ ; do
  87. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  88. if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" && $USERNAME != "tahoelafs" ]]; then
  89. # Backup any gpg keys
  90. if [ -d /home/$USERNAME/.gnupg ]; then
  91. echo $"Backing up gpg keys for $USERNAME"
  92. backup_directory_to_usb /home/$USERNAME/.gnupg gnupg/$USERNAME
  93. fi
  94. # Backup any personal settings
  95. if [ -d /home/$USERNAME/personal ]; then
  96. echo $"Backing up personal settings for $USERNAME"
  97. backup_directory_to_usb /home/$USERNAME/personal personal/$USERNAME
  98. fi
  99. # Backup ssh keys
  100. if [ -d /home/$USERNAME/.ssh ]; then
  101. echo $"Backing up ssh keys for $USERNAME"
  102. backup_directory_to_usb /home/$USERNAME/.ssh ssh/$USERNAME
  103. fi
  104. # Backup fin database if it exists
  105. if [ -d /home/$USERNAME/.fin ]; then
  106. echo $"Backing up fin files for $USERNAME"
  107. backup_directory_to_usb /home/$USERNAME/.fin fin/$USERNAME
  108. fi
  109. # Backup syncthing
  110. if [ -d /home/$USERNAME/Sync ]; then
  111. echo $"Backing up syncthing files for $USERNAME"
  112. backup_directory_to_usb /home/$USERNAME/Sync syncthing/$USERNAME
  113. # ensure that device IDs will be backed up as part of user config settings
  114. if [ ! -d /home/$USERNAME/.config/syncthing ]; then
  115. mkdir -p /home/$USERNAME/.config/syncthing
  116. chown -R $USERNAME:$USERNAME /home/$USERNAME/.config
  117. fi
  118. if [ -f /home/$USERNAME/.syncthing-server-id ]; then
  119. cp /home/$USERNAME/.syncthing-server-id /home/$USERNAME/.config/syncthing
  120. chown -R $USERNAME:$USERNAME /home/$USERNAME/.config
  121. fi
  122. if [ -f /home/$USERNAME/.syncthingids ]; then
  123. cp /home/$USERNAME/.syncthingids /home/$USERNAME/.config/syncthing
  124. chown -R $USERNAME:$USERNAME /home/$USERNAME/.config
  125. fi
  126. fi
  127. # Backup emacs
  128. if [ -d /home/$USERNAME/.emacs.d ]; then
  129. echo $"Backing up Emacs config for $USERNAME"
  130. if [ -f /home/$USERNAME/.emacs ]; then
  131. cp /home/$USERNAME/.emacs /home/$USERNAME/.emacs.d/dotemacs
  132. fi
  133. backup_directory_to_usb /home/$USERNAME/.emacs.d config/$USERNAME
  134. fi
  135. # Backup user configs
  136. if [ -d /home/$USERNAME/.config ]; then
  137. echo $"Backing up config files for $USERNAME"
  138. backup_directory_to_usb /home/$USERNAME/.config config/$USERNAME
  139. fi
  140. # Backup monkeysphere
  141. if [ -d /home/$USERNAME/.monkeysphere ]; then
  142. echo $"Backing up monkeysphere files for $USERNAME"
  143. backup_directory_to_usb /home/$USERNAME/.monkeysphere monkeysphere/$USERNAME
  144. fi
  145. # Backup user local
  146. if [ -d /home/$USERNAME/.local ]; then
  147. echo $"Backing up local files for $USERNAME"
  148. backup_directory_to_usb /home/$USERNAME/.local local/$USERNAME
  149. fi
  150. # Backup mutt
  151. if [ -f /home/$USERNAME/.muttrc ]; then
  152. echo $"Backing up Mutt settings for $USERNAME"
  153. if [ ! -d /home/$USERNAME/tempbackup ]; then
  154. mkdir -p /home/$USERNAME/tempbackup
  155. fi
  156. cp /home/$USERNAME/.muttrc /home/$USERNAME/tempbackup
  157. if [ -f /etc/Muttrc ]; then
  158. cp /etc/Muttrc /home/$USERNAME/tempbackup
  159. fi
  160. backup_directory_to_usb /home/$USERNAME/tempbackup mutt/$USERNAME
  161. fi
  162. # Backup email
  163. if [ -d /home/$USERNAME/Maildir ]; then
  164. echo $"Stopping mail server"
  165. systemctl stop exim4
  166. echo $"Creating an email archive for $USERNAME"
  167. if [ ! -d /root/tempbackupemail/$USERNAME ]; then
  168. mkdir -p /root/tempbackupemail/$USERNAME
  169. fi
  170. tar -czvf /root/tempbackupemail/$USERNAME/maildir.tar.gz /home/$USERNAME/Maildir
  171. echo $"Restarting mail server"
  172. systemctl start exim4
  173. echo $"Backing up emails for $USERNAME"
  174. backup_directory_to_usb /root/tempbackupemail/$USERNAME mail/$USERNAME
  175. fi
  176. # Backup spamassassin
  177. if [ -d /home/$USERNAME/.spamassassin ]; then
  178. echo $"Backing up spamassassin settings for $USERNAME"
  179. backup_directory_to_usb /home/$USERNAME/.spamassassin spamassassin/$USERNAME
  180. fi
  181. # Backup procmail
  182. if [ -f /home/$USERNAME/.procmailrc ]; then
  183. echo $"Backing up procmail settings for $USERNAME"
  184. if [ ! -d /home/$USERNAME/tempbackup ]; then
  185. mkdir -p /home/$USERNAME/tempbackup
  186. fi
  187. cp /home/$USERNAME/.procmailrc /home/$USERNAME/tempbackup
  188. backup_directory_to_usb /home/$USERNAME/tempbackup procmail/$USERNAME
  189. fi
  190. fi
  191. done
  192. }
  193. function backup_directories {
  194. export GVM_ROOT=$GVM_HOME
  195. if [ -d $GVM_ROOT/bin ]; then
  196. cd $GVM_ROOT/bin
  197. [[ -s "$GVM_ROOT/scripts/gvm" ]] && source "$GVM_ROOT/scripts/gvm"
  198. gvm use go${GO_VERSION} --default
  199. systemctl set-environment GOPATH=$GOPATH
  200. fi
  201. # directories to be backed up (source,dest)
  202. backup_dirs=(
  203. "/etc/letsencrypt, letsencrypt"
  204. "/etc/ssl, ssl"
  205. "/var/spool/mlmmj, mailinglist"
  206. "/etc/nginx/sites-available, web"
  207. "/var/lib/tor, tor"
  208. )
  209. for dr in "${backup_dirs[@]}"
  210. do
  211. # if this directory exists then back it up to the given destination
  212. source_directory=$(echo $dr | awk -F ',' '{print $1}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  213. if [ -d $source_directory ]; then
  214. dest_directory=$(echo $dr | awk -F ',' '{print $2}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  215. echo $"Backing up $source_directory to $dest_directory"
  216. backup_directory_to_usb $source_directory $dest_directory
  217. fi
  218. restart_site
  219. done
  220. }
  221. function remove_backup_directory {
  222. if [ $1 ]; then
  223. if [[ $1 == "remove" ]]; then
  224. if [ -d $USB_MOUNT/backup ]; then
  225. rm -rf $USB_MOUNT/backup
  226. echo $'Existing backup directory removed'
  227. backup_unmount_drive
  228. exit 0
  229. fi
  230. fi
  231. fi
  232. }
  233. function prepare_directories {
  234. export GVM_ROOT=$GVM_HOME
  235. if [ -d $GVM_ROOT/bin ]; then
  236. cd $GVM_ROOT/bin
  237. [[ -s "$GVM_ROOT/scripts/gvm" ]] && source "$GVM_ROOT/scripts/gvm"
  238. gvm use go${GO_VERSION} --default
  239. systemctl set-environment GOPATH=$GOPATH
  240. fi
  241. # Some miscellaneous preparation for backing up directories
  242. if [ -d $GOPATH/src/github.com/gogits ]; then
  243. mv /home/git/gogs-repositories/*.git /home/git/gogs-repositories/$ADMIN_USERNAME
  244. fi
  245. if [ -d /var/lib/tox-bootstrapd ]; then
  246. cp /etc/tox-bootstrapd.conf /var/lib/tox-bootstrapd
  247. if [ -d /var/lib/tox-bootstrapd/Maildir ]; then
  248. rm -rf /var/lib/tox-bootstrapd/Maildir
  249. fi
  250. fi
  251. }
  252. function backup_configuration {
  253. echo $"Backing up ${PROJECT_NAME} configuration files"
  254. temp_backup_dir=/root/tempbackupconfig
  255. if [ ! -d $temp_backup_dir ]; then
  256. mkdir -p $temp_backup_dir
  257. fi
  258. cp -f $CONFIG_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. # nginx password hashes
  264. if [ -f /etc/nginx/.htpasswd ]; then
  265. cp -f /etc/nginx/.htpasswd $temp_backup_dir/htpasswd
  266. fi
  267. backup_directory_to_usb $temp_backup_dir config
  268. }
  269. function backup_admin_readme {
  270. if [ -f /home/$ADMIN_USERNAME/README ]; then
  271. echo $"Backing up README"
  272. temp_backup_dir=/home/$ADMIN_USERNAME/tempbackup
  273. if [ ! -d $temp_backup_dir ]; then
  274. mkdir -p $temp_backup_dir
  275. fi
  276. cp -f /home/$ADMIN_USERNAME/README $temp_backup_dir
  277. backup_directory_to_usb $temp_backup_dir readme
  278. fi
  279. }
  280. function backup_mariadb {
  281. if [ ${#DATABASE_PASSWORD} -gt 1 ]; then
  282. temp_backup_dir=/root/tempmariadb
  283. if [ ! -d $temp_backup_dir ]; then
  284. mkdir $temp_backup_dir
  285. fi
  286. mysqldump --lock-tables --password="$DATABASE_PASSWORD" mysql user > $temp_backup_dir/mysql.sql
  287. if [ ! -s $temp_backup_dir/mysql.sql ]; then
  288. echo $"Unable to backup mysql settings"
  289. rm -rf $temp_backup_dir
  290. umount $USB_MOUNT
  291. rm -rf $USB_MOUNT
  292. exit 8
  293. fi
  294. echo "$DATABASE_PASSWORD" > $temp_backup_dir/db
  295. chmod 400 $temp_backup_dir/db
  296. backup_directory_to_usb $temp_backup_dir mariadb
  297. fi
  298. }
  299. function valid_backup_destination {
  300. destination_dir="$1"
  301. is_valid="yes"
  302. if [[ "$destination_dir" == "hubzilla" || \
  303. "$destination_dir" == "hubzilladata" || \
  304. "$destination_dir" == "gogs" || \
  305. "$destination_dir" == "gogsrepos" || \
  306. "$destination_dir" == "gogsssh" || \
  307. "$destination_dir" == "gnusocial" || \
  308. "$destination_dir" == "gnusocialdata" || \
  309. "$destination_dir" == "mariadb" || \
  310. "$destination_dir" == "config" || \
  311. "$destination_dir" == "letsencrypt" || \
  312. "$destination_dir" == "wiki" || \
  313. "$destination_dir" == "wiki2" || \
  314. "$destination_dir" == "xmpp" || \
  315. "$destination_dir" == "ipfs" || \
  316. "$destination_dir" == "dlna" || \
  317. "$destination_dir" == "tox" || \
  318. "$destination_dir" == "ssl" || \
  319. "$destination_dir" == "ttrss" || \
  320. "$destination_dir" == "blog" || \
  321. "$destination_dir" == "syncthingconfig" || \
  322. "$destination_dir" == "syncthingshared" || \
  323. "$destination_dir" == "syncthing" || \
  324. "$destination_dir" == "mediagoblin" || \
  325. "$destination_dir" == "mailinglist" ]]; then
  326. is_valid="no"
  327. fi
  328. echo $is_valid
  329. }
  330. function backup_extra_directories {
  331. if [ ! -f $BACKUP_EXTRA_DIRECTORIES ]; then
  332. return
  333. fi
  334. echo $"Backing up some additional directories"
  335. while read backup_line
  336. do
  337. backup_dir=$(echo "$backup_line" | awk -F ',' '{print $1}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  338. if [ -d "$backup_dir" ]; then
  339. destination_dir=$(echo "$backup_line" | awk -F ',' '{print $2}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  340. if [[ $(valid_backup_destination "$destination_dir") == "yes" ]]; then
  341. backup_directory_to_usb "$backup_dir" "$destination_dir"
  342. else
  343. echo $"WARNING: The backup directory $destination_dir is already used."
  344. echo $"Choose a different destination name for backing up $backup_dir"
  345. fi
  346. else
  347. echo $"WARNING: Directory $backup_dir does not exist"
  348. fi
  349. done <$BACKUP_EXTRA_DIRECTORIES
  350. }
  351. # has the remove option been set ?
  352. remove_option=$2
  353. if [[ $1 == "remove" ]]; then
  354. remove_option=$1
  355. fi
  356. backup_mount_drive $1 $2
  357. remove_backup_directory $remove_option
  358. make_backup_directory
  359. check_storage_space_remaining
  360. backup_users
  361. prepare_directories
  362. backup_directories
  363. backup_apps local
  364. backup_configuration
  365. backup_admin_readme
  366. backup_mariadb
  367. backup_extra_directories
  368. backup_unmount_drive $USB_DRIVE $USB_MOUNT
  369. echo $"Backup to USB drive is complete. You can now unplug it."
  370. exit 0