aboutsummaryrefslogtreecommitdiffstats
path: root/zbackup.pl
diff options
context:
space:
mode:
authorBrian Cully <bjc@kublai.com>2017-10-11 23:36:34 -0400
committerBrian Cully <bjc@kublai.com>2017-10-12 00:01:17 -0400
commitb6e77390279ca0b5ae4953df8f651311420f6238 (patch)
tree95c91c179d3ca2b62fe5922213133cc740441d93 /zbackup.pl
downloadzfs-scripts-b6e77390279ca0b5ae4953df8f651311420f6238.tar.gz
zfs-scripts-b6e77390279ca0b5ae4953df8f651311420f6238.zip
Initial commit.
Diffstat (limited to 'zbackup.pl')
-rwxr-xr-xzbackup.pl47
1 files changed, 47 insertions, 0 deletions
diff --git a/zbackup.pl b/zbackup.pl
new file mode 100755
index 0000000..a47b22f
--- /dev/null
+++ b/zbackup.pl
@@ -0,0 +1,47 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use Date::Calc qw(Today_and_Now Delta_YMD Date_to_Time);
+
+# Time machine policy
+# hourly for last 24 hours, daily for month, weekly for everything else.
+
+my $FUDGE = 900; # Number of seconds to allow for something to be w/i a day.
+
+my %vols;
+my %today;
+($today{year}, $today{mon}, $today{day}, $today{hour}, $today{min}, $today{sec}) = Today_and_Now();
+my $now = Date_to_Time($today{year}, $today{mon}, $today{day}, $today{hour}, $today{min}, $today{sec});
+
+while (my $line = <>) {
+ next unless $line =~ /([^@]+)@(\d{4})-(\d{2})-(\d{2})-(\d{2})(\d{2})(\d{2})/;
+ my($volname, $year, $mon, $day, $hour, $min, $sec) = ($1, $2, $3, $4, $5, $6, $7);
+ my $then = Date_to_Time($year, $mon, $day, $hour, $min, $sec);
+
+ my %volinfo;
+ %volinfo = %{$vols{$volname}} if defined($vols{$volname});
+
+ my $shouldkeep = 0;
+ my($dy, $dm, $dd) = Delta_YMD($year, $mon, $day, $today{year}, $today{mon}, $today{day});
+ $dm += $dy * 12;
+ if ($now - $then <= (86400 + $FUDGE)) {
+ # Keep everything less than a day old.
+ $shouldkeep = 1;
+ } elsif ($dm == 0 || ($dm == 1 && $dd >= 0)) {
+ # Less than a month old: only keep dailies.
+ if (!$volinfo{firstday} || $then - $volinfo{firstday} >= 86400) {
+ $volinfo{firstday} = $then;
+ $shouldkeep = 1;
+ }
+ } else {
+ # More than a month old: keep weeklies.
+ if (!$volinfo{firstweek} || $then - $volinfo{firstweek} >= (86400 * 7 - $FUDGE)) {
+ $volinfo{firstweek} = $then;
+ $shouldkeep = 1;
+ }
+ }
+
+ $vols{$volname} = \%volinfo;
+ print $line unless $shouldkeep;
+}