freedombone-utils-backup 30KB

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