freedombone-utils-backup 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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. echo $"Obtaining ${1} database backup"
  211. if [ ! $USE_POSTGRESQL ]; then
  212. keep_database_running
  213. mysqldump --lock-tables --password="$DATABASE_PASSWORD" ${1} > ${local_database_dir}/${1}.sql
  214. else
  215. USE_POSTGRESQL=
  216. sudo -u postgres pg_dump ${1} > ${local_database_dir}/${1}.sql
  217. fi
  218. if [ -f ${local_database_dir}/${1}.sql ]; then
  219. if [ ! -s ${local_database_dir}/${1}.sql ]; then
  220. echo $"${1} database could not be saved"
  221. shred -zu ${local_database_dir}/*
  222. rm -rf ${local_database_dir}
  223. umount $USB_MOUNT
  224. rm -rf $USB_MOUNT
  225. restart_site
  226. exit 6835872
  227. fi
  228. else
  229. echo $"${1} database could not be dumped"
  230. rm -rf ${local_database_dir}
  231. umount $USB_MOUNT
  232. rm -rf $USB_MOUNT
  233. restart_site
  234. exit 738653
  235. fi
  236. echo $"Database dump was created for ${1}"
  237. }
  238. function set_obnam_client_name {
  239. # obnam can backup multiple machines with different domain names to
  240. # a repository. To be able to restore directories from different
  241. # machines we need to enforce a single client name for all backups
  242. echo '[config]' > /etc/obnam.conf
  243. echo "client-name = ${PROJECT_NAME}" >> /etc/obnam.conf
  244. }
  245. function backup_directory_to_usb_duplicity {
  246. create_backups_temp_directory
  247. 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}
  248. if [ ! "$?" = "0" ]; then
  249. umount $USB_MOUNT
  250. rm -rf $USB_MOUNT
  251. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  252. shred -zu ${1}/*
  253. rm -rf ${1}
  254. fi
  255. function_check restart_site
  256. restart_site
  257. remove_backups_temp_directory
  258. exit 8352925
  259. fi
  260. if [[ $ENABLE_BACKUP_VERIFICATION == "yes" ]]; then
  261. 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}
  262. if [ ! "$?" = "0" ]; then
  263. umount $USB_MOUNT
  264. rm -rf $USB_MOUNT
  265. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  266. shred -zu ${1}/*
  267. rm -rf ${1}
  268. fi
  269. function_check restart_site
  270. restart_site
  271. remove_backups_temp_directory
  272. exit 683252
  273. fi
  274. fi
  275. remove_backups_temp_directory
  276. }
  277. function backup_directory_to_usb_obnam {
  278. set_obnam_client_name
  279. echo "$BACKUP_DUMMY_PASSWORD" | obnam force-lock -r $USB_MOUNT/backup/${2} --encrypt-with $MY_BACKUP_KEY_ID ${1}
  280. echo "$BACKUP_DUMMY_PASSWORD" | obnam backup -r $USB_MOUNT/backup/${2} --encrypt-with $MY_BACKUP_KEY_ID ${1}
  281. if [[ $ENABLE_BACKUP_VERIFICATION == "yes" ]]; then
  282. echo "$BACKUP_DUMMY_PASSWORD" | obnam verify -r $USB_MOUNT/backup/${2} --encrypt-with $MY_BACKUP_KEY_ID ${1}
  283. if [ ! "$?" = "0" ]; then
  284. umount $USB_MOUNT
  285. rm -rf $USB_MOUNT
  286. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  287. shred -zu ${1}/*
  288. rm -rf ${1}
  289. fi
  290. function_check restart_site
  291. restart_site
  292. exit 683252
  293. fi
  294. fi
  295. echo "$BACKUP_DUMMY_PASSWORD" | obnam forget --keep=30d -r $USB_MOUNT/backup/${2} --encrypt-with $MY_BACKUP_KEY_ID
  296. if [ ! "$?" = "0" ]; then
  297. umount $USB_MOUNT
  298. rm -rf $USB_MOUNT
  299. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  300. shred -zu ${1}/*
  301. rm -rf ${1}
  302. fi
  303. function_check restart_site
  304. restart_site
  305. exit 7
  306. fi
  307. }
  308. function backup_directory_to_usb {
  309. if [ ! -d ${1} ]; then
  310. echo $"WARNING: directory does not exist: ${1}"
  311. else
  312. BACKUP_KEY_EXISTS=$(gpg --list-keys "$ADMIN_NAME (backup key)")
  313. if [ ! "$?" = "0" ]; then
  314. echo $"Backup key could not be found"
  315. function_check restart_site
  316. restart_site
  317. exit 6
  318. fi
  319. MY_BACKUP_KEY_ID=$(gpg --list-keys "$ADMIN_NAME (backup key)" | sed -n '2p' | sed 's/^[ \t]*//')
  320. if [ ! -d $USB_MOUNT/backup/${2} ]; then
  321. mkdir -p $USB_MOUNT/backup/${2}
  322. fi
  323. backup_directory_to_usb_duplicity ${1} ${2}
  324. #backup_directory_to_usb_obnam ${1} ${2}
  325. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  326. shred -zu ${1}/*
  327. rm -rf ${1}
  328. fi
  329. fi
  330. }
  331. function restore_directory_from_usb_obnam {
  332. set_obnam_client_name
  333. echo "$BACKUP_DUMMY_PASSWORD" | obnam restore -r $USB_MOUNT/backup/${2} --to ${1}
  334. }
  335. function restore_directory_from_usb_duplicity {
  336. create_backups_temp_directory
  337. PASSPHRASE="$BACKUP_DUMMY_PASSWORD" duplicity restore --gpg-options "$BACKUP_GPG_OPTIONS" --tempdir $BACKUP_TEMP_DIRECTORY --force file://$USB_MOUNT/backup/${2} ${1}
  338. if [ ! "$?" = "0" ]; then
  339. echo "WARNING: failed to restore $USB_MOUNT/backup/${2} to ${1}"
  340. fi
  341. PASSPHRASE=
  342. remove_backups_temp_directory
  343. }
  344. function restore_directory_from_usb {
  345. if [ ! ${1} ]; then
  346. echo "$USB_MOUNT/backup/${2} -> ${1}"
  347. echo $'No restore destination given'
  348. return
  349. fi
  350. if [ ! ${2} ]; then
  351. echo "$USB_MOUNT/backup/${2} -> ${1}"
  352. echo $'No restore source given'
  353. return
  354. fi
  355. if [ ! -d ${1} ]; then
  356. mkdir ${1}
  357. fi
  358. restore_directory_from_usb_duplicity ${1} ${2}
  359. #restore_directory_from_usb_obnam ${1} ${2}
  360. }
  361. function restore_directory_from_friend_obnam {
  362. set_obnam_client_name
  363. echo "$BACKUP_DUMMY_PASSWORD" | obnam restore -r $SERVER_DIRECTORY/backup/${2} --to ${1}
  364. }
  365. function restore_directory_from_friend_duplicity {
  366. create_backups_temp_directory
  367. PASSPHRASE="$BACKUP_DUMMY_PASSWORD" duplicity restore --gpg-options "$BACKUP_GPG_OPTIONS" --tempdir $BACKUP_TEMP_DIRECTORY --force file://$SERVER_DIRECTORY/backup/${2} ${1}
  368. PASSPHRASE=
  369. remove_backups_temp_directory
  370. }
  371. function restore_directory_from_friend {
  372. if [ ! ${1} ]; then
  373. echo "obnam restore -r $SERVER_DIRECTORY/backup/${2} --to ${1}"
  374. echo $'No restore destination given'
  375. return
  376. fi
  377. if [ ! ${2} ]; then
  378. echo "obnam restore -r $SERVER_DIRECTORY/backup/${2} --to ${1}"
  379. echo $'No restore source given'
  380. return
  381. fi
  382. if [ ! -d ${1} ]; then
  383. mkdir ${1}
  384. fi
  385. restore_directory_from_friend_duplicity ${1} ${2}
  386. #restore_directory_from_friend_obnam ${1} ${2}
  387. }
  388. function backup_database_to_usb {
  389. database_name=$1
  390. local_database_dir=/root/temp${1}data
  391. backup_database_local_usb ${database_name}
  392. if [ ! -f ${local_database_dir}/${1}.sql ]; then
  393. echo $"Error backing up ${1} database to ${local_database_dir}/${1}.sql"
  394. exit 62383
  395. fi
  396. backup_directory_to_usb ${local_database_dir} ${database_name}data
  397. }
  398. # after user files have been restored permissions may need to be set
  399. function set_user_permissions {
  400. echo $"Setting permissions"
  401. for d in /home/*/ ; do
  402. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  403. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  404. chown -R $USERNAME:$USERNAME /home/$USERNAME
  405. fi
  406. done
  407. }
  408. function backup_directory_to_friend_duplicity {
  409. create_backups_temp_directory
  410. 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}
  411. if [ ! "$?" = "0" ]; then
  412. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  413. shred -zu ${1}/*
  414. rm -rf ${1}
  415. fi
  416. function_check restart_site
  417. restart_site
  418. remove_backups_temp_directory
  419. exit 5293526
  420. fi
  421. if [[ $ENABLE_BACKUP_VERIFICATION == "yes" ]]; then
  422. 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}
  423. if [ ! "$?" = "0" ]; then
  424. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  425. shred -zu ${1}/*
  426. rm -rf ${1}
  427. fi
  428. function_check restart_site
  429. restart_site
  430. remove_backups_temp_directory
  431. exit 683252
  432. fi
  433. fi
  434. remove_backups_temp_directory
  435. }
  436. function backup_directory_to_friend_obnam {
  437. set_obnam_client_name
  438. echo "$BACKUP_DUMMY_PASSWORD" | obnam force-lock -r $SERVER_DIRECTORY/backup/${2} --encrypt-with ${ADMIN_BACKUP_KEY_ID} ${1}
  439. echo "$BACKUP_DUMMY_PASSWORD" | obnam backup -r $SERVER_DIRECTORY/backup/${2} --encrypt-with ${ADMIN_BACKUP_KEY_ID} ${1}
  440. if [[ $ENABLE_VERIFICATION == "yes" ]]; then
  441. echo "$BACKUP_DUMMY_PASSWORD" | obnam verify -r $SERVER_DIRECTORY/backup/${2} --encrypt-with ${ADMIN_BACKUP_KEY_ID} ${1}
  442. if [ ! "$?" = "0" ]; then
  443. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  444. shred -zu /root/temp${2}/*
  445. rm -rf /root/temp${2}
  446. fi
  447. # Send a warning email
  448. echo "Unable to verify ${2}" | mail -s "${PROJECT_NAME} backup to friends" ${ADMIN_EMAIL_ADDRESS}
  449. function_check restart_site
  450. restart_site
  451. exit 953
  452. fi
  453. fi
  454. echo "$BACKUP_DUMMY_PASSWORD" | obnam forget --keep=30d -r $SERVER_DIRECTORY/backup/${2} --encrypt-with ${ADMIN_BACKUP_KEY_ID}
  455. if [ ! "$?" = "0" ]; then
  456. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  457. shred -zu /root/temp${2}/*
  458. rm -rf /root/temp${2}
  459. fi
  460. # Send a warning email
  461. echo "Unable to backup ${2}" | mail -s "${PROJECT_NAME} backup to friends" ${ADMIN_EMAIL_ADDRESS}
  462. function_check restart_site
  463. restart_site
  464. exit 853
  465. fi
  466. }
  467. function backup_directory_to_friend {
  468. BACKUP_KEY_EXISTS=$(gpg --list-keys "$ADMIN_NAME (backup key)")
  469. if [ ! "$?" = "0" ]; then
  470. echo $"Backup key could not be found"
  471. function_check restart_site
  472. restart_site
  473. exit 43382
  474. fi
  475. ADMIN_BACKUP_KEY_ID=$(gpg --list-keys "$ADMIN_NAME (backup key)" | sed -n '2p' | sed 's/^[ \t]*//')
  476. if [ ! -d $SERVER_DIRECTORY/backup/${2} ]; then
  477. mkdir -p $SERVER_DIRECTORY/backup/${2}
  478. fi
  479. backup_directory_to_friend_duplicity ${1} ${2}
  480. #backup_directory_to_friend_obnam ${1} ${2}
  481. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  482. shred -zu /root/temp${2}/*
  483. rm -rf /root/temp${2}
  484. fi
  485. }
  486. function backup_database_remote {
  487. if [ ${#DATABASE_PASSWORD} -lt 2 ]; then
  488. echo $"No MariaDB password was given"
  489. function_check restart_site
  490. restart_site
  491. exit 5783
  492. fi
  493. if [ ! -d $SERVER_DIRECTORY/backup/${1} ]; then
  494. mkdir -p $SERVER_DIRECTORY/backup/${1}
  495. fi
  496. if [ ! -d $SERVER_DIRECTORY/backup/${1}data ]; then
  497. mkdir -p $SERVER_DIRECTORY/backup/${1}data
  498. fi
  499. local_database_dir=/root/temp${1}data
  500. if [ ! -d ${local_database_dir} ]; then
  501. mkdir -p ${local_database_dir}
  502. fi
  503. echo "Obtaining ${1} database backup"
  504. if [ ! $USE_POSTGRESQL ]; then
  505. keep_database_running
  506. mysqldump --lock-tables --password="$DATABASE_PASSWORD" ${1} > ${local_database_dir}/${1}.sql
  507. else
  508. USE_POSTGRESQL=
  509. sudo -u postgres pg_dump ${1} > ${local_database_dir}/${1}.sql
  510. fi
  511. if [ -f ${local_database_dir}/${1}.sql ]; then
  512. if [ ! -s ${local_database_dir}/${1}.sql ]; then
  513. echo $"${1} database could not be saved"
  514. shred -zu ${local_database_dir}/*
  515. rm -rf ${local_database_dir}
  516. # Send a warning email
  517. echo $"Unable to export ${1} database" | mail -s $"${PROJECT_NAME} backup to friends" $ADMIN_EMAIL_ADDRESS
  518. function_check restart_site
  519. restart_site
  520. exit 5738
  521. fi
  522. else
  523. echo $"${1} database could not be dumped"
  524. rm -rf ${local_database_dir}
  525. # Send a warning email
  526. echo $"Unable to dump ${1} database" | mail -s $"${PROJECT_NAME} backup to friends" $ADMIN_EMAIL_ADDRESS
  527. function_check restart_site
  528. restart_site
  529. exit 3687
  530. fi
  531. }
  532. function backup_database_to_friend {
  533. database_name=$1
  534. backup_database_remote $database_name
  535. backup_directory_to_friend /root/temp${database_name}data ${database_name}data
  536. }
  537. function backup_apps {
  538. localremote=$1
  539. BACKUP_APPS_COMPLETED=()
  540. detect_installable_apps
  541. get_apps_installed_names
  542. for app_name in "${APPS_INSTALLED_NAMES[@]}"
  543. do
  544. echo $"Backup ${app_name}"
  545. app_load_variables ${app_name}
  546. function_check backup_${localremote}_${app_name}
  547. backup_${localremote}_${app_name}
  548. BACKUP_APPS_COMPLETED+=("${app_name}")
  549. echo $"Backup ${app_name} completed"
  550. done
  551. }
  552. function restore_apps {
  553. localremote=$1
  554. RESTORE_APP=$2
  555. RESTORE_APPS_COMPLETED=()
  556. detect_installable_apps
  557. get_apps_installed_names
  558. for app_name in "${APPS_INSTALLED_NAMES[@]}"
  559. do
  560. confirm_restore=
  561. if [ ! $2 ]; then
  562. confirm_restore=1
  563. else
  564. if [[ "$RESTORE_APP" == "$app_name" || "$RESTORE_APP" == "all" ]]; then
  565. confirm_restore=1
  566. fi
  567. fi
  568. if [ $confirm_restore ]; then
  569. echo $"Restoring ${app_name}"
  570. app_load_variables ${app_name}
  571. function_check restore_${localremote}_${app_name}
  572. restore_${localremote}_${app_name}
  573. RESTORE_APPS_COMPLETED+=("${app_name}")
  574. echo $"Restored ${app_name}"
  575. fi
  576. done
  577. }
  578. function restore_database_from_friend {
  579. DATABASE_PASSWORD=
  580. RESTORE_SUBDIR="root"
  581. if [ -d $SERVER_DIRECTORY/backup/${1} ]; then
  582. echo $"Restoring ${1} database"
  583. local_database_dir=/root/temp${1}data
  584. restore_directory_from_friend ${local_database_dir} ${1}data
  585. database_file=${local_database_dir}/${RESTORE_SUBDIR}/temp${restore_app_name}data/${restore_app_name}.sql
  586. if [ ! -f $database_file ]; then
  587. database_file=${local_database_dir}/${restore_app_name}.sql
  588. fi
  589. if [ ! -f $database_file ]; then
  590. echo $"Unable to restore ${1} database"
  591. rm -rf ${local_database_dir}
  592. exit 503
  593. fi
  594. if [ ! $USE_POSTGRESQL ]; then
  595. keep_database_running
  596. mysqlsuccess=$(mysql -u root --password="$DATABASE_PASSWORD" ${1} -o < ${local_database_dir}/${RESTORE_SUBDIR}/temp${1}data/${1}.sql)
  597. else
  598. USE_POSTGRESQL=
  599. mysqlsuccess=$(sudo -u postgres pg_restore ${local_database_dir}/${RESTORE_SUBDIR}/temp${1}data/${1}.sql)
  600. fi
  601. if [ ! "$?" = "0" ]; then
  602. echo "$mysqlsuccess"
  603. exit 964
  604. fi
  605. if [ -d ${local_database_dir}/${RESTORE_SUBDIR}/temp${1}data ]; then
  606. shred -zu ${local_database_dir}/${RESTORE_SUBDIR}/temp${1}data/*
  607. else
  608. shred -zu ${local_database_dir}/*.sql
  609. fi
  610. rm -rf ${local_database_dir}
  611. echo $"Restoring ${1} installation"
  612. restore_directory_from_friend /root/temp${1} ${1}
  613. RESTORE_SUBDIR="var"
  614. if [ ${1} ]; then
  615. # create directory to restore to
  616. if [ ! -d /var/www/${2}/htdocs ]; then
  617. mkdir -p /var/www/${2}/htdocs
  618. chown www-data:www-data /var/www/${2}/htdocs
  619. fi
  620. if [ -d /var/www/${2}/htdocs ]; then
  621. restore_from_dir=/root/temp${1}/${RESTORE_SUBDIR}/www/${2}/htdocs
  622. if [ ! -d /root/temp${1}/${RESTORE_SUBDIR}/www/${2}/htdocs ]; then
  623. restore_from_dir=/root/temp${1}
  624. fi
  625. if [ -d $restore_from_dir ]; then
  626. if [ -d /root/temp${1}/${RESTORE_SUBDIR}/www/${2}/htdocs ]; then
  627. rm -rf /var/www/${2}/htdocs
  628. mv $restore_from_dir /var/www/${2}/
  629. else
  630. cp -r $restore_from_dir/* /var/www/${2}/htdocs/
  631. fi
  632. if [ ! "$?" = "0" ]; then
  633. exit 683
  634. fi
  635. if [ -d /etc/letsencrypt/live/${2} ]; then
  636. ln -s /etc/letsencrypt/live/${2}/privkey.pem /etc/ssl/private/${2}.key
  637. ln -s /etc/letsencrypt/live/${2}/fullchain.pem /etc/ssl/certs/${2}.pem
  638. else
  639. # Ensure that the bundled SSL cert is being used
  640. if [ -f /etc/ssl/certs/${2}.bundle.crt ]; then
  641. sed -i "s|${2}.crt|${2}.bundle.crt|g" /etc/nginx/sites-available/${2}
  642. fi
  643. fi
  644. fi
  645. fi
  646. fi
  647. fi
  648. }
  649. function restore_database {
  650. RESTORE_SUBDIR="root"
  651. restore_app_name=$1
  652. restore_app_domain=$2
  653. if [ -d $USB_MOUNT/backup/${restore_app_name} ]; then
  654. echo $"Restoring ${restore_app_name} database"
  655. local_database_dir=/root/temp${restore_app_name}data
  656. if [ -d ${local_database_dir} ]; then
  657. rm -rf ${local_database_dir}
  658. fi
  659. function_check restore_directory_from_usb
  660. restore_directory_from_usb "${local_database_dir}" "${restore_app_name}data"
  661. database_file=${local_database_dir}/${RESTORE_SUBDIR}/temp${restore_app_name}data/${restore_app_name}.sql
  662. if [ ! -f $database_file ]; then
  663. database_file=${local_database_dir}/${restore_app_name}.sql
  664. fi
  665. if [ ! -f $database_file ]; then
  666. echo $"Unable to restore ${restore_app_name} database"
  667. rm -rf ${local_database_dir}
  668. function_check set_user_permissions
  669. set_user_permissions
  670. function_check backup_unmount_drive
  671. backup_unmount_drive
  672. exit 503
  673. fi
  674. if [ ! $USE_POSTGRESQL ]; then
  675. keep_database_running
  676. mysqlsuccess=$(mysql -u root --password="$DATABASE_PASSWORD" ${restore_app_name} -o < $database_file)
  677. else
  678. USE_POSTGRESQL=
  679. mysqlsuccess=$(sudo -u postgres pg_restore $database_file)
  680. fi
  681. if [ ! "$?" = "0" ]; then
  682. echo "$mysqlsuccess"
  683. function_check set_user_permissions
  684. set_user_permissions
  685. function_check set_user_permissions
  686. backup_unmount_drive
  687. exit 964
  688. fi
  689. if [ -d ${local_database_dir}/${RESTORE_SUBDIR}/temp${restore_app_name}data ]; then
  690. shred -zu ${local_database_dir}/${RESTORE_SUBDIR}/temp${restore_app_name}data/*
  691. else
  692. shred -zu ${local_database_dir}/*.sql
  693. fi
  694. rm -rf ${local_database_dir}
  695. echo $"Restoring ${restore_app_name} installation"
  696. if [ ! -d /root/temp${restore_app_name} ]; then
  697. mkdir /root/temp${restore_app_name}
  698. fi
  699. function_check restore_directory_from_usb
  700. restore_directory_from_usb "/root/temp${restore_app_name}" "${restore_app_name}"
  701. RESTORE_SUBDIR="var"
  702. if [ ${restore_app_domain} ]; then
  703. # create directory to restore to
  704. if [ ! -d /var/www/${restore_app_domain}/htdocs ]; then
  705. mkdir -p /var/www/${restore_app_domain}/htdocs
  706. chown www-data:www-data /var/www/${restore_app_domain}/htdocs
  707. fi
  708. if [ -d /var/www/${restore_app_domain}/htdocs ]; then
  709. restore_from_dir=/root/temp${restore_app_name}/${RESTORE_SUBDIR}/www/${restore_app_domain}/htdocs
  710. if [ ! -d $restore_from_dir ]; then
  711. restore_from_dir=/root/temp${restore_app_name}
  712. fi
  713. if [ -d $restore_from_dir ]; then
  714. if [ -d /root/temp${restore_app_name}/${RESTORE_SUBDIR}/www/${restore_app_domain}/htdocs ]; then
  715. rm -rf /var/www/${restore_app_domain}/htdocs
  716. mv $restore_from_dir /var/www/${restore_app_domain}/
  717. else
  718. cp -r $restore_from_dir/* /var/www/${restore_app_domain}/htdocs/
  719. fi
  720. if [ ! "$?" = "0" ]; then
  721. set_user_permissions
  722. backup_unmount_drive
  723. exit 683
  724. fi
  725. if [ -d /etc/letsencrypt/live/${restore_app_domain} ]; then
  726. ln -s /etc/letsencrypt/live/${restore_app_domain}/privkey.pem /etc/ssl/private/${restore_app_domain}.key
  727. ln -s /etc/letsencrypt/live/${restore_app_domain}/fullchain.pem /etc/ssl/certs/${restore_app_domain}.pem
  728. else
  729. # Ensure that the bundled SSL cert is being used
  730. if [ -f /etc/ssl/certs/${restore_app_domain}.bundle.crt ]; then
  731. sed -i "s|${restore_app_domain}.crt|${restore_app_domain}.bundle.crt|g" /etc/nginx/sites-available/${restore_app_domain}
  732. fi
  733. fi
  734. fi
  735. fi
  736. fi
  737. fi
  738. }
  739. function valid_backup_destination {
  740. # used to check whether any additional backup directories clash with
  741. # exiting apps
  742. destination_dir="$1"
  743. is_valid="yes"
  744. available_variants_list=()
  745. available_system_variants
  746. item_in_array "${destination_dir}" "${available_variants_list[@]}"
  747. if [[ $? != 0 ]]; then
  748. is_valid="no"
  749. fi
  750. echo $is_valid
  751. }
  752. function backup_extra_directories {
  753. if [ ! -f $BACKUP_EXTRA_DIRECTORIES ]; then
  754. return
  755. fi
  756. backup_type="$1"
  757. echo $"Backing up some additional directories"
  758. while read backup_line
  759. do
  760. backup_dir=$(echo "$backup_line" | awk -F ',' '{print $1}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  761. if [ -d "$backup_dir" ]; then
  762. destination_dir=$(echo "$backup_line" | awk -F ',' '{print $2}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  763. if [[ $(valid_backup_destination "$destination_dir") == "yes" ]]; then
  764. if [[ $backup_type == "local" ]]; then
  765. backup_directory_to_usb "$backup_dir" "$destination_dir"
  766. else
  767. backup_directory_to_friend "$backup_dir" "$destination_dir"
  768. fi
  769. else
  770. echo $"WARNING: The backup directory $destination_dir is already used."
  771. echo $"Choose a different destination name for backing up $backup_dir"
  772. fi
  773. else
  774. echo $"WARNING: Directory $backup_dir does not exist"
  775. fi
  776. done <$BACKUP_EXTRA_DIRECTORIES
  777. }
  778. # NOTE: deliberately no exit 0