/[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 29 by dpavlin, Sun Jul 31 12:40:51 2005 UTC revision 40 by dpavlin, Fri Aug 19 15:38:56 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]
46    
# 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 180  foreach my $host_key (keys %{$hosts}) { Line 202  foreach my $host_key (keys %{$hosts}) {
202          my $inc_nr = 0;          my $inc_nr = 0;
203    
204          foreach my $backup (@backups) {          foreach my $backup (@backups) {
205    
206                  $inc_nr++;                  $inc_nr++;
207                  last if ($opt{m} && $inc_nr > $opt{m});                  last if ($opt{m} && $inc_nr > $opt{m});
208    
209                  my $backupNum = $backup->{'num'};                  my $backupNum = $backup->{'num'};
210                  my @backupShares = ();                  my @backupShares = ();
211    
212                  print $hosts->{$host_key}->{'host'},"\t#$backupNum\n";                  print $hosts->{$host_key}->{'host'},
213                            "\t#$backupNum\t",
214                            $backup->{nFilesNew} || '?', "/", $backup->{nFiles} || '?',
215                            " files\n";
216    
217                  $sth->{backups_broj}->execute($hostID, $backupNum);                  $sth->{backups_broj}->execute($hostID, $backupNum);
218                  my ($broj) = $sth->{backups_broj}->fetchrow_array();                  my ($broj) = $sth->{backups_broj}->fetchrow_array();
219                  next if ($broj > 0);                  next if ($broj > 0);
220    
221                  my $files = BackupPC::View->new($bpc, $hostname, \@backups);                  my $files = BackupPC::View->new($bpc, $hostname, \@backups, 1);
222                  foreach my $share ($files->shareList($backupNum)) {                  foreach my $share ($files->shareList($backupNum)) {
223    
224                          print "\t$share";                          my $t = time();
225    
226                            print strftime($t_fmt,localtime())," ", $share;
227                          $shareID = getShareID($share, $hostID, $hostname);                          $shareID = getShareID($share, $hostID, $hostname);
228                                    
229                          my ($f, $nf, $d, $nd) = recurseDir($bpc, $hostname, \@backups, $backupNum, $share, "", $shareID);                          my ($f, $nf, $d, $nd) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID);
230                          print " $nf/$f files $nd/$d dirs\n";                          printf(" %d/%d files %d/%d dirs [%.2f/s]\n",
231                                    $nf, $f, $nd, $d,
232                                    ( ($f+$d) / ((time() - $t) || 1) )
233                            );
234                          $dbh->commit();                          $dbh->commit();
235                  }                  }
236    
# Line 217  undef $sth; Line 248  undef $sth;
248  $dbh->commit();  $dbh->commit();
249  $dbh->disconnect();  $dbh->disconnect();
250    
251    $pidfile->remove;
252    
253  sub getShareID() {  sub getShareID() {
254    
255          my ($share, $hostID, $hostname) = @_;          my ($share, $hostID, $hostname) = @_;
# Line 270  sub found_in_db { Line 303  sub found_in_db {
303  ####################################################  ####################################################
304  sub recurseDir($$$$$$$$) {  sub recurseDir($$$$$$$$) {
305    
306          my ($bpc, $hostname, $backups, $backupNum, $share, $dir, $shareID) = @_;          my ($bpc, $hostname, $files, $backupNum, $share, $dir, $shareID) = @_;
307    
308  #print STDERR "recurse($hostname,$backupNum,$share,$dir,$shareID)\n";          print STDERR "recurse($hostname,$backupNum,$share,$dir,$shareID)\n" if ($debug >= 1);
309    
310          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);
311    
312          { # scope          { # scope
313                  my @stack;                  my @stack;
314    
                 my $files = BackupPC::View->new($bpc, $hostname, $backups);              
315                  my $filesInBackup = $files->dirAttrib($backupNum, $share, $dir);                  my $filesInBackup = $files->dirAttrib($backupNum, $share, $dir);
316    
317                  # first, add all the entries in current directory                  # first, add all the entries in current directory
# Line 306  sub recurseDir($$$$$$$$) { Line 338  sub recurseDir($$$$$$$$) {
338    
339    
340                          if (! $beenThere->{$key} && ! found_in_db(@data)) {                          if (! $beenThere->{$key} && ! found_in_db(@data)) {
341  ##print STDERR "# key: $key [", $beenThere->{$key},"]";                                  print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2);
342                                  $sth->{'insert_files'}->execute(@data);                                  $sth->{'insert_files'}->execute(@data);
343                                  if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {                                  if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {
344                                          $new_dirs++;                                          $new_dirs++;
345  ##print STDERR " dir\n";                                          print STDERR " dir\n" if ($debug >= 2);
346                                  } else {                                  } else {
347                                          $new_files++;                                          $new_files++;
348  ##print STDERR " file\n";                                          print STDERR " file\n" if ($debug >= 2);
349                                  }                                  }
350                          }                          }
351                          $beenThere->{$key}++;                          $beenThere->{$key}++;
# Line 323  sub recurseDir($$$$$$$$) { Line 355  sub recurseDir($$$$$$$$) {
355    
356                                  my $full_path = $dir . '/' . $path_key;                                  my $full_path = $dir . '/' . $path_key;
357                                  push @stack, $full_path;                                  push @stack, $full_path;
358  ##print STDERR "### store to stack: $full_path\n";                                  print STDERR "### store to stack: $full_path\n" if ($debug >= 3);
359    
360  #                               my ($f,$nf,$d,$nd) = recurseDir($bpc, $hostname, $backups, $backupNum, $share, $path_key, $shareID) unless ($beenThere->{$key});  #                               my ($f,$nf,$d,$nd) = recurseDir($bpc, $hostname, $backups, $backupNum, $share, $path_key, $shareID) unless ($beenThere->{$key});
361  #  #
# Line 337  sub recurseDir($$$$$$$$) { Line 369  sub recurseDir($$$$$$$$) {
369                          }                          }
370                  }                  }
371    
372  ##print STDERR "# STACK ",join(", ", @stack),"\n";                  print STDERR "## STACK ",join(", ", @stack),"\n" if ($debug >= 2);
373    
374                  while ( my $dir = shift @stack ) {                  while ( my $dir = shift @stack ) {
375                          my ($f,$nf,$d,$nd) = recurseDir($bpc, $hostname, $backups, $backupNum, $share, $dir, $shareID);                          my ($f,$nf,$d,$nd) = recurseDir($bpc, $hostname, $files, $backupNum, $share, $dir, $shareID);
376  #       print STDERR "## $dir f: $f nf: $nf d: $d nd: $nd\n";                          print STDERR "# $dir f: $f nf: $nf d: $d nd: $nd\n" if ($debug >= 1);
377                          $nr_files += $f;                          $nr_files += $f;
378                          $new_files += $nf;                          $new_files += $nf;
379                          $nr_dirs += $d;                          $nr_dirs += $d;

Legend:
Removed from v.29  
changed lines
  Added in v.40

  ViewVC Help
Powered by ViewVC 1.1.26