freedombone-utils-backup 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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-2016 Bob Mottram <bob@robotics.uk.to>
  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. function suspend_site {
  33. # suspends a given website
  34. SUSPENDED_SITE="$1"
  35. nginx_dissite $SUSPENDED_SITE
  36. service nginx reload
  37. }
  38. function restart_site {
  39. # restarts a given website
  40. if [ ! $SUSPENDED_SITE ]; then
  41. return
  42. fi
  43. nginx_ensite $SUSPENDED_SITE
  44. service nginx reload
  45. SUSPENDED_SITE=
  46. }
  47. function configure_backup_key {
  48. if grep -Fxq "configure_backup_key" $COMPLETION_FILE; then
  49. return
  50. fi
  51. apt-get -y install gnupg
  52. BACKUP_KEY_EXISTS=$(gpg_key_exists "root" "$MY_NAME (backup key)")
  53. if [[ $BACKUP_KEY_EXISTS == "yes" ]]; then
  54. return
  55. fi
  56. # Generate a GPG key for backups
  57. BACKUP_KEY_EXISTS=$(gpg_key_exists "$MY_USERNAME" "$MY_NAME (backup key)")
  58. if [[ $BACKUP_KEY_EXISTS == "no" ]]; then
  59. echo 'Key-Type: 1' > /home/$MY_USERNAME/gpg-genkey.conf
  60. echo 'Key-Length: 4096' >> /home/$MY_USERNAME/gpg-genkey.conf
  61. echo 'Subkey-Type: 1' >> /home/$MY_USERNAME/gpg-genkey.conf
  62. echo 'Subkey-Length: 4096' >> /home/$MY_USERNAME/gpg-genkey.conf
  63. echo "Name-Real: $MY_NAME" >> /home/$MY_USERNAME/gpg-genkey.conf
  64. echo "Name-Email: $MY_EMAIL_ADDRESS" >> /home/$MY_USERNAME/gpg-genkey.conf
  65. echo "Name-Comment: backup key" >> /home/$MY_USERNAME/gpg-genkey.conf
  66. echo 'Expire-Date: 0' >> /home/$MY_USERNAME/gpg-genkey.conf
  67. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/gpg-genkey.conf
  68. echo $'Backup key does not exist. Creating it.'
  69. su -c "gpg --batch --gen-key /home/$MY_USERNAME/gpg-genkey.conf" - $MY_USERNAME
  70. shred -zu /home/$MY_USERNAME/gpg-genkey.conf
  71. echo $'Checking that the Backup key was created'
  72. BACKUP_KEY_EXISTS=$(gpg_key_exists "$MY_USERNAME" "$MY_NAME (backup key)")
  73. if [[ $BACKUP_KEY_EXISTS == "no" ]]; then
  74. echo $'Backup key could not be created'
  75. exit 43382
  76. fi
  77. fi
  78. MY_BACKUP_KEY_ID=$(su -c "gpg --list-keys \"$MY_NAME (backup key)\" | grep 'pub '" - $MY_USERNAME | awk -F ' ' '{print $2}' | awk -F '/' '{print $2}')
  79. echo "Backup key: $MY_BACKUP_KEY_ID"
  80. MY_BACKUP_KEY=/home/$MY_USERNAME/backup_key
  81. su -c "gpg --output ${MY_BACKUP_KEY}_public.asc --armor --export $MY_BACKUP_KEY_ID" - $MY_USERNAME
  82. su -c "gpg --output ${MY_BACKUP_KEY}_private.asc --armor --export-secret-key $MY_BACKUP_KEY_ID" - $MY_USERNAME
  83. if [ ! -f ${MY_BACKUP_KEY}_public.asc ]; then
  84. echo 'Public backup key could not be exported'
  85. exit 36829
  86. fi
  87. if [ ! -f ${MY_BACKUP_KEY}_private.asc ]; then
  88. echo 'Private backup key could not be exported'
  89. exit 29235
  90. fi
  91. # import backup key to root user
  92. gpg --import --import ${MY_BACKUP_KEY}_public.asc
  93. gpg --allow-secret-key-import --import ${MY_BACKUP_KEY}_private.asc
  94. shred -zu ${MY_BACKUP_KEY}_public.asc
  95. shred -zu ${MY_BACKUP_KEY}_private.asc
  96. echo 'configure_backup_key' >> $COMPLETION_FILE
  97. }
  98. function backup_to_friends_servers {
  99. # update crontab
  100. echo '#!/bin/bash' > /etc/cron.daily/backuptofriends
  101. echo "if [ -f /usr/local/bin/${PROJECT_NAME}-backup-remote ]; then" >> /etc/cron.daily/backuptofriends
  102. echo " /usr/local/bin/${PROJECT_NAME}-backup-remote" >> /etc/cron.daily/backuptofriends
  103. echo 'else' >> /etc/cron.daily/backuptofriends
  104. echo " /usr/bin/${PROJECT_NAME}-backup-remote" >> /etc/cron.daily/backuptofriends
  105. echo 'fi' >> /etc/cron.daily/backuptofriends
  106. chmod +x /etc/cron.daily/backuptofriends
  107. }
  108. function backup_mount_drive {
  109. if [ $1 ]; then
  110. USB_DRIVE=/dev/${1}1
  111. fi
  112. # get the admin user
  113. ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
  114. if [ $2 ]; then
  115. ADMIN_USERNAME=$2
  116. fi
  117. ADMIN_NAME=$(getent passwd $ADMIN_USERNAME | cut -d: -f5 | cut -d, -f1)
  118. if [ $3 ]; then
  119. RESTORE_APP=$3
  120. fi
  121. # check that the backup destination is available
  122. if [ ! -b $USB_DRIVE ]; then
  123. echo $"Please attach a USB drive"
  124. exit 1
  125. fi
  126. # unmount if already mounted
  127. umount -f $USB_MOUNT
  128. if [ ! -d $USB_MOUNT ]; then
  129. mkdir $USB_MOUNT
  130. fi
  131. if [ -f /dev/mapper/encrypted_usb ]; then
  132. rm -rf /dev/mapper/encrypted_usb
  133. fi
  134. cryptsetup luksClose encrypted_usb
  135. # mount the encrypted backup drive
  136. cryptsetup luksOpen $USB_DRIVE encrypted_usb
  137. if [ "$?" = "0" ]; then
  138. USB_DRIVE=/dev/mapper/encrypted_usb
  139. fi
  140. mount $USB_DRIVE $USB_MOUNT
  141. if [ ! "$?" = "0" ]; then
  142. echo $"There was a problem mounting the USB drive to $USB_MOUNT"
  143. rm -rf $USB_MOUNT
  144. exit 783452
  145. fi
  146. }
  147. function backup_unmount_drive {
  148. if [ $1 ]; then
  149. USB_DRIVE=${1}
  150. if [ $2 ]; then
  151. USB_MOUNT=${2}
  152. fi
  153. fi
  154. sync
  155. umount $USB_MOUNT
  156. if [ ! "$?" = "0" ]; then
  157. echo $"Unable to unmount the drive."
  158. rm -rf $USB_MOUNT
  159. exit 9
  160. fi
  161. rm -rf $USB_MOUNT
  162. if [[ $USB_DRIVE == /dev/mapper/encrypted_usb ]]; then
  163. echo $"Unmount encrypted USB"
  164. cryptsetup luksClose encrypted_usb
  165. fi
  166. if [ -f /dev/mapper/encrypted_usb ]; then
  167. rm -rf /dev/mapper/encrypted_usb
  168. fi
  169. }
  170. function backup_database_local {
  171. if [ ${#DATABASE_PASSWORD} -lt 2 ]; then
  172. echo $"No MariaDB password was given"
  173. function_check restart_site
  174. restart_site
  175. exit 10
  176. fi
  177. if [ ! -d $USB_MOUNT/backup/${1} ]; then
  178. mkdir -p $USB_MOUNT/backup/${1}
  179. fi
  180. if [ ! -d $USB_MOUNT/backup/${1}data ]; then
  181. mkdir -p $USB_MOUNT/backup/${1}data
  182. fi
  183. if [ ! -d /root/temp${1}data ]; then
  184. mkdir -p /root/temp${1}data
  185. fi
  186. echo $"Obtaining ${1} database backup"
  187. mysqldump --lock-tables --password="$DATABASE_PASSWORD" ${1} > /root/temp${1}data/${1}.sql
  188. if [ ! -s /root/temp${1}data/${1}.sql ]; then
  189. echo $"${1} database could not be saved"
  190. shred -zu /root/temp${1}data/*
  191. rm -rf /root/temp${1}data
  192. umount $USB_MOUNT
  193. rm -rf $USB_MOUNT
  194. restart_site
  195. exit 6835872
  196. fi
  197. }
  198. function backup_directory_to_usb {
  199. if [ ! -d ${1} ]; then
  200. echo $"WARNING: directory does not exist: ${1}"
  201. else
  202. BACKUP_KEY_EXISTS=$(gpg --list-keys "$ADMIN_NAME (backup key)")
  203. if [ ! "$?" = "0" ]; then
  204. echo $"Backup key could not be found"
  205. function_check restart_site
  206. restart_site
  207. exit 6
  208. fi
  209. MY_BACKUP_KEY_ID=$(gpg --list-keys "$ADMIN_NAME (backup key)" | grep 'pub ' | awk -F ' ' '{print $2}' | awk -F '/' '{print $2}')
  210. if [ ! -d $USB_MOUNT/backup/${2} ]; then
  211. mkdir -p $USB_MOUNT/backup/${2}
  212. fi
  213. obnam force-lock -r $USB_MOUNT/backup/${2} --encrypt-with $MY_BACKUP_KEY_ID ${1}
  214. obnam backup -r $USB_MOUNT/backup/${2} --encrypt-with $MY_BACKUP_KEY_ID ${1}
  215. if [[ $ENABLE_BACKUP_VERIFICATION == "yes" ]]; then
  216. obnam verify -r $USB_MOUNT/backup/${2} --encrypt-with $MY_BACKUP_KEY_ID ${1}
  217. if [ ! "$?" = "0" ]; then
  218. umount $USB_MOUNT
  219. rm -rf $USB_MOUNT
  220. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  221. shred -zu ${1}/*
  222. rm -rf ${1}
  223. fi
  224. function_check restart_site
  225. restart_site
  226. exit 683252
  227. fi
  228. fi
  229. obnam forget --keep=30d -r $USB_MOUNT/backup/${2} --encrypt-with $MY_BACKUP_KEY_ID
  230. if [ ! "$?" = "0" ]; then
  231. umount $USB_MOUNT
  232. rm -rf $USB_MOUNT
  233. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  234. shred -zu ${1}/*
  235. rm -rf ${1}
  236. fi
  237. function_check restart_site
  238. restart_site
  239. exit 7
  240. fi
  241. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  242. shred -zu ${1}/*
  243. rm -rf ${1}
  244. fi
  245. fi
  246. }
  247. function backup_database_to_usb {
  248. database_name=$1
  249. backup_database_local $database_name
  250. backup_directory_to_usb /root/temp${database_name}data ${database_name}data
  251. }
  252. # after user files have been restored permissions may need to be set
  253. function set_user_permissions {
  254. echo $"Setting permissions"
  255. for d in /home/*/ ; do
  256. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  257. if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" ]]; then
  258. chown -R $USERNAME:$USERNAME /home/$USERNAME
  259. fi
  260. done
  261. }
  262. # NOTE: deliberately no exit 0