-
Notifications
You must be signed in to change notification settings - Fork 57
/
zrep
executable file
·2874 lines (2375 loc) · 77.9 KB
/
zrep
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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash --noprofile
# For more detailed documentation, see zrep.txt or zrep.overview.txt
# note to self: always make this 3 fields
ZREP_VERSION=2.0.2
################################################
######## start of included files from zrep_top here
########### File: zrep_vars
# This should basically be included as common vars before all zrep stuff.
# It contains all 'constant' definitions, as well as a few crucial
# shared routines, such as lock handling ones.
########################################################################
# User tunable section. These may be overridden in user environment vars
#
# Additionally, we check /etc/default/zrep first.
# If you want to allow users to override the options set there, then be sure
# to use similar syntax to below:
# VAR=${VAR:-yourvalue}
#
if [[ -f /etc/default/zrep ]] ; then
. /etc/default/zrep
fi
SSH=${SSH:-ssh}
ZREP_PATH=${ZREP_PATH:-zrep} #Set to /full/path/to/zrep, if needed, for remote
# Set this if you want to use a different zfs property to store zrep info.
# default has things like zrep:dest-host and zrep:dest-fs
# Change this to non-default, if you want to have multiple destinations.
# You then need to run a separate zrep for each dest.
# In this case, I suggest all runs use non-default value.
# eg: ZREPTAG=zrep-1, ZREPTAG=zrep-2.
# or, ZREPTAG=zrep-uk, ZREPTAG=zrep-us
# ** make sure value can be used in a snapshot name as well, because it
# ** will be !
# !! also, make sure variable is EXPORTED !!
ZREPTAG=${ZREPTAG:-zrep}
# If you'd like a little extra debug output define this env var to something
#DEBUG=1
#If set to yes(default), renames failed sync snapshots to "zrep_#####_unsent"
ZREP_RENAME_UNSENT=${ZREP_RENAME_UNSENT:-yes}
# The default behavior for up-to-date ZFS filesystems, is that zrep will call
# a zfs send/receieve that creates the remote filesystem with pretty much the same options.
# However, sometimes, it is important to have the remote side have a diferent set of
# options, BEFORE data has been transferred. ie: with remote using compression.
# Note1: If set, this will stop the typical inheritance of src filesystem properties
# Note2: It will force the "old zfs creation style" codepath for init. But not
# for the rest of zrep
#ZREP_CREATE_FLAGS="-o compression=on,xx=yy"
#Convenience hook.
#By default, the remote fs should start with the same properties that the master has.
#However, sometimes people want different ones.
#If this var is set, a post-init ssh will be made, to set the requested properties.
#While some zfs implementations support multiple values in a single set command,
#Others do not. So if multiple are set here, multiple ssh calls will be made.
# syntax: "prop1=value [prop2=value prop3=value ... ]"
#ZREP_INIT_REMOTE_PROPERTIES="compression=on"
# The fastest alternative transport, IF you have multicore/thread CPUs,
# would seem to be bbcp. If you have both, then you probably want to
# define something like this in your environment:
#BBCP="bbcp -s 8"
# You can apply filters to tweak throughput in various ways
# Sometimes it helps receive performance to use mbuffer
# Other times, you may have highly compressible data, and custom
# compression routines such as lzop or lz4 may show significant
# gains over ssh builtin compression
#
# lz4 example:
# ZREP_OUTFILTER="lz4 -c"
# ZREP_INFILTER="lz4 -d"
#
# mbuffer example:
# ZREP_OUTFILTER="mbuffer -q -m 1G -s 128k"
# ZREP_INFILTER="mbuffer -q -m 1G -s 128k"
# If you want to recursively create/send snapshots, set this to -R
# (or use the -R option to BOTH zrep init and zrep sync
#ZREP_R=-R
# Sometimes, people may want to add extra flags to "zfs send".
# If your system supports it, then adding -c means that
# compressed filesystems will be sent in compressed form, rather
# then autouncompressing
# --raw is sometime used for encrypted filesystems
#ZREP_SEND_FLAGS="-c --raw"
# Theoretically identical in purpose to ZREP_SEND_FLAGS, but it turns out that when using
# zfs send -t (resume-token)
# you are only allowed to use a restricted set of flags. It normally uses what was
# set the first time.
# So, for resume sends, we clear send flags, and only use what is set here below
#ZREP_RESUME_FLAGS=-v
# There are two types of zfs incremental snapshots.
# The default incremental flag is -I.
# UNLESS you set ZREPTAG to something other than zrep, in which case
# you will have multiple zrep snapshot names probably going to different
# places, and expiration wont work properly on the remote sides
# So we will autochange incremental type to -i. .. unless you explicitly
# set an override value for INC_FLAG in either case.
# Probably should have named this INCR_FLAG, but it's in use now
#ZREP_INC_FLAG=-I
# This currently doesnt do much, and is probably best not user-set.
# I should probably make use of this more standardized.
# But you can set it if you want
#ZREP_VERBOSE=yes
# Some odd people like to configure a non-root user for zrep,
# and not give it expire permissions.
# Well.. okay then...
#ZREP_SKIP_EXPIRE=1
# If you want to override uname -n, with an official
# canonical name for zrep:src-host, use this environment variable when
# you run "zrep init"
#ZREP_SRC_HOST=somehost.your.dom
# Solaris hack to use native perl, which isnt always in $PATH,but should
# always be there. It's also simple, straightforward, and non-extended.
# On other OSs this path will not exist, so it will just fall back to
# use the system default perl. Unless user wants to specify perl path.
# If you dont have /usr/perl5, this wont hurt you so just ignore it.
PERL_BIN=${PERL_BIN:-/usr/perl5/bin}
# Hidden var, that isnt really meant to be used directly.
# It gets set if you use "zrep sync -c".
# But you could theoretically set this directly instead if you prefer
#ZREP_CHANGEDONLY=yes
# This only gets used at init time
ZREP_SAVE_COUNT=${ZREP_SAVE_COUNT:-5}
# This currently is only used in zrep status
# If you change it to %s then zrep will display
# seconds since the most resent snapshot was synced.
ZREP_DATEFORMAT=${ZREP_DATEFORMAT:-%Y/%m/%d-%H:%M:%S}
#########################################################################
#########################################################################
# Everything else below here, should not need to be touched.
#########################################################################
#########################################################################
_debugprint(){
if [[ "$DEBUG" != "" ]] ; then
echo DEBUG: $@
fi
}
# This consolidated function is both for prettiness, and also
# to make dealing with github issue #22 easier, about redirecting stderr
_errprint(){
# I thought /dev/fd was ksh builtin, so safest. But it glitches on some linuxen.
# echo $@ >/dev/fd/2
echo $@ >&2
}
# First we have some "special" internal vars.
# Then autodetect routines,
# and then internal utilities such as locking functions.
# zfs get syntax is so long and ugly, this is just an internal convenience
# Get a zfs property on fs or snap. Get JUST the value, and only
# a "locally set" value rather than an inherited one
ZFSGETLVAL="zfs get -H -o value -s local"
# But.. sometimes you want to allow propagated values. like
# the ones sent via the zrep_init setup
ZFSGETVAL="zfs get -H -o value"
# **warning** !!
# $ZFSGETLVAL returns "" on value not set. However,
# $ZFSGETVAL returns "-" on value not set. Grrr @zfs writers.
# Work around a bug in gentoo ksh that breaks "ls -l" builtin.
# It follows symlinks.
# Would prefer to just override with _AST_FEATURES, but apparently,
# that only gets checked when ksh first starts or something
# Maybe use getconf itself somehow, if safe?
# Trick would be to do
# export _AST_FEATURES="PATH_RESOLVE = physical"
# but cant.
#### This is not even used any more!! But I'm keeping it in as
#### documentation for historical knowlege
#if getconf PATH_RESOLVE > /dev/null 2>&1 ; then
# LS=/bin/ls
#else
# LS=ls
#fi
# -n enforces "no dereference" of existing symlink, which is default
# behaviour on some, but not all systems, apparently
#LN_S="ln -n -s"
# I dont use ln any more, but leaving this for useful historical info.
# side note: ksh built-in ln, DOES NOT SUPPORT -n !
if [[ "$ZREP_SRC_HOST" != "" ]] ; then
Z_LOCAL_HOST=${ZREP_SRC_HOST}
else
Z_LOCAL_HOST=`uname -n`
Z_LOCAL_HOST=${Z_LOCAL_HOST%%.*}
fi
# Slightly ugly to implement this as a global flag. But... it makes
# a lot of things simpler, like "ssh zrep xyhz" for multiple things.
if [[ "$1" == "-R" ]] ; then
ZREP_R="-R"
shift
fi
if [[ "$ZREP_R" == "-R" ]] ; then
# ZREP_R is a user-settable env var. It also gets used in
# "zfs send" commandlines. However.. we also need to call
# "zfs snap".. which requires LOWERCASE R. So this is an automatically set
# mirror of that.
Z_SNAP_R="-r"
fi
if [[ "$MBUFFER" != "" ]] ; then
_errprint WARNING: MBUFER variable deprecated in zrep
_errprint WARNING: use ZREP_OUTFILTER and ZREP_INFILTER instead
ZREP_OUTFILTER="$ZREP_OUTFILTER $MBUFFER"
ZREP_INFILTER="$MBUFFER $ZREP_INFILTER"
fi
# I HATE having to use a global for this.
# However, there is apparently a bug in typeset behaviour
# introduced in ksh93. UGHHH.
#ZREP_FORCE="-f"
# Sneaky vars to avoid having to use if clauses in the core code
# HOWEVER! Note that ksh doesnt seem to evaluate pipe symbols when normaly
# expanded in a command line. So, have to use 'eval' to get them to register
if [[ "$ZREP_OUTFILTER" != "" ]] ; then
Z_F_OUT="| $ZREP_OUTFILTER"
fi
if [[ "$ZREP_INFILTER" != "" ]] ; then
Z_F_IN="$ZREP_INFILTER |"
fi
# used to have polymorphic assign of ZREP_INC_FLAG here, but had
# to move it to AFTER checking if -t option used
# full name for this should probably be something like,
# PROPTYPES_THAT_ZREP_STATUS_AND_LIST_CAN_USE. But that's too long :)
# Not easy to check if property types allow type "received".
# Ancient systems do not allow it
# So, just tie this to MU6 related check,like HAS_SNAPPROPS, lower down
PROPTYPES="local,received"
# dump the usage message, and check for capabilities
# make sure we dont spew for non-root, so that "zrep status" works
case `id` in
*'(root)'*)
ZREP_RUNDIR=${ZREP_RUNDIR:-/var/run}
;;
*)
ZREP_RUNDIR=${ZREP_RUNDIR:-/tmp}
;;
esac
# allow override, for code test utility
zrep_checkfile=${_ZREP_CHECKFILE:-$ZREP_RUNDIR/zrep.check.$$}
zfs >$zrep_checkfile 2>&1
# Previously did a bit of a hack job for feature detection.
# Now attempting to make it smarter,
# at the expense of some startup speed :(
Z_HAS_X=0 # can use recv -x
Z_HAS_REC_U=0 # can use recv -u
Z_HAS_REC_O=0 # can use recv -o (note: SmartOS -o is NOT WHAT WE WANT)
Z_HAS_SNAPPROPS=0
if grep 'help' $zrep_checkfile >/dev/null ;then
# Presume Solaris 11, which has all features, but
# does not allow line-by-line feature detection easily
Z_HAS_X=1 # can use recv -x
Z_HAS_REC_U=1 # can use recv -u
Z_HAS_REC_O=1 # can use recv -o
Z_HAS_SNAPPROPS=1 # can set properties on snapshots
# This also lets me set "last synced" timestamps
# otherwise cant use zrep:sent sanely.
# Would lose information on rollbacks
DEPTHCAP="-d 1" # limits "list -r"
else
if grep 'receive[ |].*-[a-zA-Z]*x' $zrep_checkfile >/dev/null ;then
Z_HAS_X=1 # can use recv -x
fi
if grep 'receive .*-[a-zA-Z]*u' $zrep_checkfile >/dev/null ;then
Z_HAS_REC_U=1 # can use recv -u
fi
# This bit is unfortunately ugly. Two problems:
# SmartOS and FreeBSD implemented recv -o WRONG!
# They use -o to set "origin", not to set options
# So no Z_HAS_REC_O for it!
# But also, -o doesnt even show in the output of solaris zfs usage. sigh.
# So have to be creative.
# Note that some systems have '-o' directly after create, and some do not.
if grep 'create .*-o prop' $zrep_checkfile >/dev/null ;then
# This is probably nested under the create check, because we
# only use recv -o, right after using create -o.
# However, we now ALWAYS use create -o, so.. may be unneccesary
# to nest
# Skip zfs that uses recv -o origin
if ! grep 'rec[ev].*-o origin' $zrep_checkfile >/dev/null &&
grep 'rec[ev].*-o ' $zrep_checkfile >/dev/null ; then
Z_HAS_REC_O=1 # can use recv -o
fi
fi
if grep 'set .*snapshot' $zrep_checkfile >/dev/null ;then
Z_HAS_SNAPPROPS=1 # can set properties on snapshots
fi
if grep 'list.*-d' $zrep_checkfile >/dev/null ;then
DEPTHCAP="-d 1" # limits "list -r"
else
DEPTHCAP=""
echo WARNING: old ZFS version detected with no depth protection
echo WARNING: You may not nest zrep managed filesystems
fi
fi
if ((!Z_HAS_SNAPPROPS)) ; then
PROPTYPES="local"
fi
rm $zrep_checkfile
Z_LOCK_RETRY=${Z_LOCK_RETRY:-10} # default 10 second retry, 1 per sec
# This is named like a global override. and CAN be overridden by user.
# But should only be used in zrep_vars module
# Note: This path is why you should only give zfs privileges to a SINGLE USER.
Z_GLOBAL_LOCKFILE=$ZREP_RUNDIR/zrep.lock
if [[ "$Z_GLOBAL_PID" == "" ]] ; then
export Z_GLOBAL_PID=$$
fi
Z_SETHOLD=${Z_SETHOLD:-"zfs hold"}
# if your zfs isnt new enough, and you like to live dangerously,
# you can skip setting holds by using this instead.
# Although I may not have gotten around to using this in the code either!
#Z_SETHOLD="echo skipping zfs hold on"
# return PID of proc holding global lock, or nothing
zrep_global_lock_pid(){
cat $Z_GLOBAL_LOCKFILE 2>/dev/null
}
# return 0 if "we" are holding lock, 1 otherwise
# Note that we check for "us, OR our global parent", if different
#
zrep_has_global_lock(){
lockpid=`zrep_global_lock_pid`
if [[ "$lockpid" == "" ]] ; then return 1 ; fi
if [[ "$lockpid" != "$Z_GLOBAL_PID" ]] ; then
if [[ "$lockpid" != "$$" ]] ; then
_debugprint 'has_global_lock? no. lock held by PID' $lockpid
return 1
fi
fi
return 0
}
#Note: it is an ERROR to call this if you already have lock
#It is binary, not recursive ownership.
#We do NOT try to clean up stale global lock.
#This is a shortterm lock. It should never be stale. If it is,
#it could indicate a more serious system/zrep problem happening.
zrep_get_global_lock(){
typeset retry_count=$Z_LOCK_RETRY
typeset lockpid
set -C #noclobber
# ignore error this time, because we retry anyway.
echo $Z_GLOBAL_PID > $Z_GLOBAL_LOCKFILE 2>/dev/null && return 0
# Otherwise, deal with fail/retry.
# Careful of race conditions on stale CLEAN UP!
# How to resolve problem where
# * multiple instances running
# * one instance detects stale
# * multiple instances decide to remove it
# * ONE removes it and creates new symlink
# * SECOND one was paused between detection and removal.. so removes
# * VALID lockfile?!?!
# For now, must request manual cleanup
while (( retry_count > 0 )); do
sleep 1
errmsg=`echo $Z_GLOBAL_PID 2>&1 > $Z_GLOBAL_LOCKFILE `
if [[ $? -eq 0 ]] ; then return 0 ; fi
retry_count=$((retry_count-1))
lockpid=`zrep_global_lock_pid`
if [[ "$lockpid" -le 0 ]] ; then
zrep_errquit ERROR: invalid contents for global lock file $Z_GLOBAL_LOCKFILE
fi
# Does the process holding the lock actually still exist?
# In theory, teenietiny chance of race condition for false stale. That's okay.
kill -0 $lockpid 2>/dev/null
if [[ $? -ne 0 ]] ; then
_errprint ERROR: stale global lock file
_errprint ERROR: shut down ALL zrep instances, then manually remove
_errprint $Z_GLOBAL_LOCKFILE
fi
done
echo Failed to acquire global lock
echo Error message was: $errmsg
return 1
}
zrep_release_global_lock(){
if zrep_has_global_lock ; then
rm $Z_GLOBAL_LOCKFILE
return $?
else
echo ERROR: zrep_release_global_lock called, but do not own lock
return 1
fi
}
# returns PID of zrep process holding a lock on filesystem, if there is one.
# NOTE: If "-s local" used, prints "" if lock unheld
# If no -s specified, prints "-" if lock unheld
zrep_fs_lock_pid(){
$ZFSGETLVAL ${ZREPTAG}:lock-pid $1
}
zrep_has_fs_lock(){
typeset check=`$ZFSGETLVAL ${ZREPTAG}:lock-pid $1`
if ((check == $$)) ; then
return 0
else
return 1
fi
}
# use global lock first (if not already), then
# grab lock on individual fs
# return 1 on fail, 0 on lock acquired
# Note that it is an ERROR to call this, if you already have lock
# Note2: if a dead process has lock, it will forcibly override and
# acqure lock
zrep_lock_fs(){
# global lock is slow. so do quickcheck first.
typeset check=`zrep_fs_lock_pid $1` newcheck
if [[ "$check" != "" ]] ; then
# See if owning process still exists.
kill -0 $check 2>/dev/null
if [[ $? -eq 0 ]] ; then
_debugprint lock is still held by $check
return 1
else
_debugprint lock is no longer held by $check
fi
fi
zrep_get_global_lock
if [[ $? -ne 0 ]] ; then
if [[ "$DEBUG" != "" ]] ; then
_errprint zrep_lock_fs: failed to get global lock. PID=$$ fs=$1
fi
return 1
fi
# Yes we already checked this, but we didnt have global lock.
# Avoid race condition and doublecheck now that we have global lock.
if [[ "$check" != "" ]] ; then
newcheck=`zrep_fs_lock_pid $1`
if [[ "$newcheck" != "$check" ]] && [[ "$newcheck" != "" ]]
then
# oops. someone else beat us to it.
# Better luck next time.
zrep_release_global_lock
return 1
fi
# Keep in mind that stdin/out could be busy
# Cant use regular debugprint
if [[ "$DEBUG" != "" ]] ; then
_errprint overiding stale lock on $1 from pid $check
fi
fi
zfs set ${ZREPTAG}:lock-pid=$$ $1
zfs set ${ZREPTAG}:lock-time=`date +%Y%m%d%H%M%S` $1
if [[ "$DEBUG" != "" ]] ; then
_errprint DEBUG: zrep_lock_fs: set lock on $1
fi
zrep_release_global_lock
}
# release lock, if we have it.
# Since this could be called by an exit cleanup routine blindly,
# dont exit program if we dont have lock. But do return error
zrep_unlock_fs(){
typeset lockpid=`zrep_fs_lock_pid $1`
if ((lockpid != $$)) ; then return 1; fi
#since "we" already have it locked, no need to get global lock first
zfs inherit ${ZREPTAG}:lock-time $1
zfs inherit ${ZREPTAG}:lock-pid $1
if [[ "$DEBUG" != "" ]] ; then
_errprint zrep_unlock_fs: unset lock on $1
fi
return 0
}
# Quit whole program with error status, outputting args to stderr
# Release global lock if we are holding it
# Unless we're running in parallel batch mode
# I'll need to plan that out more carefully!
#
zrep_errquit(){
_errprint Error: "$@"
if zrep_has_global_lock ; then
if [[ "$$" -ne "$Z_GLOBAL_PID" ]] ; then
echo EXTRA-ERROR: Running in child proc.
echo 'Not sure whether to release global lock. NOT releasing!'
exit 1
else
zrep_release_global_lock
fi
fi
exit 1
}
# Optimization wrapper for ssh: if destination host is ourself, dont use ssh.
# Just run the local command mentioned
# Be careful about quotes here. In fact, try not to use any.
# Usage: zrep_ssh desthost commands_for_ssh go_here
zrep_ssh(){
typeset ssh_cmd
case "$1" in
localhost|$Z_LOCAL_HOST)
ssh_cmd="eval"
shift
$ssh_cmd "$@"
return $?
;;
esac
if [[ "$2" == "$ZREP_PATH "* ]] && [[ "$DEBUG" != "" ]]
then
#okay yes this is horrible. sigh.
#we normally go to great lengths to preserve ssh arg as single quoted string,
# to identically match passed in arg quoting.
#but this next line undoes that
set -- $*
ssh_cmd="$SSH $1 $ZREP_PATH -D"
shift
shift
$ssh_cmd "$@"
return $?
fi
ssh_cmd="$SSH $1"
shift
$ssh_cmd "$@"
return $?
}
zrep_gettimeinseconds(){
typeset seconds
typeset PATH=$PERL_BIN:$PATH
seconds=`printf '%(%s)T'`
if [[ -z "$seconds" ]] ; then
# Unfortunately, solaris date doesnt do '%s', so try perl first.
# It's more consistent.
seconds=`perl -e 'print int(time);' 2>/dev/null`
fi
if [[ -z "$seconds" ]] ; then
# attempt fallback if no perl present (eg: stock FreeBSD)
seconds=`date +%s`
fi
if [[ -z "$seconds" ]] ; then
zrep_errquit zrep_gettimeinseconds doesnt know what to do
fi
echo $seconds
}
###### File: zrep_status
# be sure to have included zrep_vars
# This file contains all "status" related routines.
# It should be folded into final "zrep" script
#
#Give this a top level zrep registered filesystem, NOT snapshot.
# Will echo out various status points, such as last sync date.
# Or if given no args, will echo out sync date for all zrep mastered fs
# Note that the date given is time of SNAPSHOT, not time sync completed.
#
zrep_status(){
typeset check fs srcfs desthost destfs date lastsynced
typeset verbose=0 vdate="" monitorr=0
typeset printall=0
if [[ "$1" == "-v" ]] ; then
verbose=1 ; shift
fi
if [[ "$1" == "-m" ]] ; then
monitor=1 ; shift
fi
if [[ "$1" == "" ]] ; then
set -- `zrep_list_master`
elif [[ "$1" == "-a" ]] ; then
set -- `zrep_list`
printall=1
fi
while [[ "$1" != "" ]] ; do
fs="$1"
destfs=`$ZFSGETVAL ${ZREPTAG}:dest-fs $fs`
if [[ "$destfs" == "-" ]] || [[ "$destfs" == "" ]]; then
zrep_errquit "$fs is not a zrep registered filesystem"
fi
if ((monitor)) ; then
# have to allow for ONE unsent, since it might be in progress
typeset count=`getunsentcount $fs`
if [[ "$count" -gt 1 ]] ; then
echo WARNNING: unset snapshot count at $count
fi
continue
fi
lastsynced=`getlastsnapsent $fs`
if [[ "$lastsynced" == "" ]] ; then
date="[NEVER]"
else
if (( Z_HAS_SNAPPROPS )) ; then
typeset sentseconds=`$ZFSGETVAL ${ZREPTAG}:sent $lastsynced`
date=`printf "%(${ZREP_DATEFORMAT})T" "$sentseconds"`
vdate=${date%:*}
fi
#This is also a fallback for no-perl FreeBSD systems
if [[ "$vdate" == "" ]] ; then
date=`$ZFSGETVAL creation $lastsynced`
vdate=${date#????}
fi
fi
if ((printall)) && ((verbose)) ; then
# If we are printing out ALL filesystems,
# then we have to make sure left side is always
# "src filesystem", not "named filesystem"
# then we have to check what the src fs is
srcfs=`$ZFSGETVAL ${ZREPTAG}:src-fs $fs`
else
# Yes, okay, if -a is used, then
# technically, this isnt always "src".
# but it prints out right, so close enough :)
srcfs="$fs"
fi
if ((verbose)) ; then
desthost=`$ZFSGETVAL ${ZREPTAG}:dest-host $srcfs`
printf "%-25s->%-35s %s\n" $srcfs "$desthost:$destfs" "$vdate"
else
printf "%-52s " $srcfs
echo "last: $date"
fi
shift
done
}
_master_fs_names(){
zfs get -H -o name -s local ${ZREPTAG}:master "$@"
}
# convenience function to list only local filesystems for which we are
# zrep master for.
# In contrast, zrep_list, lists ALL zrep registered filesystem, at the moment.
#
# Annoyingly... it would be way faster if we could just stick with the
# pure "zfs get" implementation, but we also need to deal with the zone
# issue. When a single zfs filesystem is visible aross multiple zones,
# we dont want them all thinking they are master
#
# Durn. Individual validation required.
zrep_list_master(){
typeset srchost
for fs in `_master_fs_names "$@"` ; do
srchost=`$ZFSGETVAL ${ZREPTAG}:src-host $fs`
if [[ "$srchost" == "$Z_LOCAL_HOST" ]] ; then
echo $fs
fi
done
}
# Given ONE filesystem, print all zrep properties for it.
# Note that this is internal routine. we do not validate input.
list_verbose(){
echo $1:
# sneaky cheat: only user-set properties will
# match these 'source' types. So "grep zrep:" is not
# neccessary. Although we may pick up other user-set values,
# but that is not neccessarily a bad thing
zfs get -H -o property,value -s $PROPTYPES all $1
echo "last snapshot synced: `getlastsnapsent $1`"
}
# Note: called by both user, AND by zrep_status
#
# Usage:
# zrep_list [-v]
# zrep_list [-L]
# zrep_list [-v] fs1 fs2
#(also zrep_list -s which passes to zrep_list_snaps)
#
# list all zrep-initialized filesystems (NOT snapshots..)
# If no specific fs listed, will show master, AND received filesystems,
# unless -L given (in which case, only local masters will be shown)
#
# Normal output is one line per fs.
#
# -v gives all properties of each filesystem
# Give only one of -L or -v
#
zrep_list(){
typeset fslist="" verbose=0
# This works because we only set this property on the actual fs.
# "source type" on snapshots for this property is "inherited" not local
# or "received"
typeset printcmd="zfs get -H -o name -s $PROPTYPES ${ZREPTAG}:dest-fs"
case $1 in
-v)
verbose=1
printcmd=list_verbose
shift
;;
-L)
# reminder: cant have this, AND verbose.
printcmd="zrep_list_master"
shift
;;
-s)
shift
zrep_list_snaps "$@"
return
;;
esac
# If specific fs(s) named, iterate over them and quit
if [[ "$1" != "" ]] ; then
while [[ "$1" != "" ]] ; do
if zfs list -t filesystem,volume $1 >/dev/null 2>&1 ; then
$printcmd $1
else
zrep_errquit "Expecting filesystem, but got $1"
fi
shift
done
return
fi
# Must be "list all" now. But which output format?
# If not verbose, we have a nice shortcut to just list
# all filesystems that zrep has marked.
if (( $verbose == 0)) ; then
$printcmd
return
fi
# oh well. have to step through them one by one now, to
# echo out the properties associated with each zrep filesystem
fslist=`zfs get -H -o name -s $PROPTYPES ${ZREPTAG}:dest-fs`
for fs in $fslist ; do
$printcmd $fs
echo ""
done
}
# Similar to zrep_list, but lists SNAPSHOTS instead of filesystems
# The purpose is to allow a sysadmin to see easily when snapshots have
# been created.
# Either give a list of specific filesystems, or no args, which
# will attempt to list all zrep-related snapshots
# It will list only zrep MASTER filesystem snapshots, in that case.
zrep_list_snaps(){
if [[ "$1" == "" ]] ; then
set -- `_master_fs_names`
if [[ "$1" == "" ]] ; then
_errprint "No zrep master filesystems found"
return 0
fi
fi
while [[ "$1" != "" ]] ; do
zfs list -r -t snapshot -o name,creation $1
shift
done
}
# Given a filesytem name, prints out full snapshot name of last successfully synced snap
zrep_getlastsent(){
if [[ "$1" == "" ]] ; then
_errprint ERROR: zrep uptodate requires the name of a zrep managed fs
exit 1
fi
echo `getlastsnapsent $1`
}
# Give a filesystem name.
# Gets last sent snapshot, and determines if there have
# been any writes since then.
# If not, then file sytem is "up to date"
zrep_uptodate(){
if [[ "$1" == "" ]] ; then
_errprint ERROR: zrep uptodate requires the name of a zrep managed fs
exit 1
fi
typeset bytecount
bytecount=`$ZFSGETVAL written "$1"`
# two fail conditions are:
# 1. not zrep filesystem
# 2. system does not support "zfs get written"
# Either way we count it as "not up to date"
if [[ $bytecount == 0 ]] ; then
return 0
else
return 1
fi
}
################ 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))