r11636@llin: dpavlin | 2005-12-12 14:53:13 +0100
[BackupPC.git] / bin / BackupPC_updatedb
index 233f4ee..1b254b2 100755 (executable)
@@ -12,6 +12,7 @@ use Time::HiRes qw/time/;
 use File::Pid;
 use POSIX qw/strftime/;
 use BackupPC::SearchLib;
+use Cwd qw/abs_path/;
 
 use constant BPC_FTYPE_DIR => 5;
 use constant EST_CHUNK => 100000;
@@ -24,7 +25,12 @@ $|=1;
 
 my $start_t = time();
 
-my $pidfile = new File::Pid;
+my $pid_path = abs_path($0);
+$pid_path =~ s/\W+/_/g;
+
+my $pidfile = new File::Pid({
+       file => "/tmp/$pid_path",
+});
 
 if (my $pid = $pidfile->running ) {
        die "$0 already running: $pid\n";
@@ -32,8 +38,8 @@ if (my $pid = $pidfile->running ) {
        $pidfile->remove;
        $pidfile = new File::Pid;
 }
-$pidfile->write;
 print STDERR "$0 using pid ",$pidfile->pid," file ",$pidfile->file,"\n";
+$pidfile->write;
 
 my $t_fmt = '%Y-%m-%d %H:%M:%S';
 
@@ -121,11 +127,14 @@ sub hest_update {
 
        my $skip_check = $opt{j} && print STDERR "Skipping check for existing files -- this should be used only with initital import\n";
 
-       unless ($use_hest) {
+       unless (defined($use_hest)) {
                print STDERR "HyperEstraier support not enabled in configuration\n";
+               $use_hest = 0;
                return;
        }
 
+       return unless($use_hest);
+
        print curr_time," updating HyperEstraier:";
 
        my $t = time();
@@ -458,7 +467,10 @@ INSERT INTO files
        VALUES (?,?,?,?,?,?,?)
 });
 
-foreach my $host_key (keys %{$hosts}) {
+my @hosts = keys %{$hosts};
+my $host_nr = 0;
+
+foreach my $host_key (@hosts) {
 
        my $hostname = $hosts->{$host_key}->{'host'} || die "can't find host for $host_key";
 
@@ -473,7 +485,9 @@ foreach my $host_key (keys %{$hosts}) {
                $hostID = $dbh->last_insert_id(undef,undef,'hosts',undef);
        }
 
-       print "host ".$hosts->{$host_key}->{'host'}.": ";
+       $host_nr++;
+       print "host ", $hosts->{$host_key}->{'host'}, " [", 
+               $host_nr, "/", ($#hosts + 1), "]: ";
  
        # get backups for a host
        my @backups = $bpc->BackupInfoRead($hostname);
@@ -525,17 +539,22 @@ foreach my $host_key (keys %{$hosts}) {
 
                        my ($f, $nf, $d, $nd, $size) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID);
 
-                       $sth->{update_backups_size}->execute(
-                               $size,
-                               $hostID,
-                               $backupNum,
-                               $backup->{'endTime'},
-                               substr($backup->{'type'},0,4),
-                               $shareID,
-                       );
-
-                       print " commit";
-                       $dbh->commit();
+                       eval {
+                               $sth->{update_backups_size}->execute(
+                                       $size,
+                                       $hostID,
+                                       $backupNum,
+                                       $backup->{'endTime'},
+                                       substr($backup->{'type'},0,4),
+                                       $shareID,
+                               );
+                               print " commit";
+                               $dbh->commit();
+                       };
+                       if ($@) {
+                               print " rollback";
+                               $dbh->rollback();
+                       }
 
                        my $dur = (time() - $t) || 1;
                        printf(" %d/%d files %d/%d dirs %0.2f MB [%.2f/s dur: %s]\n",