--- trunk/bin/BackupPC_updatedb 2005/07/31 20:53:40 34 +++ trunk/bin/BackupPC_updatedb 2005/08/20 11:24:55 44 @@ -7,11 +7,27 @@ use BackupPC::View; use Data::Dumper; use Getopt::Std; +use Time::HiRes qw/time/; +use File::Pid; +use POSIX qw/strftime/; use constant BPC_FTYPE_DIR => 5; my $debug = 0; $|=1; +my $pidfile = new File::Pid; + +if (my $pid = $pidfile->running ) { + die "$0 already running: $pid\n"; +} elsif ($pidfile->pid ne $$) { + $pidfile->remove; + $pidfile = new File::Pid; +} +$pidfile->write; +print STDERR "$0 using pid ",$pidfile->pid," file ",$pidfile->file,"\n"; + +my $t_fmt = '%Y-%m-%d %H:%M:%S'; + my $hosts; my $bpc = BackupPC::Lib->new || die; my %Conf = $bpc->Conf(); @@ -24,14 +40,15 @@ my %opt; -if ( !getopts("cdm:v", \%opt ) ) { +if ( !getopts("cdm:v:", \%opt ) ) { print STDERR < $opt{m}); my $backupNum = $backup->{'num'}; my @backupShares = (); - print $hosts->{$host_key}->{'host'},"\t#$backupNum\n"; + print $hosts->{$host_key}->{'host'}, + "\t#$backupNum\t", $backup->{type} || '?', " ", + $backup->{nFilesNew} || '?', "/", $backup->{nFiles} || '?', + " files\n"; $sth->{backups_broj}->execute($hostID, $backupNum); my ($broj) = $sth->{backups_broj}->fetchrow_array(); @@ -201,11 +222,16 @@ my $files = BackupPC::View->new($bpc, $hostname, \@backups, 1); foreach my $share ($files->shareList($backupNum)) { - print "\t$share"; + my $t = time(); + + print strftime($t_fmt,localtime())," ", $share; $shareID = getShareID($share, $hostID, $hostname); - my ($f, $nf, $d, $nd) = recurseDir($bpc, $hostname, \@backups, $backupNum, $share, "", $shareID); - print " $nf/$f files $nd/$d dirs\n"; + my ($f, $nf, $d, $nd) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID); + printf(" %d/%d files %d/%d dirs [%.2f/s]\n", + $nf, $f, $nd, $d, + ( ($f+$d) / ((time() - $t) || 1) ) + ); $dbh->commit(); } @@ -223,6 +249,8 @@ $dbh->commit(); $dbh->disconnect(); +$pidfile->remove; + sub getShareID() { my ($share, $hostID, $hostname) = @_; @@ -276,16 +304,15 @@ #################################################### sub recurseDir($$$$$$$$) { - my ($bpc, $hostname, $backups, $backupNum, $share, $dir, $shareID) = @_; + my ($bpc, $hostname, $files, $backupNum, $share, $dir, $shareID) = @_; - print STDERR "recurse($hostname,$backupNum,$share,$dir,$shareID)\n" if ($debug >= 1); + print STDERR "\nrecurse($hostname,$backupNum,$share,$dir,$shareID)\n" if ($debug >= 1); my ($nr_files, $new_files, $nr_dirs, $new_dirs) = (0,0,0,0); { # scope my @stack; - my $files = BackupPC::View->new($bpc, $hostname, $backups, 1); my $filesInBackup = $files->dirAttrib($backupNum, $share, $dir); # first, add all the entries in current directory @@ -346,7 +373,7 @@ print STDERR "## STACK ",join(", ", @stack),"\n" if ($debug >= 2); while ( my $dir = shift @stack ) { - my ($f,$nf,$d,$nd) = recurseDir($bpc, $hostname, $backups, $backupNum, $share, $dir, $shareID); + my ($f,$nf,$d,$nd) = recurseDir($bpc, $hostname, $files, $backupNum, $share, $dir, $shareID); print STDERR "# $dir f: $f nf: $nf d: $d nd: $nd\n" if ($debug >= 1); $nr_files += $f; $new_files += $nf;