--- trunk/bin/BackupPC_updatedb 2005/08/19 14:57:30 37 +++ trunk/bin/BackupPC_updatedb 2005/08/20 14:13:58 48 @@ -1,19 +1,33 @@ #!/usr/local/bin/perl -w use strict; -use DBI; use lib "__INSTALLDIR__/lib"; + +use DBI; use BackupPC::Lib; 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; @@ -28,14 +42,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(); @@ -232,6 +251,8 @@ $dbh->commit(); $dbh->disconnect(); +$pidfile->remove; + sub getShareID() { my ($share, $hostID, $hostname) = @_; @@ -261,10 +282,15 @@ sub found_in_db { - my ($shareID,undef,$name,$path,undef,$date,undef,$size) = @_; + my @data = @_; + shift @data; + + my ($key, $shareID,undef,$name,$path,undef,$date,undef,$size) = @_; + + return $beenThere->{$key} if (defined($beenThere->{$key})); $sth->{file_in_db} ||= $dbh->prepare(qq{ - SELECT count(*) FROM files + SELECT 1 FROM files WHERE shareID = ? and path = ? and name = ? and @@ -274,8 +300,12 @@ my @param = ($shareID,$path,$name,$date,$size); $sth->{file_in_db}->execute(@param); - my ($rows) = $sth->{file_in_db}->fetchrow_array(); -# print STDERR ( $rows ? '+' : '-' ), join(" ",@param), "\n"; + my $rows = $sth->{file_in_db}->rows; + print STDERR "## found_in_db ",( $rows ? '+' : '-' ), join(" ",@param), "\n" if ($debug >= 3); + + $beenThere->{$key}++; + + $sth->{'insert_files'}->execute(@data) unless ($rows); return $rows; } @@ -287,13 +317,14 @@ 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; + print STDERR "# dirAttrib($backupNum, $share, $dir)\n" if ($debug >= 2); my $filesInBackup = $files->dirAttrib($backupNum, $share, $dir); # first, add all the entries in current directory @@ -319,9 +350,9 @@ )); - if (! $beenThere->{$key} && ! found_in_db(@data)) { + if (! defined($beenThere->{$key}) && ! found_in_db($key, @data)) { print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2); - $sth->{'insert_files'}->execute(@data); + if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) { $new_dirs++; print STDERR " dir\n" if ($debug >= 2); @@ -330,7 +361,6 @@ print STDERR " file\n" if ($debug >= 2); } } - $beenThere->{$key}++; if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) { $nr_dirs++;