Kaynağa Gözat

Improve hubzilla restore command

Bob Mottram 9 yıl önce
ebeveyn
işleme
0b140417a5
1 değiştirilmiş dosya ile 81 ekleme ve 59 silme
  1. 81
    59
      src/freedombone-restore-hubzilla

+ 81
- 59
src/freedombone-restore-hubzilla Dosyayı Görüntüle

@@ -48,52 +48,95 @@ if [ -f $CONFIG_FILE ]; then
48 48
     fi
49 49
 fi
50 50
 
51
-# Get the admin username
52
-ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
53
-if [ $2 ]; then
54
-    ADMIN_USERNAME=$2
55
-fi
51
+ADMIN_USERNAME=
52
+ADMIN_NAME=
56 53
 
57
-if [ ! -b $USB_DRIVE ]; then
58
-    echo $"Please attach a USB drive"
59
-    exit 1
60
-fi
54
+# MariaDB password
55
+DATABASE_PASSWORD=$(cat /root/dbpass)
56
+
57
+function mount_drive {
58
+    if [ $1 ]; then
59
+        USB_DRIVE=/dev/${1}1
60
+    fi
61 61
 
62
-if [ ! -d $USB_MOUNT ]; then
63
-    mkdir $USB_MOUNT
62
+    # get the admin user
63
+    ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
64
+    if [ $2 ]; then
65
+        ADMIN_USERNAME=$2
66
+    fi
67
+    ADMIN_NAME=$(getent passwd $ADMIN_USERNAME | cut -d: -f5 | cut -d, -f1)
68
+
69
+    # check that the backup destination is available
70
+    if [ ! -b $USB_DRIVE ]; then
71
+        echo $"Please attach a USB drive"
72
+        exit 1
73
+    fi
74
+
75
+    # unmount if already mounted
76
+    umount -f $USB_MOUNT
77
+    if [ ! -d $USB_MOUNT ]; then
78
+        mkdir $USB_MOUNT
79
+    fi
64 80
     if [ -f /dev/mapper/encrypted_usb ]; then
65 81
         rm -rf /dev/mapper/encrypted_usb
66 82
     fi
67 83
     cryptsetup luksClose encrypted_usb
84
+
85
+    # mount the encrypted backup drive
68 86
     cryptsetup luksOpen $USB_DRIVE encrypted_usb
69 87
     if [ "$?" = "0" ]; then
70 88
         USB_DRIVE=/dev/mapper/encrypted_usb
71 89
     fi
72 90
     mount $USB_DRIVE $USB_MOUNT
73
-fi
74
-if [ ! -d $USB_MOUNT/backup ]; then
75
-    echo $"No backup directory found on the USB drive."
76
-    umount $USB_MOUNT
77
-    rm -rf $USB_MOUNT
78
-    exit 2
79
-fi
91
+    if [ ! "$?" = "0" ]; then
92
+        echo $"There was a problem mounting the USB drive to $USB_MOUNT"
93
+        rm -rf $USB_MOUNT
94
+        exit 2
95
+    fi
96
+}
80 97
 
81
-echo $"Checking that admin user exists"
82
-if [ ! -d /home/$ADMIN_USERNAME ]; then
83
-    echo $"Username $ADMIN_USERNAME not found. Reinstall ${PROJECT_NAME} with this username."
98
+function unmount_drive {
99
+    sync
84 100
     umount $USB_MOUNT
101
+    if [ ! "$?" = "0" ]; then
102
+        echo $"Unable to unmount the drive. This means that the backup did not work"
103
+        rm -rf $USB_MOUNT
104
+        exit 9
105
+    fi
85 106
     rm -rf $USB_MOUNT
86
-    exit 295
87
-fi
88 107
 
89
-echo $"Copying GPG keys to root"
90
-cp -r /home/$ADMIN_USERNAME/.gnupg /root
108
+    if [[ $USB_DRIVE == /dev/mapper/encrypted_usb ]]; then
109
+        echo $"Unmount encrypted USB"
110
+        cryptsetup luksClose encrypted_usb
111
+    fi
112
+    if [ -f /dev/mapper/encrypted_usb ]; then
113
+        rm -rf /dev/mapper/encrypted_usb
114
+    fi
115
+}
116
+
117
+function check_backup_exists {
118
+    if [ ! -d $USB_MOUNT/backup ]; then
119
+        echo $"No backup directory found on the USB drive."
120
+        unmount_drive
121
+        exit 2
122
+    fi
123
+}
91 124
 
92
-# MariaDB password
93
-DATABASE_PASSWORD=$(cat /root/dbpass)
125
+function check_admin_user {
126
+    echo $"Checking that admin user exists"
127
+    if [ ! -d /home/$ADMIN_USERNAME ]; then
128
+        echo $"Username $ADMIN_USERNAME not found. Reinstall ${PROJECT_NAME} with this username."
129
+        unmount_drive
130
+        exit 295
131
+    fi
132
+}
133
+
134
+function copy_gpg_keys {
135
+    echo $"Copying GPG keys from admin user to root"
136
+    cp -r /home/$ADMIN_USERNAME/.gnupg /root
137
+}
94 138
 
95 139
 function restore_directory_from_usb {
96
-    BACKUP_CERTIFICATE=/etc/ssl/private/backup.key
97 140
     if [ ! -d ${1} ]; then
98 141
         mkdir ${1}
99 142
     fi
@@ -109,15 +152,13 @@ function restore_database {
109 152
         if [ ! -f /root/temp${1}data/${RESTORE_SUBDIR}/temp${1}data/${1}.sql ]; then
110 153
             echo $"Unable to restore ${1} database"
111 154
             rm -rf /root/temp${1}data
112
-            umount $USB_MOUNT
113
-            rm -rf $USB_MOUNT
155
+			unmount_drive
114 156
             exit 503
115 157
         fi
116 158
         mysqlsuccess=$(mysql -u root --password=$DATABASE_PASSWORD ${1} -o < /root/temp${1}data/${RESTORE_SUBDIR}/temp${1}data/${1}.sql)
117 159
         if [ ! "$?" = "0" ]; then
118 160
             echo "$mysqlsuccess"
119
-            umount $USB_MOUNT
120
-            rm -rf $USB_MOUNT
161
+			unmount_drive
121 162
             exit 964
122 163
         fi
123 164
         shred -zu /root/temp${1}data/${RESTORE_SUBDIR}/temp${1}data/*
@@ -134,8 +175,7 @@ function restore_database {
134 175
                     rm -rf /var/www/${2}/htdocs
135 176
                     mv /root/temp${1}/${RESTORE_SUBDIR}/www/${2}/htdocs /var/www/${2}/
136 177
                     if [ ! "$?" = "0" ]; then
137
-                        umount $USB_MOUNT
138
-                        rm -rf $USB_MOUNT
178
+						unmount_drive
139 179
                         exit 683
140 180
                     fi
141 181
                     if [ -d /etc/letsencrypt/live/${2} ]; then
@@ -153,6 +193,11 @@ function restore_database {
153 193
     fi
154 194
 }
155 195
 
196
+mount_drive $1 $2
197
+check_backup_exists
198
+check_admin_user
199
+copy_gpg_keys
200
+
156 201
 # Restoring hubzilla
157 202
 if grep -q "Hubzilla domain" $COMPLETION_FILE; then
158 203
     HUBZILLA_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Hubzilla domain" | awk -F ':' '{print $2}')
@@ -169,31 +214,8 @@ if grep -q "Hubzilla domain" $COMPLETION_FILE; then
169 214
     fi
170 215
 fi
171 216
 
172
-sync
173
-
174
-# Unmount the USB drive
175
-umount $USB_MOUNT
176
-rm -rf $USB_MOUNT
177
-
178
-# Restart the web server
179
-systemctl restart nginx
180
-systemctl restart php5-fpm
181
-
182
-echo $"Setting permissions"
183
-for d in /home/*/ ; do
184
-    USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
185
-    if [[ $USERNAME != "git" ]]; then
186
-        chown -R $USERNAME:$USERNAME /home/$USERNAME
187
-    fi
188
-done
189
-
190
-if [[ $USB_DRIVE == /dev/mapper/encrypted_usb ]]; then
191
-    echo $"Unmount encrypted USB"
192
-    cryptsetup luksClose encrypted_usb
193
-fi
194
-if [ -f /dev/mapper/encrypted_usb ]; then
195
-    rm -rf /dev/mapper/encrypted_usb
196
-fi
217
+unmount_drive
197 218
 
198 219
 echo $"Hubzilla Restore from USB drive is complete. You can now remove it."
220
+
199 221
 exit 0