freedombone-utils-backup 33KB

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