aboutsummaryrefslogtreecommitdiffstats
path: root/zbackup.pl
blob: a47b22f7635befe8bc4b4e05d60760bcd615424a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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;
}