Repository navigation
Expand file tree
/
Copy pathzrep_snap
More file actions
486 lines (407 loc) · 13.9 KB
/
Copy pathzrep_snap
File metadata and controls
486 lines (407 loc) · 13.9 KB
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
################ File: zrep_snap
# be sure to have included zrep_vars
# This file contains routines related to
# "make new snapshot, using next sequence number".
# So it thus includes all snap sequence related routines
# It may contain "sync snapshot" related routines for now.
# It also is definitive for the format of snapshot names
# It also contains most "query status of snaps" type routines,
# such as "getlastsnapsent"
#
# Normal style for making a snapshot and syncing it:
# 1. create a snapshot.
# 2. sync it over
# 3. set "zrep:sent" on *snapshot*, with timestamp in seconds
# Old-nasty-zfs compat mode:
# Step 3. Add/update "zrep:lastsent->snapname", and
# "zrep:lastsenttime->timestamp", on *filesystem*
#
######################################################################
# By observation, 'zfs list' shows snapshots order of creation.
# last listed, should be last in sequence.
# But, dont take chances!!
getlastsequence(){
typeset lastval
#remember, filesystems can have '_' in them
getlastsnap $1|sed 's/.*@'${ZREPTAG}'_\(......\).*/\1/'
}
# prints out last snapshot zrep created, going purely by sequence.
# Note: "last created", which may or may NOT be "last successfully synced".
# This is basically "getallsnaps |tail -1"
getlastsnap(){
zfs list -t snapshot -H -o name $DEPTHCAP -r $1 |
sed -n "/@${ZREPTAG}_[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]/"p |
sort | tail -1
}
# Usage: getlastsnapsent zpool/FSNAME
getlastsnapsent(){
# arg. more efficient if we can just return value directly,
# but i'm using backwards compat :(
typeset lastsent
lastsent=`zfs get -H -o name -r -s local ${ZREPTAG}:sent $1 |
sort | tail -1`
if [[ "$lastsent" != "" ]] ; then
print $lastsent
return
fi
# Fallback method, for backwards compat with older ZFS code,
# since it cant set properties on snapshots
zfs get -H -o value -s local ${ZREPTAG}:lastsent $1
}
# outputs time in seconds, of when the last successful sync for the
# filesystem was done. (in format compatible with zrep_gettimeinseconds() )
# Note that this is time of actual sync, not snapshot creation time.
#
# This unfortunately needs to be compatible with both new way, and
# old-nasty-hack-way
#
# In future, may take optional argument of which HOST to check
# sync with. But since I currently only suport one host per fs... oh well.
# If never synced, will return 1, and print ""
#
getlastsynctime(){
typeset fs lastsent senttime
if [[ "$1" == "" ]] ; then
zrep_errquit Internal error: no arg to getlastsynctime
fi
fs="$1"
# Deal with possibly upgraded system;
# Check "lastsent", only as fallback.
# copy from getlastsnapsent, but only using newest method
lastsent=`zfs get -H -o name -r -s local ${ZREPTAG}:sent $fs |
sort | tail -1`
senttime=`zfs get -H -o value ${ZREPTAG}:sent $lastsent`
if [[ "$senttime" != "" ]] ; then print $senttime ; return 0; fi
# ooops. try fallback to nasty old zfs-compat style
senttime=`zfs get -H -o value ${ZREPTAG}:lastsent $fs`
print $senttime
if [[ "$senttime" != "" ]] ; then return 0; fi
return 1
}
#This is for synctosnap, and also zrep_expire
getallsnaps(){
zfs list -t snapshot -H -o name $DEPTHCAP -r $1 |
sed -n "/@${ZREPTAG}_[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]/"p |
sort
}
# list all snapshots of the given filesystem, that are made by this prog
# arg: fs
list_autosnaps(){
if [[ "$1" == "" ]] ; then
zrep_errquit "zrep internalerror: no arg for list_autosnaps"
fi
zfs list $DEPTHCAP -r -H -o name -t snapshot $1 |
grep "@${ZREPTAG}_[0-9a-f][0-9a-f]"
# Make sure this format matches other routines in here
# Okay to just check first few digits though
}
# User entrypoint. Part of pair: snaponly, sendonly
# Just makes snapshot.
zrep_snaponly(){
typeset srcfs
if [[ "$1" == "all" ]] ; then
set -- `zrep_list_master`
if [[ "$1" == "" ]] ; then
exit
fi
fi
while [[ "$1" != "" ]] ; do
srcfs="$1" ;shift
zrep_lock_fs $srcfs
if [[ $? -ne 0 ]] ; then
# this function is supposed to be coordinated by user
# therefore, if something else is competing,
# coordination has failed. no retry.
zrep_errquit zrep snaponly failed for $srcfs: cannot get lock
fi
makesnap $srcfs ||zrep_errquit snaponly for $srcfs failed
zrep_unlock_fs $srcfs
done
}
#
# creates next snapshot in sequence
# consider holding lock here
# Caller must have zrep lock on filesystem:
# we verify with zrep_has_fs_lock
makesnap(){
typeset check oldseq newseq="" newseqX newsnap
#sanity checks first!
check="`$ZFSGETVAL ${ZREPTAG}:src-host $1`"
if [[ "$check" != "$Z_LOCAL_HOST" ]] ; then
_errprint ERROR: we are not master host for $1
_errprint print master is $check, we are $Z_LOCAL_HOST
exit 1
fi
zrep_has_fs_lock $1
if [[ $? -ne 0 ]] ; then
_errprint Internal error: makesnap fail, no lock on $1
exit 1
fi
oldseq=`getlastsequence $1`
# This means input is base 16
newseq=$((16#$oldseq))
newseqX=$(printf "%.6x" $(($newseq + 1)) )
#_errprint DEBUG old=$oldseq new=$newseqX
newsnap="$1@${ZREPTAG}_$newseqX"
zfs snapshot $Z_SNAP_R $newsnap
if [[ $? -eq 0 ]] ; then
print $newsnap; return 0
else
return 1
fi
}
## This is the implentation for the "zrep clear" command
## Purpose is to remove all zrep related hooks from a local filesystem.
## (NOT delete it)
## Will remove zrep snapshots and zfs zrep: properties
zrep_clear(){
if [[ "$1" == "" ]] ; then
zrep_errquit clear command requires an argument
fi
print "WARNING: Removing all zrep configs and snapshots from $1"
print " (for TAG=${ZREPTAG})"
if [[ "$ZREP_FORCE" != "-f" ]] ; then
print Continuing in 10 seconds
sleep 10
fi
_clear $1
}
_clear(){
print Destroying any zrep-related snapshots from $1
snaplist=`list_autosnaps $1`
for snap in $snaplist ; do
zfs destroy -r $snap
done
print Removing zrep-related properties from $1
proplist=`zfs get -H -o property all $1|grep ${ZREPTAG}:`
for prop in $proplist ; do
zfs inherit $prop $1
done
}
## This is a special internal routine, used only by zrep_init,
## to reset target fs to pre-zrep state.
## call with "srcfs errmsg1 errmsg2..."
## It will also REMOVE REMOTEFS if set in PROPERTIES!!
clearquit(){
remhost=`$ZFSGETVAL ${ZREPTAG}:dest-host $1`
remfs=`$ZFSGETVAL ${ZREPTAG}:dest-fs $1`
if [[ $? -eq 0 ]] && [[ "$remhost" != "-" ]] && [[ "$remfs" != "-" ]]; then
zrep_ssh $remhost zfs destroy -r $remfs
fi
_clear $1
shift
zrep_errquit "$@"
}
# Shared internal routine.
# (internal only to zrep_snap, even!)
#
# Set the to/from properties on a fs for zrep
# Called by zrep_init and zrep_changeconfig
# Usage:
# setfsconfigs srcfs desthost destfs
# setfsconfigs -d destfs srchost srcfs
#
setfsconfigs(){
typeset srchost srcfs desthost destfs fsname
if [[ "$1" == "-d" ]] ; then
srchost="$3"
srcfs="$4"
desthost=$Z_LOCAL_HOST
destfs="$2"
fsname=${destfs}
else
srchost=$Z_LOCAL_HOST
srcfs="$1"
desthost="$2"
destfs="$3"
fsname=${srcfs}
fi
if [[ "$destfs" == "" ]] ; then
zrep_errquit "zrep: no dest fs specified"
fi
zfs list $fsname >/dev/null ||zrep_errquit "filesystem $fsname must exist already"
#
# This is really only for when we are called from init I think?
#
case $destfs in
# Originally, I had this passthrough only if fsname was at end
# However,we must allow destfs to have different leaf name,
# for circumstances such as replication to same host
*/*)
:
;;
*)
# Only the pool name given.
# With our curent workflow, that can ONLY WORK
# if recent versions of ZFS are in use.
if (( Z_HAS_REC_U )) ; then
print WARNING: forcing override sync to top level pool $desthost:$destfs
print Not using a usual sub-filesystem
sleep 5
else
zrep_errquit older zfs version, cannot initialize top level pool
fi
;;
esac
zfs set ${ZREPTAG}:src-fs=$srcfs $fsname
zfs set ${ZREPTAG}:src-host=$srchost $fsname
zfs set ${ZREPTAG}:dest-fs=$destfs $fsname
zfs set ${ZREPTAG}:dest-host=$desthost $fsname
zfs set ${ZREPTAG}:savecount=$Z_SAVE_COUNT $fsname
}
#
# Old way used to use recv -x, but not all systems have that. so
# preferred method is now to use recv -u instead.
# To init from existing snapshot instead, see documention at bolthole.com
# Note that remote fs must share same stem name as source. (for now?)
#
zrep_init(){
typeset srcfs="$1" desthost="$2" destfs="$3" snap check vol=0
typeset mountpoint vflags verbose
if [[ "$ZREP_VERBOSE" != "" ]] ; then
verbose=-v
fi
if [[ "$srcfs" == "" ]] ; then
zrep_errquit "zrep: no fs specified"
fi
#sanity checks
check="`$ZFSGETVAL ${ZREPTAG}:dest-fs $srcfs`"
if [[ "$check" != "-" ]] ; then
print "$srcfs is at least partially configured by zrep"
check="`$ZFSGETLVAL ${ZREPTAG}:master $srcfs`"
if [[ "$check" != "" ]] ; then
zrep_errquit "${ZREPTAG}:master detected!!"
fi
zrep_errquit "To re-initialize, first use zrep clear $srcfs"
fi
check="`$ZFSGETVAL type $srcfs`"
if [[ "$check" == "volume" ]] ; then
vol=1
if (( ! Z_HAS_REC_O )) ; then
echo WARNING: no proper recv -o detected
echo WARNING: extremely old versions of ZFS crash with volume init
echo Continuing in 5 seconds....
sleep 5
vflags="`$ZFSGETVAL volsize $srcfs`"
vflags="-V $vflags"
fi
# for details,see
# https://groups.google.com/forum/#!topic/comp.unix.solaris/-5bcZFInozk
# subject:"solaris 11 crash when zfs send/receive of volume"
fi
print Setting properties on $srcfs
setfsconfigs $srcfs $desthost $destfs
#setfsconfigs may do some "smarts" to adjust value, so get it again.
# yes only check for LOCAL this time. Paranoia....
destfs=`$ZFSGETLVAL ${ZREPTAG}:dest-fs $srcfs`
#get this for later
mountpoint=`$ZFSGETLVAL mountpoint $srcfs`
if (( Z_HAS_REC_O )) ; then
READONLYPROP="-o readonly=on"
else
READONLYPROP=""
print Warning: zfs recv lacking -o readonly
print Creating readonly destination filesystem as separate step
zrep_ssh $desthost zfs create $ZREP_CREATE_FLAGS -o readonly=on $vflags $destfs || zrep_errquit "Cannot create $desthost:$destfs"
fi
snap="${srcfs}@${ZREPTAG}_000000"
print Creating snapshot $snap
zfs snapshot $Z_SNAP_R $snap || clearquit $srcfs "Cannot create initial snapshot $snap"
# Note that we may not want to use -p for normal zrep syncs
# We also should not use -F for normal recv. See workflow.txt
# Note: we may have to reset readonly=on, if we used -p on send...
#
print Sending initial replication stream to $desthost:$destfs
if (( Z_HAS_REC_U )) ; then
# This is the nice, clean, modern codepath, to send
# zrep settings over automatically at first init.
# Note that we use "zfs send -p" to preserve properties.
#
if [[ "$BBCP" != "" ]] ; then
$BBCP -N io "zfs send $verbose ${ZREP_R} ${ZREP_SEND_FLAGS} -p $snap" \
"$desthost:zfs recv -u $READONLYPROP -F $destfs"
else
eval zfs send $verbose ${ZREP_R} ${ZREP_SEND_FLAGS} -p $snap ${Z_F_OUT}|
zrep_ssh $desthost "${Z_F_IN} zfs recv -u $READONLYPROP -F $destfs"
fi
else
## arg.. Update your systems!!
# without -u, risky to use send -p if mountpoint set.
# (So we cant avoid it potentially WRONGLY setting mountpoint)
# This means we have to manually set props lower down as well.
# (yeah okay I could check if $mountpoint set, but
# there are too many code branches here already, so too bad!
# Update your system! :p )
#
if [[ "$BBCP" != "" ]] ; then
$BBCP -N io "zfs send ${ZREP_R} ${ZREP_SEND_FLAGS} $snap" \
"$desthost:zfs recv $READONLYPROP -F $destfs"
else
eval zfs send ${ZREP_R} ${ZREP_SEND_FLAGS} $snap ${Z_F_OUT}|
zrep_ssh $desthost "${Z_F_IN} zfs recv $READONLYPROP -F $destfs"
fi
fi
if [[ $? -ne 0 ]] ; then
clearquit $srcfs "Error transferring $snap to $desthost:$destfs. Resetting"
fi
# Successful initial sync! Woo! okay record that, etc.
# ... after stupid old-zfs-compat junk, that is
if (( ! Z_HAS_REC_U )) ; then
_debugprint Because your zfs does not have recv -u, setting remote properties by hand
zrep_ssh $desthost zfs set readonly=on $destfs ||
clearquit Could not set readonly for $desthost:$destfs
zrep_ssh $desthost zfs set ${ZREPTAG}:src-fs=$srcfs $destfs
zrep_ssh $desthost zfs set ${ZREPTAG}:src-host=$Z_LOCAL_HOST $destfs
zrep_ssh $desthost zfs set ${ZREPTAG}:dest-fs=$destfs $destfs
zrep_ssh $desthost zfs set ${ZREPTAG}:dest-host=$desthost $destfs
zrep_ssh $desthost zfs set ${ZREPTAG}:savecount=$Z_SAVE_COUNT $destfs
fi
# Success! So need to set success marker on remote side.
# Make sure to set format to match what zrep_sync() looks for!
if (( Z_HAS_SNAPPROPS )) ; then
typeset sentprop="${ZREPTAG}:sent=`zrep_gettimeinseconds`"
zfs set $sentprop ${snap}
else
# Arg stupidold stuff cant set props on a snapshot
# So we have to manually set these on both sides also,
# "Just in case"
zfs set ${ZREPTAG}:lastsent=${snap} $srcfs
zrep_ssh $desthost zfs set ${ZREPTAG}:lastsent=${snap} $destfs
fi
if [[ "$mountpoint" != "" ]] ; then
print "clearing mountpoint value for remote"
zrep_ssh $desthost zfs inherit mountpoint $destfs
fi
# make sure the above ' set 's (sent, lastsent)
# match what zrep_sync() does !!!
# Note: we have to set master property NOW, not before,
# because "recv -x zrep:master" Does Not Work properly
# Also, it avoids things like "zrep sync all" from attempting
# to sync it before initial sync has been done.
# We don't even have to zrep_lock_fs until this is set
# Make sure value this matches zrep_sentsync
zfs set ${ZREPTAG}:master=yes $srcfs
print Initialization copy of $srcfs to $desthost:$destfs complete
if (( Z_HAS_REC_U )) ; then
print Filesystem will not be mounted
fi
}
zrep_changeconfig(){
if [[ "$1" == "-f" ]] ; then
# skip safety checks
shift
setfsconfigs $@
return
fi
typeset srcfs="$1" check
if [[ "$srcfs" == "" ]] ; then
zrep_errquit "zrep: no fs specified"
fi
check=`getlastsnap $srcfs`
if [[ "$check" == "" ]] ; then
_errprint "No pre-existing zrep snapshots found on $srcfs"
_errprint $srcfs is not initialized for zrep. cannot change config.
zrep_errquit Use zrep init on $srcfs instead
fi
setfsconfigs $@
}