-
Notifications
You must be signed in to change notification settings - Fork 0
/
mybackup
executable file
·381 lines (289 loc) · 14.1 KB
/
mybackup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/bin/bash
#Usage:
#
# mybackup - show list of machines
# myrestore - ''
#
# mybackup <machine> - show list of current backups with dates
# myrestore <machine> - ''
#
# $1 $2
# mybackup <machine> <BackupFolderName> - machine: love2d or sharon-pc
# myrestore <machine> <BackupFolderName> - by convention name is 'nn-descriptiveName' (so it sorts by sequence)
#
#
# mybackup --version - Only show version
# myrestore --version
version=2.0 # moved into git
if [ "$1" = "--version" ]; then echo version: ${version}; exit; fi
#################################################################
#################################################################
### PARTITIONS ######################
#Partition labels (also used for mount point folder names):
# Note: use labels rather than UUID as they might be more controllable.
BackupDrive='Linux backup' # USB backup drive (I removed space from 'name' & it removed it from 'label')
BackupBase="$BackupDrive/Backups" # Backup base folder directory & name
SubDir="files"
### PARTITIONS LABEL HELP:
#lsblk -o +label gives (note older method was blkid, but this suggests we use lsblk):
#NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT LABEL
#sda 8:0 0 465.8G 0 disk
#├─sda1 8:1 0 199M 0 part SYSTEM
#├─sda2 8:2 0 288.1G 0 part
#├─sda3 8:3 0 1K 0 part
#├─sda4 8:4 0 29.3G 0 part Shared
#├─sda5 8:5 0 23.3G 0 part d-root
#├─sda6 8:6 0 119.5G 0 part d-home
#└─sda7 8:7 0 5.4G 0 part [SWAP]
#sdc 8:32 0 3.7T 0 disk
#├─sdc1 8:33 0 128M 0 part
#├─sdc2 8:34 0 2.7T 0 part /media/ubuntu/Seagate Backup Plus Drive Seagate Backup Plus Drive
#└─sdc3 8:35 0 976.6G 0 part Linux backup
### MOUNTING 1of2 ######################
sudo umount "/mnt/$BackupDrive">& /dev/null # --- cleanup from prior failed attempt:
sudo mkdir "/mnt/$BackupDrive">& /dev/null; sudo mount -L "$BackupDrive" "/mnt/$BackupDrive" -o defaults,suid >& /dev/null #Allow to set user owner of files
########################################################################
### FUNCTIONS #################################################################
####################
function usage { echo; echo "Usage: ${0##*/} [machine name: Love2d|Sharon-pc] [BackupFolderName]";echo;}
####################
#If 1st parameter exists then just show dirs for that given machine, else show dirs for both machines
function myls {
echo -n "'$1' existing backups:"
if [ -d "$2" ]; then echo; ls -lFgG "$2" |grep -v ^total |grep ' [0-9][0-9]-' |sed 's/..................//'; else echo ' (none)'; fi;
}
##################
function currentbackups {
if [ "$1" ]; then
myls "$1" "/mnt/$BackupBase/$1/$SubDir"
else
myls 'Love2d' "/mnt/$BackupBase/Love2d/$SubDir"
echo
myls 'Sharon-pc' "/mnt/$BackupBase/Sharon-pc/$SubDir"
echo
fi
}
###################
function badmachine { echo "Machine type '$1' is invalid.";}
###################
function cleanup {
# echo "--- cleaning up --------------------------------------"
sudo umount "/mnt/$BackupDrive"; sudo rmdir "/mnt/$BackupDrive"
}
########################################################################
########################################################################
### CHECK INPUTS #######################################################
#Check if backup name paramter exists:
if [ $# = 0 ]; then usage; cleanup; exit; fi
if [ $# = 1 ]; then if [ "$1" != "Love2d" -a "$1" != "Sharon-pc" ]; then badmachine "$1"; usage; else usage; currentbackups "$1"; fi; cleanup; exit; fi
if [ $# = 2 ]; then if [ "$1" != "Love2d" -a "$1" != "Sharon-pc" ]; then badmachine "$1"; usage; cleanup; exit; fi; fi
### MOUNTING 2of2 ######################
#Tip use blkid to look at block labels
#Use e2label to set ext labels
#use gparted to set btrfs labels
if [ "$1" = 'Love2d' ]; then
# HOWARDS
MyRoot='d-root' # Love2 Debian / (root) partition name
MyHome='d-home' # Love2 Debian /home partition name
MyVar='d-var' # Love2 Debian /var partition name
# MyTmp= # (Don't back this up)
MyShared='Shared' # Love2 Debian Shared partition name
MyMedia='Media' # Love2 Debian Shared partition name - more static stuff, not FULL-backed up as often
sudo umount "/mnt/$MyRoot" >& /dev/null # --- cleanup from prior failed attempt:
sudo umount "/mnt/$MyHome" >& /dev/null
sudo umount "/mnt/$MyVar" >& /dev/null
sudo umount "/mnt/$MyShared" >& /dev/null
sudo umount "/mnt/$MyMedia" >& /dev/null
sudo umount "/media/ubuntu/$MyRoot" >& /dev/null # Unmount them from ubuntu so they will work properly
sudo umount "/media/ubuntu/$MyHome" >& /dev/null
sudo umount "/media/ubuntu/$MyVar" >& /dev/null
sudo umount "/media/ubuntu/$MyShared" >& /dev/null
sudo umount "/media/ubuntu/$MyMedia" >& /dev/null
sudo mkdir "/mnt/$MyRoot" >& /dev/null; sudo mount -L "$MyRoot" "/mnt/$MyRoot" >& /dev/null
sudo mkdir "/mnt/$MyHome" >& /dev/null; sudo mount -L "$MyHome" "/mnt/$MyHome" >& /dev/null
sudo mkdir "/mnt/$MyVar" >& /dev/null; sudo mount -L "$MyVar" "/mnt/$MyVar" >& /dev/null
sudo mkdir "/mnt/$MyShared" >& /dev/null; sudo mount -L "$MyShared" "/mnt/$MyShared" >& /dev/null
sudo mkdir "/mnt/$MyMedia" >& /dev/null; sudo mount -L "$MyMedia" "/mnt/$MyMedia" >& /dev/null
else
# SHARON's
# MyRoot='f3b7424c-0144-42a6-8488-62fbee94d245' # Sharon-pc Ubuntu / (root) partition name
# MyHome='a41eaa3e-bd31-4ebc-86d4-cf8ed5f3e779' # Sharon-pc Ubuntu /home partition name
MyRoot='s-root' # Sharon-pc Ubuntu / (root) partition name (includes var and tmp)
MyHome='s-home' # Sharon-pc Ubuntu /home partition name
sudo umount "/mnt/$MyRoot" >& /dev/null # --- cleanup from prior failed attempt:
sudo umount "/mnt/$MyHome" >& /dev/null
sudo umount "/media/ubuntu/$MyRoot" >& /dev/null # Unmount them from ubuntu so they will work properly
sudo umount "/media/ubuntu/$MyHome" >& /dev/null
#sudo mkdir "/mnt/$MyRoot" >& /dev/null; sudo mount -L "$MyRoot" "/mnt/$MyRoot" >& /dev/null
#sudo mkdir "/mnt/$MyHome" >& /dev/null; sudo mount -L "$MyHome" "/mnt/$MyHome" >& /dev/null
sudo mkdir "/mnt/$MyRoot" >& /dev/null; sudo mount -U "$MyRoot" "/mnt/$MyRoot" >& /dev/null
sudo mkdir "/mnt/$MyHome" >& /dev/null; sudo mount -U "$MyHome" "/mnt/$MyHome" >& /dev/null
fi
#=================================================================
BackupDir="$BackupBase/$1/$SubDir/$2" # /dir/BackupFolderName
#rSync stuff:
MyRsync="sudo rsync -aAXv --delete"
RootExclude=" --exclude={\"/dev/*\",\"/lost+found\",\"/media/*\",\"/mnt/*\",\"/proc/*\",\"/run/*\",\"/sys/*\",\"/tmp/*\"}"
#HomeExclude=" --filter='-/ howard/.cache/*'" #Exclude .cashe when subdir of howard
#HomeExclude=\ --filter=\\\'-/ howard/.cache\\\' #Exclude .cashe when subdir of howard
#echo "xxx${HomeExclude}yyy"; exit
#=================================================================
#Subdir names
ROOT_DIR=root
HOME_DIR=home
VAR_DIR=var
SHARED_DIR=Shared
MEDIA_DIR=Media
### Backup ######################
if [ "${0##*/}" = "mybackup" ]; then
#echo backing up...
echo; echo "--- Backing up: / -----------------------------------"
sudo mkdir -p "/mnt/$BackupDir/$ROOT_DIR" # Making directories to save backup to
$MyRsync $RootExclude "/mnt/$MyRoot/" "/mnt/$BackupDir/$ROOT_DIR/" ; echo
echo; echo "--- Backing up: /home -------------------------------"
sudo mkdir -p "/mnt/$BackupDir/$HOME_DIR"
# $MyRsync "$HomeExclude" "/mnt/$MyHome/" "/mnt/$BackupDir/$HOME_DIR/" ; echo CAN't get this to work
# So use this instead:
$MyRsync --filter='-/ howard/.cache' \
--filter='-/ howard/.googleearth/Cache' \
"/mnt/$MyHome/" "/mnt/$BackupDir/$HOME_DIR/" ; echo
if [ "$MyShared" ]; then #no var or shared partions on Sharon's machine
echo; echo "--- Backing up: /var --------------------------------"
sudo mkdir -p "/mnt/$BackupDir/$VAR_DIR"
$MyRsync "/mnt/$MyVar/" "/mnt/$BackupDir/$VAR_DIR/" ; echo
echo; echo "--- Backing up: Shared ------------------------------"
sudo mkdir -p "/mnt/$BackupDir/$SHARED_DIR"
$MyRsync --filter='- */cache2/' \
--filter='- */Cache/' \
"/mnt/$MyShared/" "/mnt/$BackupDir/$SHARED_DIR/" ; echo
fi
# Backup MBR (but only of sda ??)
sudo mkdir -p "/mnt/$BackupDir/MBR"
drive=`sudo blkid | grep $MyRoot | sed s/[0-9]:.*//`
part=`echo $drive | sed s#/dev/##`
echo;echo "--- Backing up: MBR of $part ----------------------"
sudo dd if="$drive" of="/mnt/$BackupDir/MBR/dd-$part-0-512" bs=1 count=512 ; echo
sudo dd if="$drive" of="/mnt/$BackupDir/MBR/dd-$part-0-1M" bs=1 count=1M
echo
echo of this drive:
lsblk | grep "$part"
### Backup media #################
elif [ "${0##*/}" = "mybackup.media" ]; then
if [ "$MyShared" ]; then #no var or shared partions on Sharon's machine
echo; echo "--- Backing up: Media -------------------------------"
sudo mkdir -p "/mnt/$BackupDir/$MEDIA_DIR"
$MyRsync "/mnt/$MyMedia/" "/mnt/$BackupDir/$MEDIA_DIR/" ; echo
else
echo "Error: No Media shared partion on Sharons machine"
fi
### Restore #####################
elif [ "${0##*/}" = "myrestore" ]; then
# Confirm
read -p "YOU ARE ABOUT TO OVERWRITE YOUR PARTITIONS - CONFIRM (y/N)?" -n 1 -r; echo;if [[ ! $REPLY =~ ^[Yy]$ ]]; then echo "Aborting.";exit; fi
echo;
read -p "DANGER! Really overwrite your hard disk partitions? (y/N)?" -n 1 -r; echo;if [[ ! $REPLY =~ ^[Yy]$ ]]; then echo "Aborting.";exit; fi
echo "--- Restoring: / -----------------------------------"
echo "--- Restoring: /home -------------------------------"
$MyRsync "/mnt/$BackupDir/$HOME_DIR/" "/mnt/$MyHome" ; echo
if [ "$MyShared" ]; then #no shared partion on Sharon's machine
echo "--- Restoring: /var --------------------------------"
$MyRsync "/mnt/$BackupDir/$VAR_DIR/" "/mnt/$MyVar" ; echo
echo "--- Restoring: Shared ------------------------------"
$MyRsync "/mnt/$BackupDir/$SHARED_DIR/" "/mnt/$MyShared" ; echo
fi
### Restore media ###############
elif [ "${0##*/}" = "myrestore.media" ]; then
if [ "$MyShared" ]; then #no Media shared partion on Sharon's machine
# Confirm
read -p "YOU ARE ABOUT TO OVERWRITE YOUR Media PARTITION - CONFIRM (y/N)?" -n 1 -r; echo;if [[ ! $REPLY =~ ^[Yy]$ ]]; then echo "Aborting.";exit; fi
echo;
read -p "DANGER! Really overwrite your hard disk partition? (y/N)?" -n 1 -r; echo;if [[ ! $REPLY =~ ^[Yy]$ ]]; then echo "Aborting.";exit; fi
echo "--- Restoring: Media -------------------------------"
$MyRsync "/mnt/$BackupDir/$MEDIA_DIR/" "/mnt/$MyMedia-media" ; echo
else
echo "Error: No Media shared partion on Sharons machine"
fi
fi
#-------------------------------------------------------------------------------------
#cleanup
sudo umount "/mnt/$MyRoot" ;sudo rmdir "/mnt/$MyRoot"
sudo umount "/mnt/$MyHome" ;sudo rmdir "/mnt/$MyHome"
if [ "$MyShared" ]; then #no var or shared partions on Sharon's machine
sudo umount "/mnt/$MyVar" ;sudo rmdir "/mnt/$MyVar"
sudo umount "/mnt/$MyShared" ;sudo rmdir "/mnt/$MyShared"
sudo umount "/mnt/$MyMedia" ;sudo rmdir "/mnt/$MyMedia"
fi
echo "=== DONE. ============================================"
exit 0
### NOTES #################################################################################
# ------------------------------------------------------------------
# Debian 8 base directories:
#
# -----------------------------------------------------------
# /boot/ - stuff to boot with
# /initrd.img@ - ramdisk
# /vmlinuz@ - kernel
# -----------------------------------------------------------
# /media/ - exclude
# /mnt/ - exclude
# -----------------------------------------------------------
# /etc/ - data, kept, between boots, settings, host specific
# /var/ - data, kept, between boots (variable files)
# /srv/ - data, kept, between boots for servers
#
# /tmp/ - data, wiped, programs must not assume that any files or directories in /tmp are preserved between invocations of the program
# /run/ - data, wiped, between boots, Run-time variable data
# -----------------------------------------------------------
# /dev/ - populated at boot - devices
# /proc/ - procfs, populated at boot - statuses, kernel and process
# /sys/ - sysfs, populated at boot - provides device & driver info and an API.
# It also makes things in /proc depricated, e.g., ACPI, more info: http://en.wikipedia.org/wiki/Sysfs - - Since ker 2.6
# -----------------------------------------------------------
# /bin/ - binaries, user commands, essential
# /sbin/ - binaries, system maintance
# /lib/ - shared libraries, essential
# /lib64/ - shared libraries, essential, x64
#
# /usr/ - utilities and applicatons (2nd level programs)
# /usr/bin/
# /usr/sbin/ - for system administrators
# /usr/lib/ - libraries for /usr/bin & /usr/sbin
# /usr/src/ - for stuff built from source
#
# /usr/local/ - users programs that you install from source, e.g. apache
# /usr/local/bin/
# /usr/local/games/
#
# /opt/ - optional add on application software
# -----------------------------------------------------------
# /root/ - like home but for user: root
# /home/ - other users
# -----------------------------------------------------------
# /live-build/ - unique to Debian (not in Ubuntu) -- empty
# ------------------------------------------------------------------
# UBUNTU's base directories:
#
# /bin/
# /boot/
# /cdrom/ - unique to Ubuntu
# /dev/
# /etc/
# /home/
# /initrd.img@
# /lib/
# /lib64/
# /media/
# /mnt/
# /opt/
# /proc/
# /rofs/ - unqique to ubuntu
# /root/
# /run/
# /sbin/
# /snap/ - unqique to ubuntu
# /srv/
# /sys/
# /tmp/
# /usr/
# /var/
# /vmlinuz@