-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraspi-backup
executable file
·211 lines (166 loc) · 3.82 KB
/
raspi-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
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
#!/bin/bash
set -eu
set -o pipefail
# target directory
target='/media/backup/$(hostname -s)'
declare -A services=(
# [var/lib/grafana]='systemd:grafana-server'
# [var/lib/influxdb]='systemd:influxdb'
# [var/lib/pihole]='docker:pihole'
)
# list of directories to exclude
exclude_dirs='var/lib/docker'
# 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)"
# number of days to keep
keep=7
# compress command
compress='pigz'
# archive extension
ext='tar.gz'
# =========================================================================
timestamp=$(date +%Y.%m.%d)
test -r $HOME/.raspi-backup && source $HOME/.raspi-backup
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
exit $_rc
}
trap epilogue EXIT
exec > $out 2>&1
apt-get clean all
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 dir in ${!services[@]} $exclude_dirs mnt media tmp var/tmp dev proc run sys ; do
echo "./$dir/*" >> $excl
done
for file in $exclude_files ; do
echo "./$file" >> $excl
done
cd /
(
set +e
tar --create -f - --exclude-from=$excl --one-file-system --warning=no-file-ignored --exclude-caches . boot
echo $?>$rc
) | $compress > $target/$ts/backup.$ext.new
case $(cat $rc) in
0|1) true ;;
*) exit 2 ;;
esac
for dir in ${!services[@]} ; do
service="${services[$dir]}"
echo "$(date) - saving $dir ($service)"
case $service in
docker:*)
stop="docker container stop ${service#*:} > /dev/null"
start="docker container start ${service#*:} > /dev/null"
;;
systemd:*)
stop="systemctl stop ${service#*:}"
start="systemctl start ${service#*:}"
;;
none)
stop="true"
start="true"
;;
*)
echo "unknown service: $service"
exit 1
esac
echo "\$cat \$scriptdir/backup.${service#*:}.$ext | tar -x --numeric-owner -f - | $compress --decompress" >> $restore
eval $stop
(
set +e
tar --create -f - --one-file-system --warning=no-file-ignored --exclude-caches $dir
echo $?>$rc
) | $compress > $target/$ts/backup.${service#*:}.$ext.new
eval $start
case $(cat $rc) in
0|1) true ;;
*) exit 2 ;;
esac
done
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"
if [ "$rsync" == yes ] || (test "$rsync" == alive && ping $rsync_host -c 4 -q 2>/dev/null) ; then
echo "$(date) - rsync start"
rsync -rav $ts $rsync_target/
echo "$(date) - rsync done"
fi
exit 0