1 |
Many thanks to Rich, Miles, Vitor and Laurence for their hints on this |
2 |
subject. |
3 |
Especially the suggestion to use BTRFS Snapshots was very helpful. |
4 |
|
5 |
Finally I have come up with the following shell script for backing up |
6 |
my /home directory, |
7 |
comments are more than welcome, of course. |
8 |
(/HBackUp contains a BTRFS file system) |
9 |
|
10 |
|
11 |
#!/bin/zsh |
12 |
mount /HBackUp |
13 |
pushd /HBackUp |
14 |
NUM_TO_KEEP=5 |
15 |
if [ -d "ohome_$NUM_TO_KEEP" ] |
16 |
then |
17 |
btrfs subvolume delete "ohome_$NUM_TO_KEEP" |
18 |
fi |
19 |
|
20 |
if ! [ -f _RSync_Failed ] |
21 |
then |
22 |
for backup_no in $(seq $((NUM_TO_KEEP-1)) -1 1) |
23 |
do |
24 |
if [ -d ohome_$backup_no ] |
25 |
then |
26 |
backup_no_plus_1="$((backup_no+1))" |
27 |
mv $ohome_prev "ohome_$backup_no_plus_1" |
28 |
fi |
29 |
done |
30 |
if ! btrfs subvolume snapshot ohome ohome_1 |
31 |
then |
32 |
echo Sorry, creating snapshot ohome_1 failed 1>&2 |
33 |
exit 1 |
34 |
fi |
35 |
fi |
36 |
|
37 |
RSync_OK=False |
38 |
touch _RSync_Failed |
39 |
|
40 |
popd |
41 |
pushd /home |
42 |
|
43 |
if rsync -aqxAHSW --delete . /HBackUp/ohome/ |
44 |
then |
45 |
RSync_OK=True |
46 |
else |
47 |
case "$?" in |
48 |
23) |
49 |
# This means some files gave permission denied - assume |
50 |
that's OK |
51 |
RSync_OK=True |
52 |
;; |
53 |
24) |
54 |
# This means one or more files disappeared |
55 |
RSync_OK=True |
56 |
;; |
57 |
*) |
58 |
echo rsync failed. Exit code was $?" |
59 |
;; |
60 |
esac |
61 |
fi |
62 |
|
63 |
popd |
64 |
|
65 |
cd /HBackUp |
66 |
|
67 |
case "$RSync_OK" in |
68 |
True) |
69 |
rm _RSync_Failed |
70 |
;; |
71 |
False) |
72 |
cd / |
73 |
umount /HBackUp |
74 |
exit 1 |
75 |
;; |
76 |
esac |
77 |
|
78 |
cd / |
79 |
umount /HBackUp |