aging v1.3
authorHeiko Schlittermann (CTQ.kx.schlittermann.de) <hs@schlittermann.de>
Tue, 03 May 2011 16:40:13 +0200
changeset 99 2d2d94f5ba3f
parent 98 fd90dec0d6ec
child 100 187dbfaf5979
aging
bin/ftbackup
ftbackup.conf.example
--- a/bin/ftbackup	Tue May 03 13:21:56 2011 +0200
+++ b/bin/ftbackup	Tue May 03 16:40:13 2011 +0200
@@ -21,7 +21,7 @@
 
 my $ME      = basename $0;
 
-my $VERSION = "1.2";
+my $VERSION = "1.3";
 my $LOG_LEVEL = $ALL;
 
 my @CONFIGS = ("/etc/$ME.conf", "$ENV{HOME}/.$ME.conf", "$ME.conf");
@@ -38,7 +38,7 @@
 
 my $opt_level     = undef;
 my $opt_today     = strftime("%F", localtime $NOW);
-my @opt_debug     = ();
+my $opt_debug     = 0;
 my $opt_verbose   = 0;
 my $opt_dry       = 0;
 my $opt_force     = 0;
@@ -59,7 +59,7 @@
 sub real_device($);
 sub get_estimate($$);
 sub devno($);
-sub unlink_old_dumps($$);
+sub unlink_old_dumps($$$);
 
 our @AT_EXIT;
 END { $_->() foreach @AT_EXIT }
@@ -71,7 +71,7 @@
     FTP_PASSIVE       => 1,
     COMPRESSION_LEVEL => 6,
     FULL_CYCLE        => 7,
-    KEEP              => 2,
+    KEEP              => "7d",
 );
 
 ALWAYS "START";
@@ -83,7 +83,7 @@
     GetOptions(
         "l|level=i" => \$opt_level,
         "L|label=s" => \$opt_label,
-        "d|debug:s" => sub { push @opt_debug, split /,/, $_[1] },
+        "d|debug!"  => \$opt_debug,
         "v|verbose" => \$opt_verbose,
         "i|info"    => \$opt_info,
         "dry"       => sub { $opt_dry = 1; $opt_verbose = 1 },
@@ -108,7 +108,6 @@
 
     ### %cf
     ### @devs
-    ### x: exit
 
     verbose +(map { "candidate: $_->{dev} as $_->{rdev}\n" } @devs), "\n";
 
@@ -168,18 +167,16 @@
         exit;
     }
 
-    if (not "output" ~~ \@opt_debug) {
-        $ftp = new FTP(
-            $cf{FTP_HOST},
-            Passive => $cf{FTP_PASSIVE},
-            Debug   => "ftp" ~~ \@opt_debug,
-        ) or LOGDIE $@;
-        $ftp->login or LOGDIE $ftp->message;
-        $ftp->home($ftp->try(pwd => ()));
-        $ftp->try(binary => ());
-        $ftp->try(mkpath => $cf{FTP_DIR});
-        $ftp->try(cwd    => $cf{FTP_DIR});
-    }
+    $ftp = new FTP(
+	$cf{FTP_HOST},
+	Passive => $cf{FTP_PASSIVE},
+	Debug   => $opt_debug,
+    ) or LOGDIE $@;
+    $ftp->login or LOGDIE $ftp->message;
+    $ftp->home($ftp->try(pwd => ()));
+    $ftp->try(binary => ());
+    $ftp->try(mkpath => $cf{FTP_DIR});
+    $ftp->try(cwd    => $cf{FTP_DIR});
 
 
     # and now we can start doing something with our filesystems
@@ -195,7 +192,7 @@
             $ftp->try(cwd    => $dir);
 
             #verbose "Now in @{[$ftp->pwd]}.\n" if $ftp;
-            unlink_old_dumps($ftp, $cf{KEEP} + 1)
+            unlink_old_dumps($ftp, $cf{KEEP}, 1)
               if $opt_clean;
 
             # examine the situation and decide about the level
@@ -331,7 +328,7 @@
         update_devnames($opt_dumpdates, $dev->{dump} => $dev->{rdev})
           if $opt_dumpdates;
 
-        unlink_old_dumps($ftp, $cf{KEEP})
+        unlink_old_dumps($ftp, $cf{KEEP}, 0)
           if $ftp and $opt_clean;
     }
 
@@ -631,8 +628,8 @@
     return <$fh>;
 }
 
-sub unlink_old_dumps($$) {
-    my ($ftp, $keep) = @_;
+sub unlink_old_dumps($$$) {
+    my ($ftp, $keep, $extra) = @_;
     my @dumps;
 
     foreach ($ftp->ls) {
@@ -650,13 +647,29 @@
 
     if ($dumps[0]) {
 	@{ $dumps[0] } = sort { $a->{stamp} <=> $b->{stamp} } @{ $dumps[0] };
-	@unlink = splice(@{ $dumps[0] }, 0, -$keep);
+
+	if ($keep =~ /^\d+$/) {
+	    $keep += $extra;
+	    @unlink = splice(@{ $dumps[0] }, 0, -$keep);
+	} 
+	elsif (my ($n, $u) = ($keep =~ /^(\d+)([mhdw])$/i)) {
+	    given($u) {
+		when(/m/i) { $keep = $n * 60 };
+		when(/h/i) { $keep = $n * 3600 };
+		when(/d/i) { $keep = $n * 86400 };
+		when(/w/i) { $keep = $n * 604800 };
+	    }
+	    while (@{$dumps[0]} and $dumps[0][0]->{stamp} < (time - $keep)) {
+		push @unlink, shift @{$dumps[0]};
+	    }
+	}
+	else { LOGDIE "Can't parse the keep time ``$keep''" }
     }
 
     ### @dumps
 
-    if ($dumps[1]) {
-        if (!@{ $dumps[0] }) { push @unlink, @{ @dumps[1 .. $#dumps] } }
+    if (@dumps[1..$#dumps]) {
+        if (!$dumps[0] or !@{ $dumps[0] }) { push @unlink, @{ @dumps[1 .. $#dumps] } }
         else {
             push @unlink => grep { $_->{stamp} <= $dumps[0][0]{stamp} }
               @{ @dumps[1 .. $#dumps] };
@@ -779,7 +792,7 @@
     FTP_PASSIVE = 1
     COMPRESSION_LEVEL = 6
     FULL_CYCLE	= 7
-    KEEP        = 2
+    KEEP        = 2[unit]
     EXCLUDE:<devpath> = <exclude file>
 
 =over
@@ -816,6 +829,9 @@
 normally you'll get KEEP backups in your backup directory. Useless
 incremental backups are deleted automgically.
 
+If a Unit (m, h, d, w) is appended, we do not count the number of full backups but
+the age. (default: 7d)
+
 =item EXCLUDE
 
 Here you may configure a list of files to be excluded on a per file system base
--- a/ftbackup.conf.example	Tue May 03 13:21:56 2011 +0200
+++ b/ftbackup.conf.example	Tue May 03 16:40:13 2011 +0200
@@ -26,6 +26,6 @@
 
 # Always keep this number of (full) backups
 # (including the current one)
-# KEEP = 2
+# KEEP = 7d
 
 # EXCLUDE:<fs> = <name of exclude list>