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);
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) {
31 print "error: failure.\n";
37 if ( -f "$backup_destdir/$backup_indexfile") {
38 if (open(INDEX, "$backup_destdir/$backup_indexfile")) {
42 # days since 1970, file.
43 if (/^(\d+) (\S+)$/) {
49 print "WARNING: can't open $backup_indexfile.\n";
52 my $now_days = (localtime)[7] + (((localtime)[5] - 70) * 365);
53 my $now_date = strftime("%Y%m%d", localtime);
55 if (scalar keys %index) {
56 my $last_days = (sort {$b <=> $a} keys %index)[0];
58 if ($now_days - $last_days < $backup_interval) {
59 print "error: shouldn't run today.\n";
64 $backup_file =~ s/##DATE/$now_date/;
65 print "backup_file => '$backup_file'.\n";
66 if ( -f $backup_file) {
67 print "error: $backup_file already exists.\n";
71 my $file = &getURL($backup_url);
72 open(OUT,">$backup_destdir/$backup_file");
76 $index{$now_days} = $backup_file;
78 my @index = sort {$b <=> $a} keys %index;
80 open(OUT,">$backup_destdir/$backup_indexfile");
81 for(my $i=0; $i<scalar(@index); $i++) {
83 print "fe: day => '$day'.\n";
85 if ($backup_count - 1 >= $i) {
86 print "DEBUG: $day $index{$day}\n";
87 print OUT "$day $index{$day}\n";
89 print "Deleting $index{$day}\n";
90 unlink $backup_destdir."/".$index{$day};