3 # backup_table-slave.pl: Backup mysql tables
5 # Version: v0.1b (20000223)
12 use POSIX qw(strftime);
14 my $backup_interval = 1; # every: 1,7,14,30.
16 my $backup_url = "http://achilles.nyip.net/~apt/tables.tar.bz2";
17 my $backup_file = "tables-##DATE.tar.bz2";
18 my $backup_destdir = "/home/xk/public_html/";
19 my $backup_indexfile = "tables-index.txt";
23 # Usage: &getURL($url);
28 $ua = new LWP::UserAgent;
29 $ua->proxy('http', $ENV{'http_proxy'}) if (exists $ENV{'http_proxy'});
30 $ua->proxy('http', $ENV{'HTTP_PROXY'}) if (exists $ENV{'HTTP_PROXY'});
32 $req = new HTTP::Request('GET',$url);
33 $res = $ua->request($req);
35 # return NULL upon error.
36 if ($res->is_success) {
39 print "error: failure.\n";
45 if ( -f "$backup_destdir/$backup_indexfile") {
46 if (open(INDEX, "$backup_destdir/$backup_indexfile")) {
50 # days since 1970, file.
51 if (/^(\d+) (\S+)$/) {
57 print "WARNING: can't open $backup_indexfile.\n";
60 my $now_days = (localtime)[7] + (((localtime)[5] - 70) * 365);
61 my $now_date = strftime("%Y%m%d", localtime);
63 if (scalar keys %index) {
64 my $last_days = (sort {$b <=> $a} keys %index)[0];
66 if ($now_days - $last_days < $backup_interval) {
67 print "error: shouldn't run today.\n";
72 $backup_file =~ s/##DATE/$now_date/;
73 print "backup_file => '$backup_file'.\n";
74 if ( -f $backup_file) {
75 print "error: $backup_file already exists.\n";
79 my $file = &getURL($backup_url);
80 open(OUT,">$backup_destdir/$backup_file");
84 $index{$now_days} = $backup_file;
86 my @index = sort {$b <=> $a} keys %index;
88 open(OUT,">$backup_destdir/$backup_indexfile");
89 for(my $i=0; $i<scalar(@index); $i++) {
91 print "fe: day => '$day'.\n";
93 if ($backup_count - 1 >= $i) {
94 print "DEBUG: $day $index{$day}\n";
95 print OUT "$day $index{$day}\n";
97 print "Deleting $backup_destdir/$index{$day}\n";
98 unlink "$backup_destdir/$index{$day}";