freedombone-utils-backup 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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 {
  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. if [ ! -d /root/temp${1}data ]; then
  188. mkdir -p /root/temp${1}data
  189. fi
  190. echo $"Obtaining ${1} database backup"
  191. mysqldump --lock-tables --password="$DATABASE_PASSWORD" ${1} > /root/temp${1}data/${1}.sql
  192. if [ ! -s /root/temp${1}data/${1}.sql ]; then
  193. echo $"${1} database could not be saved"
  194. shred -zu /root/temp${1}data/*
  195. rm -rf /root/temp${1}data
  196. umount $USB_MOUNT
  197. rm -rf $USB_MOUNT
  198. restart_site
  199. exit 6835872
  200. fi
  201. }
  202. function set_obnam_client_name {
  203. # obnam can backup multiple machines with different domain names to
  204. # a repository. To be able to restore directories from different
  205. # machines we need to enforce a single client name for all backups
  206. echo '[config]' > /etc/obnam.conf
  207. echo "client-name = ${PROJECT_NAME}" >> /etc/obnam.conf
  208. }
  209. function backup_directory_to_usb {
  210. if [ ! -d ${1} ]; then
  211. echo $"WARNING: directory does not exist: ${1}"
  212. else
  213. BACKUP_KEY_EXISTS=$(gpg --list-keys "$ADMIN_NAME (backup key)")
  214. if [ ! "$?" = "0" ]; then
  215. echo $"Backup key could not be found"
  216. function_check restart_site
  217. restart_site
  218. exit 6
  219. fi
  220. MY_BACKUP_KEY_ID=$(gpg --list-keys "$ADMIN_NAME (backup key)" | grep 'pub ' | awk -F ' ' '{print $2}' | awk -F '/' '{print $2}')
  221. if [ ! -d $USB_MOUNT/backup/${2} ]; then
  222. mkdir -p $USB_MOUNT/backup/${2}
  223. fi
  224. set_obnam_client_name
  225. obnam force-lock -r $USB_MOUNT/backup/${2} --encrypt-with $MY_BACKUP_KEY_ID ${1}
  226. obnam backup -r $USB_MOUNT/backup/${2} --encrypt-with $MY_BACKUP_KEY_ID ${1}
  227. if [[ $ENABLE_BACKUP_VERIFICATION == "yes" ]]; then
  228. obnam verify -r $USB_MOUNT/backup/${2} --encrypt-with $MY_BACKUP_KEY_ID ${1}
  229. if [ ! "$?" = "0" ]; then
  230. umount $USB_MOUNT
  231. rm -rf $USB_MOUNT
  232. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  233. shred -zu ${1}/*
  234. rm -rf ${1}
  235. fi
  236. function_check restart_site
  237. restart_site
  238. exit 683252
  239. fi
  240. fi
  241. obnam forget --keep=30d -r $USB_MOUNT/backup/${2} --encrypt-with $MY_BACKUP_KEY_ID
  242. if [ ! "$?" = "0" ]; then
  243. umount $USB_MOUNT
  244. rm -rf $USB_MOUNT
  245. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  246. shred -zu ${1}/*
  247. rm -rf ${1}
  248. fi
  249. function_check restart_site
  250. restart_site
  251. exit 7
  252. fi
  253. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  254. shred -zu ${1}/*
  255. rm -rf ${1}
  256. fi
  257. fi
  258. }
  259. function restore_directory_from_usb {
  260. if [ ! -d ${1} ]; then
  261. mkdir ${1}
  262. fi
  263. set_obnam_client_name
  264. obnam restore -r $USB_MOUNT/backup/${2} --to ${1}
  265. }
  266. function restore_directory_from_friend {
  267. if [ ! -d ${1} ]; then
  268. mkdir ${1}
  269. fi
  270. set_obnam_client_name
  271. obnam restore -r $SERVER_DIRECTORY/backup/${2} --to ${1}
  272. }
  273. function backup_database_to_usb {
  274. database_name=$1
  275. backup_database_local $database_name
  276. backup_directory_to_usb /root/temp${database_name}data ${database_name}data
  277. }
  278. # after user files have been restored permissions may need to be set
  279. function set_user_permissions {
  280. echo $"Setting permissions"
  281. for d in /home/*/ ; do
  282. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  283. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  284. chown -R $USERNAME:$USERNAME /home/$USERNAME
  285. fi
  286. done
  287. }
  288. function backup_directory_to_friend {
  289. BACKUP_KEY_EXISTS=$(gpg --list-keys "$ADMIN_NAME (backup key)")
  290. if [ ! "$?" = "0" ]; then
  291. echo $"Backup key could not be found"
  292. function_check restart_site
  293. restart_site
  294. exit 43382
  295. fi
  296. ADMIN_BACKUP_KEY_ID=$(gpg --list-keys "$ADMIN_NAME (backup key)" | grep 'pub ' | awk -F ' ' '{print $2}' | awk -F '/' '{print $2}')
  297. if [ ! -d $SERVER_DIRECTORY/backup/${2} ]; then
  298. mkdir -p $SERVER_DIRECTORY/backup/${2}
  299. fi
  300. set_obnam_client_name
  301. obnam force-lock -r $SERVER_DIRECTORY/backup/${2} --encrypt-with ${ADMIN_BACKUP_KEY_ID} ${1}
  302. obnam backup -r $SERVER_DIRECTORY/backup/${2} --encrypt-with ${ADMIN_BACKUP_KEY_ID} ${1}
  303. if [[ $ENABLE_VERIFICATION == "yes" ]]; then
  304. obnam verify -r $SERVER_DIRECTORY/backup/${2} --encrypt-with ${ADMIN_BACKUP_KEY_ID} ${1}
  305. if [ ! "$?" = "0" ]; then
  306. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  307. shred -zu /root/temp${2}/*
  308. rm -rf /root/temp${2}
  309. fi
  310. # Send a warning email
  311. echo "Unable to verify ${2}" | mail -s "${PROJECT_NAME} backup to friends" ${ADMIN_EMAIL_ADDRESS}
  312. function_check restart_site
  313. restart_site
  314. exit 953
  315. fi
  316. fi
  317. obnam forget --keep=30d -r $SERVER_DIRECTORY/backup/${2} --encrypt-with ${ADMIN_BACKUP_KEY_ID}
  318. if [ ! "$?" = "0" ]; then
  319. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  320. shred -zu /root/temp${2}/*
  321. rm -rf /root/temp${2}
  322. fi
  323. # Send a warning email
  324. echo "Unable to backup ${2}" | mail -s "${PROJECT_NAME} backup to friends" ${ADMIN_EMAIL_ADDRESS}
  325. function_check restart_site
  326. restart_site
  327. exit 853
  328. fi
  329. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  330. shred -zu /root/temp${2}/*
  331. rm -rf /root/temp${2}
  332. fi
  333. }
  334. function backup_database_remote {
  335. if [ ${#DATABASE_PASSWORD} -lt 2 ]; then
  336. echo $"No MariaDB password was given"
  337. function_check restart_site
  338. restart_site
  339. exit 5783
  340. fi
  341. if [ ! -d $SERVER_DIRECTORY/backup/${1} ]; then
  342. mkdir -p $SERVER_DIRECTORY/backup/${1}
  343. fi
  344. if [ ! -d $SERVER_DIRECTORY/backup/${1}data ]; then
  345. mkdir -p $SERVER_DIRECTORY/backup/${1}data
  346. fi
  347. if [ ! -d /root/temp${1}data ]; then
  348. mkdir -p /root/temp${1}data
  349. fi
  350. echo "Obtaining ${1} database backup"
  351. mysqldump --password=$DATABASE_PASSWORD ${1} > /root/temp${1}data/${1}.sql
  352. if [ ! -s /root/temp${1}data/${1}.sql ]; then
  353. echo $"${1} database could not be saved"
  354. shred -zu /root/temp${1}data/*
  355. rm -rf /root/temp${1}data
  356. # Send a warning email
  357. echo $"Unable to export ${1} database" | mail -s $"${PROJECT_NAME} backup to friends" $ADMIN_EMAIL_ADDRESS
  358. function_check restart_site
  359. restart_site
  360. exit 5738
  361. fi
  362. }
  363. function backup_database_to_friend {
  364. database_name=$1
  365. backup_database_remote $database_name
  366. backup_directory_to_friend /root/temp${database_name}data ${database_name}data
  367. }
  368. function backup_apps {
  369. localremote=$1
  370. BACKUP_APPS_COMPLETED=()
  371. detect_installable_apps
  372. get_apps_installed_names
  373. for app_name in "${APPS_INSTALLED_NAMES[@]}"
  374. do
  375. echo $"Backup ${app_name}"
  376. app_load_variables ${app_name}
  377. function_check backup_${localremote}_${app_name}
  378. backup_${localremote}_${app_name}
  379. BACKUP_APPS_COMPLETED+=("${app_name}")
  380. echo $"Backup ${app_name} completed"
  381. done
  382. }
  383. function restore_apps {
  384. localremote=$1
  385. RESTORE_APP=$2
  386. RESTORE_APPS_COMPLETED=()
  387. detect_installable_apps
  388. get_apps_installed_names
  389. for app_name in "${APPS_INSTALLED_NAMES[@]}"
  390. do
  391. confirm_restore=
  392. if [ ! $2 ]; then
  393. confirm_restore=1
  394. else
  395. if [[ "$RESTORE_APP" == "$app_name" || "$RESTORE_APP" == "all" ]]; then
  396. confirm_restore=1
  397. fi
  398. fi
  399. if [ $confirm_restore ]; then
  400. echo $"Restoring ${app_name}"
  401. app_load_variables ${app_name}
  402. function_check restore_${localremote}_${app_name}
  403. restore_${localremote}_${app_name}
  404. RESTORE_APPS_COMPLETED+=("${app_name}")
  405. echo $"Restored ${app_name}"
  406. fi
  407. done
  408. }
  409. function restore_database_from_friend {
  410. DATABASE_PASSWORD=
  411. RESTORE_SUBDIR="root"
  412. if [ -d $SERVER_DIRECTORY/backup/${1} ]; then
  413. echo $"Restoring ${1} database"
  414. restore_directory_from_friend /root/temp${1}data ${1}data
  415. if [ ! -f /root/temp${1}data/${RESTORE_SUBDIR}/temp${1}data/${1}.sql ]; then
  416. echo $"Unable to restore ${1} database"
  417. rm -rf /root/temp${1}data
  418. exit 503
  419. fi
  420. mysqlsuccess=$(mysql -u root --password="$DATABASE_PASSWORD" ${1} -o < /root/temp${1}data/${RESTORE_SUBDIR}/temp${1}data/${1}.sql)
  421. if [ ! "$?" = "0" ]; then
  422. echo "$mysqlsuccess"
  423. exit 964
  424. fi
  425. shred -zu /root/temp${1}data/${RESTORE_SUBDIR}/temp${1}data/*
  426. rm -rf /root/temp${1}data
  427. echo $"Restoring ${1} installation"
  428. restore_directory_from_friend /root/temp${1} ${1}
  429. RESTORE_SUBDIR="var"
  430. if [ ${1} ]; then
  431. if [ -d /var/www/${2}/htdocs ]; then
  432. if [ -d /root/temp${1}/${RESTORE_SUBDIR}/www/${2}/htdocs ]; then
  433. rm -rf /var/www/${2}/htdocs
  434. mv /root/temp${1}/${RESTORE_SUBDIR}/www/${2}/htdocs /var/www/${2}/
  435. if [ ! "$?" = "0" ]; then
  436. exit 683
  437. fi
  438. if [ -d /etc/letsencrypt/live/${2} ]; then
  439. ln -s /etc/letsencrypt/live/${2}/privkey.pem /etc/ssl/private/${2}.key
  440. ln -s /etc/letsencrypt/live/${2}/fullchain.pem /etc/ssl/certs/${2}.pem
  441. else
  442. # Ensure that the bundled SSL cert is being used
  443. if [ -f /etc/ssl/certs/${2}.bundle.crt ]; then
  444. sed -i "s|${2}.crt|${2}.bundle.crt|g" /etc/nginx/sites-available/${2}
  445. fi
  446. fi
  447. fi
  448. fi
  449. fi
  450. fi
  451. }
  452. function restore_database {
  453. RESTORE_SUBDIR="root"
  454. if [ -d $USB_MOUNT/backup/${1} ]; then
  455. echo $"Restoring ${1} database"
  456. function_check restore_directory_from_usb
  457. restore_directory_from_usb "/root/temp${1}data" "${1}data"
  458. if [ ! -f /root/temp${1}data/${RESTORE_SUBDIR}/temp${1}data/${1}.sql ]; then
  459. echo $"Unable to restore ${1} database"
  460. rm -rf /root/temp${1}data
  461. function_check set_user_permissions
  462. set_user_permissions
  463. function_check backup_unmount_drive
  464. backup_unmount_drive
  465. exit 503
  466. fi
  467. mysqlsuccess=$(mysql -u root --password=$DATABASE_PASSWORD ${1} -o < /root/temp${1}data/${RESTORE_SUBDIR}/temp${1}data/${1}.sql)
  468. if [ ! "$?" = "0" ]; then
  469. echo "$mysqlsuccess"
  470. function_check set_user_permissions
  471. set_user_permissions
  472. function_check set_user_permissions
  473. backup_unmount_drive
  474. exit 964
  475. fi
  476. shred -zu /root/temp${1}data/${RESTORE_SUBDIR}/temp${1}data/*
  477. rm -rf /root/temp${1}data
  478. echo $"Restoring ${1} installation"
  479. if [ ! -d /root/temp${1} ]; then
  480. mkdir /root/temp${1}
  481. fi
  482. function_check restore_directory_from_usb
  483. restore_directory_from_usb "/root/temp${1}" "${1}"
  484. RESTORE_SUBDIR="var"
  485. if [ ${2} ]; then
  486. if [ -d /var/www/${2}/htdocs ]; then
  487. if [ -d /root/temp${1}/${RESTORE_SUBDIR}/www/${2}/htdocs ]; then
  488. rm -rf /var/www/${2}/htdocs
  489. mv /root/temp${1}/${RESTORE_SUBDIR}/www/${2}/htdocs /var/www/${2}/
  490. if [ ! "$?" = "0" ]; then
  491. set_user_permissions
  492. backup_unmount_drive
  493. exit 683
  494. fi
  495. if [ -d /etc/letsencrypt/live/${2} ]; then
  496. ln -s /etc/letsencrypt/live/${2}/privkey.pem /etc/ssl/private/${2}.key
  497. ln -s /etc/letsencrypt/live/${2}/fullchain.pem /etc/ssl/certs/${2}.pem
  498. else
  499. # Ensure that the bundled SSL cert is being used
  500. if [ -f /etc/ssl/certs/${2}.bundle.crt ]; then
  501. sed -i "s|${2}.crt|${2}.bundle.crt|g" /etc/nginx/sites-available/${2}
  502. fi
  503. fi
  504. fi
  505. fi
  506. fi
  507. fi
  508. }
  509. function valid_backup_destination {
  510. # used to check whether any additional backup directories clash with
  511. # exiting apps
  512. destination_dir="$1"
  513. is_valid="yes"
  514. available_variants_list=()
  515. available_system_variants
  516. item_in_array "${destination_dir}" "${available_variants_list[@]}"
  517. if [[ $? != 0 ]]; then
  518. is_valid="no"
  519. fi
  520. echo $is_valid
  521. }
  522. function backup_extra_directories {
  523. if [ ! -f $BACKUP_EXTRA_DIRECTORIES ]; then
  524. return
  525. fi
  526. backup_type="$1"
  527. echo $"Backing up some additional directories"
  528. while read backup_line
  529. do
  530. backup_dir=$(echo "$backup_line" | awk -F ',' '{print $1}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  531. if [ -d "$backup_dir" ]; then
  532. destination_dir=$(echo "$backup_line" | awk -F ',' '{print $2}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  533. if [[ $(valid_backup_destination "$destination_dir") == "yes" ]]; then
  534. if [[ $backup_type == "local" ]]; then
  535. backup_directory_to_usb "$backup_dir" "$destination_dir"
  536. else
  537. backup_directory_to_friend "$backup_dir" "$destination_dir"
  538. fi
  539. else
  540. echo $"WARNING: The backup directory $destination_dir is already used."
  541. echo $"Choose a different destination name for backing up $backup_dir"
  542. fi
  543. else
  544. echo $"WARNING: Directory $backup_dir does not exist"
  545. fi
  546. done <$BACKUP_EXTRA_DIRECTORIES
  547. }
  548. # NOTE: deliberately no exit 0