/[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 15 by dpavlin, Mon Jul 11 00:07:25 2005 UTC revision 38 by dpavlin, Fri Aug 19 15:27:27 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            $pidfile->write;
26            print STDERR "$0 using pid ",$pidfile->pid," file ",$pidfile->file,"\n";
27    }
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]
46    
# Line 81  if ($opt{c}) { Line 98  if ($opt{c}) {
98                  create table files (                  create table files (
99                          ID      INTEGER         NOT NULL PRIMARY KEY,                            ID      INTEGER         NOT NULL PRIMARY KEY,  
100                          shareID INTEGER         NOT NULL references shares(id),                          shareID INTEGER         NOT NULL references shares(id),
101                          backupNum  INTEGER      NOT NULL references backups(id),                          backupNum  INTEGER      NOT NULL references backups(num),
102                          name       VARCHAR(255) NOT NULL,                          name       VARCHAR(255) NOT NULL,
103                          path       VARCHAR(255) NOT NULL,                          path       VARCHAR(255) NOT NULL,
104                          fullpath   VARCHAR(255) NOT NULL,                          fullpath   VARCHAR(255) NOT NULL,
# Line 122  if ($opt{d}) { Line 139  if ($opt{d}) {
139          print " done...\n";          print " done...\n";
140  }  }
141    
142    if ($opt{v}) {
143            print "Debug level at $opt{v}\n";
144            $debug = $opt{v};
145    }
146    
147  #################################INSERT VALUES#############################  #################################INSERT VALUES#############################
148    
149  # get hosts  # get hosts
# Line 186  foreach my $host_key (keys %{$hosts}) { Line 208  foreach my $host_key (keys %{$hosts}) {
208                  my $backupNum = $backup->{'num'};                  my $backupNum = $backup->{'num'};
209                  my @backupShares = ();                  my @backupShares = ();
210    
211                  print $hosts->{$host_key}->{'host'},"\t#$backupNum\n";                  print $hosts->{$host_key}->{'host'}, "\t#$backupNum\n";
212    
213                  $sth->{backups_broj}->execute($hostID, $backupNum);                  $sth->{backups_broj}->execute($hostID, $backupNum);
214                  my ($broj) = $sth->{backups_broj}->fetchrow_array();                  my ($broj) = $sth->{backups_broj}->fetchrow_array();
215                  next if ($broj > 0);                  next if ($broj > 0);
216    
217                    my $files = BackupPC::View->new($bpc, $hostname, \@backups, 1);
218                    foreach my $share ($files->shareList($backupNum)) {
219    
220                            my $t = time();
221    
222                            print strftime($t_fmt,localtime())," ", $share;
223                            $shareID = getShareID($share, $hostID, $hostname);
224                    
225                            my ($f, $nf, $d, $nd) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID);
226                            printf(" %d/%d files %d/%d dirs [%.2f/s]\n",
227                                    $nf, $f, $nd, $d,
228                                    ( ($f+$d) / ((time() - $t) || 1) )
229                            );
230                            $dbh->commit();
231                    }
232    
233                  $sth->{insert_backups}->execute(                  $sth->{insert_backups}->execute(
234                          $hostID,                          $hostID,
235                          $backupNum,                          $backupNum,
# Line 200  foreach my $host_key (keys %{$hosts}) { Line 238  foreach my $host_key (keys %{$hosts}) {
238                  );                  );
239                  $dbh->commit();                  $dbh->commit();
240    
                 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();  
                 }  
241          }          }
242  }  }
243  undef $sth;  undef $sth;
244  $dbh->commit();  $dbh->commit();
245  $dbh->disconnect();  $dbh->disconnect();
246    
247    $pidfile->remove;
248    
249  sub getShareID() {  sub getShareID() {
250    
251          my ($share, $hostID, $hostname) = @_;          my ($share, $hostID, $hostname) = @_;
# Line 236  sub getShareID() { Line 266  sub getShareID() {
266                  VALUES (?,?,?,?)                  VALUES (?,?,?,?)
267          });          });
268    
269          $sth->{insert_share}->execute($hostID,$share, $hostname . $share,undef);          my $drop_down = $hostname . '/' . $share;
270            $drop_down =~ s#//+#/#g;
271    
272            $sth->{insert_share}->execute($hostID,$share, $drop_down ,undef);
273          return $dbh->func('last_insert_rowid');                  return $dbh->func('last_insert_rowid');        
274  }  }
275    
# Line 266  sub found_in_db { Line 299  sub found_in_db {
299  ####################################################  ####################################################
300  sub recurseDir($$$$$$$$) {  sub recurseDir($$$$$$$$) {
301    
302          my ($bpc, $hostname, $backups, $backupNum, $share, $dir, $shareID) = @_;          my ($bpc, $hostname, $files, $backupNum, $share, $dir, $shareID) = @_;
303    
304            print STDERR "recurse($hostname,$backupNum,$share,$dir,$shareID)\n" if ($debug >= 1);
305    
306          my ($nr_files, $new_files, $nr_dirs, $new_dirs) = (0,0,0,0);          my ($nr_files, $new_files, $nr_dirs, $new_dirs) = (0,0,0,0);
307    
308          my $files = BackupPC::View->new($bpc, $hostname, $backups);                      { # scope
309          my $filesInBackup = $files->dirAttrib($backupNum, $share, $dir);                  my @stack;
310    
311          # first, add all the entries in current directory                  my $filesInBackup = $files->dirAttrib($backupNum, $share, $dir);
         foreach my $path_key (keys %{$filesInBackup}) {  
                 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'}  
                 );  
   
                 my $key = join(" ", (  
                         $shareID,  
                         $dir,  
                         $path_key,  
                         $filesInBackup->{$path_key}->{'mtime'},  
                         $filesInBackup->{$path_key}->{'size'}  
                 ));  
312    
313                    # first, add all the entries in current directory
314                    foreach my $path_key (keys %{$filesInBackup}) {
315                            my @data = (
316                                    $shareID,
317                                    $backupNum,
318                                    $path_key,
319                                    $filesInBackup->{$path_key}->{'relPath'},
320                                    $filesInBackup->{$path_key}->{'fullPath'},
321            #                       $filesInBackup->{$path_key}->{'sharePathM'},
322                                    $filesInBackup->{$path_key}->{'mtime'},
323                                    $filesInBackup->{$path_key}->{'type'},
324                                    $filesInBackup->{$path_key}->{'size'}
325                            );
326    
327                            my $key = join(" ", (
328                                    $shareID,
329                                    $dir,
330                                    $path_key,
331                                    $filesInBackup->{$path_key}->{'mtime'},
332                                    $filesInBackup->{$path_key}->{'size'}
333                            ));
334    
335    
336                            if (! $beenThere->{$key} && ! found_in_db(@data)) {
337                                    print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2);
338                                    $sth->{'insert_files'}->execute(@data);
339                                    if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {
340                                            $new_dirs++;
341                                            print STDERR " dir\n" if ($debug >= 2);
342                                    } else {
343                                            $new_files++;
344                                            print STDERR " file\n" if ($debug >= 2);
345                                    }
346                            }
347                            $beenThere->{$key}++;
348    
                 if (! $beenThere->{$key} && ! found_in_db(@data)) {  
                         $sth->{'insert_files'}->execute(@data);  
 #                       print STDERR "$key\n";  
349                          if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {                          if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {
350                                  $new_dirs++;                                  $nr_dirs++;
351    
352                                    my $full_path = $dir . '/' . $path_key;
353                                    push @stack, $full_path;
354                                    print STDERR "### store to stack: $full_path\n" if ($debug >= 3);
355    
356    #                               my ($f,$nf,$d,$nd) = recurseDir($bpc, $hostname, $backups, $backupNum, $share, $path_key, $shareID) unless ($beenThere->{$key});
357    #
358    #                               $nr_files += $f;
359    #                               $new_files += $nf;
360    #                               $nr_dirs += $d;
361    #                               $new_dirs += $nd;
362    
363                          } else {                          } else {
364                                  $new_files++;                                  $nr_files++;
365                          }                          }
366                  }                  }
                 $beenThere->{$key}++;  
   
                 if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {  
                         $nr_dirs++;  
367    
368                          my ($f,$nf,$d,$nd) = recurseDir($bpc, $hostname, $backups, $backupNum, $share, $path_key, $shareID);                  print STDERR "## STACK ",join(", ", @stack),"\n" if ($debug >= 2);
369    
370                    while ( my $dir = shift @stack ) {
371                            my ($f,$nf,$d,$nd) = recurseDir($bpc, $hostname, $files, $backupNum, $share, $dir, $shareID);
372                            print STDERR "# $dir f: $f nf: $nf d: $d nd: $nd\n" if ($debug >= 1);
373                          $nr_files += $f;                          $nr_files += $f;
374                          $new_files += $nf;                          $new_files += $nf;
375                          $nr_dirs += $d;                          $nr_dirs += $d;
376                          $new_dirs += $nd;                          $new_dirs += $nd;
   
                 } else {  
                         $nr_files++;  
377                  }                  }
378          }          }
379    

Legend:
Removed from v.15  
changed lines
  Added in v.38

  ViewVC Help
Powered by ViewVC 1.1.26