freedombone-restore-hubzilla 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Backup to 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. export TEXTDOMAIN=${PROJECT_NAME}-restore-hubzilla
  32. export TEXTDOMAINDIR="/usr/share/locale"
  33. USB_DRIVE=/dev/sdb1
  34. if [ $1 ]; then
  35. USB_DRIVE=/dev/${1}1
  36. fi
  37. USB_MOUNT=/mnt/usb
  38. # Get the admin username
  39. ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
  40. if [ $2 ]; then
  41. ADMIN_USERNAME=$2
  42. fi
  43. if [ ! -b $USB_DRIVE ]; then
  44. echo $"Please attach a USB drive"
  45. exit 1
  46. fi
  47. if [ ! -d $USB_MOUNT ]; then
  48. mkdir $USB_MOUNT
  49. if [ -f /dev/mapper/encrypted_usb ]; then
  50. rm -rf /dev/mapper/encrypted_usb
  51. fi
  52. cryptsetup luksClose encrypted_usb
  53. cryptsetup luksOpen $USB_DRIVE encrypted_usb
  54. if [ "$?" = "0" ]; then
  55. USB_DRIVE=/dev/mapper/encrypted_usb
  56. fi
  57. mount $USB_DRIVE $USB_MOUNT
  58. fi
  59. if [ ! -d $USB_MOUNT/backup ]; then
  60. echo $"No backup directory found on the USB drive."
  61. umount $USB_MOUNT
  62. rm -rf $USB_MOUNT
  63. exit 2
  64. fi
  65. echo $"Checking that admin user exists"
  66. if [ ! -d /home/$ADMIN_USERNAME ]; then
  67. echo $"Username $ADMIN_USERNAME not found. Reinstall ${PROJECT_NAME} with this username."
  68. umount $USB_MOUNT
  69. rm -rf $USB_MOUNT
  70. exit 295
  71. fi
  72. echo $"Copying GPG keys to root"
  73. cp -r /home/$ADMIN_USERNAME/.gnupg /root
  74. # MariaDB password
  75. DATABASE_PASSWORD=$(cat /root/dbpass)
  76. function restore_directory_from_usb {
  77. BACKUP_CERTIFICATE=/etc/ssl/private/backup.key
  78. if [ ! -d ${1} ]; then
  79. mkdir ${1}
  80. fi
  81. obnam restore -r $USB_MOUNT/backup/${2} --to ${1}
  82. }
  83. function restore_database {
  84. RESTORE_SUBDIR="root"
  85. if [ -d $USB_MOUNT/backup/${1} ]; then
  86. echo $"Restoring ${1} database"
  87. restore_directory_from_usb "/root/temp${1}data" "${1}data"
  88. if [ ! -f /root/temp${1}data/${RESTORE_SUBDIR}/temp${1}data/${1}.sql ]; then
  89. echo $"Unable to restore ${1} database"
  90. rm -rf /root/temp${1}data
  91. umount $USB_MOUNT
  92. rm -rf $USB_MOUNT
  93. exit 503
  94. fi
  95. mysqlsuccess=$(mysql -u root --password=$DATABASE_PASSWORD ${1} -o < /root/temp${1}data/${RESTORE_SUBDIR}/temp${1}data/${1}.sql)
  96. if [ ! "$?" = "0" ]; then
  97. echo "$mysqlsuccess"
  98. umount $USB_MOUNT
  99. rm -rf $USB_MOUNT
  100. exit 964
  101. fi
  102. shred -zu /root/temp${1}data/${RESTORE_SUBDIR}/temp${1}data/*
  103. rm -rf /root/temp${1}data
  104. echo $"Restoring ${1} installation"
  105. if [ ! -d /root/temp${1} ]; then
  106. mkdir /root/temp${1}
  107. fi
  108. restore_directory_from_usb "/root/temp${1}" "${1}"
  109. RESTORE_SUBDIR="var"
  110. if [ ${2} ]; then
  111. if [ -d /var/www/${2}/htdocs ]; then
  112. if [ -d /root/temp${1}/${RESTORE_SUBDIR}/www/${2}/htdocs ]; then
  113. rm -rf /var/www/${2}/htdocs
  114. mv /root/temp${1}/${RESTORE_SUBDIR}/www/${2}/htdocs /var/www/${2}/
  115. if [ ! "$?" = "0" ]; then
  116. umount $USB_MOUNT
  117. rm -rf $USB_MOUNT
  118. exit 683
  119. fi
  120. if [ -d /etc/letsencrypt/live/${2} ]; then
  121. ln -s /etc/letsencrypt/live/${2}/privkey.pem /etc/ssl/private/${2}.key
  122. ln -s /etc/letsencrypt/live/${2}/fullchain.pem /etc/ssl/certs/${2}.pem
  123. else
  124. # Ensure that the bundled SSL cert is being used
  125. if [ -f /etc/ssl/certs/${2}.bundle.crt ]; then
  126. sed -i "s|${2}.crt|${2}.bundle.crt|g" /etc/nginx/sites-available/${2}
  127. fi
  128. fi
  129. fi
  130. fi
  131. fi
  132. fi
  133. }
  134. # Restoring hubzilla
  135. if grep -q "Hubzilla domain" $COMPLETION_FILE; then
  136. HUBZILLA_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Hubzilla domain" | awk -F ':' '{print $2}')
  137. restore_database hubzilla ${HUBZILLA_DOMAIN_NAME}
  138. if [ -d $USB_MOUNT/backup/hubzilla ]; then
  139. if [ ! -d /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/store/[data]/smarty3 ]; then
  140. mkdir -p /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/store/[data]/smarty3
  141. fi
  142. chmod 777 /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/store/[data]/smarty3
  143. chown -R www-data:www-data /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/*
  144. if [ -d /root/temphubzilla ]; then
  145. rm -rf /root/temphubzilla
  146. fi
  147. fi
  148. fi
  149. sync
  150. # Unmount the USB drive
  151. umount $USB_MOUNT
  152. rm -rf $USB_MOUNT
  153. # Restart the web server
  154. systemctl restart nginx
  155. systemctl restart php5-fpm
  156. echo $"Setting permissions"
  157. for d in /home/*/ ; do
  158. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  159. if [[ $USERNAME != "git" ]]; then
  160. chown -R $USERNAME:$USERNAME /home/$USERNAME
  161. fi
  162. done
  163. if [[ $USB_DRIVE == /dev/mapper/encrypted_usb ]]; then
  164. echo $"Unmount encrypted USB"
  165. cryptsetup luksClose encrypted_usb
  166. fi
  167. if [ -f /dev/mapper/encrypted_usb ]; then
  168. rm -rf /dev/mapper/encrypted_usb
  169. fi
  170. echo $"Hubzilla Restore from USB drive is complete. You can now remove it."
  171. exit 0