freedombone-backup-local 21KB

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