freedombone-utils-backup 24KB

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