5 use POSIX qw(strftime);
7 my $backup_interval = 1; # every: 1,7,14,30.
9 my $backup_url = "http://core.junker.org/~apt/tables.tar.bz2";
10 my $backup_file = "tables-##DATE.tar.bz2";
11 my $backup_destdir = "/home/xk/public_html/";
12 my $backup_indexfile = "tables-index.txt";
16 # Usage: &getURL($url);
19 my ( $ua, $res, $req );
21 $ua = new LWP::UserAgent;
22 ### $ua->proxy('http', $proxy);
24 $req = new HTTP::Request( 'GET', $url );
25 $res = $ua->request($req);
27 # return NULL upon error.
28 if ( $res->is_success ) {
32 print "error: failure.\n";
38 if ( -f "$backup_destdir/$backup_indexfile" ) {
39 if ( open( INDEX, "$backup_destdir/$backup_indexfile" ) ) {
43 # days since 1970, file.
44 if (/^(\d+) (\S+)$/) {
51 print "WARNING: can't open $backup_indexfile.\n";
54 my $now_days = (localtime)[7] + ( ( (localtime)[5] - 70 ) * 365 );
55 my $now_date = strftime( "%Y%m%d", localtime );
57 if ( scalar keys %index ) {
58 my $last_days = ( sort { $b <=> $a } keys %index )[0];
60 if ( $now_days - $last_days < $backup_interval ) {
61 print "error: shouldn't run today.\n";
66 $backup_file =~ s/##DATE/$now_date/;
67 print "backup_file => '$backup_file'.\n";
68 if ( -f $backup_file ) {
69 print "error: $backup_file already exists.\n";
73 my $file = &getURL($backup_url);
74 open( OUT, ">$backup_destdir/$backup_file" );
78 $index{$now_days} = $backup_file;
80 my @index = sort { $b <=> $a } keys %index;
82 open( OUT, ">$backup_destdir/$backup_indexfile" );
83 for ( my $i = 0 ; $i < scalar(@index) ; $i++ ) {
85 print "fe: day => '$day'.\n";
87 if ( $backup_count - 1 >= $i ) {
88 print "DEBUG: $day $index{$day}\n";
89 print OUT "$day $index{$day}\n";
92 print "Deleting $index{$day}\n";
93 unlink $backup_destdir . "/" . $index{$day};
100 # vim:ts=4:sw=4:expandtab:tw=80