/[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 34 by dpavlin, Sun Jul 31 20:53:40 2005 UTC revision 44 by dpavlin, Sat Aug 20 11:24:55 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;  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 24  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:v", \%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 186  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();
# Line 201  foreach my $host_key (keys %{$hosts}) { Line 222  foreach my $host_key (keys %{$hosts}) {
222                  my $files = BackupPC::View->new($bpc, $hostname, \@backups, 1);                  my $files = BackupPC::View->new($bpc, $hostname, \@backups, 1);
223                  foreach my $share ($files->shareList($backupNum)) {                  foreach my $share ($files->shareList($backupNum)) {
224    
225                          print "\t$share";                          my $t = time();
226    
227                            print strftime($t_fmt,localtime())," ", $share;
228                          $shareID = getShareID($share, $hostID, $hostname);                          $shareID = getShareID($share, $hostID, $hostname);
229                                    
230                          my ($f, $nf, $d, $nd) = recurseDir($bpc, $hostname, \@backups, $backupNum, $share, "", $shareID);                          my ($f, $nf, $d, $nd) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID);
231                          print " $nf/$f files $nd/$d dirs\n";                          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();                          $dbh->commit();
236                  }                  }
237    
# Line 223  undef $sth; Line 249  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 276  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          print STDERR "recurse($hostname,$backupNum,$share,$dir,$shareID)\n" if ($debug >= 1);          print STDERR "\nrecurse($hostname,$backupNum,$share,$dir,$shareID)\n" if ($debug >= 1);
310    
311          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);
312    
313          { # scope          { # scope
314                  my @stack;                  my @stack;
315    
                 my $files = BackupPC::View->new($bpc, $hostname, $backups, 1);  
316                  my $filesInBackup = $files->dirAttrib($backupNum, $share, $dir);                  my $filesInBackup = $files->dirAttrib($backupNum, $share, $dir);
317    
318                  # first, add all the entries in current directory                  # first, add all the entries in current directory
# Line 346  sub recurseDir($$$$$$$$) { Line 373  sub recurseDir($$$$$$$$) {
373                  print STDERR "## STACK ",join(", ", @stack),"\n" if ($debug >= 2);                  print STDERR "## STACK ",join(", ", @stack),"\n" if ($debug >= 2);
374    
375                  while ( my $dir = shift @stack ) {                  while ( my $dir = shift @stack ) {
376                          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);
377                          print STDERR "# $dir f: $f nf: $nf d: $d nd: $nd\n" if ($debug >= 1);                          print STDERR "# $dir f: $f nf: $nf d: $d nd: $nd\n" if ($debug >= 1);
378                          $nr_files += $f;                          $nr_files += $f;
379                          $new_files += $nf;                          $new_files += $nf;

Legend:
Removed from v.34  
changed lines
  Added in v.44

  ViewVC Help
Powered by ViewVC 1.1.26