forked from rsnapshot/rsnapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsnapshot-program.pl
executable file
·7862 lines (6280 loc) · 214 KB
/
rsnapshot-program.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
#!@PERL@ -w
########################################################################
# #
# rsnapshot #
# by Nathan Rosenquist #
# now maintained by Benedikt Heine #
# #
# The official rsnapshot website is located at #
# http://www.rsnapshot.org/ #
# #
# Copyright (C) 2003-2005 Nathan Rosenquist #
# #
# Portions Copyright (C) 2002-2006 Mike Rubel, Carl Wilhelm Soderstrom,#
# Ted Zlatanov, Carl Boe, Shane Liebling, Bharat Mediratta, #
# Peter Palfrader, Nicolas Kaiser, David Cantrell, Chris Petersen, #
# Robert Jackson, Justin Grote, David Keegel, Alan Batie #
# #
# rsnapshot comes with ABSOLUTELY NO WARRANTY. This is free software, #
# and you may copy, distribute and/or modify it under the terms of #
# the GNU GPL (version 2 or at your option any later version). #
# See the GNU General Public License (in file: COPYING) for details. #
# #
# Based on code originally by Mike Rubel #
# http://www.mikerubel.org/computers/rsync_snapshots/ #
# #
########################################################################
# tabstops are set to 4 spaces
# in vi, do: set ts=4 sw=4
########################################
### STANDARD MODULES ###
########################################
require 5.004;
use strict;
use DirHandle; # DirHandle()
use Cwd; # cwd()
use Getopt::Std; # getopts()
use File::Path; # mkpath(), rmtree()
use File::stat; # stat(), lstat()
use POSIX qw(locale_h); # setlocale()
use Fcntl; # sysopen()
use IO::File; # recursive open in parse_config_file
use IPC::Open3 qw(open3); #open rsync with error output
use IO::Handle; # handle autoflush for rsync-output
########################################
### CPAN MODULES ###
########################################
# keep track of whether we have access to the Lchown module
my $have_lchown = 0;
# use_lchown() is called later, so we can log the results
########################################
### DECLARE GLOBAL VARIABLES ###
########################################
# turn off buffering
$| = 1;
# version of rsnapshot
my $VERSION = '@VERSION@';
# command or interval to execute (first cmd line arg)
my $cmd;
# default configuration file
my $config_file;
# hash to hold variables from the configuration file
my %config_vars;
# array of hash_refs containing the destination backup point
# and either a source dir or a script to run
my @backup_points;
# array of backup points to rollback, in the event of failure
my @rollback_points;
# "intervals" are user defined time periods (e.g., alpha, beta)
# this array holds hash_refs containing the name of the interval,
# and the number of snapshots to keep of it
#
# NB, intervals and now called backup levels, and the config parameter
# is 'retain'
my @intervals;
# store interval data (mostly info about which one we're on, what was before, etc.)
# this is a convenient reference to some of the data from and metadata about @intervals
my $interval_data_ref;
# intervals can't have these values, because they're either taken by other commands
# or reserved for future use
my @reserved_words = qw(
archive
check-config-version
configtest
diff
delete
du
get-latest-snapshot
help
history
list
print-config
restore
rollback
sync
upgrade-config-file
version
version-only
);
# global flags that change the outcome of the program,
# and are configurable by both cmd line and config flags
#
my $test = 0; # turn verbose on, but don't execute
# any filesystem commands
my $do_configtest = 0; # parse config file and exit
my $one_fs = 0; # one file system (don't cross
# partitions within a backup point)
my $link_dest = 0; # use the --link-dest option to rsync
my $stop_on_stale_lockfile = 0; # stop if there is a stale lockfile
# how much noise should we make? the default is 2
#
# 0 Absolutely quiet (reserved, but not implemented)
# 1 Don't display warnings about FIFOs and special files
# 2 Default (errors only)
# 3 Verbose (show shell commands and equivalents)
# 4 Extra verbose messages (individual actions inside some subroutines, output from rsync)
# 5 Debug
#
# define verbose and loglevel
my $verbose = undef;
my $loglevel = undef;
# set defaults for verbose and loglevel
my $default_verbose = 2;
my $default_loglevel = 3;
# assume the config file is valid until we find an error
my $config_perfect = 1;
# exit code for rsnapshot
my $exit_code = 0;
# global defaults for external programs
my $default_rsync_short_args = '-a';
my $default_rsync_long_args = '--delete --numeric-ids --relative --delete-excluded';
my $default_ssh_args = undef;
my $default_du_args = '-csh';
# set default for use_lazy_deletes
my $use_lazy_deletes = 0; # do not delete the oldest archive until after backup
# set default for number of tries
my $rsync_numtries = 1; # by default, try once
# exactly how the program was called, with all arguments
# this is set before getopts() modifies @ARGV
my $run_string = "$0 " . join(' ', @ARGV);
# if we have any errors, we print the run string once, at the top of the list
my $have_printed_run_string = 0;
# pre-buffer the include/exclude parameter flags
# local to parse_config_file and validate_config_file
my $rsync_include_args = undef;
my $rsync_include_file_args = undef;
# hash used to register traps and execute in bail
my %traps;
$traps{"linux_lvm_snapshot"} = 0;
$traps{"linux_lvm_mountpoint"} = 0;
########################################
### SIGNAL HANDLERS ###
########################################
# shut down gracefully if necessary
$SIG{'HUP'} = 'IGNORE';
$SIG{'INT'} = sub { bail('rsnapshot was sent INT signal... cleaning up'); };
$SIG{'QUIT'} = sub { bail('rsnapshot was sent QUIT signal... cleaning up'); };
$SIG{'ABRT'} = sub { bail('rsnapshot was sent ABRT signal... cleaning up'); };
$SIG{'TERM'} = sub { bail('rsnapshot was sent TERM signal... cleaning up'); };
# For a PIPE error, we dont want any more output so set $verbose less than 1.
$SIG{'PIPE'} = sub {
$verbose = 0;
bail(
'rsnapshot was sent PIPE signal... Hint: if rsnapshot is running from cron, check that mail is installed on this system, or redirect stdout and stderr in cron job'
);
};
########################################
### CORE PROGRAM STRUCTURE ###
########################################
# what follows is a linear sequence of events.
# all of these subroutines will either succeed or terminate the program safely.
# figure out the path to the default config file (with autoconf we have to check)
# this sets $config_file to the full config file path
find_config_file();
# parse command line options
# (this can override $config_file, if the -c flag is used on the command line)
parse_cmd_line_opts();
# if we need to run a command that doesn't require fully parsing the config file, do it now (and exit)
if (!defined($cmd) or ((!$cmd) && ('0' ne $cmd))) {
show_usage();
}
elsif ($cmd eq 'help') {
show_help();
}
elsif ($cmd eq 'version') {
show_version();
}
elsif ($cmd eq 'version-only') {
show_version_only();
}
elsif ($cmd eq 'check-config-version') {
check_config_version();
}
elsif ($cmd eq 'upgrade-config-file') {
upgrade_config_file();
}
# if we're just doing a configtest, set that flag
if ($cmd eq 'configtest') {
$do_configtest = 1;
}
# parse config file (if it exists), note: we can't parse a directory
if (defined($config_file) && (-r $config_file) && (!-d $config_file)) {
# if there is a problem, this subroutine will exit the program and notify the user of the error
parse_config_file();
validate_config_file();
}
# no config file found
else {
# warn user and exit the program
exit_no_config_file();
}
# attempt to load the Lchown module: http://search.cpan.org/dist/Lchown/
use_lchown();
# if we're just doing a configtest, exit here with the results
if (1 == $do_configtest) {
exit_configtest();
}
# if we're just using "du" or "rsnapshot-diff" to check the disk space, do it now (and exit)
# these commands are down here because they needs to know the contents of the config file
if ($cmd eq 'du') {
show_disk_usage();
}
elsif ($cmd eq 'diff') {
show_rsnapshot_diff();
}
elsif ($cmd eq 'get-latest-snapshot') {
show_latest_snapshot();
}
#
# IF WE GOT THIS FAR, PREPARE TO RUN A BACKUP
#
# log the beginning of this run
log_startup();
# this is reported to fix some semi-obscure problems with rmtree()
set_posix_locale();
# if we're using a lockfile, try to add it
# (the program will bail if one exists and it's not stale)
add_lockfile();
# create snapshot_root if it doesn't exist (and no_create_root != 1)
create_snapshot_root();
# now chdir to the snapshot_root.
# note that this is needed because in the rare case that you do this ...
# sudo -u peon rsnapshot ... and are in a directory that 'peon' can't
# read, then some versions of GNU rm will later fail, as they try to
# lstat the cwd. It's safe to chdir because all directories etc that
# we ever mention are absolute.
chdir($config_vars{'snapshot_root'});
# actually run the backup job
# $cmd should store the name of the interval we'll run against
handle_interval($cmd);
# if we have a lockfile, remove it
# however, this will have already been done if use_lazy_deletes is turned
# on, and there may be a lockfile from another process now in place,
# so in that case don't just blindly delete!
remove_lockfile() unless ($use_lazy_deletes);
# if we got this far, the program is done running
# write to the log and syslog with the status of the outcome
#
exit_with_status();
########################################
### SUBROUTINES ###
########################################
# concise usage information
# runs when rsnapshot is called with no arguments
# exits with an error condition
sub show_usage {
print <<HERE;
rsnapshot $VERSION
Usage: rsnapshot [-vtxqVD] [-c cfgfile] [command] [args]
Type \"rsnapshot help\" or \"man rsnapshot\" for more information.
HERE
exit(1);
}
# extended usage information
# runs when rsnapshot is called with "help" as an argument
# exits 0
sub show_help {
print <<HERE;
rsnapshot $VERSION
Usage: rsnapshot [-vtxqVD] [-c cfgfile] [command] [args]
Type "man rsnapshot" for more information.
rsnapshot is a filesystem snapshot utility. It can take incremental
snapshots of local and remote filesystems for any number of machines.
rsnapshot comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
See the GNU General Public License for details.
Options:
-v verbose - Show equivalent shell commands being executed.
-t test - Show verbose output, but don't touch anything.
This will be similar, but not always exactly the same
as the real output from a live run.
-c [file] - Specify alternate config file (-c /path/to/file)
-q quiet - Suppress non-fatal warnings.
-V extra verbose - The same as -v, but with more detail.
-D debug - A firehose of diagnostic information.
-x one_fs - Don't cross filesystems (same as -x option to rsync).
Commands:
[backuplevel] - A backup level as defined in rsnapshot.conf.
configtest - Syntax check the config file.
sync [dest] - Sync files, without rotating. "sync_first" must be
enabled for this to work. If a full backup point
destination is given as an optional argument, only
those files will be synced.
diff - Front-end interface to the rsnapshot-diff program.
Accepts two optional arguments which can be either
filesystem paths or backup directories within the
snapshot_root (e.g., /etc/ beta.0/etc/). The default
is to compare the two most recent snapshots.
du - Show disk usage in the snapshot_root.
Accepts an optional destination path for comparison
across snapshots (e.g., localhost/home/user/foo).
version - Show the version number for rsnapshot.
help - Show this help message.
HERE
exit(0);
}
# prints out the name and version
# exits 0
sub show_version {
print "rsnapshot $VERSION\n";
exit(0);
}
# prints only the version number
# this is "undocumented", just for use with some of the makefile targets
# exits 0
sub show_version_only {
print "$VERSION\n";
exit(0);
}
# For Getopt::Std
sub VERSION_MESSAGE {
show_version;
}
# For Getopt::Std
sub HELP_MESSAGE {
show_help;
}
# accepts no arguments
# sets the $config_file global variable
#
# this program works both "as-is" in the source tree, and when it has been parsed by autoconf for installation
# the variables with "@" symbols on both sides get replaced during ./configure
# this subroutine returns the correct path to the default config file
#
sub find_config_file {
# autoconf variables (may have too many slashes)
my $autoconf_sysconfdir = '@sysconfdir@';
my $autoconf_prefix = '@prefix@';
my $default_config_file = '/etc/rsnapshot.conf';
# consolidate multiple slashes
$autoconf_sysconfdir =~ s/\/+/\//g;
$autoconf_prefix =~ s/\/+/\//g;
# remove trailing slashes
$autoconf_sysconfdir =~ s/\/$//g;
$autoconf_prefix =~ s/\/$//g;
# if --sysconfdir was not set explicitly during ./configure, but we did use autoconf
if ($autoconf_sysconfdir eq '${prefix}/etc') {
$default_config_file = "$autoconf_prefix/etc/rsnapshot.conf";
# if --sysconfdir was set explicitly at ./configure, overriding the --prefix setting
}
elsif ($autoconf_sysconfdir ne ('@' . 'sysconfdir' . '@')) {
$default_config_file = "$autoconf_sysconfdir/rsnapshot.conf";
}
# set global variable
$config_file = $default_config_file;
}
# accepts no args
# returns no args
# sets some global flag variables
# exits the program with an error if we were passed invalid options
sub parse_cmd_line_opts {
my %opts;
my $result;
# get command line options
$result = getopts('vtxqVDc:', \%opts);
#
# validate command line args
#
# make sure config file is a file
if (defined($opts{'c'})) {
if (!-r "$opts{'c'}") {
print STDERR "File not found: $opts{'c'}\n";
$result = undef;
}
}
# die if we don't understand all the flags
if (!defined($result) or (1 != $result)) {
# At this point, getopts() or our @ARGV check will have printed out "Unknown option: -X"
print STDERR "Type \"rsnapshot help\" or \"man rsnapshot\" for more information.\n";
exit(1);
}
#
# with that out of the way, we can go about the business of setting global variables
#
# set command
$cmd = $ARGV[0];
# check for extra bogus arguments that getopts() didn't catch
if (defined($cmd) && ('du' ne $cmd) && ('diff' ne $cmd) && ('sync' ne $cmd)) {
if (scalar(@ARGV) > 1) {
for (my $i = 1; $i < scalar(@ARGV); $i++) {
print STDERR "Unknown option: $ARGV[$i]\n";
print STDERR "Please make sure all switches come before commands\n";
print STDERR "(e.g., 'rsnapshot -v alpha', not 'rsnapshot alpha -v')\n";
exit(1);
}
$result = undef;
}
}
# alternate config file?
if (defined($opts{'c'})) {
$config_file = $opts{'c'};
}
# test? (just show what WOULD be done)
if (defined($opts{'t'})) {
$test = 1;
$verbose = 3;
}
# quiet?
if (defined($opts{'q'})) { $verbose = 1; }
# verbose (or extra verbose)?
if (defined($opts{'v'})) { $verbose = 3; }
if (defined($opts{'V'})) { $verbose = 4; }
# debug
if (defined($opts{'D'})) { $verbose = 5; }
# one file system? (don't span partitions with rsync)
if (defined($opts{'x'})) { $one_fs = 1; }
}
# accepts an optional argument - no arg means to parse the default file,
# if an arg is present parse that file instead
# returns no value
# this subroutine parses the config file (rsnapshot.conf)
#
sub parse_config_file {
# count the lines in the config file, so the user can pinpoint errors more precisely
my $file_line_num = 0;
my @configs = ();
# open the config file
my $current_config_file = shift() || $config_file;
my $CONFIG;
if ($current_config_file =~ /^`(.*)`$/) {
open($CONFIG, "$1 |")
or bail("Couldn't execute \"$1\" to get config information\n");
}
else {
$CONFIG = IO::File->new($current_config_file)
or bail("Could not open config file \"$current_config_file\"\nAre you sure you have permission?");
}
# read it line by line
@configs = <$CONFIG>;
while (my $line = $configs[$file_line_num]) {
chomp($line);
# count line numbers
$file_line_num++;
# Ensure the correct filename is reported in error messages. Setting it on
# every iteration ensures it will be reset after recursive calls to this
# function.
$config_file = $current_config_file;
# assume the line is formatted incorrectly
my $line_syntax_ok = 0;
# ignore comments
if (is_comment($line)) { next; }
# ignore blank lines
if (is_blank($line)) { next; }
# if the next line begins with space or tab and also has a non-space character, then it belongs to this line as a continuation.
while (defined($configs[$file_line_num]) && $configs[$file_line_num] =~ /^[\t ]+\S/) {
(my $newline = $configs[$file_line_num]) =~ s/^\s+|\s+$//g;
$line = $line . "\t" . $newline;
$file_line_num++;
}
# parse line
my ($var, $value, $value2, $value3) = split(/\t+/, $line, 4);
# warn about entries we don't understand, and immediately prevent the
# program from running or parsing anything else
if (!defined($var)) {
config_err($file_line_num, "$line - could not find a first word on this line");
next;
}
if (!defined($value) && $var eq $line) {
# No tabs found in $line.
if ($line =~ /\s/) {
# User put spaces in config line instead of tabs.
config_err($file_line_num, "$line - missing tabs to separate words - change spaces to tabs.");
next;
}
else {
# User put only one word
config_err($file_line_num, "$line - could not find second word on this line");
next;
}
}
foreach (grep { defined($_) && index($_, ' ') == 0 } ($value, $value2, $value3)) {
print_warn("$line - extra space found between tab and $_");
}
# INCLUDEs
if ($var eq 'include_conf') {
$value =~ /^`(.*)`$/;
if ( (defined($value) && -f $value && -r $value)
|| (defined($1) && is_valid_script($1))) {
$line_syntax_ok = 1;
parse_config_file($value);
}
else {
if (defined($1)) {
config_err($file_line_num, "$line - not a valid script: '$1'");
}
else {
config_err($file_line_num, "$line - can't find or read file '$value'");
}
next;
}
}
# CONFIG_VERSION
if ($var eq 'config_version') {
if (defined($value)) {
# right now 1.2 is the only possible value
if ('1.2' eq $value) {
$config_vars{'config_version'} = $value;
$line_syntax_ok = 1;
next;
}
else {
config_err($file_line_num, "$line - config_version not recognized!");
next;
}
}
else {
config_err($file_line_num, "$line - config_version not defined!");
next;
}
}
# SNAPSHOT_ROOT
if ($var eq 'snapshot_root') {
# make sure this is a full path
if (0 == is_valid_local_abs_path($value)) {
if (is_ssh_path($value) || is_anon_rsync_path($value) || is_cwrsync_path($value)) {
config_err($file_line_num,
"$line - snapshot_root must be a local path - you cannot have a remote snapshot_root");
}
else {
config_err($file_line_num, "$line - snapshot_root must be a full path");
}
next;
# if the snapshot root already exists:
}
elsif (-e "$value") {
# if path exists already, make sure it's a directory
if ((-e "$value") && (!-d "$value")) {
config_err($file_line_num, "$line - snapshot_root must be a directory");
next;
}
# make sure it's readable
if (!-r "$value") {
config_err($file_line_num, "$line - snapshot_root exists but is not readable");
next;
}
# make sure it's writable
if ($cmd ne 'du' && !-w "$value") {
config_err($file_line_num, "$line - snapshot_root exists but is not writable");
next;
}
}
# remove the trailing slash(es) if present
$value = remove_trailing_slash($value);
$config_vars{'snapshot_root'} = $value;
$line_syntax_ok = 1;
next;
}
# SYNC_FIRST
# if this is enabled, rsnapshot syncs data to a staging directory with the "rsnapshot sync" command,
# and all "interval" runs will simply rotate files. this changes the behaviour of the lowest interval.
# when a sync occurs, no directories are rotated. the sync directory is kind of like a staging area for data transfers.
# the files in the sync directory will be hard linked with the others in the other snapshot directories.
# the sync directory lives at: /<snapshot_root>/.sync/
#
if ($var eq 'sync_first') {
if (defined($value)) {
if ('1' eq $value) {
$config_vars{'sync_first'} = 1;
$line_syntax_ok = 1;
next;
}
elsif ('0' eq $value) {
$config_vars{'sync_first'} = 0;
$line_syntax_ok = 1;
next;
}
else {
config_err($file_line_num, "$line - sync_first must be set to either 1 or 0");
next;
}
}
}
# NO_CREATE_ROOT
if ($var eq 'no_create_root') {
if (defined($value)) {
if ('1' eq $value) {
$config_vars{'no_create_root'} = 1;
$line_syntax_ok = 1;
next;
}
elsif ('0' eq $value) {
$config_vars{'no_create_root'} = 0;
$line_syntax_ok = 1;
next;
}
else {
config_err($file_line_num, "$line - no_create_root must be set to either 1 or 0");
next;
}
}
}
# CHECK FOR RSYNC (required)
if ($var eq 'cmd_rsync') {
$value =~ s/\s+$//;
if ((-f "$value") && (-x "$value") && (1 == is_real_local_abs_path($value))) {
$config_vars{'cmd_rsync'} = $value;
$line_syntax_ok = 1;
next;
}
else {
config_err($file_line_num, "$line - $value is not executable");
next;
}
}
# CHECK FOR SSH (optional)
if ($var eq 'cmd_ssh') {
$value =~ s/\s+$//;
if ((-f "$value") && (-x "$value") && (1 == is_real_local_abs_path($value))) {
$config_vars{'cmd_ssh'} = $value;
$line_syntax_ok = 1;
next;
}
else {
config_err($file_line_num, "$line - $value is not executable");
next;
}
}
# CHECK FOR GNU cp (optional)
if ($var eq 'cmd_cp') {
$value =~ s/\s+$//;
if ((-f "$value") && (-x "$value") && (1 == is_real_local_abs_path($value))) {
$config_vars{'cmd_cp'} = $value;
$line_syntax_ok = 1;
next;
}
else {
config_err($file_line_num, "$line - $value is not executable");
next;
}
}
# CHECK FOR rm (optional)
if ($var eq 'cmd_rm') {
$value =~ s/\s+$//;
if ((-f "$value") && (-x "$value") && (1 == is_real_local_abs_path($value))) {
$config_vars{'cmd_rm'} = $value;
$line_syntax_ok = 1;
next;
}
else {
config_err($file_line_num, "$line - $value is not executable");
next;
}
}
# CHECK FOR LOGGER (syslog program) (optional)
if ($var eq 'cmd_logger') {
$value =~ s/\s+$//;
if ((-f "$value") && (-x "$value") && (1 == is_real_local_abs_path($value))) {
$config_vars{'cmd_logger'} = $value;
$line_syntax_ok = 1;
next;
}
else {
config_err($file_line_num, "$line - $value is not executable");
next;
}
}
# CHECK FOR du (optional)
if ($var eq 'cmd_du') {
$value =~ s/\s+$//;
if ((-f "$value") && (-x "$value") && (1 == is_real_local_abs_path($value))) {
$config_vars{'cmd_du'} = $value;
$line_syntax_ok = 1;
next;
}
else {
config_err($file_line_num, "$line - $value is not executable");
next;
}
}
# CHECK FOR lvcreate (optional)
if ($var eq 'linux_lvm_cmd_lvcreate') {
if (is_valid_script($value)) {
$config_vars{'linux_lvm_cmd_lvcreate'} = $value;
$line_syntax_ok = 1;
next;
}
else {
config_err($file_line_num, "$line - $value is not a valid executable");
next;
}
}
# CHECK FOR lvremove (optional)
if ($var eq 'linux_lvm_cmd_lvremove') {
if (is_valid_script($value)) {
$config_vars{'linux_lvm_cmd_lvremove'} = $value;
$line_syntax_ok = 1;
next;
}
else {
config_err($file_line_num, "$line - $value is not a valid executable");
next;
}
}
# CHECK FOR mount (optional)
if ($var eq 'linux_lvm_cmd_mount') {
if (is_valid_script($value)) {
$config_vars{'linux_lvm_cmd_mount'} = $value;
$line_syntax_ok = 1;
next;
}
else {
config_err($file_line_num, "$line - $value is not a valid executable");
next;
}
}
# CHECK FOR umount (optional)
if ($var eq 'linux_lvm_cmd_umount') {
if (is_valid_script($value)) {
$config_vars{'linux_lvm_cmd_umount'} = $value;
$line_syntax_ok = 1;
next;
}
else {
config_err($file_line_num, "$line - $value is not a valid executable");
next;
}
}
# CHECK FOR cmd_preexec (optional)
if ($var eq 'cmd_preexec') {
my $script; # script file (no args)
# make sure script exists and is executable
if (!is_valid_script($value, \$script)) {
config_err($file_line_num,
"$line - \"$script\" is not executable or can't be found."
. ($script !~ m{^/} ? " Please use an absolute path." : ""));
next;
}
$config_vars{$var} = $value;
$line_syntax_ok = 1;
next;
}
# CHECK FOR cmd_postexec (optional)
if ($var eq 'cmd_postexec') {
my $script; # script file (no args)
# make sure script exists and is executable
if (!is_valid_script($value, \$script)) {
config_err($file_line_num,
"$line - \"$script\" is not executable or can't be found."
. ($script !~ m{^/} ? " Please use an absolute path." : ""));
next;
}
$config_vars{$var} = $value;
$line_syntax_ok = 1;
next;
}
# CHECK FOR rsnapshot-diff (optional)
if ($var eq 'cmd_rsnapshot_diff') {
$value =~ s/\s+$//;
if ((-f "$value") && (-x "$value") && (1 == is_real_local_abs_path($value))) {
$config_vars{'cmd_rsnapshot_diff'} = $value;
$line_syntax_ok = 1;
next;
}
else {
config_err($file_line_num, "$line - $value is not executable");
next;
}
}
# INTERVALS
# 'retain' is the new name for this parameter, although for
# Laziness reasons (plus the fact that I'm making this change
# at 10 minutes to midnight and so am wary of making changes
# throughout the code and getting it wrong) the code will
# still call it 'interval'. Documentation and messages should
# refer to 'retain'. The old 'interval' will be kept as an
# alias.
if ($var eq 'interval' || $var eq 'retain') {
my $retain = $var; # either 'interval' or 'retain'
# check if interval is blank
if (!defined($value)) { config_err($file_line_num, "$line - $retain can not be blank"); }
foreach my $word (@reserved_words) {
if ($value eq $word) {
config_err($file_line_num,
"$line - \"$value\" is not a valid interval name, reserved word conflict");
next;
}
}
# make sure interval is alpha-numeric
if ($value !~ m/^[\w\d]+$/) {
config_err($file_line_num,
"$line - \"$value\" is not a valid $retain name, must be alphanumeric characters only");
next;
}
# check if number is blank
if (!defined($value2)) {
config_err($file_line_num, "$line - \"$value\" number can not be blank");
next;
}
# check if number is valid
if ($value2 !~ m/^\d+$/) {
config_err($file_line_num, "$line - \"$value2\" is not a legal value for a retention count");
next;
}
# ok, it's a number. is it positive?
else {
# make sure number is positive
if ($value2 <= 0) {
config_err($file_line_num, "$line - \"$value\" must be at least 1 or higher");
next;
}
}
my %hash;
$hash{'interval'} = $value;
$hash{'number'} = $value2;
push(@intervals, \%hash);
$line_syntax_ok = 1;
next;
}
# BACKUP POINTS
if ($var eq 'backup') {
my $src = $value; # source directory
my $dest = $value2; # dest directory
my $opt_str = $value3; # option string from this backup point
my $opts_ref = undef; # array_ref to hold parsed opts
if (!defined($config_vars{'snapshot_root'})) {
config_err($file_line_num, "$line - snapshot_root needs to be defined before backup points");
next;
}
if (!defined($src)) {
config_err($file_line_num, "$line - no source path specified for backup point");
next;
}
if (!defined($dest) || $dest eq "") {
config_err($file_line_num, "$line - no destination path specified for backup point");
next;
}
# make sure we aren't traversing directories