소스 검색

Restore Hubzilla command

Bob Mottram 9 년 전
부모
커밋
911ef6873c
5개의 변경된 파일198개의 추가작업 그리고 4개의 파일을 삭제
  1. 3
    1
      Makefile
  2. BIN
      man/freedombone-restore-hubzilla.1.gz
  3. 2
    2
      src/freedombone-backup-remote
  4. 191
    0
      src/freedombone-restore-hubzilla
  5. 2
    1
      src/freedombone-restore-remote

+ 3
- 1
Makefile 파일 보기

@@ -22,7 +22,8 @@ install:
22 22
 	install -m 755 src/${APP}-backup-local ${DESTDIR}${PREFIX}/bin/backup
23 23
 	install -m 755 src/${APP}-backup-local ${DESTDIR}${PREFIX}/bin/backup2friends
24 24
 	install -m 755 src/${APP}-restore-local ${DESTDIR}${PREFIX}/bin/restore
25
-	install -m 755 src/${APP}-restore-local ${DESTDIR}${PREFIX}/bin/restorefromfriend
25
+	install -m 755 src/${APP}-restore-remote ${DESTDIR}${PREFIX}/bin/restorefromfriend
26
+	install -m 755 src/${APP}-restore-hubzilla ${DESTDIR}${PREFIX}/bin/restorehubzilla
26 27
 	mkdir -m 755 -p ${DESTDIR}${PREFIX}/share/man/man1
27 28
 	install -m 644 man/*.1.gz ${DESTDIR}${PREFIX}/share/man/man1
28 29
 	bash -c "./translate install"
@@ -36,6 +37,7 @@ uninstall:
36 37
 	rm -f ${PREFIX}/bin/backup2friends
37 38
 	rm -f ${PREFIX}/bin/restore
38 39
 	rm -f ${PREFIX}/bin/restorefromfriend
40
+	rm -f ${PREFIX}/bin/restorehubzilla
39 41
 	rm -f ${PREFIX}/bin/meshweb
40 42
 	rm -rf /etc/freedombone
41 43
 	bash -c "./translate uninstall"

BIN
man/freedombone-restore-hubzilla.1.gz 파일 보기


+ 2
- 2
src/freedombone-backup-remote 파일 보기

@@ -31,7 +31,7 @@
31 31
 PROJECT_NAME='freedombone'
32 32
 COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
33 33
 
34
-export TEXTDOMAIN=${PROJECT_NAME}-backup-local
34
+export TEXTDOMAIN=${PROJECT_NAME}-backup-remote
35 35
 export TEXTDOMAINDIR="/usr/share/locale"
36 36
 
37 37
 # Temporary location for data to be backed up to other servers
@@ -42,7 +42,7 @@ ADMIN_NAME=$(getent passwd $ADMIN_USERNAME | cut -d: -f5 | cut -d, -f1)
42 42
 ADMIN_EMAIL_ADDRESS=${ADMIN_USERNAME}@${HOSTNAME}
43 43
 if [ ! -f /etc/ssl/private/backup.key ]; then
44 44
     echo $"Creating backup key"
45
-    freedombone-addcert -h backup --dhkey 2048
45
+    ${PROJECT_NAME}-addcert -h backup --dhkey 2048
46 46
 fi
47 47
 
48 48
 if [ ! -f /home/${ADMIN_USERNAME}/backup.list ]; then

+ 191
- 0
src/freedombone-restore-hubzilla 파일 보기

@@ -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

+ 2
- 1
src/freedombone-restore-remote 파일 보기

@@ -31,7 +31,7 @@
31 31
 PROJECT_NAME='freedombone'
32 32
 COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
33 33
 
34
-export TEXTDOMAIN=${PROJECT_NAME}-restore-local
34
+export TEXTDOMAIN=${PROJECT_NAME}-restore-remote
35 35
 export TEXTDOMAINDIR="/usr/share/locale"
36 36
 
37 37
 SERVER_NAME=$1
@@ -476,6 +476,7 @@ if grep -q "Gogs domain" $COMPLETION_FILE; then
476 476
     fi
477 477
 fi
478 478
 
479
+# restore wiki
479 480
 if [ -d $SERVER_DIRECTORY/backup/wiki ]; then
480 481
     WIKI_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Wiki domain" | awk -F ':' '{print $2}')
481 482
     echo $"Restoring Wiki installation $WIKI_DOMAIN_NAME"