freedombone-restore-gogs 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Restore gogs from local storage - typically a USB drive
  12. # License
  13. # =======
  14. #
  15. # Copyright (C) 2015 Bob Mottram <bob@robotics.uk.to>
  16. #
  17. # This program is free software: you can redistribute it and/or modify
  18. # it under the terms of the GNU General Public License as published by
  19. # the Free Software Foundation, either version 3 of the License, or
  20. # (at your option) any later version.
  21. #
  22. # This program is distributed in the hope that it will be useful,
  23. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. # GNU General Public License for more details.
  26. #
  27. # You should have received a copy of the GNU General Public License
  28. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. PROJECT_NAME='freedombone'
  30. COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
  31. BACKUP_EXTRA_DIRECTORIES=/root/backup-extra-dirs.csv
  32. export TEXTDOMAIN=${PROJECT_NAME}-restore-gogs
  33. export TEXTDOMAINDIR="/usr/share/locale"
  34. USB_DRIVE=/dev/sdb1
  35. USB_MOUNT=/mnt/usb
  36. # get default USB from config file
  37. CONFIG_FILE=$HOME/${PROJECT_NAME}.cfg
  38. if [ -f $CONFIG_FILE ]; then
  39. if grep -q "USB_DRIVE=" $CONFIG_FILE; then
  40. USB_DRIVE=$(cat $CONFIG_FILE | grep "USB_DRIVE=" | awk -F '=' '{print $2}')
  41. fi
  42. fi
  43. ADMIN_USERNAME=
  44. ADMIN_NAME=
  45. # MariaDB password
  46. DATABASE_PASSWORD=$(cat /root/dbpass)
  47. MICROBLOG_DOMAIN_NAME=
  48. HUBZILLA_DOMAIN_NAME=
  49. OWNCLOUD_DOMAIN_NAME=
  50. GIT_DOMAIN_NAME=
  51. WIKI_DOMAIN_NAME=
  52. FULLBLOG_DOMAIN_NAME=
  53. function mount_drive {
  54. if [ $1 ]; then
  55. USB_DRIVE=/dev/${1}1
  56. fi
  57. # get the admin user
  58. ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
  59. if [ $2 ]; then
  60. ADMIN_USERNAME=$2
  61. fi
  62. ADMIN_NAME=$(getent passwd $ADMIN_USERNAME | cut -d: -f5 | cut -d, -f1)
  63. # check that the backup destination is available
  64. if [ ! -b $USB_DRIVE ]; then
  65. echo $"Please attach a USB drive"
  66. exit 1
  67. fi
  68. # unmount if already mounted
  69. umount -f $USB_MOUNT
  70. if [ ! -d $USB_MOUNT ]; then
  71. mkdir $USB_MOUNT
  72. fi
  73. if [ -f /dev/mapper/encrypted_usb ]; then
  74. rm -rf /dev/mapper/encrypted_usb
  75. fi
  76. cryptsetup luksClose encrypted_usb
  77. # mount the encrypted backup drive
  78. cryptsetup luksOpen $USB_DRIVE encrypted_usb
  79. if [ "$?" = "0" ]; then
  80. USB_DRIVE=/dev/mapper/encrypted_usb
  81. fi
  82. mount $USB_DRIVE $USB_MOUNT
  83. if [ ! "$?" = "0" ]; then
  84. echo $"There was a problem mounting the USB drive to $USB_MOUNT"
  85. rm -rf $USB_MOUNT
  86. exit 2
  87. fi
  88. }
  89. function unmount_drive {
  90. sync
  91. umount $USB_MOUNT
  92. if [ ! "$?" = "0" ]; then
  93. echo $"Unable to unmount the drive. This means that the backup did not work"
  94. rm -rf $USB_MOUNT
  95. exit 9
  96. fi
  97. rm -rf $USB_MOUNT
  98. echo $"Setting permissions"
  99. for d in /home/*/ ; do
  100. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  101. if [[ $USERNAME != "git" ]]; then
  102. chown -R $USERNAME:$USERNAME /home/$USERNAME
  103. fi
  104. done
  105. if [[ $USB_DRIVE == /dev/mapper/encrypted_usb ]]; then
  106. echo $"Unmount encrypted USB"
  107. cryptsetup luksClose encrypted_usb
  108. fi
  109. if [ -f /dev/mapper/encrypted_usb ]; then
  110. rm -rf /dev/mapper/encrypted_usb
  111. fi
  112. }
  113. function check_backup_exists {
  114. if [ ! -d $USB_MOUNT/backup ]; then
  115. echo $"No backup directory found on the USB drive."
  116. unmount_drive
  117. exit 2
  118. fi
  119. }
  120. function check_admin_user {
  121. echo $"Checking that admin user exists"
  122. if [ ! -d /home/$ADMIN_USERNAME ]; then
  123. echo $"Username $ADMIN_USERNAME not found. Reinstall ${PROJECT_NAME} with this username."
  124. unmount_drive
  125. exit 295
  126. fi
  127. }
  128. function copy_gpg_keys {
  129. echo $"Copying GPG keys from admin user to root"
  130. cp -r /home/$ADMIN_USERNAME/.gnupg /root
  131. }
  132. function restore_directory_from_usb {
  133. if [ ! -d ${1} ]; then
  134. mkdir ${1}
  135. fi
  136. obnam restore -r $USB_MOUNT/backup/${2} --to ${1}
  137. }
  138. function restore_database {
  139. RESTORE_SUBDIR="root"
  140. if [ -d $USB_MOUNT/backup/${1} ]; then
  141. echo $"Restoring ${1} database"
  142. restore_directory_from_usb "/root/temp${1}data" "${1}data"
  143. if [ ! -f /root/temp${1}data/${RESTORE_SUBDIR}/temp${1}data/${1}.sql ]; then
  144. echo $"Unable to restore ${1} database"
  145. rm -rf /root/temp${1}data
  146. unmount_drive
  147. exit 503
  148. fi
  149. mysqlsuccess=$(mysql -u root --password=$DATABASE_PASSWORD ${1} -o < /root/temp${1}data/${RESTORE_SUBDIR}/temp${1}data/${1}.sql)
  150. if [ ! "$?" = "0" ]; then
  151. echo "$mysqlsuccess"
  152. unmount_drive
  153. exit 964
  154. fi
  155. shred -zu /root/temp${1}data/${RESTORE_SUBDIR}/temp${1}data/*
  156. rm -rf /root/temp${1}data
  157. echo $"Restoring ${1} installation"
  158. if [ ! -d /root/temp${1} ]; then
  159. mkdir /root/temp${1}
  160. fi
  161. restore_directory_from_usb "/root/temp${1}" "${1}"
  162. RESTORE_SUBDIR="var"
  163. if [ ${2} ]; then
  164. if [ -d /var/www/${2}/htdocs ]; then
  165. if [ -d /root/temp${1}/${RESTORE_SUBDIR}/www/${2}/htdocs ]; then
  166. rm -rf /var/www/${2}/htdocs
  167. mv /root/temp${1}/${RESTORE_SUBDIR}/www/${2}/htdocs /var/www/${2}/
  168. if [ ! "$?" = "0" ]; then
  169. unmount_drive
  170. exit 683
  171. fi
  172. if [ -d /etc/letsencrypt/live/${2} ]; then
  173. ln -s /etc/letsencrypt/live/${2}/privkey.pem /etc/ssl/private/${2}.key
  174. ln -s /etc/letsencrypt/live/${2}/fullchain.pem /etc/ssl/certs/${2}.pem
  175. else
  176. # Ensure that the bundled SSL cert is being used
  177. if [ -f /etc/ssl/certs/${2}.bundle.crt ]; then
  178. sed -i "s|${2}.crt|${2}.bundle.crt|g" /etc/nginx/sites-available/${2}
  179. fi
  180. fi
  181. fi
  182. fi
  183. fi
  184. fi
  185. }
  186. function update_domains {
  187. if grep -q "Gogs domain" $COMPLETION_FILE; then
  188. GIT_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Gogs domain" | awk -F ':' '{print $2}')
  189. fi
  190. }
  191. function same_admin_user {
  192. PREV_ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
  193. if [[ "$PREV_ADMIN_USERNAME" != "$ADMIN_USERNAME" ]]; then
  194. echo $"The admin username has changed from $PREV_ADMIN_USERNAME to $ADMIN_USERNAME. To restore you will first need to install a new ${PROJECT_NAME} system with an initial admin user named $PREV_ADMIN_USERNAME"
  195. unmount_drive
  196. exit 73265
  197. fi
  198. }
  199. function restore_gogs {
  200. if [ $GIT_DOMAIN_NAME ]; then
  201. restore_database gogs ${GIT_DOMAIN_NAME}
  202. if [ -d $USB_MOUNT/backup/gogs ]; then
  203. echo $"Restoring Gogs settings"
  204. if [ ! -d /home/git/go/src/github.com/gogits/gogs/custom ]; then
  205. mkdir -p /home/git/go/src/github.com/gogits/gogs/custom
  206. fi
  207. cp -r /root/tempgogs/home/git/go/src/github.com/gogits/gogs/custom/* /home/git/go/src/github.com/gogits/gogs/custom
  208. if [ ! "$?" = "0" ]; then
  209. unmount_drive
  210. exit 981
  211. fi
  212. echo $"Restoring Gogs repos"
  213. restore_directory_from_usb /root/tempgogsrepos gogsrepos
  214. cp -r /root/tempgogsrepos/home/git/gogs-repositories/* /home/git/gogs-repositories/
  215. if [ ! "$?" = "0" ]; then
  216. unmount_drive
  217. exit 67574
  218. fi
  219. echo $"Restoring Gogs authorized_keys"
  220. restore_directory_from_usb /root/tempgogsssh gogsssh
  221. if [ ! -d /home/git/.ssh ]; then
  222. mkdir /home/git/.ssh
  223. fi
  224. cp -r /root/tempgogsssh/home/git/.ssh/* /home/git/.ssh/
  225. if [ ! "$?" = "0" ]; then
  226. unmount_drive
  227. exit 8463
  228. fi
  229. rm -rf /root/tempgogs
  230. rm -rf /root/tempgogsrepos
  231. rm -rf /root/tempgogsssh
  232. chown -R git:git /home/git
  233. fi
  234. fi
  235. }
  236. mount_drive $1 $2
  237. check_backup_exists
  238. check_admin_user
  239. copy_gpg_keys
  240. same_admin_user
  241. update_domains
  242. restore_gogs
  243. unmount_drive
  244. echo $"Restore Gogs from USB drive is complete. You can now unplug it."
  245. exit 0