freedombone-utils-backup 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Backup functions
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2014-2016 Bob Mottram <bob@freedombone.net>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. # whether a given site is being suspended during backup
  31. SUSPENDED_SITE=
  32. # Dummy password used for the backup key
  33. BACKUP_DUMMY_PASSWORD='backup'
  34. function suspend_site {
  35. # suspends a given website
  36. SUSPENDED_SITE="$1"
  37. nginx_dissite $SUSPENDED_SITE
  38. systemctl reload nginx
  39. }
  40. function restart_site {
  41. # restarts a given website
  42. if [ ! $SUSPENDED_SITE ]; then
  43. return
  44. fi
  45. nginx_ensite $SUSPENDED_SITE
  46. systemctl reload nginx
  47. SUSPENDED_SITE=
  48. }
  49. function configure_backup_key {
  50. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  51. return
  52. fi
  53. apt-get -yq install gnupg
  54. BACKUP_KEY_EXISTS=$(gpg_key_exists "root" "$MY_NAME (backup key)")
  55. if [[ $BACKUP_KEY_EXISTS == "yes" ]]; then
  56. return
  57. fi
  58. # Generate a GPG key for backups
  59. BACKUP_KEY_EXISTS=$(gpg_key_exists "$MY_USERNAME" "$MY_NAME (backup key)")
  60. if [[ $BACKUP_KEY_EXISTS == "no" ]]; then
  61. echo 'Key-Type: eddsa' > /home/$MY_USERNAME/gpg-genkey.conf
  62. echo 'Key-Curve: Ed25519' >> /home/$MY_USERNAME/gpg-genkey.conf
  63. echo 'Subkey-Type: eddsa' >> /home/$MY_USERNAME/gpg-genkey.conf
  64. echo 'Subkey-Curve: Ed25519' >> /home/$MY_USERNAME/gpg-genkey.conf
  65. echo "Name-Real: $MY_NAME" >> /home/$MY_USERNAME/gpg-genkey.conf
  66. echo "Name-Email: $MY_EMAIL_ADDRESS" >> /home/$MY_USERNAME/gpg-genkey.conf
  67. echo "Name-Comment: backup key" >> /home/$MY_USERNAME/gpg-genkey.conf
  68. echo 'Expire-Date: 0' >> /home/$MY_USERNAME/gpg-genkey.conf
  69. cat /home/$MY_USERNAME/gpg-genkey.conf
  70. echo "Passphrase: $BACKUP_DUMMY_PASSWORD" >> /home/$MY_USERNAME/gpg-genkey.conf
  71. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/gpg-genkey.conf
  72. echo $'Backup key does not exist. Creating it.'
  73. su -m root -c "gpg --homedir /home/$MY_USERNAME/.gnupg --batch --full-gen-key /home/$MY_USERNAME/gpg-genkey.conf" - $MY_USERNAME
  74. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.gnupg
  75. shred -zu /home/$MY_USERNAME/gpg-genkey.conf
  76. echo $'Checking that the Backup key was created'
  77. BACKUP_KEY_EXISTS=$(gpg_key_exists "$MY_USERNAME" "$MY_NAME (backup key)")
  78. if [[ $BACKUP_KEY_EXISTS == "no" ]]; then
  79. echo $'Backup key could not be created'
  80. exit 43382
  81. fi
  82. fi
  83. MY_BACKUP_KEY_ID=$(su -c "gpg --list-keys \"$MY_NAME (backup key)\"" - $MY_USERNAME | sed -n '2p' | sed 's/^[ \t]*//')
  84. echo "Backup key: $MY_BACKUP_KEY_ID"
  85. MY_BACKUP_KEY=/home/$MY_USERNAME/backup_key
  86. su -m root -c "gpg --homedir /home/$MY_USERNAME/.gnupg --output ${MY_BACKUP_KEY}_public.asc --armor --export $MY_BACKUP_KEY_ID" - $MY_USERNAME
  87. su -m root -c "echo '$BACKUP_DUMMY_PASSWORD' | gpg --homedir /home/$MY_USERNAME/.gnupg --output ${MY_BACKUP_KEY}_private.asc --armor --batch --passphrase-fd 0 --export-secret-key $MY_BACKUP_KEY_ID" - $MY_USERNAME
  88. if [ ! -f ${MY_BACKUP_KEY}_public.asc ]; then
  89. echo 'Public backup key could not be exported'
  90. exit 36829
  91. fi
  92. if [ ! -f ${MY_BACKUP_KEY}_private.asc ]; then
  93. echo 'Private backup key could not be exported'
  94. exit 29235
  95. fi
  96. # import backup key to root user
  97. gpg --import --import ${MY_BACKUP_KEY}_public.asc
  98. echo '$BACKUP_DUMMY_PASSWORD' | gpg --batch --passphrase-fd 0 --allow-secret-key-import --import ${MY_BACKUP_KEY}_private.asc
  99. shred -zu ${MY_BACKUP_KEY}_public.asc
  100. shred -zu ${MY_BACKUP_KEY}_private.asc
  101. mark_completed $FUNCNAME
  102. }
  103. function backup_to_friends_servers {
  104. # update crontab
  105. echo '#!/bin/bash' > /etc/cron.daily/backuptofriends
  106. echo "if [ -f /usr/local/bin/${PROJECT_NAME}-backup-remote ]; then" >> /etc/cron.daily/backuptofriends
  107. echo " /usr/local/bin/${PROJECT_NAME}-backup-remote" >> /etc/cron.daily/backuptofriends
  108. echo 'else' >> /etc/cron.daily/backuptofriends
  109. echo " /usr/bin/${PROJECT_NAME}-backup-remote" >> /etc/cron.daily/backuptofriends
  110. echo 'fi' >> /etc/cron.daily/backuptofriends
  111. chmod +x /etc/cron.daily/backuptofriends
  112. }
  113. function backup_mount_drive {
  114. if [ $1 ]; then
  115. if [[ "$1" == "/dev/"* ]]; then
  116. USB_DRIVE=$1
  117. else
  118. USB_DRIVE=/dev/${1}1
  119. fi
  120. fi
  121. # get the admin user
  122. ADMIN_USERNAME=$(get_completion_param "Admin user")
  123. if [ $2 ]; then
  124. ADMIN_USERNAME=$2
  125. fi
  126. ADMIN_NAME=$(getent passwd $ADMIN_USERNAME | cut -d: -f5 | cut -d, -f1)
  127. if [ $3 ]; then
  128. RESTORE_APP=$3
  129. fi
  130. # check that the backup destination is available
  131. if [ ! -b $USB_DRIVE ]; then
  132. echo $"Please attach a USB drive"
  133. exit 1
  134. fi
  135. # unmount if already mounted
  136. umount -f $USB_MOUNT
  137. if [ ! -d $USB_MOUNT ]; then
  138. mkdir $USB_MOUNT
  139. fi
  140. if [ -f /dev/mapper/encrypted_usb ]; then
  141. rm -rf /dev/mapper/encrypted_usb
  142. fi
  143. cryptsetup luksClose encrypted_usb
  144. # mount the encrypted backup drive
  145. cryptsetup luksOpen $USB_DRIVE encrypted_usb
  146. if [ "$?" = "0" ]; then
  147. USB_DRIVE=/dev/mapper/encrypted_usb
  148. fi
  149. mount $USB_DRIVE $USB_MOUNT
  150. if [ ! "$?" = "0" ]; then
  151. echo $"There was a problem mounting the USB drive to $USB_MOUNT"
  152. rm -rf $USB_MOUNT
  153. exit 783452
  154. fi
  155. }
  156. function backup_unmount_drive {
  157. if [ $1 ]; then
  158. USB_DRIVE=${1}
  159. if [ $2 ]; then
  160. USB_MOUNT=${2}
  161. fi
  162. fi
  163. sync
  164. umount $USB_MOUNT
  165. if [ ! "$?" = "0" ]; then
  166. echo $"Unable to unmount the drive."
  167. rm -rf $USB_MOUNT
  168. exit 9
  169. fi
  170. rm -rf $USB_MOUNT
  171. if [[ $USB_DRIVE == /dev/mapper/encrypted_usb ]]; then
  172. echo $"Unmount encrypted USB"
  173. cryptsetup luksClose encrypted_usb
  174. fi
  175. if [ -f /dev/mapper/encrypted_usb ]; then
  176. rm -rf /dev/mapper/encrypted_usb
  177. fi
  178. }
  179. function backup_database_local_usb {
  180. if [ ${#DATABASE_PASSWORD} -lt 2 ]; then
  181. echo $"No MariaDB password was given"
  182. function_check restart_site
  183. restart_site
  184. exit 10
  185. fi
  186. if [ ! -d $USB_MOUNT/backup/${1} ]; then
  187. mkdir -p $USB_MOUNT/backup/${1}
  188. fi
  189. if [ ! -d $USB_MOUNT/backup/${1}data ]; then
  190. mkdir -p $USB_MOUNT/backup/${1}data
  191. fi
  192. local_database_dir=/root/temp${1}data
  193. if [ ! -d ${local_database_dir} ]; then
  194. mkdir -p ${local_database_dir}
  195. fi
  196. keep_database_running
  197. echo $"Obtaining ${1} database backup"
  198. mysqldump --lock-tables --password="$DATABASE_PASSWORD" ${1} > ${local_database_dir}/${1}.sql
  199. if [ -f ${local_database_dir}/${1}.sql ]; then
  200. if [ ! -s ${local_database_dir}/${1}.sql ]; then
  201. echo $"${1} database could not be saved"
  202. shred -zu ${local_database_dir}/*
  203. rm -rf ${local_database_dir}
  204. umount $USB_MOUNT
  205. rm -rf $USB_MOUNT
  206. restart_site
  207. exit 6835872
  208. fi
  209. else
  210. echo $"${1} database could not be dumped"
  211. rm -rf ${local_database_dir}
  212. umount $USB_MOUNT
  213. rm -rf $USB_MOUNT
  214. restart_site
  215. exit 738653
  216. fi
  217. echo $"Database dump was created for ${1}"
  218. }
  219. function set_obnam_client_name {
  220. # obnam can backup multiple machines with different domain names to
  221. # a repository. To be able to restore directories from different
  222. # machines we need to enforce a single client name for all backups
  223. echo '[config]' > /etc/obnam.conf
  224. echo "client-name = ${PROJECT_NAME}" >> /etc/obnam.conf
  225. }
  226. function backup_directory_to_usb {
  227. if [ ! -d ${1} ]; then
  228. echo $"WARNING: directory does not exist: ${1}"
  229. else
  230. BACKUP_KEY_EXISTS=$(gpg --list-keys "$ADMIN_NAME (backup key)")
  231. if [ ! "$?" = "0" ]; then
  232. echo $"Backup key could not be found"
  233. function_check restart_site
  234. restart_site
  235. exit 6
  236. fi
  237. MY_BACKUP_KEY_ID=$(gpg --list-keys "$ADMIN_NAME (backup key)" | sed -n '2p' | sed 's/^[ \t]*//')
  238. if [ ! -d $USB_MOUNT/backup/${2} ]; then
  239. mkdir -p $USB_MOUNT/backup/${2}
  240. fi
  241. set_obnam_client_name
  242. echo "$BACKUP_DUMMY_PASSWORD" | obnam force-lock -r $USB_MOUNT/backup/${2} --encrypt-with $MY_BACKUP_KEY_ID ${1}
  243. echo "$BACKUP_DUMMY_PASSWORD" | obnam backup -r $USB_MOUNT/backup/${2} --encrypt-with $MY_BACKUP_KEY_ID ${1}
  244. if [[ $ENABLE_BACKUP_VERIFICATION == "yes" ]]; then
  245. echo "$BACKUP_DUMMY_PASSWORD" | obnam verify -r $USB_MOUNT/backup/${2} --encrypt-with $MY_BACKUP_KEY_ID ${1}
  246. if [ ! "$?" = "0" ]; then
  247. umount $USB_MOUNT
  248. rm -rf $USB_MOUNT
  249. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  250. shred -zu ${1}/*
  251. rm -rf ${1}
  252. fi
  253. function_check restart_site
  254. restart_site
  255. exit 683252
  256. fi
  257. fi
  258. echo "$BACKUP_DUMMY_PASSWORD" | obnam forget --keep=30d -r $USB_MOUNT/backup/${2} --encrypt-with $MY_BACKUP_KEY_ID
  259. if [ ! "$?" = "0" ]; then
  260. umount $USB_MOUNT
  261. rm -rf $USB_MOUNT
  262. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  263. shred -zu ${1}/*
  264. rm -rf ${1}
  265. fi
  266. function_check restart_site
  267. restart_site
  268. exit 7
  269. fi
  270. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  271. shred -zu ${1}/*
  272. rm -rf ${1}
  273. fi
  274. fi
  275. }
  276. function restore_directory_from_usb {
  277. if [ ! ${1} ]; then
  278. echo "obnam restore -r $USB_MOUNT/backup/${2} --to ${1}"
  279. echo $'No restore destination given'
  280. return
  281. fi
  282. if [ ! ${2} ]; then
  283. echo "obnam restore -r $USB_MOUNT/backup/${2} --to ${1}"
  284. echo $'No restore source given'
  285. return
  286. fi
  287. if [ ! -d ${1} ]; then
  288. mkdir ${1}
  289. fi
  290. set_obnam_client_name
  291. echo "$BACKUP_DUMMY_PASSWORD" | obnam restore -r $USB_MOUNT/backup/${2} --to ${1}
  292. }
  293. function restore_directory_from_friend {
  294. if [ ! ${1} ]; then
  295. echo "obnam restore -r $SERVER_DIRECTORY/backup/${2} --to ${1}"
  296. echo $'No restore destination given'
  297. return
  298. fi
  299. if [ ! ${2} ]; then
  300. echo "obnam restore -r $SERVER_DIRECTORY/backup/${2} --to ${1}"
  301. echo $'No restore source given'
  302. return
  303. fi
  304. if [ ! -d ${1} ]; then
  305. mkdir ${1}
  306. fi
  307. set_obnam_client_name
  308. echo "$BACKUP_DUMMY_PASSWORD" | obnam restore -r $SERVER_DIRECTORY/backup/${2} --to ${1}
  309. }
  310. function backup_database_to_usb {
  311. database_name=$1
  312. local_database_dir=/root/temp${1}data
  313. backup_database_local_usb ${database_name}
  314. if [ ! -f ${local_database_dir}/${1}.sql ]; then
  315. echo $"Error backing up ${1} database to ${local_database_dir}/${1}.sql"
  316. exit 62383
  317. fi
  318. backup_directory_to_usb ${local_database_dir} ${database_name}data
  319. }
  320. # after user files have been restored permissions may need to be set
  321. function set_user_permissions {
  322. echo $"Setting permissions"
  323. for d in /home/*/ ; do
  324. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  325. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  326. chown -R $USERNAME:$USERNAME /home/$USERNAME
  327. fi
  328. done
  329. }
  330. function backup_directory_to_friend {
  331. BACKUP_KEY_EXISTS=$(gpg --list-keys "$ADMIN_NAME (backup key)")
  332. if [ ! "$?" = "0" ]; then
  333. echo $"Backup key could not be found"
  334. function_check restart_site
  335. restart_site
  336. exit 43382
  337. fi
  338. ADMIN_BACKUP_KEY_ID=$(gpg --list-keys "$ADMIN_NAME (backup key)" | sed -n '2p' | sed 's/^[ \t]*//')
  339. if [ ! -d $SERVER_DIRECTORY/backup/${2} ]; then
  340. mkdir -p $SERVER_DIRECTORY/backup/${2}
  341. fi
  342. set_obnam_client_name
  343. echo "$BACKUP_DUMMY_PASSWORD" | obnam force-lock -r $SERVER_DIRECTORY/backup/${2} --encrypt-with ${ADMIN_BACKUP_KEY_ID} ${1}
  344. echo "$BACKUP_DUMMY_PASSWORD" | obnam backup -r $SERVER_DIRECTORY/backup/${2} --encrypt-with ${ADMIN_BACKUP_KEY_ID} ${1}
  345. if [[ $ENABLE_VERIFICATION == "yes" ]]; then
  346. echo "$BACKUP_DUMMY_PASSWORD" | obnam verify -r $SERVER_DIRECTORY/backup/${2} --encrypt-with ${ADMIN_BACKUP_KEY_ID} ${1}
  347. if [ ! "$?" = "0" ]; then
  348. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  349. shred -zu /root/temp${2}/*
  350. rm -rf /root/temp${2}
  351. fi
  352. # Send a warning email
  353. echo "Unable to verify ${2}" | mail -s "${PROJECT_NAME} backup to friends" ${ADMIN_EMAIL_ADDRESS}
  354. function_check restart_site
  355. restart_site
  356. exit 953
  357. fi
  358. fi
  359. echo "$BACKUP_DUMMY_PASSWORD" | obnam forget --keep=30d -r $SERVER_DIRECTORY/backup/${2} --encrypt-with ${ADMIN_BACKUP_KEY_ID}
  360. if [ ! "$?" = "0" ]; then
  361. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  362. shred -zu /root/temp${2}/*
  363. rm -rf /root/temp${2}
  364. fi
  365. # Send a warning email
  366. echo "Unable to backup ${2}" | mail -s "${PROJECT_NAME} backup to friends" ${ADMIN_EMAIL_ADDRESS}
  367. function_check restart_site
  368. restart_site
  369. exit 853
  370. fi
  371. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  372. shred -zu /root/temp${2}/*
  373. rm -rf /root/temp${2}
  374. fi
  375. }
  376. function backup_database_remote {
  377. if [ ${#DATABASE_PASSWORD} -lt 2 ]; then
  378. echo $"No MariaDB password was given"
  379. function_check restart_site
  380. restart_site
  381. exit 5783
  382. fi
  383. if [ ! -d $SERVER_DIRECTORY/backup/${1} ]; then
  384. mkdir -p $SERVER_DIRECTORY/backup/${1}
  385. fi
  386. if [ ! -d $SERVER_DIRECTORY/backup/${1}data ]; then
  387. mkdir -p $SERVER_DIRECTORY/backup/${1}data
  388. fi
  389. local_database_dir=/root/temp${1}data
  390. if [ ! -d ${local_database_dir} ]; then
  391. mkdir -p ${local_database_dir}
  392. fi
  393. keep_database_running
  394. echo "Obtaining ${1} database backup"
  395. mysqldump --password="$DATABASE_PASSWORD" ${1} > ${local_database_dir}/${1}.sql
  396. if [ -f ${local_database_dir}/${1}.sql ]; then
  397. if [ ! -s ${local_database_dir}/${1}.sql ]; then
  398. echo $"${1} database could not be saved"
  399. shred -zu ${local_database_dir}/*
  400. rm -rf ${local_database_dir}
  401. # Send a warning email
  402. echo $"Unable to export ${1} database" | mail -s $"${PROJECT_NAME} backup to friends" $ADMIN_EMAIL_ADDRESS
  403. function_check restart_site
  404. restart_site
  405. exit 5738
  406. fi
  407. else
  408. echo $"${1} database could not be dumped"
  409. rm -rf ${local_database_dir}
  410. # Send a warning email
  411. echo $"Unable to dump ${1} database" | mail -s $"${PROJECT_NAME} backup to friends" $ADMIN_EMAIL_ADDRESS
  412. function_check restart_site
  413. restart_site
  414. exit 3687
  415. fi
  416. }
  417. function backup_database_to_friend {
  418. database_name=$1
  419. backup_database_remote $database_name
  420. backup_directory_to_friend /root/temp${database_name}data ${database_name}data
  421. }
  422. function backup_apps {
  423. localremote=$1
  424. BACKUP_APPS_COMPLETED=()
  425. detect_installable_apps
  426. get_apps_installed_names
  427. for app_name in "${APPS_INSTALLED_NAMES[@]}"
  428. do
  429. echo $"Backup ${app_name}"
  430. app_load_variables ${app_name}
  431. function_check backup_${localremote}_${app_name}
  432. backup_${localremote}_${app_name}
  433. BACKUP_APPS_COMPLETED+=("${app_name}")
  434. echo $"Backup ${app_name} completed"
  435. done
  436. }
  437. function restore_apps {
  438. localremote=$1
  439. RESTORE_APP=$2
  440. RESTORE_APPS_COMPLETED=()
  441. detect_installable_apps
  442. get_apps_installed_names
  443. for app_name in "${APPS_INSTALLED_NAMES[@]}"
  444. do
  445. confirm_restore=
  446. if [ ! $2 ]; then
  447. confirm_restore=1
  448. else
  449. if [[ "$RESTORE_APP" == "$app_name" || "$RESTORE_APP" == "all" ]]; then
  450. confirm_restore=1
  451. fi
  452. fi
  453. if [ $confirm_restore ]; then
  454. echo $"Restoring ${app_name}"
  455. app_load_variables ${app_name}
  456. function_check restore_${localremote}_${app_name}
  457. restore_${localremote}_${app_name}
  458. RESTORE_APPS_COMPLETED+=("${app_name}")
  459. echo $"Restored ${app_name}"
  460. fi
  461. done
  462. }
  463. function restore_database_from_friend {
  464. DATABASE_PASSWORD=
  465. RESTORE_SUBDIR="root"
  466. if [ -d $SERVER_DIRECTORY/backup/${1} ]; then
  467. echo $"Restoring ${1} database"
  468. local_database_dir=/root/temp${1}data
  469. restore_directory_from_friend ${local_database_dir} ${1}data
  470. if [ ! -f ${local_database_dir}/${RESTORE_SUBDIR}/temp${1}data/${1}.sql ]; then
  471. echo $"Unable to restore ${1} database"
  472. rm -rf ${local_database_dir}
  473. exit 503
  474. fi
  475. keep_database_running
  476. mysqlsuccess=$(mysql -u root --password="$DATABASE_PASSWORD" ${1} -o < ${local_database_dir}/${RESTORE_SUBDIR}/temp${1}data/${1}.sql)
  477. if [ ! "$?" = "0" ]; then
  478. echo "$mysqlsuccess"
  479. exit 964
  480. fi
  481. shred -zu ${local_database_dir}/${RESTORE_SUBDIR}/temp${1}data/*
  482. rm -rf ${local_database_dir}
  483. echo $"Restoring ${1} installation"
  484. restore_directory_from_friend /root/temp${1} ${1}
  485. RESTORE_SUBDIR="var"
  486. if [ ${1} ]; then
  487. if [ ! -d /var/www/${2}/htdocs ]; then
  488. mkdir -p /var/www/${2}/htdocs
  489. chown www-data:www-data /var/www/${2}/htdocs
  490. fi
  491. if [ -d /var/www/${2}/htdocs ]; then
  492. if [ -d /root/temp${1}/${RESTORE_SUBDIR}/www/${2}/htdocs ]; then
  493. rm -rf /var/www/${2}/htdocs
  494. mv /root/temp${1}/${RESTORE_SUBDIR}/www/${2}/htdocs /var/www/${2}/
  495. if [ ! "$?" = "0" ]; then
  496. exit 683
  497. fi
  498. if [ -d /etc/letsencrypt/live/${2} ]; then
  499. ln -s /etc/letsencrypt/live/${2}/privkey.pem /etc/ssl/private/${2}.key
  500. ln -s /etc/letsencrypt/live/${2}/fullchain.pem /etc/ssl/certs/${2}.pem
  501. else
  502. # Ensure that the bundled SSL cert is being used
  503. if [ -f /etc/ssl/certs/${2}.bundle.crt ]; then
  504. sed -i "s|${2}.crt|${2}.bundle.crt|g" /etc/nginx/sites-available/${2}
  505. fi
  506. fi
  507. fi
  508. fi
  509. fi
  510. fi
  511. }
  512. function restore_database {
  513. RESTORE_SUBDIR="root"
  514. restore_app_name=$1
  515. restore_app_domain=$2
  516. if [ -d $USB_MOUNT/backup/${restore_app_name} ]; then
  517. echo $"Restoring ${restore_app_name} database"
  518. local_database_dir=/root/temp${restore_app_name}data
  519. if [ -d ${local_database_dir} ]; then
  520. rm -rf ${local_database_dir}
  521. fi
  522. function_check restore_directory_from_usb
  523. restore_directory_from_usb "${local_database_dir}" "${restore_app_name}data"
  524. if [ ! -f /root/temp${restore_app_name}data/${RESTORE_SUBDIR}/temp${restore_app_name}data/${restore_app_name}.sql ]; then
  525. echo $"Unable to restore ${restore_app_name} database"
  526. rm -rf ${local_database_dir}
  527. function_check set_user_permissions
  528. set_user_permissions
  529. function_check backup_unmount_drive
  530. backup_unmount_drive
  531. exit 503
  532. fi
  533. keep_database_running
  534. mysqlsuccess=$(mysql -u root --password="$DATABASE_PASSWORD" ${restore_app_name} -o < ${local_database_dir}/${RESTORE_SUBDIR}/temp${restore_app_name}data/${restore_app_name}.sql)
  535. if [ ! "$?" = "0" ]; then
  536. echo "$mysqlsuccess"
  537. function_check set_user_permissions
  538. set_user_permissions
  539. function_check set_user_permissions
  540. backup_unmount_drive
  541. exit 964
  542. fi
  543. shred -zu ${local_database_dir}/${RESTORE_SUBDIR}/temp${restore_app_name}data/*
  544. rm -rf ${local_database_dir}
  545. echo $"Restoring ${restore_app_name} installation"
  546. if [ ! -d /root/temp${restore_app_name} ]; then
  547. mkdir /root/temp${restore_app_name}
  548. fi
  549. function_check restore_directory_from_usb
  550. restore_directory_from_usb "/root/temp${restore_app_name}" "${restore_app_name}"
  551. RESTORE_SUBDIR="var"
  552. if [ ${restore_app_domain} ]; then
  553. if [ ! -d /var/www/${restore_app_domain}/htdocs ]; then
  554. mkdir -p /var/www/${restore_app_domain}/htdocs
  555. chown www-data:www-data /var/www/${restore_app_domain}/htdocs
  556. fi
  557. if [ -d /var/www/${restore_app_domain}/htdocs ]; then
  558. if [ -d /root/temp${restore_app_name}/${RESTORE_SUBDIR}/www/${restore_app_domain}/htdocs ]; then
  559. rm -rf /var/www/${restore_app_domain}/htdocs
  560. mv /root/temp${restore_app_name}/${RESTORE_SUBDIR}/www/${restore_app_domain}/htdocs /var/www/${restore_app_domain}/
  561. if [ ! "$?" = "0" ]; then
  562. set_user_permissions
  563. backup_unmount_drive
  564. exit 683
  565. fi
  566. if [ -d /etc/letsencrypt/live/${restore_app_domain} ]; then
  567. ln -s /etc/letsencrypt/live/${restore_app_domain}/privkey.pem /etc/ssl/private/${restore_app_domain}.key
  568. ln -s /etc/letsencrypt/live/${restore_app_domain}/fullchain.pem /etc/ssl/certs/${restore_app_domain}.pem
  569. else
  570. # Ensure that the bundled SSL cert is being used
  571. if [ -f /etc/ssl/certs/${restore_app_domain}.bundle.crt ]; then
  572. sed -i "s|${restore_app_domain}.crt|${restore_app_domain}.bundle.crt|g" /etc/nginx/sites-available/${restore_app_domain}
  573. fi
  574. fi
  575. fi
  576. fi
  577. fi
  578. fi
  579. }
  580. function valid_backup_destination {
  581. # used to check whether any additional backup directories clash with
  582. # exiting apps
  583. destination_dir="$1"
  584. is_valid="yes"
  585. available_variants_list=()
  586. available_system_variants
  587. item_in_array "${destination_dir}" "${available_variants_list[@]}"
  588. if [[ $? != 0 ]]; then
  589. is_valid="no"
  590. fi
  591. echo $is_valid
  592. }
  593. function backup_extra_directories {
  594. if [ ! -f $BACKUP_EXTRA_DIRECTORIES ]; then
  595. return
  596. fi
  597. backup_type="$1"
  598. echo $"Backing up some additional directories"
  599. while read backup_line
  600. do
  601. backup_dir=$(echo "$backup_line" | awk -F ',' '{print $1}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  602. if [ -d "$backup_dir" ]; then
  603. destination_dir=$(echo "$backup_line" | awk -F ',' '{print $2}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  604. if [[ $(valid_backup_destination "$destination_dir") == "yes" ]]; then
  605. if [[ $backup_type == "local" ]]; then
  606. backup_directory_to_usb "$backup_dir" "$destination_dir"
  607. else
  608. backup_directory_to_friend "$backup_dir" "$destination_dir"
  609. fi
  610. else
  611. echo $"WARNING: The backup directory $destination_dir is already used."
  612. echo $"Choose a different destination name for backing up $backup_dir"
  613. fi
  614. else
  615. echo $"WARNING: Directory $backup_dir does not exist"
  616. fi
  617. done <$BACKUP_EXTRA_DIRECTORIES
  618. }
  619. # NOTE: deliberately no exit 0