-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathbtrfs-snap
executable file
·291 lines (251 loc) · 8.68 KB
/
btrfs-snap
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/bin/bash
# btrfs-snap - make periodic snapshots of btrfs filesystem
#
# Copyright (C) 2010 Birger Monsen [email protected]
# Copyright (C) 2013 James FitzGibbon [email protected]
# Copyright (C) 2014 Brian Kloppenberg
# Copyright (C) 2015 Lukas Pirl [email protected]
# Copyright (C) 2015-2017 Michael Walz [email protected]
# Copyright (C) 2017 Raimund Jacob-Bloedorn [email protected]
# This program is distributed under the GNU General Public License
# http://www.gnu.org/licenses/gpl.txt
#
set -u
LOG_FACILITY=local0
VERSION="1.7.4"
prog=${0##*/}
PATH="$PATH:/usr/sbin:/usr/bin:/sbin:/bin"
USAGE="Usage: ${prog} -h for usage help
${prog} -V for version
${prog} [options] <mountpoint> <prefix> <count>"
SYNOPSIS="${prog} [options] <mountpoint> <prefix> <count>
<mountpoint> is the mountpoint of the btrfs file system to make a
snapshot of
<prefix> is the prefix to be used in the name of the snapshot.
E.g. hourly, daily, weekly...
If prefix=VFS a Samba vfs_shadow_copy snapshot naming convention
will be used (@GMT-%Y.%m.%d-%H.%M.%S).
<count> The number of snapshots with the given prefix to keep.
btrfs-snap / hourly 24
would make a snapshot in /.snapshot called hourly_<date>_<time>
where <date> is on the form YYYY-MM-DD and <time> is on the form
HH:MM:SS. This format makes shure snapshot names sort correctly in
cronological order even when sorted alphabetically. The 24 newest
snapshots matching the prefix are kept around. The rest are deleted.
if '-r' is given, snapshots are created read only
if '-c' is given, snapshots are created with more compatible names
(i.e. no colons in file names, so that SAMBA does not mangle the names)
if '-p' is given, the <prefix> will be used as a postfix, i.e. it is
appended which might be usefull for automatic chronological sorting.
if '-E' is given, omitting a snapshot (e.g. due to options -t/-T) is
treated as an error and a non-zero exit code is returned.
if '-d dir' is given, snapshots are created in dir, relative to
mountpoint (default: '.snapshot', conflicts with -b and -B).
if '-b dir' is given, snapshots are created in dir, with a directory
structure that mimics the source (conflicts with -d and -B).
if '-B dir' is given, snapshots are created in dir, with NO additional
subdirectory structure; choose <prefix> wisely to avoid conflicts
(conflicts with -d and -b).
if '-t time' is given, a snapshot will only be created if the newest already
existing snapshot is older than 'time' seconds. Additionally, no snapshot will
be made if <mountpoint> has identical timestamp as the newest snapshot.
(The modification timestamps of the subvolumes/folders are used for comparison
which MIGHT NOT work in some scenarios.)
(conflicts with -T)
if '-T time' is given, a snapshot will only be created if the newest already
existing snapshot is older than 'time' seconds. Additonally, no snapshot will
be made if <mountpoint> has a lower or equal transition-id than newest snapshot.
(conflicts with -t)
if '-q' is given, output becomes cron-compatible: silent unless an error occurs.
Example usage for a system with 2 btrfs file systems mounted as
/ and /home (remember to make these scripts executable):
/etc/cron.hourly/btrfs-snap
#!/bin/bash
${0} / hourly 24
${0} /home hourly 24
/etc/cron.daily/btrfs-snap
#!/bin/bash
${0} / daily 7
${0} /home daily 7
/etc/cron.weekly/btrfs-snap
#!/bin/bash
${0} /home weekly 4
Alternative (more complicated) example for a system with 1 btrfs subvolume called /data.
These three commands should be run regularily (e.g. all 5 minutes) and
test themselves if a snapshot is needed:
${0} -r -c -B <SNAPSHOT_PATH> -t 3600 /data data_hourly 24
${0} -r -c -B <SNAPSHOT_PATH> -t 86400 /data data_daily 7
${0} -r -c -B <SNAPSHOT_PATH> -t 604800 /data data_weekly 4
"
base=""
dir=""
full_base=""
readonly=""
quiet=""
use_as_prefix=true
use_transid=false
time_delim=":"
ONE_OF_b_B_d_GIVEN=false
OMITT_ERROR_CODE=0
time_duration=0
while getopts "hVrqpEb:d:B:ct:T:" arg; do
case "${arg}" in
h )
echo "$SYNOPSIS"
exit 0
;;
r )
readonly="-r"
;;
q )
quiet="1"
;;
p )
use_as_prefix=false
;;
E )
OMITT_ERROR_CODE=1
;;
t )
time_duration=$((0+$OPTARG))
;;
T )
time_duration=$((0+$OPTARG))
use_transid=true
;;
b )
base=$OPTARG
if $ONE_OF_b_B_d_GIVEN; then echo "Error: the switches -b -B -d are mutually exclusive."; exit 1; fi
ONE_OF_b_B_d_GIVEN=true
;;
B )
full_base=$OPTARG
if $ONE_OF_b_B_d_GIVEN; then echo "Error: the switches -b -B -d are mutually exclusive."; exit 1; fi
ONE_OF_b_B_d_GIVEN=true
;;
d )
dir=$OPTARG
if $ONE_OF_b_B_d_GIVEN; then echo "Error: the switches -b -B -d are mutually exclusive."; exit 1; fi
ONE_OF_b_B_d_GIVEN=true
;;
V )
echo "${prog} Version ${VERSION}"
exit 0
;;
c )
time_delim="-"
;;
* )
echo "$USAGE"
exit 1
;;
esac
done
shift $((OPTIND-1))
if [ $# -ne 3 ] ; then
echo "$USAGE"
exit 1
fi
# Canonicalize the mountpoint path (strip trailing slashes, etc)
mp=$(readlink -f $1)
prefix=$2
cnt=$(( $3+1 ))
function log.info() {
logger -p ${LOG_FACILITY}.info -t ${prog} "$1"
if test -z "$quiet"
then
echo "$1"
fi
}
function log.error() {
logger -p ${LOG_FACILITY}.err -t ${prog} "$1"
echo "ERROR: $1" >&2
exit 1;
}
# Verify that the path is either a valid btrfs mountpoint
if ! findmnt -t btrfs -T "${mp}" &> /dev/null; then
# or a valid snapshot matching mp
btrfs subvolume show $mp > /dev/null
if [ $? -ne 0 ] ; then
log.error "${mp} is not a btrfs mountpoint (or old version of btrfs-progs, try >=5.10)"
exit 1;
fi
fi
# set default for dir
if [ "x$dir" == "x" ]
then
dir=".snapshot"
fi
if [ "x$base" == "x" ]
then
base="${mp}/${dir}"
else
base="${base}/${mp}"
fi
if [ "x$full_base" != "x" ]; then
base=$full_base
fi
if [ ! -d $base ]; then
log.info "Creating $base"
mkdir -p $base
fi
base=$(readlink -f $base)
if [ ${prefix} == VFS ]; then
snapname=$(TZ=GMT date +@GMT-%Y.%m.%d-%H.%M.%S)
snappattern="@GMT-????.??.??-??.??.??"
else
if $use_as_prefix; then
snapname=${prefix}_$(date +%Y-%m-%d_%H${time_delim}%M${time_delim}%S)
snappattern="${prefix}_????-??-??_??${time_delim}??${time_delim}??"
else
snapname=$(date +%Y-%m-%d_%H${time_delim}%M${time_delim}%S)_${prefix}
snappattern="????-??-??_??${time_delim}??${time_delim}??_${prefix}"
fi
fi
if [ -e ${base}/${snapname} ]; then
log.error "cannot create snapshot in ${base}/${snapname} since a folder/file with that name already exists"
exit 1;
fi
if [ $time_duration -gt 0 ]; then
newestSnapshot=`ls -dr $base/${snappattern} 2>/dev/null| head -n 1`
if [ ! -z "$newestSnapshot" -a -e "$newestSnapshot" ]; then
snap_time=`stat -c "%Y" "${newestSnapshot}"`
cur_time=`date +%s`
if $use_transid; then
# get transaction ids
id_snap=$(btrfs subvolume find-new "$newestSnapshot" 99999999| sed 's/[^0-9]//g')
id_mount=$(btrfs subvolume find-new "${mp}" 99999999| sed 's/[^0-9]//g')
if [ $id_mount -le $id_snap ]; then
log.info "No snapshot created since no changes since last snapshot. (Transaction id of $newestSnapshot is newer or equal to $mp.)"
exit $OMITT_ERROR_CODE
fi
else
mp_time=`stat -c "%Y" "${mp}"`
if [ $snap_time == $mp_time ]; then
log.info "No snapshot created since timestamp of newest snapshot $newestSnapshot equal $mp."
exit $OMITT_ERROR_CODE
fi
fi
if [ $(($snap_time + $time_duration)) -gt $cur_time ]; then
log.info "No snapshot created since timestamp of newest snapshot $newestSnapshot is less than $time_duration seconds away."
exit $OMITT_ERROR_CODE
fi
fi
# Force update of source timestamp to prevent outdated timestamps on the folders
touch "${mp}"
fi
out=`btrfs subvol snapshot ${readonly} ${mp} ${base}/${snapname} 2>&1`
if [ $? -eq 0 ] ; then
log.info "${out}"
else
log.error "${out}"
exit 1;
fi
ls -dr $base/${snappattern} | tail -n +${cnt} | while read snap ; do
out=`btrfs subvolume delete ${snap} 2>&1`
if [ $? -eq 0 ] ; then
log.info "${out}"
else
log.error "${out}"
fi
done