/[BackupPC]/trunk/bin/BackupPC_updatedb
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/bin/BackupPC_updatedb

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 14 by dpavlin, Sun Jul 10 22:56:43 2005 UTC revision 45 by dpavlin, Sat Aug 20 11:39:05 2005 UTC
# Line 7  use BackupPC::Lib; Line 7  use BackupPC::Lib;
7  use BackupPC::View;  use BackupPC::View;
8  use Data::Dumper;  use Data::Dumper;
9  use Getopt::Std;  use Getopt::Std;
10    use Time::HiRes qw/time/;
11    use File::Pid;
12    use POSIX qw/strftime/;
13  use constant BPC_FTYPE_DIR => 5;  use constant BPC_FTYPE_DIR => 5;
14    
15    my $debug = 0;
16  $|=1;  $|=1;
17    
18    my $pidfile = new File::Pid;
19    
20    if (my $pid = $pidfile->running ) {
21            die "$0 already running: $pid\n";
22    } elsif ($pidfile->pid ne $$) {
23            $pidfile->remove;
24            $pidfile = new File::Pid;
25    }
26    $pidfile->write;
27    print STDERR "$0 using pid ",$pidfile->pid," file ",$pidfile->file,"\n";
28    
29    my $t_fmt = '%Y-%m-%d %H:%M:%S';
30    
31  my $hosts;  my $hosts;
32  my $bpc = BackupPC::Lib->new || die;  my $bpc = BackupPC::Lib->new || die;
33  my %Conf = $bpc->Conf();  my %Conf = $bpc->Conf();
# Line 23  my $dbh = DBI->connect($dsn, "", "", { R Line 40  my $dbh = DBI->connect($dsn, "", "", { R
40    
41  my %opt;  my %opt;
42    
43  if ( !getopts("cdm:", \%opt ) ) {  if ( !getopts("cdm:v:", \%opt ) ) {
44          print STDERR <<EOF;          print STDERR <<EOF;
45  usage: $0 [-c|-d] [-m num]  usage: $0 [-c|-d] [-m num] [-v|-v level]
46    
47  Options:  Options:
48          -c      create database on first use          -c      create database on first use
49          -d      delete database before import          -d      delete database before import
50          -m num  import just num increments for one host          -m num  import just num increments for one host
51            -v num  set verbosity (debug) level (default $debug)
52  EOF  EOF
53          exit 1;          exit 1;
54  }  }
# Line 81  if ($opt{c}) { Line 99  if ($opt{c}) {
99                  create table files (                  create table files (
100                          ID      INTEGER         NOT NULL PRIMARY KEY,                            ID      INTEGER         NOT NULL PRIMARY KEY,  
101                          shareID INTEGER         NOT NULL references shares(id),                          shareID INTEGER         NOT NULL references shares(id),
102                          backupNum  INTEGER      NOT NULL references backups(id),                          backupNum  INTEGER      NOT NULL references backups(num),
103                          name       VARCHAR(255) NOT NULL,                          name       VARCHAR(255) NOT NULL,
104                          path       VARCHAR(255) NOT NULL,                          path       VARCHAR(255) NOT NULL,
105                          fullpath   VARCHAR(255) NOT NULL,                          fullpath   VARCHAR(255) NOT NULL,
# Line 122  if ($opt{d}) { Line 140  if ($opt{d}) {
140          print " done...\n";          print " done...\n";
141  }  }
142    
143    if ($opt{v}) {
144            print "Debug level at $opt{v}\n";
145            $debug = $opt{v};
146    }
147    
148  #################################INSERT VALUES#############################  #################################INSERT VALUES#############################
149    
150  # get hosts  # get hosts
# Line 180  foreach my $host_key (keys %{$hosts}) { Line 203  foreach my $host_key (keys %{$hosts}) {
203          my $inc_nr = 0;          my $inc_nr = 0;
204    
205          foreach my $backup (@backups) {          foreach my $backup (@backups) {
206    
207                  $inc_nr++;                  $inc_nr++;
208                  last if ($opt{m} && $inc_nr > $opt{m});                  last if ($opt{m} && $inc_nr > $opt{m});
209    
210                  my $backupNum = $backup->{'num'};                  my $backupNum = $backup->{'num'};
211                  my @backupShares = ();                  my @backupShares = ();
212    
213                  print $hosts->{$host_key}->{'host'},"\t$backupNum\n";                  print $hosts->{$host_key}->{'host'},
214                            "\t#$backupNum\t", $backup->{type} || '?', " ",
215                            $backup->{nFilesNew} || '?', "/", $backup->{nFiles} || '?',
216                            " files\n";
217    
218                  $sth->{backups_broj}->execute($hostID, $backupNum);                  $sth->{backups_broj}->execute($hostID, $backupNum);
219                  my ($broj) = $sth->{backups_broj}->fetchrow_array();                  my ($broj) = $sth->{backups_broj}->fetchrow_array();
220                  next if ($broj > 0);                  next if ($broj > 0);
221    
222                    my $files = BackupPC::View->new($bpc, $hostname, \@backups, 1);
223                    foreach my $share ($files->shareList($backupNum)) {
224    
225                            my $t = time();
226    
227                            print strftime($t_fmt,localtime())," ", $share;
228                            $shareID = getShareID($share, $hostID, $hostname);
229                    
230                            my ($f, $nf, $d, $nd) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID);
231                            printf(" %d/%d files %d/%d dirs [%.2f/s]\n",
232                                    $nf, $f, $nd, $d,
233                                    ( ($f+$d) / ((time() - $t) || 1) )
234                            );
235                            $dbh->commit();
236                    }
237    
238                  $sth->{insert_backups}->execute(                  $sth->{insert_backups}->execute(
239                          $hostID,                          $hostID,
240                          $backupNum,                          $backupNum,
# Line 200  foreach my $host_key (keys %{$hosts}) { Line 243  foreach my $host_key (keys %{$hosts}) {
243                  );                  );
244                  $dbh->commit();                  $dbh->commit();
245    
                 my $files = BackupPC::View->new($bpc, $hostname, \@backups);  
                 foreach my $share ($files->shareList($backupNum)) {  
   
                         print "\t$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";  
                         $dbh->commit();  
                 }  
246          }          }
247  }  }
248  undef $sth;  undef $sth;
249  $dbh->commit();  $dbh->commit();
250  $dbh->disconnect();  $dbh->disconnect();
251    
252    $pidfile->remove;
253    
254  sub getShareID() {  sub getShareID() {
255    
256          my ($share, $hostID, $hostname) = @_;          my ($share, $hostID, $hostname) = @_;
# Line 236  sub getShareID() { Line 271  sub getShareID() {
271                  VALUES (?,?,?,?)                  VALUES (?,?,?,?)
272          });          });
273    
274          $sth->{insert_share}->execute($hostID,$share, $hostname . $share,undef);          my $drop_down = $hostname . '/' . $share;
275            $drop_down =~ s#//+#/#g;
276    
277            $sth->{insert_share}->execute($hostID,$share, $drop_down ,undef);
278          return $dbh->func('last_insert_rowid');                  return $dbh->func('last_insert_rowid');        
279  }  }
280    
# Line 256  sub found_in_db { Line 294  sub found_in_db {
294          my @param = ($shareID,$path,$name,$date,$size);          my @param = ($shareID,$path,$name,$date,$size);
295          $sth->{file_in_db}->execute(@param);          $sth->{file_in_db}->execute(@param);
296          my ($rows) = $sth->{file_in_db}->fetchrow_array();          my ($rows) = $sth->{file_in_db}->fetchrow_array();
297          print STDERR ( $rows ? '+' : '-' ), join(" ",@param), "\n";          print STDERR "## found_in_db ",( $rows ? '+' : '-' ), join(" ",@param), "\n" if ($debug >= 3);
298          return $rows;          return $rows;
299  }  }
300    
# Line 266  sub found_in_db { Line 304  sub found_in_db {
304  ####################################################  ####################################################
305  sub recurseDir($$$$$$$$) {  sub recurseDir($$$$$$$$) {
306    
307          my ($bpc, $hostname, $backups, $backupNum, $share, $dir, $shareID) = @_;          my ($bpc, $hostname, $files, $backupNum, $share, $dir, $shareID) = @_;
308    
309          my ($nr_files, $new_files, $nr_dirs, $new_dirs) = (0,0,0,0);          print STDERR "\nrecurse($hostname,$backupNum,$share,$dir,$shareID)\n" if ($debug >= 1);
310    
311          my $files = BackupPC::View->new($bpc, $hostname, $backups);                      my ($nr_files, $new_files, $nr_dirs, $new_dirs) = (0,0,0,0);
         my $filesInBackup = $files->dirAttrib($backupNum, $share, $dir);  
312    
313          # first, add all the entries in current directory          { # scope
314          foreach my $path_key (keys %{$filesInBackup}) {                  my @stack;
                 my @data = (  
                         $shareID,  
                         $backupNum,  
                         $path_key,  
                         $filesInBackup->{$path_key}->{'relPath'},  
                         $filesInBackup->{$path_key}->{'fullPath'},  
 #                       $filesInBackup->{$path_key}->{'sharePathM'},  
                         $filesInBackup->{$path_key}->{'mtime'},  
                         $filesInBackup->{$path_key}->{'type'},  
                         $filesInBackup->{$path_key}->{'size'}  
                 );  
315    
316                  my $key = join(" ", (                  print STDERR "# dirAttrib($backupNum, $share, $dir)\n" if ($debug >= 2);
317                          $shareID,                  my $filesInBackup = $files->dirAttrib($backupNum, $share, $dir);
                         $dir,  
                         $path_key,  
                         $filesInBackup->{$path_key}->{'mtime'},  
                         $filesInBackup->{$path_key}->{'size'}  
                 ));  
318    
319                    # first, add all the entries in current directory
320                    foreach my $path_key (keys %{$filesInBackup}) {
321                            my @data = (
322                                    $shareID,
323                                    $backupNum,
324                                    $path_key,
325                                    $filesInBackup->{$path_key}->{'relPath'},
326                                    $filesInBackup->{$path_key}->{'fullPath'},
327            #                       $filesInBackup->{$path_key}->{'sharePathM'},
328                                    $filesInBackup->{$path_key}->{'mtime'},
329                                    $filesInBackup->{$path_key}->{'type'},
330                                    $filesInBackup->{$path_key}->{'size'}
331                            );
332    
333                            my $key = join(" ", (
334                                    $shareID,
335                                    $dir,
336                                    $path_key,
337                                    $filesInBackup->{$path_key}->{'mtime'},
338                                    $filesInBackup->{$path_key}->{'size'}
339                            ));
340    
341    
342                            if (! $beenThere->{$key} && ! found_in_db(@data)) {
343                                    print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2);
344                                    $sth->{'insert_files'}->execute(@data);
345                                    if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {
346                                            $new_dirs++;
347                                            print STDERR " dir\n" if ($debug >= 2);
348                                    } else {
349                                            $new_files++;
350                                            print STDERR " file\n" if ($debug >= 2);
351                                    }
352                            }
353                            $beenThere->{$key}++;
354    
                 if (! $beenThere->{$key} && ! found_in_db(@data)) {  
                         $sth->{'insert_files'}->execute(@data);  
                         print STDERR "$key\n";  
355                          if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {                          if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {
356                                  $new_dirs++;                                  $nr_dirs++;
357    
358                                    my $full_path = $dir . '/' . $path_key;
359                                    push @stack, $full_path;
360                                    print STDERR "### store to stack: $full_path\n" if ($debug >= 3);
361    
362    #                               my ($f,$nf,$d,$nd) = recurseDir($bpc, $hostname, $backups, $backupNum, $share, $path_key, $shareID) unless ($beenThere->{$key});
363    #
364    #                               $nr_files += $f;
365    #                               $new_files += $nf;
366    #                               $nr_dirs += $d;
367    #                               $new_dirs += $nd;
368    
369                          } else {                          } else {
370                                  $new_files++;                                  $nr_files++;
371                          }                          }
372                  }                  }
                 $beenThere->{$key}++;  
373    
374                  if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {                  print STDERR "## STACK ",join(", ", @stack),"\n" if ($debug >= 2);
                         $nr_dirs++;  
   
                         my ($f,$nf,$d,$nd) = recurseDir($bpc, $hostname, $backups, $backupNum, $share, $path_key, $shareID);  
375    
376                    while ( my $dir = shift @stack ) {
377                            my ($f,$nf,$d,$nd) = recurseDir($bpc, $hostname, $files, $backupNum, $share, $dir, $shareID);
378                            print STDERR "# $dir f: $f nf: $nf d: $d nd: $nd\n" if ($debug >= 1);
379                          $nr_files += $f;                          $nr_files += $f;
380                          $new_files += $nf;                          $new_files += $nf;
381                          $nr_dirs += $d;                          $nr_dirs += $d;
382                          $new_dirs += $nd;                          $new_dirs += $nd;
   
                 } else {  
                         $nr_files++;  
383                  }                  }
384          }          }
385    

Legend:
Removed from v.14  
changed lines
  Added in v.45

  ViewVC Help
Powered by ViewVC 1.1.26