freedombone-utils-backup 27KB

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