/[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 27 by dpavlin, Fri Jul 29 17:31:14 2005 UTC revision 57 by dpavlin, Sun Aug 21 14:25:46 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 $start_t = time();
21    
22    my $pidfile = new File::Pid;
23    
24    if (my $pid = $pidfile->running ) {
25            die "$0 already running: $pid\n";
26    } elsif ($pidfile->pid ne $$) {
27            $pidfile->remove;
28            $pidfile = new File::Pid;
29    }
30    $pidfile->write;
31    print STDERR "$0 using pid ",$pidfile->pid," file ",$pidfile->file,"\n";
32    
33    my $t_fmt = '%Y-%m-%d %H:%M:%S';
34    
35  my $hosts;  my $hosts;
36  my $bpc = BackupPC::Lib->new || die;  my $bpc = BackupPC::Lib->new || die;
37  my %Conf = $bpc->Conf();  my %Conf = $bpc->Conf();
38  my $TopDir = $bpc->TopDir();  my $TopDir = $bpc->TopDir();
39  my $beenThere = {};  my $beenThere = {};
40    
41  my $dsn = "dbi:SQLite:dbname=$TopDir/$Conf{SearchDB}";  my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";
42    my $user = $Conf{SearchUser} || '';
43    
44  my $dbh = DBI->connect($dsn, "", "", { RaiseError => 1, AutoCommit => 0 });  my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 });
45    
46  my %opt;  my %opt;
47    
48  if ( !getopts("cdm:", \%opt ) ) {  if ( !getopts("cdm:v:", \%opt ) ) {
49          print STDERR <<EOF;          print STDERR <<EOF;
50  usage: $0 [-c|-d] [-m num]  usage: $0 [-c|-d] [-m num] [-v|-v level]
51    
52  Options:  Options:
53          -c      create database on first use          -c      create database on first use
54          -d      delete database before import          -d      delete database before import
55          -m num  import just num increments for one host          -m num  import just num increments for one host
56            -v num  set verbosity (debug) level (default $debug)
57  EOF  EOF
58          exit 1;          exit 1;
59  }  }
# Line 38  EOF Line 61  EOF
61  ###################################create tables############################3  ###################################create tables############################3
62    
63  if ($opt{c}) {  if ($opt{c}) {
64            sub do_index {
65                    my $index = shift || return;
66                    my ($table,$col,$unique) = split(/_/, $index);
67                    $unique ||= '';
68                    $index =~ s/,/_/g;
69                    $dbh->do(qq{ create $unique index $index on $table($col) });
70            }
71    
72          print "creating tables...\n";          print "creating tables...\n";
73                
74          $dbh->do(qq{          $dbh->do(qq{
75                  create table hosts (                  create table hosts (
76                          ID      INTEGER         PRIMARY KEY,                          ID      SERIAL          PRIMARY KEY,
77                          name    VARCHAR(30)     NOT NULL,                          name    VARCHAR(30)     NOT NULL,
78                          IP      VARCHAR(15)                          IP      VARCHAR(15)
79                  );                              );            
# Line 50  if ($opt{c}) { Line 81  if ($opt{c}) {
81                                
82          $dbh->do(qq{          $dbh->do(qq{
83                  create table shares (                  create table shares (
84                          ID      INTEGER         PRIMARY KEY,                          ID      SERIAL          PRIMARY KEY,
85                          hostID  INTEGER         NOT NULL references hosts(id),                          hostID  INTEGER         NOT NULL references hosts(id),
86                          name    VARCHAR(30)     NOT NULL,                          name    VARCHAR(30)     NOT NULL,
87                          share   VARCHAR(200)    NOT NULL,                          share   VARCHAR(200)    NOT NULL,
# Line 62  if ($opt{c}) { Line 93  if ($opt{c}) {
93                  create table backups (                  create table backups (
94                          hostID  INTEGER         NOT NULL references hosts(id),                          hostID  INTEGER         NOT NULL references hosts(id),
95                          num     INTEGER         NOT NULL,                          num     INTEGER         NOT NULL,
96                          date    DATE,                          date    integer         NOT NULL,
97                          type    CHAR(1),                          type    CHAR(4)         not null,
98                          PRIMARY KEY(hostID, num)                          PRIMARY KEY(hostID, num)
99                  );                              );            
100          });          });
101    
102            do_index('backups_hostid,num_unique');
103    
104          $dbh->do(qq{          $dbh->do(qq{
105                  create table dvds (                  create table dvds (
106                          ID      INTEGER         PRIMARY KEY,                          ID      SERIAL          PRIMARY KEY,
107                          num     INTEGER         NOT NULL,                          num     INTEGER         NOT NULL,
108                          name    VARCHAR(255)    NOT NULL,                          name    VARCHAR(255)    NOT NULL,
109                          mjesto  VARCHAR(255)                          mjesto  VARCHAR(255)
# Line 79  if ($opt{c}) { Line 112  if ($opt{c}) {
112    
113          $dbh->do(qq{              $dbh->do(qq{    
114                  create table files (                  create table files (
115                          ID      INTEGER         NOT NULL PRIMARY KEY,                            ID      SERIAL          PRIMARY KEY,  
116                          shareID INTEGER         NOT NULL references shares(id),                          shareID INTEGER         NOT NULL references shares(id),
117                          backupNum  INTEGER      NOT NULL references backups(num),                          backupNum  INTEGER      NOT NULL,
118                          name       VARCHAR(255) NOT NULL,                          name       VARCHAR(255) NOT NULL,
119                          path       VARCHAR(255) NOT NULL,                          path       VARCHAR(255) NOT NULL,
120                          fullpath   VARCHAR(255) NOT NULL,                          fullpath   VARCHAR(255) NOT NULL,
121                          date       TIMESTAMP    NOT NULL,                          date       integer      NOT NULL,
122                          type       INTEGER      NOT NULL,                          type       INTEGER      NOT NULL,
123                          size       INTEGER      NOT NULL,                          size       INTEGER      NOT NULL,
124                          dvdid      INTEGER      references dvds(id)                              dvdid      INTEGER      references dvds(id)    
125                  );                  );
126          });          });
127    
128          print "creating indexes...\n";          print "creating indexes:";
129    
130          foreach my $index (qw(          foreach my $index (qw(
131                  hosts_name                  hosts_name
# Line 106  if ($opt{c}) { Line 139  if ($opt{c}) {
139                  files_date                  files_date
140                  files_size                  files_size
141          )) {          )) {
142                  my ($table,$col) = split(/_/, $index);                  print " $index";
143                  $dbh->do(qq{ create index $index on $table($col) });                  do_index($index);
144          }          }
145            print "...\n";
146    
147            $dbh->commit;
148    
149  }  }
150    
151  if ($opt{d}) {  if ($opt{d}) {
152          print "deleting ";          print "deleting ";
153          foreach my $table (qw(hosts shares files dvds backups)) {          foreach my $table (qw(files dvds backups shares hosts)) {
154                  print "$table ";                  print "$table ";
155                  $dbh->do(qq{ DELETE FROM $table });                  $dbh->do(qq{ DELETE FROM $table });
156          }          }
157          print " done...\n";          print " done...\n";
158    
159            $dbh->commit;
160    }
161    
162    if ($opt{v}) {
163            print "Debug level at $opt{v}\n";
164            $debug = $opt{v};
165  }  }
166    
167  #################################INSERT VALUES#############################  #################################INSERT VALUES#############################
# Line 156  INSERT INTO files Line 198  INSERT INTO files
198          VALUES (?,?,?,?,?,?,?,?)          VALUES (?,?,?,?,?,?,?,?)
199  });  });
200    
201    sub fmt_time {
202            my $t = shift || return;
203            my $out = "";
204            my ($ss,$mm,$hh) = gmtime($t);
205            $out .= "${hh}h" if ($hh);
206            $out .= sprintf("%02d:%02d", $mm,$ss);
207            return $out;
208    }
209    
210  foreach my $host_key (keys %{$hosts}) {  foreach my $host_key (keys %{$hosts}) {
211    
212          my $hostname = $hosts->{$host_key}->{'host'} || die "can't find host for $host_key";          my $hostname = $hosts->{$host_key}->{'host'} || die "can't find host for $host_key";
# Line 168  foreach my $host_key (keys %{$hosts}) { Line 219  foreach my $host_key (keys %{$hosts}) {
219                          $hosts->{$host_key}->{'ip'}                          $hosts->{$host_key}->{'ip'}
220                  );                  );
221    
222                  $hostID = $dbh->func('last_insert_rowid');                  $hostID = $dbh->last_insert_id(undef,undef,'hosts',undef);
223          }          }
224    
225          print("host ".$hosts->{$host_key}->{'host'}.": ");          print("host ".$hosts->{$host_key}->{'host'}.": ");
# Line 180  foreach my $host_key (keys %{$hosts}) { Line 231  foreach my $host_key (keys %{$hosts}) {
231          my $inc_nr = 0;          my $inc_nr = 0;
232    
233          foreach my $backup (@backups) {          foreach my $backup (@backups) {
234    
235                  $inc_nr++;                  $inc_nr++;
236                  last if ($opt{m} && $inc_nr > $opt{m});                  last if ($opt{m} && $inc_nr > $opt{m});
237    
238                  my $backupNum = $backup->{'num'};                  my $backupNum = $backup->{'num'};
239                  my @backupShares = ();                  my @backupShares = ();
240    
241                  print $hosts->{$host_key}->{'host'},"\t#$backupNum\n";                  print $hosts->{$host_key}->{'host'},
242                            "\t#$backupNum\t", $backup->{type} || '?', " ",
243                            $backup->{nFilesNew} || '?', "/", $backup->{nFiles} || '?',
244                            " files (date: ",
245                            strftime($t_fmt,localtime($backup->{startTime})),
246                            " dur: ",
247                            fmt_time($backup->{endTime} - $backup->{startTime}),
248                            ")\n";
249    
250                  $sth->{backups_broj}->execute($hostID, $backupNum);                  $sth->{backups_broj}->execute($hostID, $backupNum);
251                  my ($broj) = $sth->{backups_broj}->fetchrow_array();                  my ($broj) = $sth->{backups_broj}->fetchrow_array();
# Line 200  foreach my $host_key (keys %{$hosts}) { Line 259  foreach my $host_key (keys %{$hosts}) {
259                  );                  );
260                  $dbh->commit();                  $dbh->commit();
261    
262                  my $files = BackupPC::View->new($bpc, $hostname, \@backups);                  my $files = BackupPC::View->new($bpc, $hostname, \@backups, 1);
263                  foreach my $share ($files->shareList($backupNum)) {                  foreach my $share ($files->shareList($backupNum)) {
264    
265                          print "\t$share";                          my $t = time();
266    
267                            print strftime($t_fmt,localtime())," ", $share;
268                          $shareID = getShareID($share, $hostID, $hostname);                          $shareID = getShareID($share, $hostID, $hostname);
269                                    
270                          my ($f, $nf, $d, $nd) = recurseDir($bpc, $hostname, \@backups, $backupNum, $share, "", $shareID);                          my ($f, $nf, $d, $nd) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID);
271                          print " $nf/$f files $nd/$d dirs\n";                          my $dur = (time() - $t) || 1;
272                            printf(" %d/%d files %d/%d dirs [%.2f/s dur: %s]\n",
273                                    $nf, $f, $nd, $d,
274                                    ( ($f+$d) / $dur ),
275                                    fmt_time($dur)
276                            );
277                          $dbh->commit();                          $dbh->commit();
278                  }                  }
279    
280          }          }
281  }  }
282  undef $sth;  undef $sth;
283  $dbh->commit();  $dbh->commit();
284  $dbh->disconnect();  $dbh->disconnect();
285    
286    print "total duration: ",fmt_time(time() - $start_t),"\n";
287    
288    $pidfile->remove;
289    
290  sub getShareID() {  sub getShareID() {
291    
292          my ($share, $hostID, $hostname) = @_;          my ($share, $hostID, $hostname) = @_;
# Line 240  sub getShareID() { Line 311  sub getShareID() {
311          $drop_down =~ s#//+#/#g;          $drop_down =~ s#//+#/#g;
312    
313          $sth->{insert_share}->execute($hostID,$share, $drop_down ,undef);          $sth->{insert_share}->execute($hostID,$share, $drop_down ,undef);
314          return $dbh->func('last_insert_rowid');                  return $dbh->last_insert_id(undef,undef,'shares',undef);
315  }  }
316    
317  sub found_in_db {  sub found_in_db {
318    
319          my ($shareID,undef,$name,$path,undef,$date,undef,$size) = @_;          my @data = @_;
320            shift @data;
321    
322            my ($key, $shareID,undef,$name,$path,undef,$date,undef,$size) = @_;
323    
324            return $beenThere->{$key} if (defined($beenThere->{$key}));
325    
326          $sth->{file_in_db} ||= $dbh->prepare(qq{          $sth->{file_in_db} ||= $dbh->prepare(qq{
327                  SELECT count(*) FROM files                  SELECT 1 FROM files
328                  WHERE shareID = ? and                  WHERE shareID = ? and
329                          path = ? and                          path = ? and
330                          name = ? and                          name = ? and
# Line 258  sub found_in_db { Line 334  sub found_in_db {
334    
335          my @param = ($shareID,$path,$name,$date,$size);          my @param = ($shareID,$path,$name,$date,$size);
336          $sth->{file_in_db}->execute(@param);          $sth->{file_in_db}->execute(@param);
337          my ($rows) = $sth->{file_in_db}->fetchrow_array();          my $rows = $sth->{file_in_db}->rows;
338  #       print STDERR ( $rows ? '+' : '-' ), join(" ",@param), "\n";          print STDERR "## found_in_db ",( $rows ? '+' : '-' ), join(" ",@param), "\n" if ($debug >= 3);
339    
340            $beenThere->{$key}++;
341    
342            $sth->{'insert_files'}->execute(@data) unless ($rows);
343          return $rows;          return $rows;
344  }  }
345    
# Line 269  sub found_in_db { Line 349  sub found_in_db {
349  ####################################################  ####################################################
350  sub recurseDir($$$$$$$$) {  sub recurseDir($$$$$$$$) {
351    
352          my ($bpc, $hostname, $backups, $backupNum, $share, $dir, $shareID) = @_;          my ($bpc, $hostname, $files, $backupNum, $share, $dir, $shareID) = @_;
353    
354          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);
355    
356          my @stack;          my ($nr_files, $new_files, $nr_dirs, $new_dirs) = (0,0,0,0);
357    
358          { # scope          { # scope
359                    my @stack;
360    
361                  my $files = BackupPC::View->new($bpc, $hostname, $backups);                              print STDERR "# dirAttrib($backupNum, $share, $dir)\n" if ($debug >= 2);
362                  my $filesInBackup = $files->dirAttrib($backupNum, $share, $dir);                  my $filesInBackup = $files->dirAttrib($backupNum, $share, $dir);
363    
 print STDERR "$hostname: $share | $dir | $backupNum\n";  
   
364                  # first, add all the entries in current directory                  # first, add all the entries in current directory
365                  foreach my $path_key (keys %{$filesInBackup}) {                  foreach my $path_key (keys %{$filesInBackup}) {
366                          my @data = (                          my @data = (
# Line 305  print STDERR "$hostname: $share | $dir | Line 384  print STDERR "$hostname: $share | $dir |
384                          ));                          ));
385    
386    
387                          if (! $beenThere->{$key} && ! found_in_db(@data)) {                          if (! defined($beenThere->{$key}) && ! found_in_db($key, @data)) {
388                                  $sth->{'insert_files'}->execute(@data);                                  print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2);
389                                  print STDERR "$key\n";  
390                                  if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {                                  if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {
391                                          $new_dirs++;                                          $new_dirs++;
392                                            print STDERR " dir\n" if ($debug >= 2);
393                                  } else {                                  } else {
394                                          $new_files++;                                          $new_files++;
395                                            print STDERR " file\n" if ($debug >= 2);
396                                  }                                  }
397                          }                          }
                         $beenThere->{$key}++;  
398    
399                          if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {                          if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {
400                                  $nr_dirs++;                                  $nr_dirs++;
401    
402                                  push @stack, [ $bpc, $hostname, $backups, $backupNum, $share, $path_key, $shareID ] unless ($beenThere->{$key});                                  my $full_path = $dir . '/' . $path_key;
403                                    push @stack, $full_path;
404                                    print STDERR "### store to stack: $full_path\n" if ($debug >= 3);
405    
406  #                               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});
407  #  #
408  #                               $nr_files += $f;  #                               $nr_files += $f;
# Line 332  print STDERR "$hostname: $share | $dir | Line 415  print STDERR "$hostname: $share | $dir |
415                          }                          }
416                  }                  }
417    
418          }                  print STDERR "## STACK ",join(", ", @stack),"\n" if ($debug >= 2);
419    
420          foreach my $r (@stack) {                  while ( my $dir = shift @stack ) {
421                  my ($f,$nf,$d,$nd) = recurseDir(@{$r});                          my ($f,$nf,$d,$nd) = recurseDir($bpc, $hostname, $files, $backupNum, $share, $dir, $shareID);
422                  $nr_files += $f;                          print STDERR "# $dir f: $f nf: $nf d: $d nd: $nd\n" if ($debug >= 1);
423                  $new_files += $nf;                          $nr_files += $f;
424                  $nr_dirs += $d;                          $new_files += $nf;
425                  $new_dirs += $nd;                          $nr_dirs += $d;
426                            $new_dirs += $nd;
427                    }
428          }          }
429    
430          return ($nr_files, $new_files, $nr_dirs, $new_dirs);          return ($nr_files, $new_files, $nr_dirs, $new_dirs);

Legend:
Removed from v.27  
changed lines
  Added in v.57

  ViewVC Help
Powered by ViewVC 1.1.26