btrbk: changed command line semantics, accepting commands

pull/30/head
Axel Burri 2014-12-13 15:15:58 +01:00
parent a5ad796aeb
commit ff504b508f
2 changed files with 144 additions and 115 deletions

73
btrbk
View File

@ -54,10 +54,11 @@ our $PROJECT_HOME = '<http://www.digint.ch/btrbk>';
my $version_info = "btrfs-backup command line client, version $VERSION"; my $version_info = "btrfs-backup command line client, version $VERSION";
my $time_format = "%Y%m%d_%H%M%S"; my $time_format = "%Y%m%d_%H%M%S";
my $default_config = "/etc/btrbk.conf";
my $src_snapshot_dir = "_btrbk_snap"; my $src_snapshot_dir = "_btrbk_snap";
my %vol_info; my %vol_info;
my $pretend; my $dryrun;
my $verbose = 0; my $verbose = 0;
my $debug = 0; my $debug = 0;
@ -68,16 +69,19 @@ sub VERSION_MESSAGE
sub HELP_MESSAGE sub HELP_MESSAGE
{ {
print STDERR "usage: $0 [options] <src_root_volume> <subvol> <dest_root_volume> <subvol>\n"; print STDERR "usage: $0 [options] <command>\n";
print STDERR "\n"; print STDERR "\n";
print STDERR "options:\n"; print STDERR "options:\n";
print STDERR " -h, --help display this help message\n"; print STDERR " --help display this help message\n";
print STDERR " --version display version information\n"; print STDERR " --version display version information\n";
# print STDERR " -i incremental backup\n";
print STDERR " -c config file\n"; print STDERR " -c config file\n";
print STDERR " -v verbose\n"; print STDERR " -v verbose\n";
print STDERR " -d debug\n"; print STDERR " -d debug\n";
print STDERR " -p pretend only (dryrun)\n"; print STDERR "\n";
print STDERR "commands:\n";
print STDERR " info shows information\n";
print STDERR " execute perform all backups\n";
print STDERR " dryrun don't run btrfs commands, just show what would be executed\n";
print STDERR "\n"; print STDERR "\n";
print STDERR "For additional information, see $PROJECT_HOME\n"; print STDERR "For additional information, see $PROJECT_HOME\n";
} }
@ -93,7 +97,7 @@ sub run_cmd($;$)
my $non_destructive = shift; my $non_destructive = shift;
my $ret = ""; my $ret = "";
INFO "### $cmd" unless($non_destructive); INFO "### $cmd" unless($non_destructive);
if($non_destructive || (not $pretend)) { if($non_destructive || (not $dryrun)) {
DEBUG "### $cmd"; DEBUG "### $cmd";
$ret = `$cmd`; $ret = `$cmd`;
chomp($ret); chomp($ret);
@ -121,7 +125,7 @@ sub check_src($$)
my $root = shift; my $root = shift;
my $vol = shift; my $vol = shift;
return 0 unless(check_vol($root, $vol)); return 0 unless(check_vol($root, $vol));
unless($pretend) unless($dryrun)
{ {
my $dir = "${root}/${src_snapshot_dir}"; my $dir = "${root}/${src_snapshot_dir}";
unless(-d $dir) { unless(-d $dir) {
@ -293,7 +297,7 @@ sub btrfs_send_receive($$;$$)
my $cmd = "/sbin/btrfs send $parent_option $src | /sbin/btrfs receive $receive_option $dst/ 2>&1"; my $cmd = "/sbin/btrfs send $parent_option $src | /sbin/btrfs receive $receive_option $dst/ 2>&1";
my $ret = run_cmd($cmd); my $ret = run_cmd($cmd);
# run_cmd("/bin/sync"); # run_cmd("/bin/sync");
if($changelog && (not $pretend)) if($changelog && (not $dryrun))
{ {
INFO "--- writing changelog: $changelog"; INFO "--- writing changelog: $changelog";
if(open(LOGFILE, '>>', $changelog)) { if(open(LOGFILE, '>>', $changelog)) {
@ -354,36 +358,47 @@ MAIN:
$Data::Dumper::Sortkeys = 1; $Data::Dumper::Sortkeys = 1;
my %opts; my %opts;
getopts('hc:vdp', \%opts); getopts('c:vdp', \%opts);
# my $sroot = shift @ARGV; my $command = shift @ARGV;
# my $svol = shift @ARGV;
# my $droot = shift @ARGV;
# my $dvol = shift @ARGV;
# assign command line options # assign command line options
$pretend = $opts{p};
$debug = $opts{d}; $debug = $opts{d};
$verbose = $opts{v} || $debug; $verbose = $opts{v} || $debug;
# my $incremental = $opts{i}; my $config = $opts{c} || $default_config;
my $config = $opts{c};
# check command line options # check command line options
if($opts{h} || (not $config)) { if($opts{h} || (not $command)) {
VERSION_MESSAGE(); VERSION_MESSAGE();
HELP_MESSAGE(0); HELP_MESSAGE(0);
exit 0; exit 0;
} }
my $jobs = parse_config($config);
unless($jobs) { my $action_execute;
ERROR "Failed to parse configuration file"; my $action_info;
if(($command eq "execute") || ($command eq "dryrun")) {
$action_execute = 1;
$dryrun = 1 if($command eq "dryrun");
}
elsif($command eq "info") {
$action_info = 1;
}
else {
ERROR "Unrecognized command: $command";
HELP_MESSAGE(0);
exit 1; exit 1;
} }
my $postfix = '.' . strftime($time_format, localtime); my $postfix = '.' . strftime($time_format, localtime);
# #
# check jobs, fill vol_info hash # check jobs, fill vol_info hash
# #
my $jobs = parse_config($config);
unless($jobs) {
ERROR "Failed to parse configuration file";
exit 1;
}
foreach my $job (@$jobs) foreach my $job (@$jobs)
{ {
my $sroot = $job->{sroot} || die; my $sroot = $job->{sroot} || die;
@ -393,6 +408,21 @@ MAIN:
} }
DEBUG(Data::Dumper->Dump([\%vol_info], ["vol_info"])); DEBUG(Data::Dumper->Dump([\%vol_info], ["vol_info"]));
if($action_info)
{
INFO(Data::Dumper->Dump([\%vol_info], ["vol_info"]));
foreach my $job (@$jobs)
{
my $sroot = $job->{sroot} || die;
my $svol = $job->{svol} || die;
my $droot = $job->{droot} || die;
my $dvol = $job->{dvol} || die;
print "$sroot/$svol\n"
}
}
if($action_execute)
{
# #
# create snapshots # create snapshots
# #
@ -404,7 +434,6 @@ MAIN:
my $droot = $job->{droot} || die; my $droot = $job->{droot} || die;
my $dvol = $job->{dvol} || die; my $dvol = $job->{dvol} || die;
my $type = $job->{type} || die; my $type = $job->{type} || die;
my @job_opts = @{$job->{options}} || die;
my $ssnap = "$src_snapshot_dir/$svol$postfix"; my $ssnap = "$src_snapshot_dir/$svol$postfix";
if(check_vol($droot, "$dvol/$svol$postfix")) { if(check_vol($droot, "$dvol/$svol$postfix")) {
@ -493,5 +522,5 @@ MAIN:
} }
} }
} }
}
1; 1;