freedombone-backup-local 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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_VERIFICATION="no"
  33. export TEXTDOMAIN=${PROJECT_NAME}-backup-local
  34. export TEXTDOMAINDIR="/usr/share/locale"
  35. USB_DRIVE=/dev/sdb1
  36. USB_MOUNT=/mnt/usb
  37. # get default USB from config file
  38. CONFIG_FILE=$HOME/${PROJECT_NAME}.cfg
  39. if [ -f $CONFIG_FILE ]; then
  40. if grep -q "USB_DRIVE=" $CONFIG_FILE; then
  41. USB_DRIVE=$(cat $CONFIG_FILE | grep "USB_DRIVE=" | awk -F '=' '{print $2}')
  42. fi
  43. fi
  44. # get the version of Go being used
  45. if [ -f /usr/local/bin/${PROJECT_NAME} ]; then
  46. GO_VERSION=$(cat /usr/local/bin/${PROJECT_NAME} | grep 'GO_VERSION=' | head -n 1 | awk -F '=' '{print $2}')
  47. GVM_HOME=$(cat /usr/local/bin/${PROJECT_NAME} | grep 'GVM_HOME=' | head -n 1 | awk -F '=' '{print $2}')
  48. else
  49. GO_VERSION=$(cat /usr/bin/${PROJECT_NAME} | grep 'GO_VERSION=' | head -n 1 | awk -F '=' '{print $2}')
  50. GVM_HOME=$(cat /usr/bin/${PROJECT_NAME} | grep 'GVM_HOME=' | head -n 1 | awk -F '=' '{print $2}')
  51. fi
  52. ADMIN_USERNAME=
  53. ADMIN_NAME=
  54. # The name of a currently suspended site
  55. # Sites are suspended so that verification should work
  56. SUSPENDED_SITE=
  57. DATABASE_PASSWORD=''
  58. if [ -f /root/dbpass ]; then
  59. DATABASE_PASSWORD=$(cat /root/dbpass)
  60. fi
  61. function suspend_site {
  62. # suspends a given website
  63. if [[ $ENABLE_VERIFICATION != "yes" ]]; then
  64. return
  65. fi
  66. SUSPENDED_SITE="$1"
  67. nginx_dissite $SUSPENDED_SITE
  68. service nginx reload
  69. }
  70. function restart_site {
  71. # restarts a given website
  72. if [ ! $SUSPENDED_SITE ]; then
  73. return
  74. fi
  75. nginx_ensite $SUSPENDED_SITE
  76. service nginx reload
  77. SUSPENDED_SITE=
  78. }
  79. function update_domains {
  80. RSS_READER_DOMAIN_NAME='ttrss'
  81. if grep -q "RSS reader domain" $COMPLETION_FILE; then
  82. RSS_READER_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Gogs domain" | awk -F ':' '{print $2}')
  83. fi
  84. GIT_DOMAIN_NAME='gogs'
  85. if grep -q "Gogs domain" $COMPLETION_FILE; then
  86. GIT_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Gogs domain" | awk -F ':' '{print $2}')
  87. fi
  88. MICROBLOG_DOMAIN_NAME='microblog'
  89. if grep -q "GNU Social domain" $COMPLETION_FILE; then
  90. MICROBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "GNU Social domain" | awk -F ':' '{print $2}')
  91. fi
  92. HUBZILLA_DOMAIN_NAME='hubzilla'
  93. if grep -q "Hubzilla domain" $COMPLETION_FILE; then
  94. HUBZILLA_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Hubzilla domain" | awk -F ':' '{print $2}')
  95. fi
  96. FULLBLOG_DOMAIN_NAME='blog'
  97. if grep -q "Blog domain" $COMPLETION_FILE; then
  98. FULLBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Blog domain" | awk -F ':' '{print $2}')
  99. fi
  100. MEDIAGOBLIN_DOMAIN_NAME='mediagoblin'
  101. if grep -q "Mediagoblin domain" $COMPLETION_FILE; then
  102. MEDIAGOBLIN_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Mediagoblin domain" | awk -F ':' '{print $2}')
  103. fi
  104. }
  105. function mount_drive {
  106. if [ $1 ]; then
  107. USB_DRIVE=/dev/${1}1
  108. fi
  109. # get the admin user
  110. ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
  111. if [ $2 ]; then
  112. ADMIN_USERNAME=$2
  113. fi
  114. ADMIN_NAME=$(getent passwd $ADMIN_USERNAME | cut -d: -f5 | cut -d, -f1)
  115. # check that the backup destination is available
  116. if [ ! -b $USB_DRIVE ]; then
  117. echo $"Please attach a USB drive"
  118. exit 1
  119. fi
  120. # unmount if already mounted
  121. umount -f $USB_MOUNT
  122. if [ ! -d $USB_MOUNT ]; then
  123. mkdir $USB_MOUNT
  124. fi
  125. if [ -f /dev/mapper/encrypted_usb ]; then
  126. rm -rf /dev/mapper/encrypted_usb
  127. fi
  128. cryptsetup luksClose encrypted_usb
  129. # mount the encrypted backup drive
  130. cryptsetup luksOpen $USB_DRIVE encrypted_usb
  131. if [ "$?" = "0" ]; then
  132. USB_DRIVE=/dev/mapper/encrypted_usb
  133. fi
  134. mount $USB_DRIVE $USB_MOUNT
  135. if [ ! "$?" = "0" ]; then
  136. echo $"There was a problem mounting the USB drive to $USB_MOUNT"
  137. rm -rf $USB_MOUNT
  138. exit 2
  139. fi
  140. }
  141. function unmount_drive {
  142. sync
  143. umount $USB_MOUNT
  144. if [ ! "$?" = "0" ]; then
  145. echo $"Unable to unmount the drive. This means that the backup did not work"
  146. rm -rf $USB_MOUNT
  147. exit 9
  148. fi
  149. rm -rf $USB_MOUNT
  150. if [[ $USB_DRIVE == /dev/mapper/encrypted_usb ]]; then
  151. echo $"Unmount encrypted USB"
  152. cryptsetup luksClose encrypted_usb
  153. fi
  154. if [ -f /dev/mapper/encrypted_usb ]; then
  155. rm -rf /dev/mapper/encrypted_usb
  156. fi
  157. echo $"Backup to USB drive is complete. You can now unplug it."
  158. }
  159. function backup_database {
  160. if [ ${#DATABASE_PASSWORD} -lt 2 ]; then
  161. echo $"No MariaDB password was given"
  162. restart_site
  163. exit 10
  164. fi
  165. if [ ! -d $USB_MOUNT/backup/${1} ]; then
  166. mkdir -p $USB_MOUNT/backup/${1}
  167. fi
  168. if [ ! -d $USB_MOUNT/backup/${1}data ]; then
  169. mkdir -p $USB_MOUNT/backup/${1}data
  170. fi
  171. if [ ! -d /root/temp${1}data ]; then
  172. mkdir -p /root/temp${1}data
  173. fi
  174. echo $"Obtaining ${1} database backup"
  175. mysqldump --lock-tables --password="$DATABASE_PASSWORD" ${1} > /root/temp${1}data/${1}.sql
  176. if [ ! -s /root/temp${1}data/${1}.sql ]; then
  177. echo $"${1} database could not be saved"
  178. shred -zu /root/temp${1}data/*
  179. rm -rf /root/temp${1}data
  180. umount $USB_MOUNT
  181. rm -rf $USB_MOUNT
  182. restart_site
  183. exit 5
  184. fi
  185. }
  186. function backup_directory_to_usb {
  187. if [ ! -d ${1} ]; then
  188. echo $"WARNING: directory does not exist: ${1}"
  189. else
  190. BACKUP_KEY_EXISTS=$(gpg --list-keys "$ADMIN_NAME (backup key)")
  191. if [ ! "$?" = "0" ]; then
  192. echo $"Backup key could not be found"
  193. restart_site
  194. exit 6
  195. fi
  196. MY_BACKUP_KEY_ID=$(gpg --list-keys "$ADMIN_NAME (backup key)" | grep 'pub ' | awk -F ' ' '{print $2}' | awk -F '/' '{print $2}')
  197. if [ ! -d $USB_MOUNT/backup/${2} ]; then
  198. mkdir -p $USB_MOUNT/backup/${2}
  199. fi
  200. obnam force-lock -r $USB_MOUNT/backup/${2} --encrypt-with $MY_BACKUP_KEY_ID ${1}
  201. obnam backup -r $USB_MOUNT/backup/${2} --encrypt-with $MY_BACKUP_KEY_ID ${1}
  202. if [[ $ENABLE_VERIFICATION == "yes" ]]; then
  203. obnam verify -r $USB_MOUNT/backup/${2} --encrypt-with $MY_BACKUP_KEY_ID ${1}
  204. if [ ! "$?" = "0" ]; then
  205. umount $USB_MOUNT
  206. rm -rf $USB_MOUNT
  207. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  208. shred -zu ${1}/*
  209. rm -rf ${1}
  210. fi
  211. restart_site
  212. exit 71
  213. fi
  214. fi
  215. obnam forget --keep=30d -r $USB_MOUNT/backup/${2} --encrypt-with $MY_BACKUP_KEY_ID
  216. if [ ! "$?" = "0" ]; then
  217. umount $USB_MOUNT
  218. rm -rf $USB_MOUNT
  219. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  220. shred -zu ${1}/*
  221. rm -rf ${1}
  222. fi
  223. restart_site
  224. exit 7
  225. fi
  226. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  227. shred -zu ${1}/*
  228. rm -rf ${1}
  229. fi
  230. fi
  231. }
  232. function make_backup_directory {
  233. # make a backup directory on the drive
  234. if [ ! -d $USB_MOUNT/backup ]; then
  235. mkdir $USB_MOUNT/backup
  236. fi
  237. if [ ! -d $USB_MOUNT/backup ]; then
  238. echo $"There was a problem making the directory $USB_MOUNT/backup."
  239. umount $USB_MOUNT
  240. rm -rf $USB_MOUNT
  241. exit 3
  242. fi
  243. }
  244. function check_storage_space_remaining {
  245. # Check space remaining on the usb drive
  246. used_percent=$(df -k $USB_MOUNT | tail -n 1 | awk -F ' ' '{print $5}' | awk -F '%' '{print $1}')
  247. if [ $used_percent -gt 95 ]; then
  248. echo $"Less than 5% of space remaining on backup drive"
  249. umount $USB_MOUNT
  250. rm -rf $USB_MOUNT
  251. exit 4
  252. fi
  253. }
  254. function backup_users {
  255. # Backup user files
  256. for d in /home/*/ ; do
  257. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  258. if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" ]]; then
  259. # Backup any gpg keys
  260. if [ -d /home/$USERNAME/.gnupg ]; then
  261. echo $"Backing up gpg keys for $USERNAME"
  262. backup_directory_to_usb /home/$USERNAME/.gnupg gnupg/$USERNAME
  263. fi
  264. # Backup any personal settings
  265. if [ -d /home/$USERNAME/personal ]; then
  266. echo $"Backing up personal settings for $USERNAME"
  267. backup_directory_to_usb /home/$USERNAME/personal personal/$USERNAME
  268. fi
  269. # Backup ssh keys
  270. if [ -d /home/$USERNAME/.ssh ]; then
  271. echo $"Backing up ssh keys for $USERNAME"
  272. backup_directory_to_usb /home/$USERNAME/.ssh ssh/$USERNAME
  273. fi
  274. # Backup fin database if it exists
  275. if [ -d /home/$USERNAME/.fin ]; then
  276. echo $"Backing up fin files for $USERNAME"
  277. backup_directory_to_usb /home/$USERNAME/.fin fin/$USERNAME
  278. fi
  279. # Backup syncthing
  280. if [ -d /home/$USERNAME/Sync ]; then
  281. echo $"Backing up syncthing files for $USERNAME"
  282. backup_directory_to_usb /home/$USERNAME/Sync syncthing/$USERNAME
  283. # ensure that device IDs will be backed up as part of user config settings
  284. if [ ! -d /home/$USERNAME/.config/syncthing ]; then
  285. mkdir -p /home/$USERNAME/.config/syncthing
  286. chown -R $USERNAME:$USERNAME /home/$USERNAME/.config
  287. fi
  288. if [ -f /home/$USERNAME/.syncthing-server-id ]; then
  289. cp /home/$USERNAME/.syncthing-server-id /home/$USERNAME/.config/syncthing
  290. chown -R $USERNAME:$USERNAME /home/$USERNAME/.config
  291. fi
  292. if [ -f /home/$USERNAME/.syncthingids ]; then
  293. cp /home/$USERNAME/.syncthingids /home/$USERNAME/.config/syncthing
  294. chown -R $USERNAME:$USERNAME /home/$USERNAME/.config
  295. fi
  296. fi
  297. # Backup user configs
  298. if [ -d /home/$USERNAME/.config ]; then
  299. echo $"Backing up config files for $USERNAME"
  300. backup_directory_to_usb /home/$USERNAME/.config config/$USERNAME
  301. fi
  302. # Backup monkeysphere
  303. if [ -d /home/$USERNAME/.monkeysphere ]; then
  304. echo $"Backing up monkeysphere files for $USERNAME"
  305. backup_directory_to_usb /home/$USERNAME/.monkeysphere monkeysphere/$USERNAME
  306. fi
  307. # Backup user local
  308. if [ -d /home/$USERNAME/.local ]; then
  309. echo $"Backing up local files for $USERNAME"
  310. backup_directory_to_usb /home/$USERNAME/.local local/$USERNAME
  311. fi
  312. # Backup mutt
  313. if [ -f /home/$USERNAME/.muttrc ]; then
  314. echo $"Backing up Mutt settings for $USERNAME"
  315. if [ ! -d /home/$USERNAME/tempbackup ]; then
  316. mkdir -p /home/$USERNAME/tempbackup
  317. fi
  318. cp /home/$USERNAME/.muttrc /home/$USERNAME/tempbackup
  319. if [ -f /etc/Muttrc ]; then
  320. cp /etc/Muttrc /home/$USERNAME/tempbackup
  321. fi
  322. backup_directory_to_usb /home/$USERNAME/tempbackup mutt/$USERNAME
  323. fi
  324. # Backup email
  325. if [ -d /home/$USERNAME/Maildir ]; then
  326. echo $"Stopping mail server"
  327. systemctl stop exim4
  328. echo $"Creating an email archive for $USERNAME"
  329. if [ ! -d /root/tempbackupemail/$USERNAME ]; then
  330. mkdir -p /root/tempbackupemail/$USERNAME
  331. fi
  332. tar -czvf /root/tempbackupemail/$USERNAME/maildir.tar.gz /home/$USERNAME/Maildir
  333. echo $"Restarting mail server"
  334. systemctl start exim4
  335. echo $"Backing up emails for $USERNAME"
  336. backup_directory_to_usb /root/tempbackupemail/$USERNAME mail/$USERNAME
  337. fi
  338. # Backup spamassassin
  339. if [ -d /home/$USERNAME/.spamassassin ]; then
  340. echo $"Backing up spamassassin settings for $USERNAME"
  341. backup_directory_to_usb /home/$USERNAME/.spamassassin spamassassin/$USERNAME
  342. fi
  343. # Backup procmail
  344. if [ -f /home/$USERNAME/.procmailrc ]; then
  345. echo $"Backing up procmail settings for $USERNAME"
  346. if [ ! -d /home/$USERNAME/tempbackup ]; then
  347. mkdir -p /home/$USERNAME/tempbackup
  348. fi
  349. cp /home/$USERNAME/.procmailrc /home/$USERNAME/tempbackup
  350. backup_directory_to_usb /home/$USERNAME/tempbackup procmail/$USERNAME
  351. fi
  352. fi
  353. done
  354. }
  355. function backup_directories {
  356. export GVM_ROOT=$GVM_HOME
  357. if [ -d $GVM_ROOT/bin ]; then
  358. cd $GVM_ROOT/bin
  359. [[ -s "$GVM_ROOT/scripts/gvm" ]] && source "$GVM_ROOT/scripts/gvm"
  360. gvm use go${GO_VERSION} --default
  361. systemctl set-environment GOPATH=$GOPATH
  362. fi
  363. # directories to be backed up (source,dest)
  364. backup_dirs=(
  365. "none, none, /etc/letsencrypt, letsencrypt"
  366. "none, none, /var/lib/dokuwiki, wiki"
  367. "none, none, /etc/dokuwiki, wiki2"
  368. "none, none, /etc/ssl, ssl"
  369. "/etc/share/tt-rss, ttrss, /root/tempttrssdata, ttrss"
  370. "none, none, /var/spool/mlmmj, mailinglist"
  371. "none, none, /var/lib/prosody, xmpp"
  372. "none, none, /etc/nginx/sites-available, web"
  373. "none, none, /home/$ADMIN_USERNAME/.ipfs, ipfs"
  374. "none, none, /var/cache/minidlna, dlna"
  375. "$GOPATH/src/github.com/gogits, gogs, /root/tempgogsdata, gogsdata"
  376. "none, none, $GOPATH/src/github.com/gogits/gogs/custom, gogs"
  377. "none, none, /home/git/gogs-repositories, gogsrepos"
  378. "none, none, /home/git/.ssh, gogsssh"
  379. "none, none, /var/lib/tox-bootstrapd, tox"
  380. "/var/www/${MICROBLOG_DOMAIN_NAME}, gnusocial, /root/tempgnusocialdata, gnusocialdata"
  381. "none, none, /var/www/${MICROBLOG_DOMAIN_NAME}/htdocs, gnusocial"
  382. "none, none, /var/lib/syncthing/SyncShared, syncthingshared"
  383. "none, none, /root/.config/syncthing, syncthingconfig"
  384. "/var/www/${HUBZILLA_DOMAIN_NAME}, hubzilla, /root/temphubzilladata, hubzilladata"
  385. "none, none, /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs, hubzilla"
  386. "none, none, /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs, blog"
  387. "none, none, /var/lib/tor, tor"
  388. "none, none, /var/www/${MEDIAGOBLIN_DOMAIN_NAME}/htdocs, mediagoblin"
  389. )
  390. for dr in "${backup_dirs[@]}"
  391. do
  392. # if this directory exists then backup the given database
  393. required_directory=$(echo $dr | awk -F ',' '{print $1}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  394. database_name=$(echo $dr | awk -F ',' '{print $2}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  395. if [[ "$database_name" == *"hubzilla"* ]]; then
  396. suspend_site ${HUBZILLA_DOMAIN_NAME}
  397. fi
  398. if [[ "$database_name" == *"gnusocial"* ]]; then
  399. suspend_site ${MICROBLOG_DOMAIN_NAME}
  400. fi
  401. if [[ "$database_name" == *"gogs"* ]]; then
  402. suspend_site ${GIT_DOMAIN_NAME}
  403. fi
  404. if [[ "$database_name" == *"ttrss"* ]]; then
  405. suspend_site ${RSS_READER_DOMAIN_NAME}
  406. fi
  407. if [[ $required_directory != "none" ]]; then
  408. if [ -d $required_directory ]; then
  409. if [[ $database_name != "none" ]]; then
  410. backup_database $database_name
  411. fi
  412. fi
  413. fi
  414. # if this directory exists then back it up to the given destination
  415. source_directory=$(echo $dr | awk -F ',' '{print $3}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  416. if [ -d $source_directory ]; then
  417. dest_directory=$(echo $dr | awk -F ',' '{print $4}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  418. echo $"Backing up $source_directory to $dest_directory"
  419. backup_directory_to_usb $source_directory $dest_directory
  420. fi
  421. restart_site
  422. done
  423. }
  424. function remove_backup_directory {
  425. if [ $1 ]; then
  426. if [[ $1 == "remove" ]]; then
  427. if [ -d $USB_MOUNT/backup ]; then
  428. rm -rf $USB_MOUNT/backup
  429. echo $'Existing backup directory removed'
  430. unmount_drive
  431. exit 0
  432. fi
  433. fi
  434. fi
  435. }
  436. function prepare_directories {
  437. export GVM_ROOT=$GVM_HOME
  438. if [ -d $GVM_ROOT/bin ]; then
  439. cd $GVM_ROOT/bin
  440. [[ -s "$GVM_ROOT/scripts/gvm" ]] && source "$GVM_ROOT/scripts/gvm"
  441. gvm use go${GO_VERSION} --default
  442. systemctl set-environment GOPATH=$GOPATH
  443. fi
  444. # Some miscellaneous preparation for backing up directories
  445. if [ -d $GOPATH/src/github.com/gogits ]; then
  446. mv /home/git/gogs-repositories/*.git /home/git/gogs-repositories/$ADMIN_USERNAME
  447. fi
  448. if [ -d /var/lib/tox-bootstrapd ]; then
  449. cp /etc/tox-bootstrapd.conf /var/lib/tox-bootstrapd
  450. if [ -d /var/lib/tox-bootstrapd/Maildir ]; then
  451. rm -rf /var/lib/tox-bootstrapd/Maildir
  452. fi
  453. fi
  454. }
  455. function backup_configuration {
  456. echo $"Backing up ${PROJECT_NAME} configuration files"
  457. if [ ! -d /root/tempbackupconfig ]; then
  458. mkdir -p /root/tempbackupconfig
  459. fi
  460. cp -f $CONFIG_FILE /root/tempbackupconfig
  461. cp -f $COMPLETION_FILE /root/tempbackupconfig
  462. if [ -f $BACKUP_EXTRA_DIRECTORIES ]; then
  463. cp -f $BACKUP_EXTRA_DIRECTORIES /root/tempbackupconfig
  464. fi
  465. # nginx password hashes
  466. if [ -f /etc/nginx/.htpasswd ]; then
  467. cp -f /etc/nginx/.htpasswd /root/tempbackupconfig/htpasswd
  468. fi
  469. backup_directory_to_usb /root/tempbackupconfig config
  470. }
  471. function backup_admin_readme {
  472. if [ -f /home/$ADMIN_USERNAME/README ]; then
  473. echo $"Backing up README"
  474. if [ ! -d /home/$ADMIN_USERNAME/tempbackup ]; then
  475. mkdir -p /home/$ADMIN_USERNAME/tempbackup
  476. fi
  477. cp -f /home/$ADMIN_USERNAME/README /home/$ADMIN_USERNAME/tempbackup
  478. backup_directory_to_usb /home/$ADMIN_USERNAME/tempbackup readme
  479. fi
  480. }
  481. function backup_voip {
  482. if [ -f /etc/mumble-server.ini ]; then
  483. echo $"Backing up VoIP settings"
  484. if [ ! -d /root/tempvoipbackup ]; then
  485. mkdir -p /root/tempvoipbackup
  486. fi
  487. cp -f /etc/mumble-server.ini /root/tempvoipbackup
  488. cp -f /var/lib/mumble-server/mumble-server.sqlite /root/tempvoipbackup
  489. cp -f /etc/sipwitch.conf /root/tempvoipbackup
  490. backup_directory_to_usb /root/tempvoipbackup voip
  491. fi
  492. }
  493. function backup_mariadb {
  494. if [ ${#DATABASE_PASSWORD} -gt 1 ]; then
  495. if [ ! -d /root/tempmariadb ]; then
  496. mkdir /root/tempmariadb
  497. fi
  498. mysqldump --lock-tables --password="$DATABASE_PASSWORD" mysql user > /root/tempmariadb/mysql.sql
  499. if [ ! -s /root/tempmariadb/mysql.sql ]; then
  500. echo $"Unable to backup mysql settings"
  501. rm -rf /root/tempmariadb
  502. umount $USB_MOUNT
  503. rm -rf $USB_MOUNT
  504. exit 8
  505. fi
  506. echo "$DATABASE_PASSWORD" > /root/tempmariadb/db
  507. chmod 400 /root/tempmariadb/db
  508. backup_directory_to_usb /root/tempmariadb mariadb
  509. fi
  510. }
  511. function valid_backup_destination {
  512. destination_dir="$1"
  513. is_valid="yes"
  514. if [[ "$destination_dir" == "hubzilla" || \
  515. "$destination_dir" == "hubzilladata" || \
  516. "$destination_dir" == "gogs" || \
  517. "$destination_dir" == "gogsrepos" || \
  518. "$destination_dir" == "gogsssh" || \
  519. "$destination_dir" == "gnusocial" || \
  520. "$destination_dir" == "gnusocialdata" || \
  521. "$destination_dir" == "mariadb" || \
  522. "$destination_dir" == "config" || \
  523. "$destination_dir" == "letsencrypt" || \
  524. "$destination_dir" == "wiki" || \
  525. "$destination_dir" == "wiki2" || \
  526. "$destination_dir" == "xmpp" || \
  527. "$destination_dir" == "ipfs" || \
  528. "$destination_dir" == "dlna" || \
  529. "$destination_dir" == "tox" || \
  530. "$destination_dir" == "ssl" || \
  531. "$destination_dir" == "ttrss" || \
  532. "$destination_dir" == "blog" || \
  533. "$destination_dir" == "syncthingconfig" || \
  534. "$destination_dir" == "syncthingshared" || \
  535. "$destination_dir" == "syncthing" || \
  536. "$destination_dir" == "mediagoblin" || \
  537. "$destination_dir" == "mailinglist" ]]; then
  538. is_valid="no"
  539. fi
  540. echo $is_valid
  541. }
  542. function backup_extra_directories {
  543. if [ ! -f $BACKUP_EXTRA_DIRECTORIES ]; then
  544. return
  545. fi
  546. echo $"Backing up some additional directories"
  547. while read backup_line
  548. do
  549. backup_dir=$(echo "$backup_line" | awk -F ',' '{print $1}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  550. if [ -d "$backup_dir" ]; then
  551. destination_dir=$(echo "$backup_line" | awk -F ',' '{print $2}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  552. if [[ $(valid_backup_destination "$destination_dir") == "yes" ]]; then
  553. backup_directory_to_usb "$backup_dir" "$destination_dir"
  554. else
  555. echo $"WARNING: The backup directory $destination_dir is already used."
  556. echo $"Choose a different destination name for backing up $backup_dir"
  557. fi
  558. else
  559. echo $"WARNING: Directory $backup_dir does not exist"
  560. fi
  561. done <$BACKUP_EXTRA_DIRECTORIES
  562. }
  563. # has the remove option been set ?
  564. remove_option=$2
  565. if [[ $1 == "remove" ]]; then
  566. remove_option=$1
  567. fi
  568. mount_drive $1 $2
  569. remove_backup_directory $remove_option
  570. make_backup_directory
  571. check_storage_space_remaining
  572. update_domains
  573. backup_users
  574. prepare_directories
  575. backup_directories
  576. backup_configuration
  577. backup_admin_readme
  578. backup_voip
  579. backup_mariadb
  580. backup_extra_directories
  581. unmount_drive
  582. exit 0