forked from bolthole/zrep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzrep_snap
622 lines (510 loc) · 17.9 KB
/
zrep_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
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
################ 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*
#
######################################################################
#pass in a zrep ZFS snapshot name. strip our our sequence number and echo in back
_getseqnum(){
echo "$1" | sed 's/.*@'${ZREPTAG}'_\(......\).*/\1/'
}
# 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
_getseqnum `getlastsnap $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
# note to self: in modern zrep, ALL successfuly synced snapshots have
# a timestamp value, zrep:sent=TIMESTAMPHERE
# Thats why the sort and tail is neccessary
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
echo $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
}
# HORRIBLY DANGEROUS
# ONLY should be used by zrep_sentsync.
# Kind of the inverse to getlastsnapsent. But should NOT use "fallback method" in getlastsnapsent.
# This is only for zrep_sentsync, which only uses newer method.
_clearlast(){
typeset lastsent
lastsent=`zfs get -H -o name -r -s local ${ZREPTAG}:sent $1|tail -1`
while [[ "$lastsent" != "" ]] ; do
_errprint WARNING: clearing sent value from $lastsent
zfs inherit ${ZREPTAG}:sent $lastsent
lastsent=`zfs get -H -o name -r -s local ${ZREPTAG}:sent $1|tail -1`
done
}
# return a number, which is the difference between the lastsnapshot counter, and
# the last successfully synced snapshot counter.
# In theory, can only be positive.
#
getunsentcount(){
typeset lastsynced lastsnap lastsyncedseq lastsnapseq
lastsynced=`getlastsnapsent $1`
lastsnap=`getlastsnap $1`
if [[ "$lastsynced" == "$lastsnap" ]] ; then
return 1
fi
lastsyncedseq=`getseqnum $lastsynced`
lastsnapseq=`getseqnum $lastsnap`
lastsyncedseq=$((16#$lastsyncedseq))
lastsnapseq=$((16#$lastsnapseq))
if [[ "$lastsyncedseq -lt 1 ]] ; then
zrep_errquit Error: cannot parse $lastsynced
fi
if [[ "$lastsnapseq -lt 1 ]] ; then
zrep_errquit Error: cannot parse $lastsnap
fi
echo $(( $lastsnapseq - $lastsyncedseq ))
}
# 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 echo ""
#
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 echo $senttime ; return 0; fi
# ooops. try fallback to nasty old zfs-compat style
senttime=`zfs get -H -o value ${ZREPTAG}:lastsent $fs`
echo $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 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
echo -n $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
echo "WARNING: Removing all zrep configs and snapshots from $1"
echo " (for TAG=${ZREPTAG})"
if [[ "$ZREP_FORCE" != "-f" ]] ; then
echo Continuing in 10 seconds
sleep 10
fi
_clearsnaps $1
_clearvars $1
}
_clearsnaps(){
echo Destroying any zrep-related snapshots from $1
snaplist=`list_autosnaps $1`
for snap in $snaplist ; do
zfs destroy -r $snap
done
}
_clearvars(){
echo 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
_clearsnaps $1
_clearvars $1
shift
zrep_errquit "$@"
}
# Shared internal routine.
#
# 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
echo WARNING: forcing override sync to top level pool $desthost:$destfs
echo 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=$ZREP_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
typeset token
if [[ "$ZREP_FORCE" != "-f" ]] ; then
zrep_ssh $desthost zfs list $destfs 2>/dev/null &&
zrep_errquit "$desthost:$destfs already exists! Will not overwrite without -f"
fi
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
echo "$srcfs is at least partially configured by zrep"
check="`$ZFSGETLVAL ${ZREPTAG}:master $srcfs`"
if [[ "$check" != "" ]] ; then
zrep_errquit "${ZREPTAG}:master detected!!"
fi
if [[ "$ZREP_RESUME" == "" ]] ; then
zrep_errquit "To re-initialize, first use zrep clear $srcfs"
fi
# if it exists already, presume we need to resume an init.
# if we cant, then its an error.
token=`zrep_ssh $desthost $ZFSGETVAL receive_resume_token $destfs`
if [[ "$token" == "-" ]] ; then token="" ; fi
if [[ "$token" == "" ]] ; then
zrep_errquit Partial init of $src detected but no resume token found. Suggest you zrep clear and start again
fi
echo "Partially complete init detected. Attempting to resume send"
if [[ "$BBCP" != "" ]] ; then
SENDCMD="zfs send -t $token ${ZREP_RESUME_FLAGS}"
$BBCP -N io "$SENDCMD" \
"$desthost:zfs recv $destfs"
else
eval zfs send ${ZREP_RESUME_FLAGS} -t $token ${Z_F_OUT} |
zrep_ssh $desthost "${Z_F_IN} zfs recv $destfs"
fi
if [[ $? -ne 0 ]] ; then
zrep_errquit resume send of zrep init $srcfs failed
fi
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
#get this for later
mountpoint=`$ZFSGETLVAL mountpoint $srcfs`
# Make this section conditional, so that we keep shared codepath for completion
# of initial sync.
# This is actually the "normal" snapshot and sync path.
#
if [[ "$token" == "" ]] ; then
if [[ "$ZREP_RESUME" != "" ]] ; then
recv_s=-s
fi
echo Setting zrep 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`
if (( Z_HAS_REC_O )) ; then
READONLYPROP="-o readonly=on"
else
READONLYPROP=""
echo Warning: zfs recv lacking -o readonly
fi
if [[ "$ZREP_CREATE_FLAGS" != "" ]] || [[ "$READONLYPROP" == "" ]] ; then
echo Creating destination filesystem as separate step
# normally would want to use -o readonly here.
# however, that breaks when -R is used.
# set it after transfer instead
zrep_ssh $desthost zfs create $ZREP_CREATE_FLAGS $vflags $destfs || zrep_errquit "Cannot create $desthost:$destfs"
fi
snap="${srcfs}@${ZREPTAG}_000000"
echo 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...
#
echo 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 $recv_s -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 $recv_s -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 $recv_s -F $destfs"
else
eval zfs send ${ZREP_R} ${ZREP_SEND_FLAGS} $snap ${Z_F_OUT}|
zrep_ssh $desthost "${Z_F_IN} zfs recv $READONLYPROP $recv_s -F $destfs"
fi
fi
if [[ $? -ne 0 ]] ; then
if [[ "$ZREP_RESUME" == "" ]] ; then
clearquit $srcfs "Error transferring $snap to $desthost:$destfs. Resetting"
else
zrep_errquit "Error transferring $snap to $desthost:$destfs. RESUME set. Not clearing $srcfs"
fi
fi
fi #resume token set
# Successful initial sync! Woo! okay, record that fact and complete setting properties
# ... after stupid old-zfs-compat junk, that is
if (( ! Z_HAS_REC_U )) || [[ "$ZREP_CREATE_FLAGS" != "" ]] ; then
_debugprint Because your zfs does not have recv -u,
_debugprint or maybe because ZREP_CREATE_FLAGS set,
_debugprint setting remote properties by hand
zrep_ssh $desthost zfs set readonly=on $destfs
if [[ $? -ne 0 ]] ; then
errquit Could not set readonly for $desthost:$destfs. However, snapshot has been synced. \
Manual intervention required if you do not wish to simply clear and start again.
fi
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=$ZREP_SAVE_COUNT $destfs
fi
if [[ "$ZREP_CREATE_FLAGS" != "" ]] || [[ "$READONLYPROP" == "" ]] ; then
echo setting readonly on $desthost:$destfs manually
zrep_ssh $desthost zfs set readonly=on $destfs
fi
# Extra, non-symmeric properties the user might want on the remote side.
if [[ "$ZREP_INIT_REMOTE_PROPERTIES" != "" ]] ; then
for prop in $ZREP_INIT_REMOTE_PROPERTIES ; do
zrep_ssh $desthost zfs set $prop $destfs
done
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
echo "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
echo Initialization copy of $srcfs to $desthost:$destfs complete
if (( Z_HAS_REC_U )) ; then
echo 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 $@
}