-
Notifications
You must be signed in to change notification settings - Fork 0
/
removable-backup
executable file
·166 lines (143 loc) · 4.13 KB
/
removable-backup
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
#!/bin/bash
#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#
#
# Backup the system to the removable drive in /dev/sdd1 with duplicity.
#
set -e -o pipefail
# Our mount expectations. There are two physical disks and either (or both)
# could be connected. We use the first one we find.
dir1=/removable/backup1
device1=/dev/disk/by-id/usb-WD_My_Passport_25E1_57584631413437364330354C-0:0-part1
dir2=/removable/backup2
device2=/dev/disk/by-id/usb-WD_My_Passport_25E2_57584A3141373741324B4A48-0:0-part1
#-----------------------------------------------------------------------------
# Which device and dir should we use?
# Look for them in order.
dir=
device=
for i in 1 2; do
ldir=dir$i
ldevice=device$i
echo -n "Looking for ${!ldir} device ${!ldevice} . . ."
if [ -e "${!ldevice}" ]; then
dir=${!ldir}
device=${!ldevice}
echo " found!"
break
fi
echo " not found."
done
if [ -z "$dir" -o -z "$device" ]; then
echo "Couldn't find any devices connected."
fi
unset ldir ldevice
#-----------------------------------------------------------------------------
# Pre-flight check.
# Figure out where $1 is mounted and echo it.
function find-dir-mount ()
{
perl -ne 'if(m!^(\S+)\s+('$1')\s!){ print "$1\n"; last}' /proc/mounts
}
# See if the device is mounted and where.
dev=$(find-dir-mount $dir)
if [ -z "$dev" ]; then
echo -n "Did not find $dir mounted. Attempt to mount it now [y/N]? "
read input
if [ "$input" != "y" ]; then
echo "Not confirmed."
exit 1
fi
mount $dir
# Auto unmount on exit if we can
trap 'echo "Unmounting $dir since we mounted it."; umount $dir' EXIT TERM INT QUIT
echo "Will unmount $dir when the backup terminates."
fi
# Check again.
dev=$(find-dir-mount $dir)
if [ -z "$dev" ]; then
echo "Did not found $dir mounted."
exit 1
fi
# Confirm that $dev starts with '/dev/'
if [ "$(echo "$dev" |cut -b1-5)" != "/dev/" ]; then
echo "Can't determine mount point of $dir; found: $dev"
exit 1
fi
echo "Found $dir mounted on $dev"
# Check identity of $dev
if [ "$(readlink -e $device)" != "$dev" ]; then
echo "Could not confirm that $dev is $device."
exit 1
fi
echo "Confirmed that $dev is the expected device $device"
#-----------------------------------------------------------------------------
# Prep.
# Show the collection status.
echo "Gathering current collection status . . ."
duplicity collection-status file://$dir/duplicity
# First display all but the most recent full backup to prepare for deleting
# them.
duplicity remove-all-but-n-full 1 file://$dir/duplicity
echo -n "Delete all but the most recent full backup [y/N]? "
read input
if [ "$input" = "y" ]; then
duplicity remove-all-but-n-full 1 file://$dir/duplicity --force
else
echo "Leaving all exiting backups in place."
fi
#-----------------------------------------------------------------------------
# Print an approximate usage total.
du -hsc \
--exclude /home/spencer/.cache \
--exclude /root/.cache \
--exclude /var/autofs \
--exclude /var/cache \
--exclude /var/lib/mythtv \
--exclude /var/lock \
--exclude /var/run \
/boot \
/data \
/etc \
/home \
/opt \
/root \
/sbin \
/usr/local \
/var
#-----------------------------------------------------------------------------
# Perform the backup. This will prompt for the passphrase.
duplicity \
--progress full \
--exclude-device-files \
\
--include /boot \
--include /data \
--include /etc \
\
--exclude '/home/*/.cache' \
--include /home \
\
--include /opt \
--exclude /root/.cache \
--include /root \
--include /sbin \
--include /usr/local \
\
--exclude /var/autofs \
--exclude /var/cache \
--exclude /var/lib/mythtv \
--exclude /var/lock \
--exclude /var/run \
--exclude /var/tmp \
--include /var \
\
--exclude '**' \
\
/ file://$dir/duplicity
#-----------------------------------------------------------------------------
# Finished
# How to power it off:
echo "To power off the drive:"
echo "udisksctl power-off -b $dev"
echo "Done."
#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#