]> git.donarmstrong.com Git - infobot.git/blob - scripts/backup_table-slave.pl
dunno
[infobot.git] / scripts / backup_table-slave.pl
1 #!/usr/bin/perl -w
2 #
3 # backup_table-slave.pl: Backup mysql tables
4 #     Author: dms
5 #    Version: v0.1b (20000223)
6 #    Created: 20000210
7 #
8
9
10 use strict;
11 use LWP;
12 use POSIX qw(strftime);
13
14 my $backup_interval = 1;    # every: 1,7,14,30.
15 my $backup_count    = 7;
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";
20
21 my %index;
22
23 # Usage: &getURL($url);
24 sub getURL {
25     my ($url) = @_;
26     my ( $ua, $res, $req );
27
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'} );
31
32     $req = new HTTP::Request( 'GET', $url );
33     $res = $ua->request($req);
34
35     # return NULL upon error.
36     if ( $res->is_success ) {
37         return $res->content;
38     }
39     else {
40         print "error: failure.\n";
41         exit 1;
42     }
43 }
44
45 #...
46 if ( -f "$backup_destdir/$backup_indexfile" ) {
47     if ( open( INDEX, "$backup_destdir/$backup_indexfile" ) ) {
48         while (<INDEX>) {
49             chop;
50
51             # days since 1970, file.
52             if (/^(\d+) (\S+)$/) {
53                 $index{$1} = $2;
54             }
55         }
56         close INDEX;
57     }
58     else {
59         print "WARNING: can't open $backup_indexfile.\n";
60     }
61 }
62 my $now_days = (localtime)[7] + ( ( (localtime)[5] - 70 ) * 365 );
63 my $now_date = strftime( "%Y%m%d", localtime );
64
65 if ( scalar keys %index ) {
66     my $last_days = ( sort { $b <=> $a } keys %index )[0];
67
68     if ( $now_days - $last_days < $backup_interval ) {
69         print "error: shouldn't run today.\n";
70         goto recycle;
71     }
72 }
73
74 $backup_file =~ s/##DATE/$now_date/;
75 print "backup_file => '$backup_file'.\n";
76 if ( -f $backup_file ) {
77     print "error: $backup_file already exists.\n";
78     exit 1;
79 }
80
81 my $file = &getURL($backup_url);
82 open( OUT, ">$backup_destdir/$backup_file" );
83 print OUT $file;
84 close OUT;
85
86 $index{$now_days} = $backup_file;
87 recycle:;
88 my @index = sort { $b <=> $a } keys %index;
89
90 open( OUT, ">$backup_destdir/$backup_indexfile" );
91 for ( my $i = 0 ; $i < scalar(@index) ; $i++ ) {
92     my $day = $index[$i];
93     print "fe: day => '$day'.\n";
94
95     if ( $backup_count - 1 >= $i ) {
96         print "DEBUG: $day $index{$day}\n";
97         print OUT "$day $index{$day}\n";
98     }
99     else {
100         print "Deleting $backup_destdir/$index{$day}\n";
101         unlink "$backup_destdir/$index{$day}";
102     }
103 }
104 close OUT;
105
106 print "Done.\n";
107
108 # vim:ts=4:sw=4:expandtab:tw=80