X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=scripts%2Fbackup_table-slave.pl;h=133fd74742ff54fdd9c514f20c8822e231bf737f;hb=45e51047c2df703229843193e49fb9829489170f;hp=bc7cbc7d04c4c6c7fe9141b1336da1eab89e7633;hpb=1c0a2202fe58159dd4d4197c4ff8b21f9387685e;p=infobot.git diff --git a/scripts/backup_table-slave.pl b/scripts/backup_table-slave.pl index bc7cbc7..133fd74 100755 --- a/scripts/backup_table-slave.pl +++ b/scripts/backup_table-slave.pl @@ -11,93 +11,98 @@ use strict; use LWP; use POSIX qw(strftime); -my $backup_interval = 1; # every: 1,7,14,30. -my $backup_count = 7; -my $backup_url = "http://achilles.nyip.net/~apt/tables.tar.bz2"; -my $backup_file = "tables-##DATE.tar.bz2"; -my $backup_destdir = "/home/xk/public_html/"; -my $backup_indexfile = "tables-index.txt"; +my $backup_interval = 1; # every: 1,7,14,30. +my $backup_count = 7; +my $backup_url = "http://achilles.nyip.net/~apt/tables.tar.bz2"; +my $backup_file = "tables-##DATE.tar.bz2"; +my $backup_destdir = "/home/xk/public_html/"; +my $backup_indexfile = "tables-index.txt"; my %index; # Usage: &getURL($url); sub getURL { my ($url) = @_; - my ($ua,$res,$req); + my ( $ua, $res, $req ); $ua = new LWP::UserAgent; - $ua->proxy('http', $ENV{'http_proxy'}) if (exists $ENV{'http_proxy'}); - $ua->proxy('http', $ENV{'HTTP_PROXY'}) if (exists $ENV{'HTTP_PROXY'}); + $ua->proxy( 'http', $ENV{'http_proxy'} ) if ( exists $ENV{'http_proxy'} ); + $ua->proxy( 'http', $ENV{'HTTP_PROXY'} ) if ( exists $ENV{'HTTP_PROXY'} ); - $req = new HTTP::Request('GET',$url); + $req = new HTTP::Request( 'GET', $url ); $res = $ua->request($req); # return NULL upon error. - if ($res->is_success) { - return $res->content; - } else { - print "error: failure.\n"; - exit 1; + if ( $res->is_success ) { + return $res->content; + } + else { + print "error: failure.\n"; + exit 1; } } #... -if ( -f "$backup_destdir/$backup_indexfile") { - if (open(INDEX, "$backup_destdir/$backup_indexfile")) { - while () { - chop; - - # days since 1970, file. - if (/^(\d+) (\S+)$/) { - $index{$1} = $2; - } - } - close INDEX; - } else { - print "WARNING: can't open $backup_indexfile.\n"; +if ( -f "$backup_destdir/$backup_indexfile" ) { + if ( open( INDEX, "$backup_destdir/$backup_indexfile" ) ) { + while () { + chop; + + # days since 1970, file. + if (/^(\d+) (\S+)$/) { + $index{$1} = $2; + } + } + close INDEX; + } + else { + print "WARNING: can't open $backup_indexfile.\n"; } } -my $now_days = (localtime)[7] + (((localtime)[5] - 70) * 365); -my $now_date = strftime("%Y%m%d", localtime); +my $now_days = (localtime)[7] + ( ( (localtime)[5] - 70 ) * 365 ); +my $now_date = strftime( "%Y%m%d", localtime ); -if (scalar keys %index) { - my $last_days = (sort {$b <=> $a} keys %index)[0]; +if ( scalar keys %index ) { + my $last_days = ( sort { $b <=> $a } keys %index )[0]; - if ($now_days - $last_days < $backup_interval) { - print "error: shouldn't run today.\n"; - goto recycle; + if ( $now_days - $last_days < $backup_interval ) { + print "error: shouldn't run today.\n"; + goto recycle; } } $backup_file =~ s/##DATE/$now_date/; print "backup_file => '$backup_file'.\n"; -if ( -f $backup_file) { +if ( -f $backup_file ) { print "error: $backup_file already exists.\n"; exit 1; } my $file = &getURL($backup_url); -open(OUT,">$backup_destdir/$backup_file"); +open( OUT, ">$backup_destdir/$backup_file" ); print OUT $file; close OUT; $index{$now_days} = $backup_file; recycle:; -my @index = sort {$b <=> $a} keys %index; +my @index = sort { $b <=> $a } keys %index; -open(OUT,">$backup_destdir/$backup_indexfile"); -for(my $i=0; $i$backup_destdir/$backup_indexfile" ); +for ( my $i = 0 ; $i < scalar(@index) ; $i++ ) { my $day = $index[$i]; print "fe: day => '$day'.\n"; - if ($backup_count - 1 >= $i) { - print "DEBUG: $day $index{$day}\n"; - print OUT "$day $index{$day}\n"; - } else { - print "Deleting $backup_destdir/$index{$day}\n"; - unlink "$backup_destdir/$index{$day}"; + if ( $backup_count - 1 >= $i ) { + print "DEBUG: $day $index{$day}\n"; + print OUT "$day $index{$day}\n"; + } + else { + print "Deleting $backup_destdir/$index{$day}\n"; + unlink "$backup_destdir/$index{$day}"; } } close OUT; print "Done.\n"; + +# vim:ts=4:sw=4:expandtab:tw=80