freedombone-utils-backup 30KB

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