浏览代码

Tidying backup command

Bob Mottram 9 年前
父节点
当前提交
2d1add80ae
共有 1 个文件被更改,包括 174 次插入176 次删除
  1. 174
    176
      src/freedombone-backup-local

+ 174
- 176
src/freedombone-backup-local 查看文件

@@ -34,72 +34,89 @@ COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
34 34
 export TEXTDOMAIN=${PROJECT_NAME}-backup-local
35 35
 export TEXTDOMAINDIR="/usr/share/locale"
36 36
 
37
+# directories to be backed up (source,dest)
38
+backup_dirs=(
39
+    "/etc/letsencrypt,            letsencrypt"
40
+    "/var/lib/dokuwiki,           wiki"
41
+    "/etc/dokuwiki,               wiki2"
42
+    "/etc/ssl,                    ssl"
43
+    "/var/spool/mlmmj,            mailinglist"
44
+    "/var/lib/prosody,            xmpp"
45
+    "/etc/nginx/sites-available,  web"
46
+    "/home/$ADMIN_USERNAME/.ipfs, ipfs"
47
+    "/var/cache/minidlna,         dlna"
48
+)
49
+
37 50
 USB_DRIVE=/dev/sdb1
38
-if [ $1 ]; then
39
-    USB_DRIVE=/dev/${1}1
40
-fi
41 51
 USB_MOUNT=/mnt/usb
42 52
 
43
-# get the admin user
44
-ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
45
-if [ $2 ]; then
46
-    ADMIN_USERNAME=$2
47
-fi
48
-ADMIN_NAME=$(getent passwd $ADMIN_USERNAME | cut -d: -f5 | cut -d, -f1)
53
+ADMIN_USERNAME=
54
+ADMIN_NAME=
49 55
 
50
-# check that the backup destination is available
51
-if [ ! -b $USB_DRIVE ]; then
52
-    echo $"Please attach a USB drive"
53
-    exit 1
56
+DATABASE_PASSWORD=''
57
+if [ -f /root/dbpass ]; then
58
+    DATABASE_PASSWORD=$(cat /root/dbpass)
54 59
 fi
55 60
 
56
-# unmount if already mounted
57
-umount -f $USB_MOUNT
58
-if [ ! -d $USB_MOUNT ]; then
59
-    mkdir $USB_MOUNT
60
-fi
61
-if [ -f /dev/mapper/encrypted_usb ]; then
62
-    rm -rf /dev/mapper/encrypted_usb
63
-fi
64
-cryptsetup luksClose encrypted_usb
61
+function mount_drive {
62
+    if [ $1 ]; then
63
+        USB_DRIVE=/dev/${1}1
64
+    fi
65 65
 
66
-# mount the encrypted backup drive
67
-cryptsetup luksOpen $USB_DRIVE encrypted_usb
68
-if [ "$?" = "0" ]; then
69
-    USB_DRIVE=/dev/mapper/encrypted_usb
70
-fi
71
-mount $USB_DRIVE $USB_MOUNT
72
-if [ ! "$?" = "0" ]; then
73
-    echo $"There was a problem mounting the USB drive to $USB_MOUNT"
74
-    rm -rf $USB_MOUNT
75
-    exit 2
76
-fi
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)
77 72
 
78
-# make a backup directory on the drive
79
-if [ ! -d $USB_MOUNT/backup ]; then
80
-    mkdir $USB_MOUNT/backup
81
-fi
82
-if [ ! -d $USB_MOUNT/backup ]; then
83
-    echo $"There was a problem making the directory $USB_MOUNT/backup."
84
-    umount $USB_MOUNT
85
-    rm -rf $USB_MOUNT
86
-    exit 3
87
-fi
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
78
+
79
+    # unmount if already mounted
80
+    umount -f $USB_MOUNT
81
+    if [ ! -d $USB_MOUNT ]; then
82
+        mkdir $USB_MOUNT
83
+    fi
84
+    if [ -f /dev/mapper/encrypted_usb ]; then
85
+        rm -rf /dev/mapper/encrypted_usb
86
+    fi
87
+    cryptsetup luksClose encrypted_usb
88 88
 
89
-# Check space remaining on the usb drive
90
-used_percent=$(df -k $USB_MOUNT | tail -n 1 | awk -F ' ' '{print $5}' | awk -F '%' '{print $1}')
91
-if [ $used_percent -gt 95 ]; then
92
-    echo $"Less than 5% of space remaining on backup drive"
89
+    # mount the encrypted backup drive
90
+    cryptsetup luksOpen $USB_DRIVE encrypted_usb
91
+    if [ "$?" = "0" ]; then
92
+        USB_DRIVE=/dev/mapper/encrypted_usb
93
+    fi
94
+    mount $USB_DRIVE $USB_MOUNT
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
+}
101
+
102
+function unmount_drive {
103
+    sync
93 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
94 110
     rm -rf $USB_MOUNT
95
-    exit 4
96
-fi
97
-
98
-# MariaDB password
99
-DATABASE_PASSWORD=''
100
-if [ -f /root/dbpass ]; then
101
-    DATABASE_PASSWORD=$(cat /root/dbpass)
102
-fi
111
+    if [[ $USB_DRIVE == /dev/mapper/encrypted_usb ]]; then
112
+        echo $"Unmount encrypted USB"
113
+        cryptsetup luksClose encrypted_usb
114
+    fi
115
+    if [ -f /dev/mapper/encrypted_usb ]; then
116
+        rm -rf /dev/mapper/encrypted_usb
117
+    fi
118
+    echo $"Backup to USB drive is complete. You can now unplug it."
119
+}
103 120
 
104 121
 function backup_database {
105 122
     if [ ${#DATABASE_PASSWORD} -lt 2 ]; then
@@ -159,83 +176,121 @@ function backup_directory_to_usb {
159 176
     fi
160 177
 }
161 178
 
162
-# Backup user files
163
-for d in /home/*/ ; do
164
-    USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
165
-    if [[ $USERNAME != "git" ]]; then
179
+function make_backup_directory {
180
+    # make a backup directory on the drive
181
+    if [ ! -d $USB_MOUNT/backup ]; then
182
+        mkdir $USB_MOUNT/backup
183
+    fi
184
+    if [ ! -d $USB_MOUNT/backup ]; then
185
+        echo $"There was a problem making the directory $USB_MOUNT/backup."
186
+        umount $USB_MOUNT
187
+        rm -rf $USB_MOUNT
188
+        exit 3
189
+    fi
190
+}
191
+
192
+function check_storage_space_remaining {
193
+    # Check space remaining on the usb drive
194
+    used_percent=$(df -k $USB_MOUNT | tail -n 1 | awk -F ' ' '{print $5}' | awk -F '%' '{print $1}')
195
+    if [ $used_percent -gt 95 ]; then
196
+        echo $"Less than 5% of space remaining on backup drive"
197
+        umount $USB_MOUNT
198
+        rm -rf $USB_MOUNT
199
+        exit 4
200
+    fi
201
+}
166 202
 
167
-        # Backup any gpg keys
168
-        if [ -d /home/$USERNAME/.gnupg ]; then
169
-            echo $"Backing up gpg keys for $USERNAME"
170
-            backup_directory_to_usb /home/$USERNAME/.gnupg gnupg/$USERNAME
171
-        fi
203
+function backup_users {
204
+    # Backup user files
205
+    for d in /home/*/ ; do
206
+        USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
207
+        if [[ $USERNAME != "git" ]]; then
172 208
 
173
-        # Backup any personal settings
174
-        if [ -d /home/$USERNAME/personal ]; then
175
-            echo $"Backing up personal settings for $USERNAME"
176
-            backup_directory_to_usb /home/$USERNAME/personal personal/$USERNAME
177
-        fi
209
+            # Backup any gpg keys
210
+            if [ -d /home/$USERNAME/.gnupg ]; then
211
+                echo $"Backing up gpg keys for $USERNAME"
212
+                backup_directory_to_usb /home/$USERNAME/.gnupg gnupg/$USERNAME
213
+            fi
178 214
 
179
-        # Backup ssh keys
180
-        if [ -d /home/$USERNAME/.ssh ]; then
181
-            echo $"Backing up ssh keys for $USERNAME"
182
-            backup_directory_to_usb /home/$USERNAME/.ssh ssh/$USERNAME
183
-        fi
215
+            # Backup any personal settings
216
+            if [ -d /home/$USERNAME/personal ]; then
217
+                echo $"Backing up personal settings for $USERNAME"
218
+                backup_directory_to_usb /home/$USERNAME/personal personal/$USERNAME
219
+            fi
184 220
 
185
-        # Backup user configs
186
-        if [ -d /home/$USERNAME/.config ]; then
187
-            echo $"Backing up config files for $USERNAME"
188
-            backup_directory_to_usb /home/$USERNAME/.config config/$USERNAME
189
-        fi
221
+            # Backup ssh keys
222
+            if [ -d /home/$USERNAME/.ssh ]; then
223
+                echo $"Backing up ssh keys for $USERNAME"
224
+                backup_directory_to_usb /home/$USERNAME/.ssh ssh/$USERNAME
225
+            fi
190 226
 
191
-        # Backup mutt
192
-        if [ -f /home/$USERNAME/.muttrc ]; then
193
-            echo $"Backing up Mutt settings for $USERNAME"
194
-            if [ ! -d /home/$USERNAME/tempbackup ]; then
195
-                mkdir -p /home/$USERNAME/tempbackup
227
+            # Backup user configs
228
+            if [ -d /home/$USERNAME/.config ]; then
229
+                echo $"Backing up config files for $USERNAME"
230
+                backup_directory_to_usb /home/$USERNAME/.config config/$USERNAME
196 231
             fi
197
-            cp /home/$USERNAME/.muttrc /home/$USERNAME/tempbackup
198
-            if [ -f /etc/Muttrc ]; then
199
-                cp /etc/Muttrc /home/$USERNAME/tempbackup
232
+
233
+            # Backup mutt
234
+            if [ -f /home/$USERNAME/.muttrc ]; then
235
+                echo $"Backing up Mutt settings for $USERNAME"
236
+                if [ ! -d /home/$USERNAME/tempbackup ]; then
237
+                    mkdir -p /home/$USERNAME/tempbackup
238
+                fi
239
+                cp /home/$USERNAME/.muttrc /home/$USERNAME/tempbackup
240
+                if [ -f /etc/Muttrc ]; then
241
+                    cp /etc/Muttrc /home/$USERNAME/tempbackup
242
+                fi
243
+                backup_directory_to_usb /home/$USERNAME/tempbackup mutt/$USERNAME
200 244
             fi
201
-            backup_directory_to_usb /home/$USERNAME/tempbackup mutt/$USERNAME
202
-        fi
203 245
 
204
-        # Backup email
205
-        if [ -d /home/$USERNAME/Maildir ]; then
206
-            echo $"Creating an email archive for $USERNAME"
207
-            if [ ! -d /root/tempbackupemail/$USERNAME ]; then
208
-                mkdir -p /root/tempbackupemail/$USERNAME
246
+            # Backup email
247
+            if [ -d /home/$USERNAME/Maildir ]; then
248
+                echo $"Creating an email archive for $USERNAME"
249
+                if [ ! -d /root/tempbackupemail/$USERNAME ]; then
250
+                    mkdir -p /root/tempbackupemail/$USERNAME
251
+                fi
252
+                tar -czvf /root/tempbackupemail/$USERNAME/maildir.tar.gz /home/$USERNAME/Maildir
253
+                echo $"Backing up emails for $USERNAME"
254
+                backup_directory_to_usb /root/tempbackupemail/$USERNAME mail/$USERNAME
209 255
             fi
210
-            tar -czvf /root/tempbackupemail/$USERNAME/maildir.tar.gz /home/$USERNAME/Maildir
211
-            echo $"Backing up emails for $USERNAME"
212
-            backup_directory_to_usb /root/tempbackupemail/$USERNAME mail/$USERNAME
213
-        fi
214 256
 
215
-        # Backup spamassassin
216
-        if [ -d /home/$USERNAME/.spamassassin ]; then
217
-            echo $"Backing up spamassassin settings for $USERNAME"
218
-            backup_directory_to_usb /home/$USERNAME/.spamassassin spamassassin/$USERNAME
219
-        fi
257
+            # Backup spamassassin
258
+            if [ -d /home/$USERNAME/.spamassassin ]; then
259
+                echo $"Backing up spamassassin settings for $USERNAME"
260
+                backup_directory_to_usb /home/$USERNAME/.spamassassin spamassassin/$USERNAME
261
+            fi
220 262
 
221
-        # Backup procmail
222
-        if [ -f /home/$USERNAME/.procmailrc ]; then
223
-            echo $"Backing up procmail settings for $USERNAME"
224
-            if [ ! -d /home/$USERNAME/tempbackup ]; then
225
-                mkdir -p /home/$USERNAME/tempbackup
263
+            # Backup procmail
264
+            if [ -f /home/$USERNAME/.procmailrc ]; then
265
+                echo $"Backing up procmail settings for $USERNAME"
266
+                if [ ! -d /home/$USERNAME/tempbackup ]; then
267
+                    mkdir -p /home/$USERNAME/tempbackup
268
+                fi
269
+                cp /home/$USERNAME/.procmailrc /home/$USERNAME/tempbackup
270
+                backup_directory_to_usb /home/$USERNAME/tempbackup procmail/$USERNAME
226 271
             fi
227
-            cp /home/$USERNAME/.procmailrc /home/$USERNAME/tempbackup
228
-            backup_directory_to_usb /home/$USERNAME/tempbackup procmail/$USERNAME
272
+
229 273
         fi
274
+    done
275
+}
230 276
 
231
-    fi
232
-done
277
+function backup_directories {
278
+    for dr in "${backup_dirs[@]}"
279
+    do
280
+        source_directory=$(echo $dr | awk -F ',' '{print $1}'  | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
281
+        if [ -d $source_directory ]; then
282
+            dest_directory=$(echo $dr | awk -F ',' '{print $2}'  | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
283
+            echo $"Backing up $source_directory to $dest_directory"
284
+            backup_directory_to_usb $source_directory $dest_directory
285
+        fi
286
+    done
287
+}
233 288
 
234
-# Backup Let's Encrypt
235
-if [ -d /etc/letsencrypt ]; then
236
-    echo $"Backing up Lets Encrypt settings"
237
-    backup_directory_to_usb /etc/letsencrypt letsencrypt
238
-fi
289
+mount_drive $1 $2
290
+make_backup_directory
291
+check_storage_space_remaining
292
+backup_users
293
+backup_directories
239 294
 
240 295
 # backup gnusocial
241 296
 if grep -q "GNU Social domain" $COMPLETION_FILE; then
@@ -290,13 +345,6 @@ if [ -d /home/git/go/src/github.com/gogits ]; then
290 345
     backup_directory_to_usb /home/git/.ssh gogsssh
291 346
 fi
292 347
 
293
-# Backup wiki
294
-if [ -d /etc/dokuwiki ]; then
295
-    echo $"Obtaining wiki data backup"
296
-    backup_directory_to_usb /var/lib/dokuwiki wiki
297
-    backup_directory_to_usb /etc/dokuwiki wiki2
298
-fi
299
-
300 348
 # Backup blog
301 349
 if grep -q "Blog domain" $COMPLETION_FILE; then
302 350
     FULLBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Blog domain" | awk -F ':' '{print $2}')
@@ -310,30 +358,6 @@ if grep -q "Blog domain" $COMPLETION_FILE; then
310 358
     fi
311 359
 fi
312 360
 
313
-# Backup certificates
314
-if [ -d /etc/ssl ]; then
315
-    echo $"Backing up certificates"
316
-    backup_directory_to_usb /etc/ssl ssl
317
-fi
318
-
319
-# Backup the public mailing list
320
-if [ -d /var/spool/mlmmj ]; then
321
-    echo $"Backing up the public mailing list"
322
-    backup_directory_to_usb /var/spool/mlmmj mailinglist
323
-fi
324
-
325
-# Backup xmpp settings
326
-if [ -d /var/lib/prosody ]; then
327
-    echo $"Backing up the XMPP settings"
328
-    backup_directory_to_usb /var/lib/prosody xmpp
329
-fi
330
-
331
-# Backup web sites
332
-if [ -d /etc/nginx ]; then
333
-    echo $"Backing up web settings"
334
-    backup_directory_to_usb /etc/nginx/sites-available web
335
-fi
336
-
337 361
 # Backup admin user README file
338 362
 if [ -f /home/$ADMIN_USERNAME/README ]; then
339 363
     echo $"Backing up README"
@@ -344,18 +368,6 @@ if [ -f /home/$ADMIN_USERNAME/README ]; then
344 368
     backup_directory_to_usb /home/$ADMIN_USERNAME/tempbackup readme
345 369
 fi
346 370
 
347
-# Backup IPFS
348
-if [ -d /home/$ADMIN_USERNAME/.ipfs ]; then
349
-    echo $"Backing up IPFS"
350
-    backup_directory_to_usb /home/$ADMIN_USERNAME/.ipfs ipfs
351
-fi
352
-
353
-# Backup DLNA cache
354
-if [ -d /var/cache/minidlna ]; then
355
-    echo $"Backing up DLNA cache"
356
-    backup_directory_to_usb /var/cache/minidlna dlna
357
-fi
358
-
359 371
 # Backup VoIP settings
360 372
 if [ -f /etc/mumble-server.ini ]; then
361 373
     echo $"Backing up VoIP settings"
@@ -396,20 +408,6 @@ if [ -d /var/lib/tox-bootstrapd ]; then
396 408
     backup_directory_to_usb /var/lib/tox-bootstrapd tox
397 409
 fi
398 410
 
399
-sync
400
-umount $USB_MOUNT
401
-if [ ! "$?" = "0" ]; then
402
-    echo $"Unable to unmount the drive. This means that the backup did not work"
403
-    rm -rf $USB_MOUNT
404
-    exit 9
405
-fi
406
-rm -rf $USB_MOUNT
407
-if [[ $USB_DRIVE == /dev/mapper/encrypted_usb ]]; then
408
-    echo $"Unmount encrypted USB"
409
-    cryptsetup luksClose encrypted_usb
410
-fi
411
-if [ -f /dev/mapper/encrypted_usb ]; then
412
-    rm -rf /dev/mapper/encrypted_usb
413
-fi
414
-echo $"Backup to USB drive is complete. You can now unplug it."
411
+unmount_drive
412
+
415 413
 exit 0