-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraspi-backup-btrfs
executable file
·195 lines (146 loc) · 3.25 KB
/
raspi-backup-btrfs
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
#!/bin/bash
set -eu
set -o pipefail
# target directory
target='/media/backup/$(hostname -s)'
# list of files to exclude
exclude_files='var/swap var/log/wtmp'
##
# rsync=(no|alive|yes)
# no - no rsync
# alive - run rsync if host is alive
# yes - always run rsync
#
rsync=no
# rsync host
rsync_host='nas'
# rsync target
rsync_target="rsync://$rsync_host/backup/$(hostname -s)"
# how long to keep in days
keep=14
# compress command
compress='gzip'
# archive extension
ext='tar.gz'
# number of snapshots to keep
snapkeep=5
# snapshots directory
snapdir='/.btrfs/.snapshots'
# =========================================================================
timestamp=$(date +%Y.%m.%d)
test -r $HOME/.raspi-backup-btrfs && source $HOME/.raspi-backup-btrfs
PATH=/usr/local/bin:$PATH:/sbin
ts=$timestamp
out=$(mktemp)
excl=$(mktemp)
rc=$(mktemp)
restore=$target/$ts/restore
function epilogue()
{
_rc=$?
test $_rc -eq 0 && status=ok || status=failed
echo "$(date) - backup $status"
(
cat $out
test -f $target/${ts}/backup.out && cat $target/${ts}/backup.out
) | mail "$(hostname -s) backup $status"
rm $excl $out $rc
test -f $snapdir/$ts/boot/firmware/cmdline.txt && {
umount $snapdir/$ts/boot/firmware
}
exit $_rc
}
trap epilogue EXIT
exec > $out 2>&1
until apt-get clean all; do sleep 30; done
test -d $target || {
1>&2 echo "The directory '$target' does not exist!"
exit 1
}
test -f $target/$ts/backup.ok && {
echo "$(date) - backup already exists"
exit 0
}
mkdir -p $target/$ts
exec > $target/$ts/backup.out 2>&1
echo "$(date) - backup start"
cat <<EOF > $restore
#!/bin/bash
set -eu
if [ \${#} -ne 1 ] || [ ! -d "\$1" ]; then
echo "usage: \$0 <TARGET-DIR>"
exit 1
fi
scriptdir=\$PWD
cat=\$(which pv) || cat=cat
set -x
set -o pipefail
function decompress()
{
$compress -d
}
cd "\$1"
\$cat \$scriptdir/backup.$ext | decompress | tar -x --numeric-owner -f -
EOF
for file in $exclude_files ; do
echo "./$file" >> $excl
done
test -d $snapdir/$ts || {
btrfs subvolume snapshot -r / $snapdir/$ts
}
cd $snapdir/$ts
set -- $(mount | grep 'on /boot/firmware type')
mount $1 boot/firmware
(
set +e
tar --create -f - --exclude-from=$excl --one-file-system --warning=no-file-ignored --exclude-caches . boot/firmware
echo $?>$rc
) | $compress > $target/$ts/backup.$ext.new
case $(cat $rc) in
0|1) true ;;
*) exit 2 ;;
esac
echo '
cd -
sync
' >> $restore
chmod +x $restore
cd $target/$ts
for file in backup.*.new ; do
mv -f $file ${file%.new}
done
ls -lhrt
touch backup.ok
set -- $(LANG=C df $target/ -PT | grep -v '^Filesystem')
echo "$(date) - backup done ($6 used)"
echo "$(date) - reorg start"
##
# reorg
#
cd $target
for del in $(find * -type f -ctime +${keep}) ; do
echo remove $del
rm -f $del
done
for del in $(find * -type d -atime +${keep} -size 0) ; do
echo remove $del
rmdir $del
done
echo "$(date) - reorg done"
echo "$(date) - delete snapshots start"
cd $snapdir
n=$(ls | wc -l)
for s in $(ls | head -n$((n - snapkeep))); do
btrfs subvolume delete $s
done
echo "$(date) - delete snapshots done"
##
# rsync
#
if [ "$rsync" == yes ] || (test "$rsync" == alive && ping $rsync_host -c 4 -q 2>/dev/null) ; then
echo "$(date) - rsync start"
cd $target
rsync -rav $ts $rsync_target/
echo "$(date) - rsync done"
fi
exit 0