mirror of https://github.com/digint/btrbk
btrbk: changed command line semantics, accepting commands
parent
a5ad796aeb
commit
ff504b508f
73
btrbk
73
btrbk
|
@ -54,10 +54,11 @@ our $PROJECT_HOME = '<http://www.digint.ch/btrbk>';
|
|||
my $version_info = "btrfs-backup command line client, version $VERSION";
|
||||
my $time_format = "%Y%m%d_%H%M%S";
|
||||
|
||||
my $default_config = "/etc/btrbk.conf";
|
||||
my $src_snapshot_dir = "_btrbk_snap";
|
||||
|
||||
my %vol_info;
|
||||
my $pretend;
|
||||
my $dryrun;
|
||||
my $verbose = 0;
|
||||
my $debug = 0;
|
||||
|
||||
|
@ -68,16 +69,19 @@ sub VERSION_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 "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 " -i incremental backup\n";
|
||||
print STDERR " -c config file\n";
|
||||
print STDERR " -v verbose\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 "For additional information, see $PROJECT_HOME\n";
|
||||
}
|
||||
|
@ -93,7 +97,7 @@ sub run_cmd($;$)
|
|||
my $non_destructive = shift;
|
||||
my $ret = "";
|
||||
INFO "### $cmd" unless($non_destructive);
|
||||
if($non_destructive || (not $pretend)) {
|
||||
if($non_destructive || (not $dryrun)) {
|
||||
DEBUG "### $cmd";
|
||||
$ret = `$cmd`;
|
||||
chomp($ret);
|
||||
|
@ -121,7 +125,7 @@ sub check_src($$)
|
|||
my $root = shift;
|
||||
my $vol = shift;
|
||||
return 0 unless(check_vol($root, $vol));
|
||||
unless($pretend)
|
||||
unless($dryrun)
|
||||
{
|
||||
my $dir = "${root}/${src_snapshot_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 $ret = run_cmd($cmd);
|
||||
# run_cmd("/bin/sync");
|
||||
if($changelog && (not $pretend))
|
||||
if($changelog && (not $dryrun))
|
||||
{
|
||||
INFO "--- writing changelog: $changelog";
|
||||
if(open(LOGFILE, '>>', $changelog)) {
|
||||
|
@ -354,36 +358,47 @@ MAIN:
|
|||
$Data::Dumper::Sortkeys = 1;
|
||||
|
||||
my %opts;
|
||||
getopts('hc:vdp', \%opts);
|
||||
# my $sroot = shift @ARGV;
|
||||
# my $svol = shift @ARGV;
|
||||
# my $droot = shift @ARGV;
|
||||
# my $dvol = shift @ARGV;
|
||||
getopts('c:vdp', \%opts);
|
||||
my $command = shift @ARGV;
|
||||
|
||||
# assign command line options
|
||||
$pretend = $opts{p};
|
||||
$debug = $opts{d};
|
||||
$verbose = $opts{v} || $debug;
|
||||
# my $incremental = $opts{i};
|
||||
my $config = $opts{c};
|
||||
my $config = $opts{c} || $default_config;
|
||||
|
||||
# check command line options
|
||||
if($opts{h} || (not $config)) {
|
||||
if($opts{h} || (not $command)) {
|
||||
VERSION_MESSAGE();
|
||||
HELP_MESSAGE(0);
|
||||
exit 0;
|
||||
}
|
||||
my $jobs = parse_config($config);
|
||||
unless($jobs) {
|
||||
ERROR "Failed to parse configuration file";
|
||||
|
||||
my $action_execute;
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
my $postfix = '.' . strftime($time_format, localtime);
|
||||
|
||||
#
|
||||
# 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)
|
||||
{
|
||||
my $sroot = $job->{sroot} || die;
|
||||
|
@ -393,6 +408,21 @@ MAIN:
|
|||
}
|
||||
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
|
||||
#
|
||||
|
@ -404,7 +434,6 @@ MAIN:
|
|||
my $droot = $job->{droot} || die;
|
||||
my $dvol = $job->{dvol} || die;
|
||||
my $type = $job->{type} || die;
|
||||
my @job_opts = @{$job->{options}} || die;
|
||||
my $ssnap = "$src_snapshot_dir/$svol$postfix";
|
||||
|
||||
if(check_vol($droot, "$dvol/$svol$postfix")) {
|
||||
|
@ -492,6 +521,6 @@ MAIN:
|
|||
btrfs_send_receive($snapshot, "${droot}/${dvol}", undef, $changelog);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
1;
|
||||
|
|
Loading…
Reference in New Issue