-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathduplicity-backup.pl
executable file
·1938 lines (1615 loc) · 53.3 KB
/
duplicity-backup.pl
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
#!/usr/bin/env perl
#
#
# backup tool, wrapper around duplicity
#
# written by Andreas 'ads' Scherbaum <andreas@scherbaum.biz>
#
# "duplicity-backup.pl" is released under the PostgreSQL License,
# a liberal Open Source license, similar to the BSD or MIT
# licenses.
#
#
# Copyright (c) 2012, Andreas Scherbaum
#
# Permission to use, copy, modify, and distribute this software
# and its documentation for any purpose, without fee, and without
# a written agreement is hereby granted, provided that the above
# copyright notice and this paragraph and the following two paragraphs
# appear in all copies.
#
# IN NO EVENT SHALL Andreas Scherbaum BE LIABLE TO ANY PARTY FOR
# DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
# INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE
# AND ITS DOCUMENTATION, EVEN IF Andreas Scherbaum HAS BEEN ADVISED
# OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Andreas Scherbaum SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
# BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED
# HEREUNDER IS ON AN "AS IS" BASIS, AND Andreas Scherbaum HAS
# NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
# ENHANCEMENTS, OR MODIFICATIONS.
package backup::config;
#
# config class for backup
#
# config file schema:
#
# key = value
# key= value
# key =value
# key=value
# key=
# key = value
# ...
# usage:
#
# $config = backup::config->new(<configfile>);
# $var = $config->config_get_key(<key name>);
# $config->config_set_key(<key name>, <new value>);
# $config->config_delete_key(<key name>);
# %var = $config->config_get_keys();
use strict;
use POSIX; # some standards
use FileHandle; # have variables for the filehandles
use Data::Dumper;
use YAML::XS qw (/./);
# new()
#
# constructor
#
# parameter:
# - class name
# return:
# - pointer to config class
sub new {
my $class = shift;
my $self = {};
# bless mysqlf
bless($self, $class);
# define own variables
# config file name
$self->{config_file} = undef;
$self->{config} = undef;
# return reference
return $self;
}
# read_config()
#
# read in a config file
#
# parameter:
# - self
# - config filename
# return:
# none
sub read_config {
my $self = shift;
my $config_file = shift;
# test if config file exists
if (!-f $config_file) {
die "could not find config file: $config_file\n";
}
my $config = LoadFile($config_file);
# save config file name for later use
$self->{config_file} = $config_file;
# set config to 'not changed'
$self->{changed} = 0;
# store config
$self->{config} = $config;
return;
}
# DESTROY()
#
# destructor
#
# parameter:
# - self
# return:
# none
sub DESTROY {
my $self = shift;
}
# config_get_key1()
#
# return a config value
#
# parameter:
# - self
# - config key name 1
# return:
# - value of config parameter (or undef)
sub config_get_key1 {
my $self = shift;
my $key1 = shift;
if (!defined($self->{config}->{$key1})) {
return undef;
}
# return value
return $self->{config}->{$key1};
}
# config_set_key1()
#
# set a config value
#
# parameter:
# - self
# - config key name 1
# - new value
# return:
# none
sub config_set_key1 {
my $self = shift;
my $key1 = shift;
my $value = shift;
$self->{config}->{$key1} = $value;
return;
}
# config_get_key2()
#
# return a config value
#
# parameter:
# - self
# - config key name 1
# - config key name 2
# return:
# - value of config parameter (or undef)
sub config_get_key2 {
my $self = shift;
my $key1 = shift;
my $key2 = shift;
if (!defined($self->{config}->{$key1})) {
return undef;
}
if (!defined($self->{config}->{$key1}->{$key2})) {
return undef;
}
# return value
return $self->{config}->{$key1}->{$key2};
}
# config_set_key2()
#
# set a config value
#
# parameter:
# - self
# - config key name 1
# - config key name 2
# - value
# return:
# none
sub config_set_key2 {
my $self = shift;
my $key1 = shift;
my $key2 = shift;
my $value = shift;
$self->{config}->{$key1}->{$key2} = $value;
return;
}
# config_get_key3()
#
# return a config value
#
# parameter:
# - self
# - config key name 1
# - config key name 2
# - config key name 3
# return:
# - value of config parameter (or undef)
sub config_get_key3 {
my $self = shift;
my $key1 = shift;
my $key2 = shift;
my $key3 = shift;
if (!defined($self->{config}->{$key1})) {
return undef;
}
if (!defined($self->{config}->{$key1}->{$key2})) {
return undef;
}
if (!defined($self->{config}->{$key1}->{$key2}->{$key3})) {
return undef;
}
# return value
return $self->{config}->{$key1}->{$key2}->{$key3};
}
# config_set_key3()
#
# set a config value
#
# parameter:
# - self
# - config key name 1
# - config key name 2
# - config key name 3
# - value
# return:
# none
sub config_set_key3 {
my $self = shift;
my $key1 = shift;
my $key2 = shift;
my $key3 = shift;
my $value = shift;
$self->{config}->{$key1}->{$key2}->{$key3} = $value;
return;
}
# config_delete_key()
#
# delete a config key
#
# parameter:
# - self
# - config key name 1
# return:
# none
sub config_delete_key1 {
my $self = shift;
my $key1 = shift;
# delete key
delete($self->{config}->{$key1});
# mark config changed
$self->{changed} = 1;
}
# config_delete_key2()
#
# delete a config key
#
# parameter:
# - self
# - config key name 1
# - config key name 2
# return:
# none
sub config_delete_key2 {
my $self = shift;
my $key1 = shift;
my $key2 = shift;
# delete key
delete($self->{config}->{$key1}->{$key2});
# mark config changed
$self->{changed} = 1;
}
# config_delete_key3()
#
# delete a config key
#
# parameter:
# - self
# - config key name 1
# - config key name 2
# - config key name 3
# return:
# none
sub config_delete_key3 {
my $self = shift;
my $key1 = shift;
my $key2 = shift;
my $key3 = shift;
# delete key
delete($self->{config}->{$key1}->{$key2}->{$key3});
# mark config changed
$self->{changed} = 1;
}
# config_get_keys1()
#
# return hash with all defined config keys
#
# parameter:
# - self
# - config key 1
# return:
# - hash with config keys
sub config_get_keys1 {
my $self = shift;
my $key1 = shift;
if (!defined($self->{config}->{$key1})) {
return undef;
}
if (ref($self->{config}->{$key1}) eq '') {
# return value
return $self->{config}->{$key1};
}
if (ref($self->{config}->{$key1}) eq 'HASH') {
# return sorted hash
my $config = $self->{config}->{$key1};
my %config = %$config;
# returns an array with the values
return sort(keys(%config));
}
if (ref($self->{config}->{$key1}) eq 'ARRAY') {
die("FIXME: not yet implemented - required?\n");
}
return undef;
}
# config_get_keys2()
#
# return hash with all defined config keys
#
# parameter:
# - self
# - config key 1
# - config key 2
# return:
# - hash with config keys
sub config_get_keys2 {
my $self = shift;
my $key1 = shift;
my $key2 = shift;
if (!defined($self->{config}->{$key1})) {
return undef;
}
if (!defined($self->{config}->{$key1}->{$key2})) {
return undef;
}
if (ref($self->{config}->{$key1}->{$key2}) eq '') {
# return value
return $self->{config}->{$key1}->{$key2};
}
if (ref($self->{config}->{$key1}->{$key2}) eq 'HASH') {
# return sorted hash
my $config = $self->{config}->{$key1}->{$key2};
my %config = %$config;
# returns an array with the values
return sort(keys(%config));
}
if (ref($self->{config}->{$key1}->{$key2}) eq 'ARRAY') {
die("FIXME: not yet implemented - required?\n");
}
return undef;
}
# config_file()
#
# return the name of the config file
#
# parameter:
# none
# return:
# - config file name
sub config_file {
my $self = shift;
return $self->{config_file};
}
# end module
1;
######################################################################
# #
# Main #
# #
######################################################################
package main;
use strict;
use warnings;
use Getopt::Long qw( :config no_ignore_case );
use FileHandle;
use Data::Dumper;
use YAML::XS qw (/./);
use Date::Manip;
use Date::Parse;
use Time::Duration;
use Time::Duration::Parse;
use Time::HiRes qw( gettimeofday tv_interval );
use Net::FTP;
import backup::config;
######################################################################
# initialize global variables
######################################################################
use constant DEBUG2 => 5;
use constant DEBUG => 4;
use constant INFO => 3;
use constant WARN => 2;
use constant ERROR => 1;
%main::loglevels = (
5 => 'DEBUG2',
4 => 'DEBUG',
3 => 'INFO',
2 => 'WARN',
1 => 'ERROR',
);
$main::loglevel = INFO;
######################################################################
# handle command line arguments
######################################################################
# defaults
$main::help = 0;
$main::debug = 0;
$main::quiet = 0;
$main::config_file = "";
$main::status = 0;
$main::cleanup_old_logs = 0;
$main::monitoring = 0;
# parse options
unless (
GetOptions(
'help|h|?' => \$main::help,
'debug|d' => \$main::debug,
'quiet|q' => \$main::quiet,
'config|c=s' => \$main::config_file,
'logfile|l=s' => \$main::logfile,
'status|s' => \$main::status,
'cleanup-old-logs' => \$main::cleanup_old_logs,
'monitoring|m' => \$main::monitoring,
)
) {
# There were errors with parsing command line options - show help.
$main::help = 1;
}
if ($main::debug == 1) {
$main::loglevel = DEBUG;
}
if ($main::quiet == 1) {
$main::loglevel = ERROR;
}
######################################################################
# Main
######################################################################
if ($main::help == 1) {
print <<_END_OF_HELP_;
$0 [options]
The following options are available:
-h --help This help
-d --debug Enable debug messages
-c --config Config file (required)
-l --logfile Logfile
-s --status Add backup status to output
-m --monitoring Output monitoring status
(make sure to add the -q option)
_END_OF_HELP_
exit(0);
}
init_config();
if (length($main::config_file) > 0) {
read_config($main::config_file);
} else {
print_msg("No configfile!", ERROR);
exit(1);
}
# pre-flight checks
my $check_duplicity = config_get_keys2('config', 'duplicity');
if (!defined($check_duplicity)) {
print_msg("duplicity path must be set in config", ERROR);
exit(1);
}
if (!-f $check_duplicity or !-e $check_duplicity) {
print_msg("duplicity binary not available", ERROR);
exit(1);
}
my $check_volsize = config_get_keys2('config', 'volsize');
if (!defined($check_volsize) or length($check_volsize) == 0) {
print_msg("global volsize must be set", ERROR);
exit(1);
}
my $check_log_directory = config_get_keys2('config', 'log-directory');
if (!defined($check_log_directory) or length($check_log_directory) == 0) {
print_msg("global log-directory must be set", ERROR);
exit(1);
}
if (!-d $check_log_directory) {
print_msg("global log-directory must exist", ERROR);
exit(1);
}
if ($check_log_directory !~ /\/$/) {
# complete path of log directory, make it easier for repetitive calls
config_set_key2('config', 'log-directory', $check_log_directory . '/');
}
my $check_archive_directory = config_get_keys2('config', 'archive-directory');
if (!defined($check_archive_directory) or length($check_archive_directory) == 0) {
# set to an empty value, will not be used later
config_set_key2('config', 'archive-directory', '');
print_msg("no archive-dir is used", DEBUG);
} else {
if (!-d $check_archive_directory) {
print_msg("global archive-directory must exist", ERROR);
exit(1);
}
print_msg("archive-dir is: $check_archive_directory", DEBUG);
}
print_msg("Initialization done, start backup", INFO);
if ($main::monitoring == 1) {
main_monitoring();
exit(0);
} else {
main_backup();
exit(0);
}
######################################################################
# regular functions used by the backup
######################################################################
# main_backup()
#
# main routine doing the backup
#
# parameter:
# none
# return:
# none
sub main_backup {
my @backups = config_get_keys1('backup');
# walk through all backups
foreach my $backup (@backups) {
print_msg("Backup task: " . $backup, INFO);
my $status = run_backup($backup);
if ($status == 0) {
print_msg("backup task '$backup' did not run, because of errors", ERROR);
} elsif ($status == 1) {
print_msg("backup task '$backup' finished successfully", INFO);
}
}
}
# main_monitoring()
#
# main routine doing the monitoring
#
# parameter:
# none
# return:
# none
sub main_monitoring {
my @backups = config_get_keys1('backup');
# check all backups for status
my $backup_status = 1;
my @backup_ok = ();
my @backup_warning = ();
my @backup_fail = ();
my @backup_unknown = ();
foreach my $backup (@backups) {
# keep in mind that this line will be printed to STDOUT - and might confuse monitoring plugins
# use the -q option
print_msg("Backup task: " . $backup, INFO);
my $status = status_backup($backup);
if ($status == -1) {
# ignore
print_msg("Status: disabled", INFO);
} elsif ($status == 0) {
# OK
push(@backup_ok, $backup);
print_msg("Status: OK", INFO);
} elsif ($status == 1) {
# WARNING
push(@backup_warning, $backup);
print_msg("Status: WARNING", INFO);
} elsif ($status == 2) {
# CRITICAL
push(@backup_fail, $backup);
print_msg("Status: CRITICAL", INFO);
} elsif ($status == 3) {
# UNKNOWN
push(@backup_unknown, $backup);
print_msg("Status: UNKNOWN", INFO);
}
}
my @backup_results = ();
my $backup_result = 0;
if (scalar(@backup_fail) > 0) {
push(@backup_results, scalar(@backup_fail) . " backup" . ((scalar(@backup_fail) == 1) ? "" : "s") . " CRITICAL");
$backup_result = 2;
}
if (scalar(@backup_warning) > 0) {
push(@backup_results, scalar(@backup_warning) . " backup" . ((scalar(@backup_warning) == 1) ? "" : "s") . " WARNING");
if ($backup_result == 0) {
$backup_result = 1;
}
}
if (scalar(@backup_unknown) > 0) {
push(@backup_results, scalar(@backup_unknown) . " backup" . ((scalar(@backup_unknown) == 1) ? "" : "s") . " UNKNOWN");
if ($backup_result == 0) {
$backup_result = 3;
}
}
if (scalar(@backup_ok) > 0) {
push(@backup_results, scalar(@backup_ok) . " backup" . ((scalar(@backup_ok) == 1) ? "" : "s") . " OK");
}
# print to STDOUT instead of print_msg()
print join(", ", @backup_results) . "\n";
exit($backup_result);
}
# status_backup()
#
# retrieve backup status (from cached status files)
#
# parameter:
# - backup task name, from config
# return:
# - return status
# 0: OK
# 1: WARNING
# 2: CRITICAL
# 3: UNKNOWN
# -1: ignore
sub status_backup {
my $backup = shift;
my $backup_type = config_get_key3('backup', $backup, 'type');
my $backup_disabled = config_get_key3('backup', $backup, 'disabled');
my $backup_incremental = config_get_key3('backup', $backup, 'incremental');
my $backup_full_every = config_get_key3('backup', $backup, 'full-every');
my $backup_incremental_every = config_get_key3('backup', $backup, 'incremental-every');
my $backup_keep_full = config_get_key3('backup', $backup, 'keep-full');
my $backup_cd_directory = config_get_key3('backup', $backup, 'backup-cd-directory');
my $backup_this_directory = config_get_key3('backup', $backup, 'backup-this-directory');
my $backup_target = config_get_key3('backup', $backup, 'backup-target');
my $backup_target_sub_directory = config_get_key3('backup', $backup, 'backup-target-sub-directory');
my $backup_include = config_get_key3('backup', $backup, 'include');
my $backup_exclude = config_get_key3('backup', $backup, 'exclude');
my $config_add_time = config_get_key2('config', 'monitoring-add-time');
# pre-flight checks
if (!defined($backup_disabled)) {
$backup_disabled = 'no';
}
if ($backup_disabled eq 'yes' or $backup_disabled eq '1') {
print_msg("task is disabled", INFO);
return -1;
}
if (defined($config_add_time) and length($config_add_time) > 0) {
$config_add_time = parse_duration($config_add_time);
print_msg("add additional $config_add_time seconds as safety", DEBUG);
} else {
$config_add_time = 0;
}
# looks for all $backup*-overview.txt files in cache
my $last_logfile = identify_last_logfile($backup, config_get_keys2('config', 'log-directory'));
if ($last_logfile eq "-1") {
print_msg("error identifying statusfile for backup: $backup", INFO);
return 2;
}
if ($last_logfile eq "") {
print_msg("no statusfile for backup: $backup", INFO);
return 3;
}
print_msg("using logfile $last_logfile for backup $backup", DEBUG);
my $fh = new FileHandle;
if (!open($fh, "<", $last_logfile)) {
print_msg("error opening statusfile ($last_logfile) for backup: $backup", INFO);
return 2;
}
my @file = <$fh>;
close($fh);
my $this_backup_path_info = parse_backup_status(\@file);
my %this_backup_path_info = %$this_backup_path_info;
print_msg("number of full backups: " . $this_backup_path_info{'number_full_backups'}, DEBUG);
if ($this_backup_path_info{'number_full_backups'} > 0) {
print_msg("last full backup: " . $this_backup_path_info{'last_full_backup'}, DEBUG);
if ($this_backup_path_info{'number_incremental_backups'} > 0) {
print_msg("number of incremental backups: " . $this_backup_path_info{'number_incremental_backups'}, DEBUG);
print_msg("last incremental backups: " . $this_backup_path_info{'last_incremental_backup'}, DEBUG);
}
}
if ($this_backup_path_info{'error_in_sets'} == 1) {
print_msg("errors in backup set found, please verify backup!", ERROR);
return 2;
}
# is full backup necessary?
my $need_full_backup = 0;
my $need_incremental_backup = 0;
if ($this_backup_path_info{'number_full_backups'} == 0) {
print_msg("no existing backup", INFO);
$need_full_backup = 1;
} else {
my $backup_full_duration = undef;
eval { $backup_full_duration = parse_duration($backup_full_every); };
if ($@) {
print_msg("full-every ($backup_full_every) is invalid", ERROR);
return 2;
}
print_msg("full backup every $backup_full_duration seconds ($backup_full_every)", DEBUG);
my $time_last_full_backup = str2time($this_backup_path_info{'last_full_backup'});
if (($time_last_full_backup + $backup_full_duration) < time()) {
if (($time_last_full_backup + $backup_full_duration + $config_add_time) < time()) {
print_msg("last full backup is " . duration_exact(time() - ($time_last_full_backup + $backup_full_duration)) . " too old, full backup required", DEBUG);
$need_full_backup = 1;
} else {
print_msg("last full backup is " . duration_exact(time() - ($time_last_full_backup + $backup_full_duration)) . " too old, but still in safety time", DEBUG);
}
} else {
print_msg("" . duration_exact(($time_last_full_backup + $backup_full_duration) - time()) . " until next full backup", DEBUG);
}
}
# if no full backup, is incremental backup allowed?
# if incremental backup allowed, is it necessary?
if ($need_full_backup == 0 and $backup_incremental eq 'yes') {
if ($this_backup_path_info{'number_incremental_backups'} == 0) {
# set last incremental backup timestamp to last full backup timestamp
$this_backup_path_info{'last_incremental_backup'} = $this_backup_path_info{'last_full_backup'};
}
my $backup_incremental_duration = undef;
eval { $backup_incremental_duration = parse_duration($backup_incremental_every); };
if ($@) {
print_msg("incremental-every ($backup_incremental_every) is invalid", ERROR);
return 2;
}
print_msg("incremental backup every $backup_incremental_duration seconds ($backup_incremental_every)", DEBUG);
my $time_last_incremental_backup = str2time($this_backup_path_info{'last_incremental_backup'});
if (($time_last_incremental_backup + $backup_incremental_duration) < time()) {
if (($time_last_incremental_backup + $backup_incremental_duration + $config_add_time) < time()) {
print_msg("last incremental backup is " . duration_exact(time() - ($time_last_incremental_backup + $backup_incremental_duration)) . " too old, incremental backup required", DEBUG);
$need_incremental_backup = 1;
} else {
print_msg("last incremental backup is " . duration_exact(time() - ($time_last_incremental_backup + $backup_incremental_duration)) . " too old, but still in safety time", DEBUG);
}
} else {
print_msg("" . duration_exact(($time_last_incremental_backup + $backup_incremental_duration) - time()) . " until next incremental backup", DEBUG);
}
}
if ($need_full_backup == 1 or $need_incremental_backup == 1) {
return 2;
}
}
# identify_last_logfile()
#
# search latest $backup*-overview.txt file in cache
#
# parameter:
# - backup name
# - cache directory
# return:
# - filename, or "-1"
# note:
# - will not attempt to retrieve status from backup, only the local cache is used
sub identify_last_logfile {
my $backup = shift;
my $dir = shift;
my $dh = new FileHandle;
if (!opendir($dh, $dir)) {
print_msg("cannot open log directory ($dir): $!", ERROR);
return -1;
}
my @found = ();
while (my $file = readdir($dh)) {
if ($file =~ /^$backup\-\d+\-\d+\-overview.txt$/) {
push(@found, $file);
}
}
closedir($dh);
if (scalar(@found) == 0) {
return 0;
}
@found = sort(@found);
my $last = pop(@found);
return $dir . $last;
}
# run_backup()
#
# run a backup task
#
# parameter:
# - backup task name, from config
# return:
# - return status
# 0: error
# 1: OK
# >1: ignore value
# note: don't exit in this function, return back to caller
sub run_backup {
my $backup = shift;
my @execute = ();
my $backup_type = config_get_key3('backup', $backup, 'type');
my $backup_disabled = config_get_key3('backup', $backup, 'disabled');
my $backup_incremental = config_get_key3('backup', $backup, 'incremental');
my $backup_full_every = config_get_key3('backup', $backup, 'full-every');
my $backup_incremental_every = config_get_key3('backup', $backup, 'incremental-every');
my $backup_keep_full = config_get_key3('backup', $backup, 'keep-full');
my $backup_cd_directory = config_get_key3('backup', $backup, 'backup-cd-directory');
my $backup_this_directory = config_get_key3('backup', $backup, 'backup-this-directory');
my $backup_target = config_get_key3('backup', $backup, 'backup-target');
my $backup_target_sub_directory = config_get_key3('backup', $backup, 'backup-target-sub-directory');
my $backup_include = config_get_key3('backup', $backup, 'include');
my $backup_exclude = config_get_key3('backup', $backup, 'exclude');
# pre-flight checks
if (!defined($backup_disabled)) {
$backup_disabled = 'no';
}
if ($backup_disabled eq 'yes' or $backup_disabled eq '1') {
print_msg("task is disabled", INFO);
return 2;
}
# create logfile prefix, so every "duplicity" call can use the same prefix
my ($gtod_seconds, $gtod_microseconds) = gettimeofday();
my $logfile_prefix = sprintf("%s-%010d-%06d", $backup, $gtod_seconds, $gtod_microseconds);
print_msg("logfile names start with: $logfile_prefix", INFO);
$logfile_prefix = config_get_keys2('config', 'log-directory') . $logfile_prefix . '-';
if (!defined($backup_type)) {
print_msg("backup type must be set", ERROR);
return 0;