Bläddra i källkod

Tidying of restore command

Bob Mottram 9 år sedan
förälder
incheckning
68442896af
2 ändrade filer med 213 tillägg och 148 borttagningar
  1. 25
    12
      src/freedombone-backup-local
  2. 188
    136
      src/freedombone-restore-local

+ 25
- 12
src/freedombone-backup-local Visa fil

@@ -53,18 +53,21 @@ if [ -f /root/dbpass ]; then
53 53
     DATABASE_PASSWORD=$(cat /root/dbpass)
54 54
 fi
55 55
 
56
-MICROBLOG_DOMAIN_NAME='microblog'
57
-if grep -q "GNU Social domain" $COMPLETION_FILE; then
58
-    MICROBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "GNU Social domain" | awk -F ':' '{print $2}')
59
-fi
60
-HUBZILLA_DOMAIN_NAME='hubzilla'
61
-if grep -q "Hubzilla domain" $COMPLETION_FILE; then
62
-    HUBZILLA_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Hubzilla domain" | awk -F ':' '{print $2}')
63
-fi
64
-FULLBLOG_DOMAIN_NAME='blog'
65
-if grep -q "Blog domain" $COMPLETION_FILE; then
66
-    FULLBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Blog domain" | awk -F ':' '{print $2}')
67
-fi
56
+
57
+function update_domains {
58
+	MICROBLOG_DOMAIN_NAME='microblog'
59
+	if grep -q "GNU Social domain" $COMPLETION_FILE; then
60
+		MICROBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "GNU Social domain" | awk -F ':' '{print $2}')
61
+	fi
62
+	HUBZILLA_DOMAIN_NAME='hubzilla'
63
+	if grep -q "Hubzilla domain" $COMPLETION_FILE; then
64
+		HUBZILLA_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Hubzilla domain" | awk -F ':' '{print $2}')
65
+	fi
66
+	FULLBLOG_DOMAIN_NAME='blog'
67
+	if grep -q "Blog domain" $COMPLETION_FILE; then
68
+		FULLBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Blog domain" | awk -F ':' '{print $2}')
69
+	fi
70
+}
68 71
 
69 72
 function mount_drive {
70 73
     if [ $1 ]; then
@@ -354,6 +357,7 @@ mount_drive $1 $2
354 357
 remove_backup_directory $remove_option
355 358
 make_backup_directory
356 359
 check_storage_space_remaining
360
+update_domains
357 361
 backup_users
358 362
 
359 363
 if [ -d /home/git/go/src/github.com/gogits ]; then
@@ -368,6 +372,15 @@ fi
368 372
 
369 373
 backup_directories
370 374
 
375
+# configuration files
376
+echo $"Backing up ${PROJECT_NAME} configuration files"
377
+if [ ! -d /root/tempbackupconfig ]; then
378
+    mkdir -p /root/tempbackupconfig
379
+fi
380
+cp -f $CONFIG_FILE /root/tempbackupconfig
381
+cp -f $COMPLETION_FILE /root/tempbackupconfig
382
+backup_directory_to_usb /root/tempbackupconfig config
383
+
371 384
 # Backup admin user README file
372 385
 if [ -f /home/$ADMIN_USERNAME/README ]; then
373 386
     echo $"Backing up README"

+ 188
- 136
src/freedombone-restore-local Visa fil

@@ -35,9 +35,6 @@ export TEXTDOMAIN=${PROJECT_NAME}-restore-local
35 35
 export TEXTDOMAINDIR="/usr/share/locale"
36 36
 
37 37
 USB_DRIVE=/dev/sdb1
38
-if [ $1 ]; then
39
-    USB_DRIVE=/dev/${1}1
40
-fi
41 38
 USB_MOUNT=/mnt/usb
42 39
 
43 40
 # get default USB from config file
@@ -48,52 +45,110 @@ if [ -f $CONFIG_FILE ]; then
48 45
     fi
49 46
 fi
50 47
 
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
48
+ADMIN_USERNAME=
49
+ADMIN_NAME=
56 50
 
57
-if [ ! -b $USB_DRIVE ]; then
58
-    echo $"Please attach a USB drive"
59
-    exit 1
60
-fi
51
+# MariaDB password
52
+DATABASE_PASSWORD=$(cat /root/dbpass)
53
+
54
+MICROBLOG_DOMAIN_NAME=
55
+HUBZILLA_DOMAIN_NAME=
56
+OWNCLOUD_DOMAIN_NAME=
57
+GIT_DOMAIN_NAME=
58
+WIKI_DOMAIN_NAME=
59
+FULLBLOG_DOMAIN_NAME=
60
+
61
+function mount_drive {
62
+    if [ $1 ]; then
63
+        USB_DRIVE=/dev/${1}1
64
+    fi
65
+
66
+    # get the admin user
67
+    ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
68
+    if [ $2 ]; then
69
+        ADMIN_USERNAME=$2
70
+    fi
71
+    ADMIN_NAME=$(getent passwd $ADMIN_USERNAME | cut -d: -f5 | cut -d, -f1)
72
+
73
+    # check that the backup destination is available
74
+    if [ ! -b $USB_DRIVE ]; then
75
+        echo $"Please attach a USB drive"
76
+        exit 1
77
+    fi
61 78
 
62
-if [ ! -d $USB_MOUNT ]; then
63
-    mkdir $USB_MOUNT
79
+    # unmount if already mounted
80
+    umount -f $USB_MOUNT
81
+    if [ ! -d $USB_MOUNT ]; then
82
+        mkdir $USB_MOUNT
83
+    fi
64 84
     if [ -f /dev/mapper/encrypted_usb ]; then
65 85
         rm -rf /dev/mapper/encrypted_usb
66 86
     fi
67 87
     cryptsetup luksClose encrypted_usb
88
+
89
+    # mount the encrypted backup drive
68 90
     cryptsetup luksOpen $USB_DRIVE encrypted_usb
69 91
     if [ "$?" = "0" ]; then
70 92
         USB_DRIVE=/dev/mapper/encrypted_usb
71 93
     fi
72 94
     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
95
+    if [ ! "$?" = "0" ]; then
96
+        echo $"There was a problem mounting the USB drive to $USB_MOUNT"
97
+        rm -rf $USB_MOUNT
98
+        exit 2
99
+    fi
100
+}
80 101
 
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."
102
+function unmount_drive {
103
+    sync
84 104
     umount $USB_MOUNT
105
+    if [ ! "$?" = "0" ]; then
106
+        echo $"Unable to unmount the drive. This means that the backup did not work"
107
+        rm -rf $USB_MOUNT
108
+        exit 9
109
+    fi
85 110
     rm -rf $USB_MOUNT
86
-    exit 295
87
-fi
88 111
 
89
-echo $"Copying GPG keys to root"
90
-cp -r /home/$ADMIN_USERNAME/.gnupg /root
112
+    echo $"Setting permissions"
113
+    for d in /home/*/ ; do
114
+        USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
115
+        if [[ $USERNAME != "git" ]]; then
116
+            chown -R $USERNAME:$USERNAME /home/$USERNAME
117
+        fi
118
+    done
91 119
 
92
-# MariaDB password
93
-DATABASE_PASSWORD=$(cat /root/dbpass)
120
+    if [[ $USB_DRIVE == /dev/mapper/encrypted_usb ]]; then
121
+        echo $"Unmount encrypted USB"
122
+        cryptsetup luksClose encrypted_usb
123
+    fi
124
+    if [ -f /dev/mapper/encrypted_usb ]; then
125
+        rm -rf /dev/mapper/encrypted_usb
126
+    fi
127
+}
128
+
129
+function check_backup_exists {
130
+    if [ ! -d $USB_MOUNT/backup ]; then
131
+        echo $"No backup directory found on the USB drive."
132
+        unmount_drive
133
+        exit 2
134
+    fi
135
+}
136
+
137
+function check_admin_user {
138
+    echo $"Checking that admin user exists"
139
+    if [ ! -d /home/$ADMIN_USERNAME ]; then
140
+        echo $"Username $ADMIN_USERNAME not found. Reinstall ${PROJECT_NAME} with this username."
141
+        unmount_drive
142
+        exit 295
143
+    fi
144
+}
145
+
146
+function copy_gpg_keys {
147
+    echo $"Copying GPG keys from admin user to root"
148
+    cp -r /home/$ADMIN_USERNAME/.gnupg /root
149
+}
94 150
 
95 151
 function restore_directory_from_usb {
96
-    BACKUP_CERTIFICATE=/etc/ssl/private/backup.key
97 152
     if [ ! -d ${1} ]; then
98 153
         mkdir ${1}
99 154
     fi
@@ -109,15 +164,13 @@ function restore_database {
109 164
         if [ ! -f /root/temp${1}data/${RESTORE_SUBDIR}/temp${1}data/${1}.sql ]; then
110 165
             echo $"Unable to restore ${1} database"
111 166
             rm -rf /root/temp${1}data
112
-            umount $USB_MOUNT
113
-            rm -rf $USB_MOUNT
167
+            unmount_drive
114 168
             exit 503
115 169
         fi
116 170
         mysqlsuccess=$(mysql -u root --password=$DATABASE_PASSWORD ${1} -o < /root/temp${1}data/${RESTORE_SUBDIR}/temp${1}data/${1}.sql)
117 171
         if [ ! "$?" = "0" ]; then
118 172
             echo "$mysqlsuccess"
119
-            umount $USB_MOUNT
120
-            rm -rf $USB_MOUNT
173
+            unmount_drive
121 174
             exit 964
122 175
         fi
123 176
         shred -zu /root/temp${1}data/${RESTORE_SUBDIR}/temp${1}data/*
@@ -134,8 +187,7 @@ function restore_database {
134 187
                     rm -rf /var/www/${2}/htdocs
135 188
                     mv /root/temp${1}/${RESTORE_SUBDIR}/www/${2}/htdocs /var/www/${2}/
136 189
                     if [ ! "$?" = "0" ]; then
137
-                        umount $USB_MOUNT
138
-                        rm -rf $USB_MOUNT
190
+                        unmount_drive
139 191
                         exit 683
140 192
                     fi
141 193
                     if [ -d /etc/letsencrypt/live/${2} ]; then
@@ -153,6 +205,65 @@ function restore_database {
153 205
     fi
154 206
 }
155 207
 
208
+function update_domains {
209
+    if grep -q "GNU Social domain" $COMPLETION_FILE; then
210
+        MICROBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "GNU Social domain" | awk -F ':' '{print $2}')
211
+    fi
212
+    if grep -q "Hubzilla domain" $COMPLETION_FILE; then
213
+        HUBZILLA_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Hubzilla domain" | awk -F ':' '{print $2}')
214
+    fi
215
+    if grep -q "Owncloud domain" $COMPLETION_FILE; then
216
+        OWNCLOUD_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Owncloud domain" | awk -F ':' '{print $2}')
217
+    fi
218
+    if grep -q "Gogs domain" $COMPLETION_FILE; then
219
+        GIT_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Gogs domain" | awk -F ':' '{print $2}')
220
+    fi
221
+    if [ -d $USB_MOUNT/backup/wiki ]; then
222
+        WIKI_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Wiki domain" | awk -F ':' '{print $2}')
223
+    fi
224
+    if [ -d $USB_MOUNT/backup/blog ]; then
225
+        FULLBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Blog domain" | awk -F ':' '{print $2}')
226
+    fi
227
+}
228
+
229
+function restore_configuration {
230
+    # this restores *.cfg and COMPLETION_FILE
231
+    if [ -d $USB_MOUNT/backup/config ]; then
232
+        echo $"Restoring configuration files"
233
+        restore_directory_from_usb /root/tempconfig config
234
+        cp -f /root/tempconfig/root/${PROJECT_NAME}.cfg /homeroot/${PROJECT_NAME}.cfg
235
+        if [ ! "$?" = "0" ]; then
236
+            unmount_drive
237
+            rm -rf /root/tempconfig
238
+            exit 5294
239
+        fi
240
+        cp -f /root/tempconfig/root/${PROJECT_NAME}-completed.txt /root/${PROJECT_NAME}-completed.txt
241
+        if [ ! "$?" = "0" ]; then
242
+            unmount_drive
243
+            rm -rf /root/tempconfig
244
+            exit 6382
245
+        fi
246
+        rm -rf /root/tempconfig
247
+    fi
248
+}
249
+
250
+function same_admin_user {
251
+    PREV_ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
252
+    if [[ "$PREV_ADMIN_USERNAME" != "$ADMIN_USERNAME" ]]; then
253
+        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"
254
+        unmount_drive
255
+        exit 73265
256
+    fi
257
+}
258
+
259
+mount_drive $1 $2
260
+check_backup_exists
261
+check_admin_user
262
+copy_gpg_keys
263
+restore_configuration
264
+same_admin_user
265
+update_domains
266
+
156 267
 # Make a backup of the original README file
157 268
 # incase old passwords need to be used
158 269
 if [ -f /home/$ADMIN_USERNAME/README ]; then
@@ -179,8 +290,7 @@ if [ -d $USB_MOUNT/backup/mariadb ]; then
179 290
         fi
180 291
         if [ ! "$?" = "0" ]; then
181 292
             echo "$mysqlsuccess"
182
-            umount $USB_MOUNT
183
-            rm -rf $USB_MOUNT
293
+            unmount_drive
184 294
             exit 962
185 295
         fi
186 296
         echo $"Restarting database"
@@ -217,9 +327,8 @@ if [ -d $USB_MOUNT/backup/mutt ]; then
217 327
                 cp -f /root/tempmutt/home/$USERNAME/tempbackup/Muttrc /etc/Muttrc
218 328
             fi
219 329
             if [ ! "$?" = "0" ]; then
220
-                umount $USB_MOUNT
221
-                rm -rf $USB_MOUNT
222 330
                 rm -rf /root/tempmutt
331
+                unmount_drive
223 332
                 exit 276
224 333
             fi
225 334
             rm -rf /root/tempmutt
@@ -238,17 +347,15 @@ if [ -d $USB_MOUNT/backup/gnupg ]; then
238 347
             restore_directory_from_usb /root/tempgnupg gnupg/$USERNAME
239 348
             cp -r /root/tempgnupg/home/$USERNAME/.gnupg /home/$USERNAME/
240 349
             if [ ! "$?" = "0" ]; then
241
-                umount $USB_MOUNT
242
-                rm -rf $USB_MOUNT
243 350
                 rm -rf /root/tempgnupg
351
+                unmount_drive
244 352
                 exit 276
245 353
             fi
246 354
             rm -rf /root/tempgnupg
247 355
             if [[ "$USERNAME" == "$ADMIN_USERNAME" ]]; then
248 356
                 cp -r /home/$USERNAME/.gnupg /root
249 357
                 if [ ! "$?" = "0" ]; then
250
-                    umount $USB_MOUNT
251
-                    rm -rf $USB_MOUNT
358
+                    unmount_drive
252 359
                     exit 283
253 360
                 fi
254 361
             fi
@@ -267,9 +374,8 @@ if [ -d $USB_MOUNT/backup/procmail ]; then
267 374
             restore_directory_from_usb /root/tempprocmail procmail/$USERNAME
268 375
             cp -f /root/tempprocmail/home/$USERNAME/tempbackup/.procmailrc /home/$USERNAME/
269 376
             if [ ! "$?" = "0" ]; then
270
-                umount $USB_MOUNT
271
-                rm -rf $USB_MOUNT
272 377
                 rm -rf /root/tempprocmail
378
+                unmount_drive
273 379
                 exit 276
274 380
             fi
275 381
             rm -rf /root/tempprocmail
@@ -289,9 +395,8 @@ if [ -d $USB_MOUNT/backup/spamassassin ]; then
289 395
                 restore_directory_from_usb /root/tempspamassassin spamassassin/$USERNAME
290 396
                 cp -rf /root/tempspamassassin/home/$USERNAME/.spamassassin /home/$USERNAME/
291 397
                 if [ ! "$?" = "0" ]; then
292
-                    umount $USB_MOUNT
293
-                    rm -rf $USB_MOUNT
294 398
                     rm -rf /root/tempspamassassin
399
+                    unmount_drive
295 400
                     exit 276
296 401
                 fi
297 402
                 rm -rf /root/tempspamassassin
@@ -305,9 +410,8 @@ if [ -d $USB_MOUNT/backup/readme ]; then
305 410
     restore_directory_from_usb /root/tempreadme readme
306 411
     cp -f /root/tempreadme/home/$ADMIN_USERNAME/tempbackup/README /home/$ADMIN_USERNAME/
307 412
     if [ ! "$?" = "0" ]; then
308
-        umount $USB_MOUNT
309
-        rm -rf $USB_MOUNT
310 413
         rm -rf /root/tempreadme
414
+        unmount_drive
311 415
         exit 276
312 416
     fi
313 417
     rm -rf /root/tempreadme
@@ -318,9 +422,8 @@ if [ -d $USB_MOUNT/backup/ipfs ]; then
318 422
     restore_directory_from_usb /root/tempipfs ipfs
319 423
     cp -rf /root/tempipfs/home/$ADMIN_USERNAME/.ipfs/* /home/$ADMIN_USERNAME/.ipfs
320 424
     if [ ! "$?" = "0" ]; then
321
-        umount $USB_MOUNT
322
-        rm -rf $USB_MOUNT
323 425
         rm -rf /root/tempipfs
426
+        unmount_drive
324 427
         exit 276
325 428
     fi
326 429
     rm -rf /root/tempipfs
@@ -337,9 +440,8 @@ if [ -d $USB_MOUNT/backup/ssh ]; then
337 440
             restore_directory_from_usb /root/tempssh ssh/$USERNAME
338 441
             cp -r /root/tempssh/home/$USERNAME/.ssh /home/$USERNAME/
339 442
             if [ ! "$?" = "0" ]; then
340
-                umount $USB_MOUNT
341
-                rm -rf $USB_MOUNT
342 443
                 rm -rf /root/tempssh
444
+                unmount_drive
343 445
                 exit 664
344 446
             fi
345 447
             rm -rf /root/tempssh
@@ -358,9 +460,8 @@ if [ -d $USB_MOUNT/backup/config ]; then
358 460
             restore_directory_from_usb /root/tempconfig config/$USERNAME
359 461
             cp -r /root/tempconfig/home/$USERNAME/.config /home/$USERNAME/
360 462
             if [ ! "$?" = "0" ]; then
361
-                umount $USB_MOUNT
362
-                rm -rf $USB_MOUNT
363 463
                 rm -rf /root/tempconfig
464
+                unmount_drive
364 465
                 exit 664
365 466
             fi
366 467
             rm -rf /root/tempconfig
@@ -374,8 +475,7 @@ if [ -d $USB_MOUNT/backup/ssl ]; then
374 475
     restore_directory_from_usb /root/tempssl ssl
375 476
     cp -r /root/tempssl/etc/ssl/* /etc/ssl
376 477
     if [ ! "$?" = "0" ]; then
377
-        umount $USB_MOUNT
378
-        rm -rf $USB_MOUNT
478
+        unmount_drive
379 479
         exit 276
380 480
     fi
381 481
     rm -rf /root/tempssl
@@ -395,8 +495,7 @@ if [ -d $USB_MOUNT/backup/projects ]; then
395 495
             fi
396 496
             mv /root/tempprojects/home/$USERNAME/projects /home/$USERNAME
397 497
             if [ ! "$?" = "0" ]; then
398
-                umount $USB_MOUNT
399
-                rm -rf $USB_MOUNT
498
+                unmount_drive
400 499
                 exit 166
401 500
             fi
402 501
             rm -rf /root/tempprojects
@@ -419,8 +518,7 @@ if [ -d $USB_MOUNT/backup/personal ]; then
419 518
                 fi
420 519
                 mv /root/temppersonal/home/$USERNAME/personal /home/$USERNAME
421 520
                 if [ ! "$?" = "0" ]; then
422
-                    umount $USB_MOUNT
423
-                    rm -rf $USB_MOUNT
521
+                    unmount_drive
424 522
                     exit 184
425 523
                 fi
426 524
                 rm -rf /root/temppersonal
@@ -434,8 +532,7 @@ if [ -d /var/spool/mlmmj ]; then
434 532
     restore_directory_from_usb /root/tempmailinglist mailinglist
435 533
     cp -r /root/tempmailinglist/root/spool/mlmmj/* /var/spool/mlmmj
436 534
     if [ ! "$?" = "0" ]; then
437
-        umount $USB_MOUNT
438
-        rm -rf $USB_MOUNT
535
+        unmount_drive
439 536
         exit 526
440 537
     fi
441 538
     rm -rf /root/tempmailinglist
@@ -446,8 +543,7 @@ if [ -d /var/lib/prosody ]; then
446 543
     restore_directory_from_usb /root/tempxmpp xmpp
447 544
     cp -r /root/tempxmpp/var/lib/prosody/* /var/lib/prosody
448 545
     if [ ! "$?" = "0" ]; then
449
-        umount $USB_MOUNT
450
-        rm -rf $USB_MOUNT
546
+        unmount_drive
451 547
         exit 725
452 548
     fi
453 549
     rm -rf /root/tempxmpp
@@ -456,8 +552,7 @@ if [ -d /var/lib/prosody ]; then
456 552
 fi
457 553
 
458 554
 # Restoring GNU Social
459
-if grep -q "GNU Social domain" $COMPLETION_FILE; then
460
-    MICROBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "GNU Social domain" | awk -F ':' '{print $2}')
555
+if [ $MICROBLOG_DOMAIN_NAME ]; then
461 556
     restore_database gnusocial ${MICROBLOG_DOMAIN_NAME}
462 557
     if [ -d /root/tempgnusocial ]; then
463 558
         rm -rf /root/tempgnusocial
@@ -465,8 +560,7 @@ if grep -q "GNU Social domain" $COMPLETION_FILE; then
465 560
 fi
466 561
 
467 562
 # Restoring hubzilla
468
-if grep -q "Hubzilla domain" $COMPLETION_FILE; then
469
-    HUBZILLA_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Hubzilla domain" | awk -F ':' '{print $2}')
563
+if [ $HUBZILLA_DOMAIN_NAME ]; then
470 564
     restore_database hubzilla ${HUBZILLA_DOMAIN_NAME}
471 565
     if [ -d $USB_MOUNT/backup/hubzilla ]; then
472 566
         if [ ! -d /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/store/[data]/smarty3 ]; then
@@ -480,15 +574,13 @@ if grep -q "Hubzilla domain" $COMPLETION_FILE; then
480 574
     fi
481 575
 fi
482 576
 
483
-if grep -q "Owncloud domain" $COMPLETION_FILE; then
484
-    OWNCLOUD_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Owncloud domain" | awk -F ':' '{print $2}')
577
+if [ $OWNCLOUD_DOMAIN_NAME ]; then
485 578
     restore_database owncloud $OWNCLOUD_DOMAIN_NAME
486 579
     if [ -d $USB_MOUNT/backup/owncloud2 ]; then
487 580
         restore_directory_from_usb /root/tempowncloud2 owncloud2
488 581
         cp -r /root/tempowncloud2/etc/owncloud/* /etc/owncloud/
489 582
         if [ ! "$?" = "0" ]; then
490
-            umount $USB_MOUNT
491
-            rm -rf $USB_MOUNT
583
+            unmount_drive
492 584
             exit 982
493 585
         fi
494 586
         rm -rf /root/tempowncloud
@@ -506,8 +598,7 @@ if grep -q "Owncloud domain" $COMPLETION_FILE; then
506 598
     fi
507 599
 fi
508 600
 
509
-if grep -q "Gogs domain" $COMPLETION_FILE; then
510
-    GIT_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Gogs domain" | awk -F ':' '{print $2}')
601
+if [ $GIT_DOMAIN_NAME ]; then
511 602
     restore_database gogs ${GIT_DOMAIN_NAME}
512 603
     if [ -d $USB_MOUNT/backup/gogs ]; then
513 604
         echo $"Restoring Gogs settings"
@@ -516,16 +607,14 @@ if grep -q "Gogs domain" $COMPLETION_FILE; then
516 607
         fi
517 608
         cp -r /root/tempgogs/home/git/go/src/github.com/gogits/gogs/custom/* /home/git/go/src/github.com/gogits/gogs/custom
518 609
         if [ ! "$?" = "0" ]; then
519
-            umount $USB_MOUNT
520
-            rm -rf $USB_MOUNT
610
+            unmount_drive
521 611
             exit 981
522 612
         fi
523 613
         echo $"Restoring Gogs repos"
524 614
         restore_directory_from_usb /root/tempgogsrepos gogsrepos
525 615
         cp -r /root/tempgogsrepos/home/git/gogs-repositories/* /home/git/gogs-repositories/
526 616
         if [ ! "$?" = "0" ]; then
527
-            umount $USB_MOUNT
528
-            rm -rf $USB_MOUNT
617
+            unmount_drive
529 618
             exit 67574
530 619
         fi
531 620
         echo $"Restoring Gogs authorized_keys"
@@ -535,8 +624,7 @@ if grep -q "Gogs domain" $COMPLETION_FILE; then
535 624
         fi
536 625
         cp -r /root/tempgogsssh/home/git/.ssh/* /home/git/.ssh/
537 626
         if [ ! "$?" = "0" ]; then
538
-            umount $USB_MOUNT
539
-            rm -rf $USB_MOUNT
627
+            unmount_drive
540 628
             exit 8463
541 629
         fi
542 630
         rm -rf /root/tempgogs
@@ -546,21 +634,18 @@ if grep -q "Gogs domain" $COMPLETION_FILE; then
546 634
     fi
547 635
 fi
548 636
 
549
-if [ -d $USB_MOUNT/backup/wiki ]; then
550
-    WIKI_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Wiki domain" | awk -F ':' '{print $2}')
637
+if [ $WIKI_DOMAIN_NAME ]; then
551 638
     echo $"Restoring Wiki installation ${WIKI_DOMAIN_NAME}"
552 639
     restore_directory_from_usb /root/tempwiki wiki
553 640
     cp -r /root/tempwiki/var/lib/dokuwiki/* /var/lib/dokuwiki/
554 641
     if [ ! "$?" = "0" ]; then
555
-        umount $USB_MOUNT
556
-        rm -rf $USB_MOUNT
642
+        unmount_drive
557 643
         exit 868
558 644
     fi
559 645
     restore_directory_from_usb /root/tempwiki2 wiki2
560 646
     cp -r /root/tempwiki2/etc/dokuwiki/* /etc/dokuwiki/
561 647
     if [ ! "$?" = "0" ]; then
562
-        umount $USB_MOUNT
563
-        rm -rf $USB_MOUNT
648
+        unmount_drive
564 649
         exit 869
565 650
     fi
566 651
     rm -rf /root/tempwiki
@@ -576,22 +661,19 @@ if [ -d $USB_MOUNT/backup/wiki ]; then
576 661
     fi
577 662
 fi
578 663
 
579
-if [ -d $USB_MOUNT/backup/blog ]; then
580
-    FULLBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Blog domain" | awk -F ':' '{print $2}')
664
+if [ $FULLBLOG_DOMAIN_NAME ]; then
581 665
     echo $"Restoring blog installation"
582 666
     restore_directory_from_usb /root/tempblog blog
583 667
     rm -rf /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs
584 668
     cp -r /root/tempblog/var/www/${FULLBLOG_DOMAIN_NAME}/htdocs /var/www/${FULLBLOG_DOMAIN_NAME}/
585 669
     if [ ! "$?" = "0" ]; then
586
-        umount $USB_MOUNT
587
-        rm -rf $USB_MOUNT
670
+        unmount_drive
588 671
         exit 593
589 672
     fi
590 673
     rm -rf /root/tempblog
591 674
     if [ ! -d /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content ]; then
592 675
         echo $"No content directory found after restoring blog"
593
-        umount $USB_MOUNT
594
-        rm -rf $USB_MOUNT
676
+        unmount_drive
595 677
         exit 287
596 678
     fi
597 679
     chown -R www-data:www-data /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs
@@ -619,8 +701,7 @@ if [ -d $USB_MOUNT/backup/cjdns ]; then
619 701
     rm -rf /etc/cjdns
620 702
     cp -r /root/tempcjdns/etc/cjdns /etc/
621 703
     if [ ! "$?" = "0" ]; then
622
-        umount $USB_MOUNT
623
-        rm -rf $USB_MOUNT
704
+        unmount_drive
624 705
         exit 8472
625 706
     fi
626 707
     rm -rf /root/tempcjdns
@@ -640,8 +721,7 @@ if [ -d $USB_MOUNT/backup/mail ]; then
640 721
             fi
641 722
             tar -xzvf /root/tempmail/root/tempbackupemail/$USERNAME/maildir.tar.gz -C /
642 723
             if [ ! "$?" = "0" ]; then
643
-                umount $USB_MOUNT
644
-                rm -rf $USB_MOUNT
724
+                unmount_drive
645 725
                 exit 927
646 726
             fi
647 727
             rm -rf /root/tempmail
@@ -655,8 +735,8 @@ if [ -d /var/cache/minidlna ]; then
655 735
         restore_directory_from_usb /root/tempdlna dlna
656 736
         cp -r /root/tempdlna/var/cache/minidlna/* /var/cache/minidlna/
657 737
         if [ ! "$?" = "0" ]; then
658
-            umount $USB_MOUNT
659
-            rm -rf $USB_MOUNT
738
+            rm -rf /root/tempdlna
739
+            unmount_drive
660 740
             exit 982
661 741
         fi
662 742
         rm -rf /root/tempdlna
@@ -668,23 +748,20 @@ if [ -d $USB_MOUNT/backup/voip ]; then
668 748
     restore_directory_from_usb /root/tempvoip voip
669 749
     cp -f /root/tempvoip/home/$ADMIN_USERNAME/tempbackup/mumble-server.ini /etc/
670 750
     if [ ! "$?" = "0" ]; then
671
-        umount $USB_MOUNT
672
-        rm -rf $USB_MOUNT
673 751
         rm -rf /root/tempvoip
752
+        unmount_drive
674 753
         exit 3679
675 754
     fi
676 755
     cp -f /root/tempvoip/home/$ADMIN_USERNAME/tempbackup/sipwitch.conf /etc/sipwitch.conf
677 756
     if [ ! "$?" = "0" ]; then
678
-        umount $USB_MOUNT
679
-        rm -rf $USB_MOUNT
680 757
         rm -rf /root/tempvoip
758
+        unmount_drive
681 759
         exit 3679
682 760
     fi
683 761
     cp -f /root/tempvoip/home/$ADMIN_USERNAME/tempbackup/mumble-server.sqlite /var/lib/mumble-server/
684 762
     if [ ! "$?" = "0" ]; then
685
-        umount $USB_MOUNT
686
-        rm -rf $USB_MOUNT
687 763
         rm -rf /root/tempvoip
764
+        unmount_drive
688 765
         exit 276
689 766
     fi
690 767
     rm -rf /root/tempvoip
@@ -699,45 +776,20 @@ if [ -d $USB_MOUNT/backup/tox ]; then
699 776
     echo $"Restoring Tox node settings"
700 777
     restore_directory_from_usb / tox
701 778
     if [ ! "$?" = "0" ]; then
702
-        umount $USB_MOUNT
703
-        rm -rf $USB_MOUNT
779
+        unmount_drive
704 780
         exit 6393
705 781
     fi
706 782
     cp /var/lib/tox-bootstrapd/tox-bootstrapd.conf /etc/tox-bootstrapd.conf
707 783
     systemctl restart tox-bootstrapd.service
708 784
     if [ ! "$?" = "0" ]; then
709 785
         systemctl status tox-bootstrapd.service
710
-        umount $USB_MOUNT
711
-        rm -rf $USB_MOUNT
786
+        unmount_drive
712 787
         exit 59369
713 788
     fi
714 789
 fi
715 790
 
716
-sync
717
-
718
-# Unmount the USB drive
719
-umount $USB_MOUNT
720
-rm -rf $USB_MOUNT
721
-
722
-# Restart the web server
723
-systemctl restart nginx
724
-systemctl restart php5-fpm
791
+unmount_drive
725 792
 
726
-echo $"Setting permissions"
727
-for d in /home/*/ ; do
728
-    USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
729
-    if [[ $USERNAME != "git" ]]; then
730
-        chown -R $USERNAME:$USERNAME /home/$USERNAME
731
-    fi
732
-done
733
-
734
-if [[ $USB_DRIVE == /dev/mapper/encrypted_usb ]]; then
735
-    echo $"Unmount encrypted USB"
736
-    cryptsetup luksClose encrypted_usb
737
-fi
738
-if [ -f /dev/mapper/encrypted_usb ]; then
739
-    rm -rf /dev/mapper/encrypted_usb
740
-fi
793
+echo $"Restore from USB drive is complete. You can now unplug it."
741 794
 
742
-echo $"Restore from USB drive is complete. You can now remove it."
743 795
 exit 0