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

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

  ViewVC Help
Powered by ViewVC 1.1.26