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