forked from oetiker/znapzend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZnapZend.pm
1743 lines (1521 loc) · 89 KB
/
ZnapZend.pm
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
package ZnapZend;
use Mojo::Base -base;
use Mojo::IOLoop::Subprocess;
use Mojo::Log;
use ZnapZend::Config;
use ZnapZend::ZFS;
use ZnapZend::Time;
use POSIX qw(setsid SIGTERM SIGKILL WNOHANG);
use Scalar::Util qw(blessed);
use Sys::Syslog;
use File::Basename;
use Data::Dumper;
### loglevels ###
my %logLevels = (
debug => 'debug',
info => 'info',
warn => 'warning',
error => 'err',
fatal => 'alert',
);
### attributes ###
has debug => sub { 0 };
has resume => sub { 0 };
has noaction => sub { 0 };
has nodestroy => sub { 0 };
has oracleMode => sub { 0 };
has recvu => sub { 0 };
has compressed => sub { 0 };
has sendRaw => sub { 0 };
has skipIntermediates => sub { 0 };
has forbidDestRollback => sub { 0 };
has lowmemRecurse => sub { 0 };
has rootExec => sub { q{} };
has zfsGetType => sub { 0 };
has connectTimeout => sub { 30 };
has runonce => sub { 0 };
has recursive => sub { 0 };
has inherited => sub { 0 };
has since => sub { 0 };
has dataset => sub { undef };
has daemonize => sub { 0 };
has loglevel => sub { q{debug} };
has logto => sub { q{} };
has pidfile => sub { q{} };
has forcedSnapshotSuffix => sub { q{} };
has defaultPidFile => sub { q{/var/run/znapzend.pid} };
has terminate => sub { 0 };
has autoCreation => sub { undef };
has timeWarp => sub { undef };
has nodelay => sub { 0 };
has skipOnPreSnapCmdFail => sub { 0 };
has skipOnPreSendCmdFail => sub { 0 };
has cleanOffline => sub { 0 };
has 'mailErrorSummaryTo';
has backupSets => sub { [] };
has zConfig => sub {
my $self = shift;
ZnapZend::Config->new(debug => $self->debug, noaction => $self->noaction,
rootExec => $self->rootExec, timeWarp => $self->timeWarp,
zfsGetType => $self->zfsGetType,
zLog => $self->zLog);
};
has zZfs => sub {
my $self = shift;
ZnapZend::ZFS->new(debug => $self->debug, noaction => $self->noaction,
nodestroy => $self->nodestroy, oracleMode => $self->oracleMode,
resume => $self->resume,
recvu => $self->recvu, connectTimeout => $self->connectTimeout,
lowmemRecurse => $self->lowmemRecurse, skipIntermediates => $self->skipIntermediates,
rootExec => $self->rootExec, zfsGetType => $self->zfsGetType,
zLog => $self->zLog, compressed => $self->compressed,
sendRaw => $self->sendRaw, forbidDestRollback => $self->forbidDestRollback);
};
has zTime => sub { ZnapZend::Time->new(timeWarp=>shift->timeWarp) };
has zLog => sub {
my $self = shift;
#check if we are logging to syslog
my ($syslog) = $self->logto =~ /^syslog::(\w+)$/;
# logging defaults to syslog::daemon (STDERR if runonce)
$syslog = 'daemon' if !$self->logto && !$self->runonce;
#make level mojo conform
my ($level) = grep { $logLevels{$_} eq $self->loglevel } keys %logLevels
or die "ERROR: only log levels '" . join("', '", values %logLevels)
. "' are supported\n";
my $log = Mojo::Log->new(
path => $syslog ? '/dev/null'
: $self->runonce && !$self->logto ? undef
: $self->logto,
level => $level
);
if ($syslog) {
$log->unsubscribe('message');
#add syslog handler if either syslog is explicitly specified or no logfile is given
openlog(basename($0), 'cons,pid', $syslog);
$log->on(
message => sub {
my ($log, $level, @lines) = @_;
syslog($logLevels{$level}, @lines) if $log->is_level($level);
}
);
}
else {
$log->with_roles('+Clearable');
$SIG{USR1} = sub { $log->clear_handle };
}
return $log;
};
### private methods ###
my $killThemAll = sub {
my $self = shift;
$self->zLog->info("terminating znapzend (PID=$$) ...");
#set termination flag
$self->terminate(1);
Mojo::IOLoop->reset;
for my $backupSet (@{$self->backupSets}){
kill (SIGTERM, $backupSet->{snap_pid}) if $backupSet->{snap_pid};
kill (SIGTERM, $backupSet->{send_pid}) if $backupSet->{send_pid};
}
sleep 1;
for my $backupSet (@{$self->backupSets}){
waitpid($backupSet->{snap_pid}, WNOHANG)
|| kill(SIGKILL, $backupSet->{snap_pid}) if $backupSet->{snap_pid};
waitpid($backupSet->{send_pid}, WNOHANG)
|| kill(SIGKILL, $backupSet->{send_pid}) if $backupSet->{send_pid};
}
$self->zLog->info("znapzend (PID=$$) terminated.");
exit 0;
};
# Return an array of dataset names which are local descendants of the
# provided $backupSet->{src} and have an explicit org.znapzend:enabled=off
# and/or an intermediate nearest ancestor which is disabled with recursion.
# The array may be empty if not in recursive mode, or no descendants
# exist, or none are disabled.
my $listDisabledSourceDescendants = sub {
my $self = shift;
my $backupSet = shift;
#no HUP handler in child
$SIG{HUP} = 'IGNORE';
my @dataSetsExplicitlyDisabled = ();
my %explicitEnabled = ();
my %explicitRecursiveLocal = ();
my %explicitRecursiveInherited = ();
if ($backupSet->{recursive} eq 'on') {
$self->zLog->info("checking for explicitly excluded ZFS dependent datasets under '$backupSet->{src}'");
# restrict the list to the datasets that are descendant from the current
###my @dataSetList = grep /^$backupSet->{src}($|\/)/, @{$self->zZfs->listDataSets()};
my @dataSetList = @{$self->zZfs->listDataSets(undef, $backupSet->{src}, 1)};
if (@dataSetList) {
# the default sub-dataset enablement value is implicitly "on"
# (technically, the value inherited from $backupSet which we
# are currently processing, because it is enabled)
my $enabled_default = 'on';
if (defined($backupSet->{enabled})) {
$enabled_default = $backupSet->{enabled};
}
# for each sub-dataset: if the property "enabled" is set to "off", set the
# newly created snapshot for removal
for my $dataSet (@dataSetList){
# get the value for org.znapzend property
my @cmdLE = (@{$self->zZfs->priv}, qw(zfs get -H -s local -o value org.znapzend:enabled), $dataSet);
print STDERR '# ' . join(' ', @cmdLE) . "\n" if $self->debug;
open my $cmdOutLE, '-|', @cmdLE;
my @cmdLR = (@{$self->zZfs->priv}, qw(zfs get -H -s local -o value org.znapzend:recursive), $dataSet);
print STDERR '# ' . join(' ', @cmdLR) . "\n" if $self->debug;
open my $cmdOutLR, '-|', @cmdLR;
# if the property does not exist, the command will just return.
# In this case, use the default determined above.
my $propLE = <$cmdOutLE>; # || $enabled_default;
if ($propLE) {
chomp($propLE);
}
my $propLR = <$cmdOutLR>;
my $propIR;
if ($propLR) {
chomp($propLR);
} else {
my @cmdIR = (@{$self->zZfs->priv}, qw(zfs get -H -s inherited -o value org.znapzend:recursive), $dataSet);
print STDERR '# ' . join(' ', @cmdIR) . "\n" if $self->debug;
open my $cmdOutIR, '-|', @cmdIR;
$propIR = <$cmdOutIR>;
if ($propIR) {
chomp($propIR);
}
}
# ASSUMPTION: We process the dataSetList in alphanumeric
# order, so parent datasets were seen before child ones!
$explicitEnabled{$dataSet} = $propLE;
$explicitRecursiveLocal{$dataSet} = $propLR;
$explicitRecursiveInherited{$dataSet} = $propIR;
$self->zLog->debug("=== $dataSet snapshotting:" .
" enabled=" . (defined($propLE) ? $propLE : "<undef>") .
" recursive=" . (defined($propLR) ? $propLR : "<undef>") .
" (inherited_recursive=" . (defined($propIR) ? $propIR : "<undef>") . ")"
) if $self->debug;
# We treat a dataset as individually not-enabled for backup
# snapshot processing if either:
# * its local enabled==off, whatever the recursive setting
# * the nearest ancestor with a local setting about this has
# both enabled==off and recursive==on (explicitly set in it)
# Note that a dataset, whose nearest ancestor has enabled==off
# but does not set (or enable as "on") the "recursive" option,
# would be backed up as usual.
# In case of partial pruning and un-pruning, for a sub-dataset
# whose nearest ancestor has enabled==on again (which may also
# re-define the "recursive" option), a ZFS-inherited definition
# of "recursive" would be used.
# It is not valid for a single dataset configuration to only
# specify "recursive" (however, specifying only "enabled" is
# valid, as well as specifying "enabled" and "recursive" as
# the only two local znapzend-related properties).
# Is this source dataset not-enabled for snapshotting? Tri-state:
# -1 Known enabled (loop to next item)
# 0 Continue investigating
# 1 Known disabled (add to output list)
my $isDisabled = 0;
# Is the local "enabled" property value set for this dataset?
if (defined($propLE)) {
if ($propLE eq 'off') { $isDisabled = 1; }
elsif ($propLE eq 'on') { $isDisabled = -1; }
}
if (!$isDisabled) {
# Is anything "inherited" from local property definitions?
# Backtrack through collected hashmaps...
my $nearestLE; # local enabled
my $nearestLR; # local recursive
my $nearestLEds; # dataset with local enabled setting
my $nearestLRds; # dataset with local recursive setting
my $ancestor = $dataSet;
while ($ancestor ne $backupSet->{src}) {
# Chop off last slash and dataset name after it
my $idx = rindex($ancestor, '/');
if ($idx == 0) {
die "Did not expect to see a starting slash in dataset name in the loop under $backupSet->{src}: $dataSet => $ancestor"
}
elsif ($idx < 0) {
# -1 means no slash, looking at a pool's root dataset
# May be our starting point or last loop cycle, so process it?
# Although $backupSet->{src} should have ruled it out anyway.
# In any case, avoid infinite loop upon errors, bail out below.
$self->zLog->debug("WARNING: Did not expect to see a root dataset name in the loop under $backupSet->{src}: $dataSet => $ancestor");
} else {
$ancestor = substr($ancestor, 0, $idx);
}
if ((!defined($nearestLE)) and defined($explicitEnabled{$ancestor})) {
$nearestLE = $explicitEnabled{$ancestor};
$nearestLEds = $ancestor;
}
if ((!defined($nearestLR)) and defined($explicitRecursiveLocal{$ancestor})) {
$nearestLR = $explicitRecursiveLocal{$ancestor};
$nearestLRds = $ancestor;
}
if (defined($nearestLE) and defined($nearestLR)) {
last;
}
if ($idx < 1 || $ancestor eq $dataSet) {
# Chopping did not go well
last;
}
}
if (defined($nearestLE)) {
# Got something, is it "on" or "off", is recursion involved?
if (defined($nearestLR)) {
# Both properties are locally defined in some ancestor(s)
if ($nearestLE eq "off" and $nearestLR eq "on" and $nearestLEds eq $nearestLRds) {
# An ancestor defines both enabled==off and recursive==on
# explicitly, and is the nearest one to define such things.
$isDisabled = 1;
}
# else enabled=on, or recursive=off, or not set in same dataset
elsif ($nearestLE eq "on" and $nearestLR eq "on" and $nearestLEds eq $nearestLRds) {
# logical inheritance of "enabled=on"
$isDisabled = -1;
}
# else enabled=on, or recursive=off, or not set in same dataset
elsif ($nearestLE eq "on" and $nearestLR eq "off" and $nearestLEds eq $nearestLRds) {
# logical inheritance of "enabled=..." from parent of that
# ancestor because its own "enabled=on" is not recursive
my $ancestor2 = $nearestLEds;
while ($ancestor2) {
$ancestor2 =~ s/\/[^\/]+$// ;
if (grep {$_ eq $ancestor2} @dataSetsExplicitlyDisabled) {
# We handled that ancestor and found it disabled, in an earlier loop
$isDisabled = 1;
last;
}
if (index($ancestor2, '/') < 0) {
last;
}
}
}
elsif ($nearestLE eq "on" and defined($propIR) and $propIR eq "on") {
# zfs-inheritance of "enabled=on"
$isDisabled = -1;
}
} # else only "enabled=whatever" is defined in some ancestor, but
# not "recursive" anywhere => this one remains enabled, probably
} # else no ancestor defines a local "enabled" setting
}
if (!$isDisabled) {
if ($enabled_default eq 'off') {
# Somehow processing a disabled backupSet?..
$isDisabled = 1;
} elsif ($enabled_default eq 'on') {
$isDisabled = -1;
}
}
$self->zLog->debug("=== $dataSet snapshotting: isDisabled=$isDisabled (" . ($isDisabled==1 ? "known-disabled" : ($isDisabled==-1 ? "known-enabled" : "uncertain")) . ")") if $self->debug;
if ($isDisabled == 1) {
push(@dataSetsExplicitlyDisabled, $dataSet);
}
}
}
}
return \@dataSetsExplicitlyDisabled;
};
my $refreshBackupPlans = sub {
my $self = shift;
my $recurse = shift;
my $inherit = shift;
my $dataSet = shift;
$self->zLog->info('refreshing backup plans' .
(defined($dataSet) ? ' for dataset "' . $dataSet . '"' : '') .
' ...');
$self->backupSets($self->zConfig->getBackupSetEnabled($recurse, $inherit, $dataSet));
($self->backupSets && @{$self->backupSets})
or die "No backup set defined or enabled, yet. run 'znapzendzetup' to setup znapzend\n";
for my $backupSet (@{$self->backupSets}){
$backupSet->{srcDisabledDescendants} = $self->$listDisabledSourceDescendants($backupSet);
if ($self->debug) {
for my $ds (@{$backupSet->{srcDisabledDescendants}}) {
$self->zLog->info('Found disabled sub-dataset: ' . $ds);
}
}
$backupSet->{srcPlanHash} = $self->zTime->backupPlanToHash($backupSet->{src_plan});
#check destination for remote pre-command
for (keys %$backupSet){
my ($key) = /^dst_([^_]+)_precmd$/ or next;
#perform pre-send-command if any
if ($backupSet->{"dst_$key" . '_precmd'} && $backupSet->{"dst_$key" . '_precmd'} ne 'off'){
if ($backupSet->{"dst_$key" . '_enabled'} && $backupSet->{"dst_$key" . '_enabled'} eq 'off'){
$self->zLog->info("Skipping pre-send-command for disabled destination " . $backupSet->{"dst_$key"});
} else {
# set env var for script to use
local $ENV{WORKER} = $backupSet->{"dst_$key"} . '-refresh';
$self->zLog->info("running pre-send-command for " . $backupSet->{"dst_$key"});
system($backupSet->{"dst_$key" . '_precmd'})
&& $self->zLog->warn("command \'" . $backupSet->{"dst_$key" . '_precmd'} . "\' failed");
# clean up env var
delete $ENV{WORKER};
}
}
}
}
for my $backupSet (@{$self->backupSets}){
$backupSet->{srcPlanHash} = $self->zTime->backupPlanToHash($backupSet->{src_plan});
#create backup hashes for all destinations
for (keys %$backupSet){
my ($key) = /^dst_([^_]+)_plan$/ or next;
my $autoCreation = $self->autoCreation;
if (!defined($autoCreation)) {
# Caller did not require any particular behavior, so
# check the ZFS property name (note lower-case "c"):
$autoCreation = (exists $backupSet->{"dst_$key" . '_autocreation'} ? $backupSet->{"dst_$key" . '_autocreation'} : undef);
}
if (!defined($autoCreation)) {
$autoCreation = 0;
}
#check if destination exists (i.e. is valid) otherwise recheck as dst might be online, now
if (!$backupSet->{"dst_$key" . '_valid'}){
$backupSet->{"dst_$key" . '_valid'} =
$self->zZfs->dataSetExists($backupSet->{"dst_$key"}) or do {
# Do not automatically create destination when using
# feature sendRaw, receiving a raw encrypted stream
# is not supported on unencrypted datasets
if ($autoCreation && !$self->sendRaw) {
my ($zpool) = $backupSet->{"dst_$key"} =~ /(^[^\/]+)\//;
# check if we can access destination zpool, if so create parent dataset
$self->zZfs->dataSetExists($zpool) && do {
$self->zLog->info("creating destination dataset '" . $backupSet->{"dst_$key"} . "'...");
$backupSet->{"dst_$key" . '_valid'} =
$self->zZfs->createDataSet($backupSet->{"dst_$key"});
if ($backupSet->{"dst_$key" . '_valid'}) {
$backupSet->{"dst_$key" . '_justCreated'} = 1;
}
};
}
$backupSet->{"dst_$key" . '_valid'} or
$self->zLog->warn("destination '" . $backupSet->{"dst_$key"}
. "' does not exist or is offline. will be rechecked every run..."
. ( $autoCreation ? "" : " Consider running znapzend --autoCreation" ) );
};
$self->zLog->debug('refreshBackupPlans(): detected dst_' . $key . '_valid status for ' . $backupSet->{"dst_$key"} . ': ' . $backupSet->{"dst_$key" . '_valid'}) if ($self->debug);
}
$backupSet->{"dst$key" . 'PlanHash'}
= $self->zTime->backupPlanToHash($backupSet->{"dst_$key" . '_plan'});
}
$backupSet->{interval} = $self->zTime->getInterval($backupSet->{srcPlanHash});
# NOTE that actual cleanup operations below exclude $self->since (as regex pattern) if defined.
$backupSet->{snapCleanFilter} = $self->zTime->getSnapshotFilter($backupSet->{tsformat});
# Due to support of possible intermediate snapshots named outside the
# generated configured pattern (tsformat), to send (and not destroy on
# destination) the arbitrary names, and find last common ones properly,
# we should match all snap names here and there.
### TODO: Revise the options commented away below, as they might only
### apply to different situations.
### $backupSet->{snapSendFilter} = $self->zTime->getSnapshotFilter($backupSet->{tsformat});
### if ($self->since) { $backupSet->{snapSendFilter} = "(".$backupSet->{snapSendFilter}."|".$self->since.")"; }
$backupSet->{snapSendFilter} = qr/.*/;
# $backupSet->{snapSendFilter} = $backupSet->{snapCleanFilter};
# if (defined($self->forcedSnapshotSuffix) && $self->forcedSnapshotSuffix ne '') {
# # TODO : Should this include ^ (or ^.*@) and $ boundaries?
# #$backupSet->{snapSendFilter} = '(' . $backupSet->{snapSendFilter} . '|' . $self->forcedSnapshotSuffix . ')';
# }
$backupSet->{UTC} = $self->zTime->useUTC($backupSet->{tsformat});
$self->zLog->info("found a valid backup plan for $backupSet->{src}...");
}
for my $backupSet (@{$self->backupSets}){
$backupSet->{srcPlanHash} = $self->zTime->backupPlanToHash($backupSet->{src_plan});
#check destination for remote post-command
for (keys %$backupSet){
my ($key) = /^dst_([^_]+)_pstcmd$/ or next;
#perform post-send-command if any
if ($backupSet->{"dst_$key" . '_pstcmd'} && $backupSet->{"dst_$key" . '_pstcmd'} ne 'off'){
if ($backupSet->{"dst_$key" . '_enabled'} && $backupSet->{"dst_$key" . '_enabled'} eq 'off'){
$self->zLog->info("Skipping post-send-command for disabled destination " . $backupSet->{"dst_$key"});
} else {
# set env var for script to use
local $ENV{WORKER} = $backupSet->{"dst_$key"} . '-refresh';
$self->zLog->info("running post-send-command for " . $backupSet->{"dst_$key"});
system($backupSet->{"dst_$key" . '_pstcmd'})
&& $self->zLog->warn("command \'" . $backupSet->{"dst_$key" . '_pstcmd'} . "\' failed");
# clean up env var
delete $ENV{WORKER};
}
}
}
}
};
my $sendRecvCleanup = sub {
my $self = shift;
my $backupSet = shift;
my $timeStamp = shift;
#no HUP handler in child
$SIG{HUP} = 'IGNORE';
if ($self->nodelay && $backupSet->{zend_delay}) {
$self->zLog->warn("CLI option --nodelay was requested, so ignoring backup plan option 'zend-delay' (was $backupSet->{zend_delay}) on backupSet $backupSet->{src}");
$backupSet->{zend_delay} = undef;
}
if (defined($backupSet->{zend_delay})) {
chomp $backupSet->{zend_delay};
if (!($backupSet->{zend_delay} =~ /^\d+$/)) {
$self->zLog->warn("Backup plan option 'zend-delay' has an invalid value ('$backupSet->{zend_delay}' is not a number) on backupSet $backupSet->{src}, ignored");
$backupSet->{zend_delay} = undef;
} else {
if($backupSet->{zend_delay} > 0) {
$self->zLog->info("waiting $backupSet->{zend_delay} seconds before sending snaps on backupSet $backupSet->{src}...");
sleep $backupSet->{zend_delay};
$self->zLog->info("resume sending action on backupSet $backupSet->{src}");
}
}
}
my @snapshots;
my $toDestroy;
my @sendFailed; # List of messages about failed sends
my $startTime = time;
$self->zLog->info('starting work on backupSet ' . $backupSet->{src});
#get all sub datasets of source filesystem; need to send them all individually if recursive
my $srcSubDataSets = $backupSet->{recursive} eq 'on'
? $self->zZfs->listSubDataSets($backupSet->{src}) : [ $backupSet->{src} ];
my @dataSetsExplicitlyDisabled = ();
if (defined($backupSet->{srcDisabledDescendants})) {
@dataSetsExplicitlyDisabled = @{$backupSet->{srcDisabledDescendants}};
}
#loop through all destinations
for my $dst (sort grep { /^dst_[^_]+$/ } keys %$backupSet){
my ($key) = $dst =~ /dst_([^_]+)$/;
my $thisSendFailed = 0; # Track if we don't want THIS destination cleaned up
#allow users to disable some destinations (e.g. reserved/templated
#backup plan config items, or known broken targets) without deleting
#them outright. Note it is likely that the common (automatic) snapshots
#between source and that destination would disappear over time, making
#incremental sync impossible at some point in the future.
if ($backupSet->{"dst_$key" . '_enabled'} && $backupSet->{"dst_$key" . '_enabled'} eq 'off'){
$self->zLog->info("Skipping disabled destination " . $backupSet->{"dst_$key"}
. ". Note that you would likely need to recreate the backup data tree there");
next;
}
#check destination for pre-send-command
if ($backupSet->{"dst_$key" . '_precmd'} && $backupSet->{"dst_$key" . '_precmd'} ne 'off'){
local $ENV{WORKER} = $backupSet->{"dst_$key"};
$self->zLog->info("running pre-send-command for " . $backupSet->{"dst_$key"});
my $ev = system($backupSet->{"dst_$key" . '_precmd'});
delete $ENV{WORKER};
if ($ev){
$self->zLog->warn("pre-send-command \'" . $backupSet->{"dst_$key" . '_precmd'} . "\' failed");
if ($self->skipOnPreSendCmdFail) {
my $errmsg = "skipping " . $backupSet->{"dst_$key"} . " due to pre-send-command failure";
$self->zLog->warn($errmsg);
push (@sendFailed, $errmsg);
$thisSendFailed = 1;
next;
}
}
}
#recheck non valid dst as it might be online, now
if (!$backupSet->{"dst_$key" . '_valid'}) {
my $autoCreation = $self->autoCreation;
if (!defined($autoCreation)) {
# Caller did not require any particular behavior, so
# check the ZFS property name (note lower-case "c").
# Note we are looking at "root" datasets with a backup
# schedule here (enumerated earlier); children if any
# would be checked below:
$autoCreation = (exists $backupSet->{"dst_$key" . '_autocreation'} ? $backupSet->{"dst_$key" . '_autocreation'} : undef);
}
if (!defined($autoCreation)) {
$autoCreation = 0;
}
$backupSet->{"dst_$key" . '_valid'} =
$self->zZfs->dataSetExists($backupSet->{"dst_$key"}) or do {
# Do not automatically create destination when using
# feature sendRaw, receiving a raw encrypted stream
# is not supported on unencrypted datasets
if ($autoCreation && !$self->sendRaw) {
my ($zpool) = $backupSet->{"dst_$key"} =~ /(^[^\/]+)\//;
# check if we can access destination zpool, if so -
# create parent dataset (e.g. this backupSet root;
# note that if we recurse into children that may be
# absent, they are treated separately below)
$self->zZfs->dataSetExists($zpool) && do {
$self->zLog->info("creating destination dataset '" . $backupSet->{"dst_$key"} . "'...");
$backupSet->{"dst_$key" . '_valid'} =
$self->zZfs->createDataSet($backupSet->{"dst_$key"});
if ($backupSet->{"dst_$key" . '_valid'}) {
$backupSet->{"dst_$key" . '_justCreated'} = 1;
}
};
}
# TOTHINK: Is the sendRaw comparison correct here for the intent
# and purpose of PR https://github.com/oetiker/znapzend/pull/496 ?
( $backupSet->{"dst_$key" . '_valid'} || ($self->sendRaw && $autoCreation) ) or do {
my $errmsg = "destination '" . $backupSet->{"dst_$key"}
. "' does not exist or is offline; ignoring it for this round...";
# Avoid spamming for every loop cycle, if we do not have
# the dataset and know we do not intend to auto-create it
$self->zLog->warn($errmsg) if ($autoCreation or $self->debug);
if (!$autoCreation) {
$self->zLog->warn("Autocreation is disabled for this dataset or whole run, so skipping without error") if ($self->debug);
} else {
push (@sendFailed, $errmsg);
$thisSendFailed = 1;
}
next;
};
};
$self->zLog->debug('sendRecvCleanup(): detected dst_' . $key . '_valid status for ' . $backupSet->{"dst_$key"} . ': ' . $backupSet->{"dst_$key" . '_valid'}) if ($self->debug);
}
#sending loop through all subdatasets
#note: given that transfers can take long even locally, we do not
#really want recursive sending (so retries can go dataset by dataset)
#also, we can disable individual children of recursive ZFS datasets
#from being snapshot/sent by setting property "org.znapzend:enabled"
#to "off" on them
for my $srcDataSet (@$srcSubDataSets){
my $dstDataSet = $srcDataSet;
$dstDataSet =~ s/^\Q$backupSet->{src}\E/$backupSet->{$dst}/;
my $autoCreation = $self->autoCreation;
if (!defined($autoCreation)) {
# Caller did not require any particular behavior, so
# check the ZFS property name (note lower-case "c").
# Look at properties of this dataset, allow inherited
# values. TOTHINK: Get properties once for all tree?
my $properties = $self->zZfs->getDataSetProperties($srcDataSet, 0, 1);
if ($properties->[0]) {
for my $prop (keys %{$properties->[0]}) {
if ($prop eq "dst_$key" . '_autocreation') {
$autoCreation = (%{$properties->[0]}{$prop} eq "on" ? 1 : 0);
last;
}
}
}
}
if (!defined($autoCreation)) {
$autoCreation = 0;
}
my $srcDataSetDisabled = (grep (/^\Q$srcDataSet\E$/, @dataSetsExplicitlyDisabled));
$self->zLog->debug('sending snapshots from ' . $srcDataSet . ' to ' . $dstDataSet .
($srcDataSetDisabled ? ": not enabled, skipped" : ""));
if ($srcDataSetDisabled) {
next;
}
# Time to check if the target sub-dataset exists
# at all (unless we would auto-create one anyway).
# Do not automatically create destination when using
# feature sendRaw, receiving a raw encrypted stream
# is not supported on unencrypted datasets.
if ((!$autoCreation || $self->sendRaw) && !($self->zZfs->dataSetExists($dstDataSet))) {
my $errmsg = "sub-destination '" . $dstDataSet
. "' does not exist or is offline; ignoring it for this round... Consider "
. ( $autoCreation || $self->sendRaw ? "" : "running znapzend --autoCreation or " )
. "disabling this dataset from znapzend handling.";
# Avoid spamming for every loop cycle, if we do not have
# the dataset and know we do not intend to auto-create it
$self->zLog->warn($errmsg) if ($autoCreation or $self->debug);
if (!$autoCreation) {
$self->zLog->warn("Autocreation is disabled for this dataset or whole run, so skipping without error") if ($self->debug);
} else {
push (@sendFailed, $errmsg);
$thisSendFailed = 1;
}
next;
}
{ # scoping
local $@;
eval {
local $SIG{__DIE__};
$self->zLog->debug('Are we sending "--since"? '.
'since=="' . $self->since . '"'.
', skipIntermediates=="' . $self->skipIntermediates . '"' .
', forbidDestRollback=="' . $self->forbidDestRollback . '"' .
', autoCreation=="' . ( $autoCreation ? "true" : "false" ) . '"' .
', sendRaw=="' . $self->sendRaw . '"' .
', valid=="' . ( $backupSet->{"dst_$key" . '_valid'} ? "true" : "false" ) . '"' .
', justCreated=="' . ( $backupSet->{"dst_$key" . '_justCreated'} ? "true" : "false" ) . '"'
) if $self->debug;
if ($self->since) {
# Make sure that if we use the "--sinceForced=X" or
# "--since=X" option, this named snapshot exists (or
# appears) in the destination dataset history of
# snapshots.
# Note if "X" does not yet exist on destination AND
# if there are newer than "X" snapshots on destination,
# they would be removed to allow receiving "X" with
# the "--sinceForced=X" mode, but not with "--since=X"
# which would only add it if it is an intermediate snap.
# FIXME: Implementation below seems wasteful for I/Os
# doing many "zfs list" calls (at least metadata is
# cached by the OSes involved). Luckily "--since=X"
# is not default and for PoC this should be readable.
# Largely copied from lastAndCommonSnapshots() and
# listSnapshots() routines.
# In some cases we do not have to actively replicate "X":
# 1) "X" does not exist in history of src, no-op
if ($self->zZfs->snapshotExists($srcDataSet . "@" . $self->since)) {
# 2) "X" exists in history of both src and dst, no-op
if (!$self->zZfs->snapshotExists($dstDataSet . "@" . $self->since)) {
# ...So "X" exists in src and not in dst; where is it
# in our history relative to latest common snapshot?
# Is there one at all?
### Inspect ALL snapshots in this case, not just auto's
### my $snapSendFilter = $backupSet->{snapSendFilter};
my $snapSendFilter = qr/.*/;
my $srcSnapshots = $self->zZfs->listSnapshots($srcDataSet, $snapSendFilter);
if (scalar @$srcSnapshots) {
my $dstSnapshots = $self->zZfs->listSnapshots($dstDataSet, $snapSendFilter);
my $i;
my $snapName;
my $lastCommon; # track the newest common snapshot, if any
my $lastCommonNum; # Flips to "i" if we had any common snapshots
my $firstCommon; # track the oldest known common snapshot (if we can roll back to use it)
my $firstCommonNum ; # Flips to "i" if we had any common snapshots
my $seenX; # Flips to "i" if we saw "X" before (or as) the newest common snapshot, looking from newest snapshots in src
my $seenD; # Flips to "i" if we saw "X" in DST during this search
# Note that depending on conditions, we might not look through ALL snapnames and stop earlier when we saw enough
for ($i = $#{$srcSnapshots}; $i >= 0; $i--){
($snapName) = ${$srcSnapshots}[$i] =~ /^\Q$srcDataSet\E\@($snapSendFilter)/;
#print STDERR "=== LOOKING: #$i : FQSN: ${$srcSnapshots}[$i] => '$snapName' cf. '" . $self->since ."'\n" if $self->debug;
if ( $snapName eq $self->since || $snapName =~ m/^$self->since$/ ) {
$seenX = $i;
print STDERR "+++ SEENSRC: #$i : FQSN: ${$srcSnapshots}[$i] => '$snapName' cf. '" . $self->since ."'\n" if $self->debug;
}
if ( grep { /\@$snapName$/ } @$dstSnapshots ) {
if ( $snapName eq $self->since || $snapName =~ m/^$self->since$/ ) {
$seenD = $i;
print STDERR "+++ SEENDST: #$i : FQSN: ${$srcSnapshots}[$i] => '$snapName' cf. '" . $self->since ."'\n" if $self->debug;
}
$firstCommonNum = $i;
$firstCommon = ${$srcSnapshots}[$i];
print STDERR "||| COMMON : #$i : FQSN: ${$srcSnapshots}[$i] => '$snapName' cf. '" . $self->since ."'\n" if $self->debug;
if (!defined($lastCommon)) {
# This may be our last iteration here, or not...
$lastCommonNum = $i;
$lastCommon = ${$srcSnapshots}[$i];
# Handle the situation when we have a --sinceForced=X
# request, so the "X" has to be (or appear) in history
# of destination dataset. Even if we found the newest
# common snapshot, it may be newer than "X" and there
# might be no common snapshots older than "X" - then
# we would have to roll back to replicate from scratch.
last if defined($seenX); # lastCommon is same or older than X
last if ($self->forbidDestRollback && !($backupSet->{"dst_$key" . '_justCreated'})); # not asked to/can't roll back anyway... well we're unlikely to see snaps in a justCreated destination as well
$self->zLog->debug("sendRecvCleanup() [--since mode]: found a newest common snapshot between $srcDataSet and $dstDataSet is '$lastCommon' that matches --since='" . $self->since . "', but have not seen a --sinceForced='" . $self->since . "' match so keep looking where we can roll back to");
}
last if defined($seenX); # firstCommon is same or older than X
}
}
if ($i < 0) {
$self->zLog->debug("sendRecvCleanup() [--since mode]: looked through all snapshots of $srcDataSet;"
. " seenX=" . (defined($seenX) ? $seenX : "undef")
. " seenD=" . (defined($seenD) ? $seenD : "undef")
. " lastCommonNum=" . (defined($lastCommonNum) ? $lastCommonNum : "undef")
. " lastCommon=" . (defined($lastCommon) ? $lastCommon : "undef")
. " firstCommonNum=" . (defined($firstCommonNum) ? $firstCommonNum : "undef")
. " firstCommon=" . (defined($firstCommon) ? $firstCommon : "undef")
) if $self->debug;
}
# Flag for whether we should run an extra sendRecvSnapshots()
# to get the "X" snapshot into history of destination.
# A value of 2 enforces this, allowing to destroy snapshots
# on destination to allow repopulation of that dataset with
# new history if user said --sinceForced=X
my $doPromote = 0;
if (defined($seenD)) {
# if seenD - skip the below decisions; note it also means seenX and that "X" is among common snapshots
$self->zLog->debug("sendRecvCleanup() [--since mode]: A common snapshot between $srcDataSet and $dstDataSet that already matches --since='" . $self->since . "' is '${$srcSnapshots}[$seenX]'");
} else {
if (defined($lastCommon)) { # also means defined firstCommon
# 3) "X" in src is newer than the newest common snapshot
# or "X" exists in src and there is NO common snapshot
# => promote it into dst explicitly if we skipIntermediates
# and proceed to resync starting from "X" afterwards
# (if we do not skipIntermediates then no-op -
# it will be included in replication below from
# that older common point anyway)
if (defined($seenX)) {
# Note: the value of a defined seenX is a number of
# that snapshot in our list, so may validly be zero
if ($self->skipIntermediates) {
if (defined($lastCommon) && $lastCommon ne '') {
if ($lastCommonNum == $seenX || $lastCommon =~ m/\@$self->since$/ ) {
$self->zLog->debug("sendRecvCleanup() [--since mode]: Newest common snapshot between $srcDataSet and $dstDataSet is '$lastCommon' and already matches --since='" . $self->since . "'");
} else {
if ($lastCommonNum < $seenX) {
$self->zLog->debug("sendRecvCleanup() [--since mode]: Newest common snapshot between $srcDataSet and $dstDataSet is '$lastCommon' and older than a --since='" . $self->since . "' match (${$srcSnapshots}[$seenX])");
$doPromote = 1;
} else {
if (!$self->forbidDestRollback) { # || ($backupSet->{"dst_$key" . '_justCreated'})) {
$self->zLog->debug("sendRecvCleanup() [--since mode]: Newest common snapshot between $srcDataSet and $dstDataSet is '$lastCommon' and newer than a --sinceForced='" . $self->since . "' match (${$srcSnapshots}[$seenX]), should try to roll back and resync from that");
$doPromote = 2;
} else {
$self->zLog->debug("sendRecvCleanup() [--since mode]: Newest common snapshot between $srcDataSet and $dstDataSet is '$lastCommon' and newer than a --since='" . $self->since . "' match (${$srcSnapshots}[$seenX]), but rollback of dest is forbidden");
}
}
}
} else {
if ( (scalar @$dstSnapshots) > 0) {
if ($self->forbidDestRollback && !($backupSet->{"dst_$key" . '_justCreated'})) {
$self->zLog->debug("sendRecvCleanup() [--since mode]: There is no common snapshot between $srcDataSet and $dstDataSet to compare with a --since='" . $self->since . "' match, but rollback of dest is forbidden");
} else {
$self->zLog->debug("sendRecvCleanup() [--since mode]: There is no common snapshot between $srcDataSet and $dstDataSet to compare with a --since='" . $self->since . "' match, should try resync from scratch");
$doPromote = 2;
}
} else {
$self->zLog->debug("sendRecvCleanup() [--since mode]: There is no common snapshot between $srcDataSet and $dstDataSet to compare with a --since='" . $self->since . "' match, because there are no snapshots in dst, should try resync from scratch");
$doPromote = 2;
}
}
} else {
# if (seenX && !skipIntermediates) :
$self->zLog->debug("sendRecvCleanup() [--since mode]: Newest common snapshot between $srcDataSet and $dstDataSet is '$lastCommon' and older than a --since='" . $self->since . "' match (${$srcSnapshots}[$seenX]), but we would send a complete replication stream with all intermediates below anyway");
}
} # // 3. if seenX
# 4) "X" in src is older than the newest common snapshot
# => promote it into dst explicitly ONLY IF WE DO NOT
# forbidDestRollback (deleting whatever is newer
# on dst) and proceed to resync starting from "X"
# afterwards (if we forbidDestRollback honor that)
if (!defined($seenX)) {
# Did not see "X" as we went through history of SRC
# Either it is not there (should not happen here per
# checks done above), or is older than the newest
# common snapshot and we did not intend to roll back
if (!$self->forbidDestRollback || ($backupSet->{"dst_$key" . '_justCreated'})) {
# We should have looked through whole SRC
# history to get here. And not seenX. Fishy!
if ($firstCommonNum == $lastCommonNum) {
$self->zLog->debug("sendRecvCleanup() [--since mode]: The newest (and oldest) common snapshot between $srcDataSet and $dstDataSet is '$lastCommon' and there is no --sinceForced='" . $self->since . "' match in destination, would try to resync from previous common point or from scratch");
$doPromote = 2;
} else {
$self->zLog->debug("sendRecvCleanup() [--since mode]: The newest common snapshot between $srcDataSet and $dstDataSet is '$lastCommon', and the oldest is '$firstCommon', and there is no --sinceForced='" . $self->since . "' match in destination, would try to resync from previous common point or from scratch");
$doPromote = 2;
}
} else {
$self->zLog->debug("sendRecvCleanup() [--since mode]: Newest common snapshot between $srcDataSet and $dstDataSet is '$lastCommon' and newer than a --since='" . $self->since . "' match (if any), but we forbidDestRollback so will not ensure it appears on destination");
}
} # // 4. if !seenX => "X" is too old or absent
} else { ### => if (!defined($lastCommon)) ...
# 5) There may be no common snapshot at all.
# There may be no snapshots on destination at all.
# Destination may be "justCreated" and/or a value
# for forbidDestRollback==false can permit us to
# rewrite any contents of that destination (live
# data or existing "unneeded" snapshots).
if (scalar(@$dstSnapshots) > 0) {
if (!$self->forbidDestRollback) { # || ($backupSet->{"dst_$key" . '_justCreated'})) {
# 5b) There are discardable snapshots on destination...
$self->zLog->debug("sendRecvCleanup() [--since mode]: There is no common snapshot between $srcDataSet and $dstDataSet, and we may roll it back");
$doPromote = 2;
} else {
$self->zLog->debug("sendRecvCleanup() [--since mode]: There is no common snapshot between $srcDataSet and $dstDataSet, but we may not roll it back");
}
} else {
# 5a) There are no snapshots on destination...
# TODO: Find a way to state that destination is empty
# de-facto (e.g. created last run) and can be rolled
# back without loss of data because there are none...
if (!$self->forbidDestRollback || ($backupSet->{"dst_$key" . '_justCreated'})) {
$self->zLog->debug("sendRecvCleanup() [--since mode]: There are no snapshots on destination $dstDataSet, and we may roll it back");
$doPromote = 1;
} else {
$self->zLog->debug("sendRecvCleanup() [--since mode]: There are no snapshots on destination $dstDataSet, but we may not roll it back");
}
} # // 5a. No dest snaps at all
} # // 5. No common snapshots at all
} # // seenD => "X" is a common snapshot already
if ($doPromote > 0) {
$self->zLog->debug("sendRecvCleanup() [--since mode]: Making sure that snapshot '" . $self->since . "' exists in history of '$dstDataSet' ...");
my $lastSnapshotToSee = $self->since;
if (defined($seenX)) {
$lastSnapshotToSee = ${$srcSnapshots}[$seenX];
}
$self->zZfs->sendRecvSnapshots($srcDataSet, $dstDataSet, $dst,
$backupSet->{src_mbuffer}, $backupSet->{src_mbuffer_size},
$backupSet->{"dst_$key" . '_mbuffer'}, $backupSet->{"dst_$key" . '_mbuffer_size'},
$backupSet->{snapSendFilter}, $lastSnapshotToSee,
( $backupSet->{"dst_$key" . '_justCreated'} ? 1 : ($doPromote > 1 ? $doPromote : undef ) )
);
} else {
$self->zLog->debug("sendRecvCleanup() [--since mode]: We considered --since='" . $self->since . "' and did not find reasons to use sendRecvSnapshots() explicitly to make it appear in $dstDataSet");
}
} else {
$self->zLog->debug("sendRecvCleanup() [--since mode]: Got an empty list, does source dataset $srcDataSet have any snapshots?");
} # if not scalar - no src snaps?
} else {
$self->zLog->debug("sendRecvCleanup() [--since mode]: Destination dataset $dstDataSet already has a snapshot named by --since='" . $self->since . "'");
} # // 2. if dst has "X"
} else {
$self->zLog->debug("sendRecvCleanup() [--since mode]: Source dataset $srcDataSet does not have a snapshot named by --since='" . $self->since . "'");
if (!$self->forbidDestRollback) { # || ($backupSet->{"dst_$key" . '_justCreated'})) {
die "User required --sinceForced='" . $self->since . "' but there is no match in source dataset $srcDataSet";
}
} # // 1. if src does not have "X"
} # if have to care about "--since=X"
# Synchronize snapshot history from source to destination
# starting from newest snapshot that they have in common
# (or create/rewrite destination if it has no snapshots).
# With "--since=X" option handled above, such newest common
# snapshot can likely be this "X".
# Note this can fail if we forbidDestRollback and there are
# snapshots or data on dst newer than the last common snap.
$self->zZfs->sendRecvSnapshots($srcDataSet, $dstDataSet, $dst,
$backupSet->{src_mbuffer}, $backupSet->{src_mbuffer_size},
$backupSet->{"dst_$key" . '_mbuffer'}, $backupSet->{"dst_$key" . '_mbuffer_size'},
$backupSet->{snapSendFilter}, undef,
( $backupSet->{"dst_$key" . '_justCreated'} ? 1 : undef )
);
};
if (my $err = $@){
$thisSendFailed = 1;
if (blessed $err && $err->isa('Mojo::Exception')){
$self->zLog->warn($err->message);
push (@sendFailed, $err->message);
}
else{
$self->zLog->warn($err);
push (@sendFailed, $err);
}
}
}
}
# do not destroy data sets on the destination, or run post-send-command, unless all operations have been successful
next if ($thisSendFailed);
# Remember which snapnames we already decided about in first phase
# (recursive cleanup from top backupSet-dst) if we did run it indeed.
# Do so with hash array for faster lookups into snapname existence.
my %snapnamesRecursive = ();
#cleanup current destination
if ($backupSet->{recursive} eq 'on') {
# First we try to recursively (and atomically quickly)
# remove snapshots of "root" dataset with the recursive
# configuration; then go looking for leftovers if any.
# On many distros, ZFS has some atomicity lock for pool
# operations - so one destroy operation takes ages...
# but hundreds of queued operations take the same time
# and are all committed at once.
@snapshots = @{$self->zZfs->listSnapshots($backupSet->{$dst}, $backupSet->{snapCleanFilter})};
$toDestroy = $self->zTime->getSnapshotsToDestroy(\@snapshots,
$backupSet->{"dst$key" . 'PlanHash'}, $backupSet->{tsformat}, $timeStamp, $self->since);
# Save the names we have seen, to not revisit them below for children
for (@{$self->zZfs->extractSnapshotNames(\@snapshots)}) {
$snapnamesRecursive{$_} = 1;
}
# Note to devs: move extractSnapshotNames(toDestroy) up here
# if you would introduce logic that cleans that array.
if (scalar($toDestroy) == 0) {
$self->zLog->debug('got an empty toDestroy list for cleaning up destination snapshots recursively under ' . $backupSet->{dst});
} else {
# Note to devs: Unlike code for sources, here we only extract
# snapnames when we know the @toDestroy array is not empty -
# and it was not cleaned by any (missing) logic above.
for (@{$self->zZfs->extractSnapshotNames(\@{$toDestroy})}) {