freedombone-utils-backup 23KB

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