-
Notifications
You must be signed in to change notification settings - Fork 1
/
balmora-snapshots
executable file
·193 lines (168 loc) · 5.23 KB
/
balmora-snapshots
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
#!/bin/bash
#
# Inspiration / etc
#
# - Mark Merlin (script)
# http://marc.merlins.org/perso/btrfs/
# post_2014-03-22_Btrfs-Tips_-Doing-Fast-
# Incremental-Backups-With-Btrfs-Send-and-Receive.html
#
# - Martin Wilck
# clone everything to fresh btrf: https://github.com/mwilck/btrfs-clone
#
# check out
# - https://github.com/mwilck/btrfs-clone
# - https://github.com/Thomas-Tsai/partclone
#
# maybe read
# - https://helmundwalter.de/en/blog/next-gen-backup-with-btrfs-snapshots-for-root-fs-and-databases/#
# - https://github.com/csirac2/snazzer/#snazzer
#
# Dependencies
#
# - sys-apps/util-linux (/bin/mountpoint)
if [[ $USER != root ]]; then
exec sudo $0 "$@"
fi
mountpoint -q /ss || mount /ss || exit 124
mountpoint -q /B/rr || mount /B/rr || exit 124
if [[ $1 = ls ]]; then
for i in /ss /B/rr; do sudo btrfs subvolume list -at --sort=path $i | awk '{ print $(NF) }' ; done |
grep -e rr -e ss | grep -v FS_T
exit 0
fi
if [[ $UID -ne 0 ]]; then
echo sudo $0
exit 1
fi
DEBUG=${DEBUG:-}
# set -x
set -o nounset
set -o errexit
set -o pipefail
die () {
set +x
msg=$*
trap '' ERR
echo >&2
echo "$msg" >&2
# This is a fancy shell core dumper
if echo $msg | grep -q 'Error line .* with status'; then
line=`echo $msg | sed 's/.*Error line \(.*\) with status.*/\1/'`
nl -ba $0 | grep -3 "\b$line\b" >&2
fi
touch ~/__________________CHECK_BACKUPS__.$(date +%Y-%m-%d).balmora-snapshots
if [ -o errexit ]
then
exit 55
fi
}
trap 'die "Error line $LINENO with status $?"' ERR
ymd=$(date +%Y-%m-%d)
mkdir -p /ss/$ymd /ss/last /B/rr/$ymd
btrfs filesystem show /ss > /dev/null || exit 124
btrfs filesystem show /B/rr > /dev/null || exit 124
declare -A mp=(
[/T]=T
['/']=slash
[/home]=home
[/srv]=srv
)
##
## Create daily snapshots to avoid losing data
##
for mount in ${!mp[@]}; do
snap=${mp[$mount]}
if [[ ! -e /ss/$ymd/$snap ]]; then
[[ -n $DEBUG ]] && echo b s snap-r $mount /ss/$ymd/$snap
btrfs subvolume snapshot -r $mount /ss/$ymd/$snap &&
ln -snf ../$ymd/$snap /ss/last/$snap;
else
[[ -n $DEBUG ]] && echo have /ss/$ymd/$snap
fi
done
sync
##
## Send snapshots to a secondary volume for redundancy
##
for mount in ${!mp[@]}; do
snap=${mp[$mount]}
#prev_local=$(ls -dv1 /ss/*/$snap | grep $(realpath /ss/last/$snap) -B1 | sed 1q)
prev_local=$( printf "%s\n" /ss/*/$snap | sort | grep $(realpath /ss/last/$snap) -B1 | sed 1q)
last_remote=$( { ls -dv1 /B/rr/*/$snap | tail -n1; } || echo seed)
if [[ -n $DEBUG ]]; then
printf "prev_local..:%30s\n" $prev_local
printf "last_remote.:%30s\n" $last_remote
fi
if [[ ! -e /B/rr/$ymd/$snap ]]; then
if [[ $last_remote == seed ]]; then
#set -x
btrfs send /ss/$ymd/$snap | btrfs receive /B/rr/$ymd/
else
rrs=/tmp/backups.err.$ymd.send.$$
rrr=/tmp/backups.err.$ymd.recv.$$
echo errors in $rrs $rrr
declare -a ps=()
function incremental {
#set -x
echo ":incr: btrfs send -v -p $prev_local /ss/$ymd/$snap | btrfs receive -v /B/rr/$ymd " | tee $rrr $rrs
btrfs send -v -p $prev_local /ss/$ymd/$snap 2>$rrs | btrfs receive -v /B/rr/$ymd 2>$rrr
ps=( "${PIPESTATUS[@]}" )
set +x
}
#function reseed {
# reseed should find the latest possible snapshot and use that as the parent
# this is useful if backups fail 4 days in a row or something.
#
# so on the 20th we should use 16th as the partent if 17-18-19 are missing.
#
# an exception like this we need to re-transmit from even earlier
##
## receiving snapshot home uuid=776adb95-9f15-8748-be6a-55a4255808c3, ctransid=1408097 parent_uuid=3a19a0e4-ded6-7441-988d-c786c4abd697, parent_ctransid=1405312
## ERROR: cannot find parent subvolume
##
## This means that the remote end hoes not have the parent volume you have specified on the source.
##
## eg.
# btrfs send -v -p /ss/2023-04-15/home /ss/2023-04-20/home | btrfs receive -v /B/rr/2023-04-20
# btrfs send -v -p /ss/2023-04-15/srv /ss/2023-04-20/srv | btrfs receive -v /B/rr/2023-04-20
# btrfs send -v -p /ss/2023-04-15/T /ss/2023-04-20/T | btrfs receive /B/rr/2023-04-20
# btrfs send -v -p /ss/2023-04-15/slash /ss/2023-04-20/slash | btrfs receive /B/rr/2023-04-20
function reseed {
# $ errno `imath 141-128`
# EACCES 13 Permission denied
if [[ ${ps[0]} -ne 0 ]]; then
export DEBUG=1
echo -e "\e[33;01m"
echo -e "remote end failed ${ps[0]}"
echo -e "\e[0m"
eval $(grep -oE '[a-z_]+=[0-9a-z-]+' $rrr)
#... if ${ps[0]} == 141
parent=$(btrfs subvolume list -sru /ss \
| awk -v u=$parent_uuid '$(NF-2) == u { print $NF }')
#set -x
parent_date=${parent%%/*}
echo ":reseed: btrfs send /ss/$parent | btrfs receive /B/rr/$parent_date/"
mkdir -p /B/rr/$parent_date
btrfs send /ss/$parent | btrfs receive /B/rr/$parent_date/
set +x
incremental
exit ${ps[0]}
fi
}
set +o errexit
incremental
reseed
set -o errexit
fi
set +x
else
if [[ -n $DEBUG ]]; then
date '+present.....: %Y-%m-%d (snapshot exists)'
fi
fi
if [[ -n $DEBUG ]]; then
echo
fi
done
umount /ss /B/rr || true