From 207936f85e4e5494c63a39249a6d20414a44c6f2 Mon Sep 17 00:00:00 2001 From: Andrew Ruthven Date: Sat, 16 Feb 2008 23:04:37 +1300 Subject: [PATCH] Set some sane defaults and refactor the docleanup code. --- mirror | 59 +++++++++++++++++++++++++--------------------------------- 1 file changed, 25 insertions(+), 34 deletions(-) diff --git a/mirror b/mirror index b67b9a3..cc70024 100644 --- a/mirror +++ b/mirror @@ -140,6 +140,18 @@ sub load_config { unless defined $c->{$required} && $c->{$required} ne ''; } + # set some defaults. + for my $type (qw/days weeks/) { + $c->{'save'}{$type} ||= 0; + } + $c->{'save'}{'hours'} ||= 1; + + # To ensure we have enough days to take weekly snapshots, make sure + # we have enough. + if ($c->{'save'}{'weeks'} > 0 && $c->{'save'}{'days'} < 7) { + $c->{'save'}{'days'} = 7; + } + $c->{'cp'} ||= "cp -alf"; $c->{'weekdir'} = "$c->{'backuproot'}/$c->{'hostid'}/weekly"; $c->{'daydir'} = "$c->{'backuproot'}/$c->{'hostid'}/daily"; @@ -166,7 +178,7 @@ sub dodir { sub doweek { return - if defined $c->{'save'}{'weeks'} && $c->{'save'}{'weeks'} == 0; + if $c->{'save'}{'weeks'} == 0; if (test("-d $c->{'daydir'}/$c->{'lweek'}")) { run_and_check("mv $c->{'daydir'}/$c->{'lweek'} $c->{'weekdir'}") @@ -177,7 +189,7 @@ sub doweek { sub doday { return - if defined $c->{'save'}{'days'} && $c->{'save'}{'days'} == 0; + if $c->{'save'}{'days'} == 0; if (test("-d $c->{'hourdir'}/$c->{'lday'}")) { run_and_check("mv $c->{'hourdir'}/$c->{'lday'} $c->{'daydir'}"); @@ -220,48 +232,27 @@ sub dosync { } sub docleanup { - $c->{'save'}{'hours'} ||= 1; - - my @hours = split("\n", run_and_return("ls -t $c->{'hourdir'}")); - - my $count = 0; - for my $hour (@hours) { - $count++; - - if ($count > $c->{'save'}{'hours'}) { - run_and_check("rm -Rf $c->{'hourdir'}/$hour"); - } + for my $type (qw/day hour week/) { + _docleanup($type}; } +} - if ($c->{'save'}{'weeks'} > 0 && $c->{'save'}{'days'} < 7) { - $c->{'save'}{'days'} = 7; - } - - my @days = split("\n", run_and_return("ls -t $c->{'daydir'}")); - - $count = 0; - for my $day (@days) { - $count++; - - if ($count > $c->{'save'}{'days'}) { - run_and_check("rm -Rf $c->{'daydir'}/$day"); - } - } +sub _docleanup { + my $type = shift; + my $dir = $type . "dir"; - my @weeks = split("\n", run_and_return("ls -t $c->{'weekdir'}")); + my @snapshots = split("\n", run_and_return("ls -t $c->{$dir}")); - $count = 0; - for my $week (@weeks) { + my $count = 0; + for my $snapshot (@snapshots) { $count++; - if ($count > $c->{'save'}{'weeks'}) { - run_and_check("rm -Rf $c->{'weekdir'}/$week"); + if ($count > $c->{'save'}{$type}) { + run_and_check("rm -Rf $c->{$dir}/$snapshot"); } } } - - # Perform a test, either via ssh or using a local perl check sub test { my ($test) = shift; -- 2.30.2