/[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 44 by dpavlin, Sat Aug 20 11:24:55 2005 UTC revision 50 by dpavlin, Sat Aug 20 15:51:16 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;
# Line 10  use Getopt::Std; Line 11  use Getopt::Std;
11  use Time::HiRes qw/time/;  use Time::HiRes qw/time/;
12  use File::Pid;  use File::Pid;
13  use POSIX qw/strftime/;  use POSIX qw/strftime/;
14    
15  use constant BPC_FTYPE_DIR => 5;  use constant BPC_FTYPE_DIR => 5;
16    
17  my $debug = 0;  my $debug = 0;
# Line 35  my $TopDir = $bpc->TopDir(); Line 37  my $TopDir = $bpc->TopDir();
37  my $beenThere = {};  my $beenThere = {};
38    
39  my $dsn = "dbi:SQLite:dbname=$TopDir/$Conf{SearchDB}";  my $dsn = "dbi:SQLite:dbname=$TopDir/$Conf{SearchDB}";
40    my $user = '';
41    
42  my $dbh = DBI->connect($dsn, "", "", { RaiseError => 1, AutoCommit => 0 });  # DEBUG option!
43    ($dsn,$user) = qw/dbi:Pg:dbname=backuppc dpavlin/;
44    
45    my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 });
46    
47  my %opt;  my %opt;
48    
# Line 56  EOF Line 62  EOF
62  ###################################create tables############################3  ###################################create tables############################3
63    
64  if ($opt{c}) {  if ($opt{c}) {
65            sub do_index {
66                    my $index = shift || return;
67                    my ($table,$col,$unique) = split(/_/, $index);
68                    $unique ||= '';
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 68  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 80  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_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 97  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 references backups(num),
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 124  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            eval { $dbh->commit; };
160  }  }
161    
162  if ($opt{v}) {  if ($opt{v}) {
# Line 179  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 191  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 219  foreach my $host_key (keys %{$hosts}) { Line 247  foreach my $host_key (keys %{$hosts}) {
247                  my ($broj) = $sth->{backups_broj}->fetchrow_array();                  my ($broj) = $sth->{backups_broj}->fetchrow_array();
248                  next if ($broj > 0);                  next if ($broj > 0);
249    
250                    $sth->{insert_backups}->execute(
251                            $hostID,
252                            $backupNum,
253                            $backup->{'endTime'},
254                            $backup->{'type'}
255                    );
256                    $dbh->commit();
257    
258                  my $files = BackupPC::View->new($bpc, $hostname, \@backups, 1);                  my $files = BackupPC::View->new($bpc, $hostname, \@backups, 1);
259                  foreach my $share ($files->shareList($backupNum)) {                  foreach my $share ($files->shareList($backupNum)) {
260    
# Line 228  foreach my $host_key (keys %{$hosts}) { Line 264  foreach my $host_key (keys %{$hosts}) {
264                          $shareID = getShareID($share, $hostID, $hostname);                          $shareID = getShareID($share, $hostID, $hostname);
265                                    
266                          my ($f, $nf, $d, $nd) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID);                          my ($f, $nf, $d, $nd) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID);
267                          printf(" %d/%d files %d/%d dirs [%.2f/s]\n",                          my $dur = (time() - $t) || 1;
268                            printf(" %d/%d files %d/%d dirs [%.2f/s dur: %s]\n",
269                                  $nf, $f, $nd, $d,                                  $nf, $f, $nd, $d,
270                                  ( ($f+$d) / ((time() - $t) || 1) )                                  ( ($f+$d) / $dur ),
271                                    fmt_time($dur)
272                          );                          );
273                          $dbh->commit();                          $dbh->commit();
274                  }                  }
275    
                 $sth->{insert_backups}->execute(  
                         $hostID,  
                         $backupNum,  
                         $backup->{'endTime'},  
                         $backup->{'type'}  
                 );  
                 $dbh->commit();  
   
276          }          }
277  }  }
278  undef $sth;  undef $sth;
# Line 275  sub getShareID() { Line 305  sub getShareID() {
305          $drop_down =~ s#//+#/#g;          $drop_down =~ s#//+#/#g;
306    
307          $sth->{insert_share}->execute($hostID,$share, $drop_down ,undef);          $sth->{insert_share}->execute($hostID,$share, $drop_down ,undef);
308          return $dbh->func('last_insert_rowid');                  return $dbh->last_insert_id(undef,undef,'shares',undef);
309  }  }
310    
311  sub found_in_db {  sub found_in_db {
312    
313          my ($shareID,undef,$name,$path,undef,$date,undef,$size) = @_;          my @data = @_;
314            shift @data;
315    
316            my ($key, $shareID,undef,$name,$path,undef,$date,undef,$size) = @_;
317    
318            return $beenThere->{$key} if (defined($beenThere->{$key}));
319    
320          $sth->{file_in_db} ||= $dbh->prepare(qq{          $sth->{file_in_db} ||= $dbh->prepare(qq{
321                  SELECT count(*) FROM files                  SELECT 1 FROM files
322                  WHERE shareID = ? and                  WHERE shareID = ? and
323                          path = ? and                          path = ? and
324                          name = ? and                          name = ? and
# Line 293  sub found_in_db { Line 328  sub found_in_db {
328    
329          my @param = ($shareID,$path,$name,$date,$size);          my @param = ($shareID,$path,$name,$date,$size);
330          $sth->{file_in_db}->execute(@param);          $sth->{file_in_db}->execute(@param);
331          my ($rows) = $sth->{file_in_db}->fetchrow_array();          my $rows = $sth->{file_in_db}->rows;
332  #       print STDERR ( $rows ? '+' : '-' ), join(" ",@param), "\n";          print STDERR "## found_in_db ",( $rows ? '+' : '-' ), join(" ",@param), "\n" if ($debug >= 3);
333    
334            $beenThere->{$key}++;
335    
336            $sth->{'insert_files'}->execute(@data) unless ($rows);
337          return $rows;          return $rows;
338  }  }
339    
# Line 313  sub recurseDir($$$$$$$$) { Line 352  sub recurseDir($$$$$$$$) {
352          { # scope          { # scope
353                  my @stack;                  my @stack;
354    
355                    print STDERR "# dirAttrib($backupNum, $share, $dir)\n" if ($debug >= 2);
356                  my $filesInBackup = $files->dirAttrib($backupNum, $share, $dir);                  my $filesInBackup = $files->dirAttrib($backupNum, $share, $dir);
357    
358                  # first, add all the entries in current directory                  # first, add all the entries in current directory
# Line 338  sub recurseDir($$$$$$$$) { Line 378  sub recurseDir($$$$$$$$) {
378                          ));                          ));
379    
380    
381                          if (! $beenThere->{$key} && ! found_in_db(@data)) {                          if (! defined($beenThere->{$key}) && ! found_in_db($key, @data)) {
382                                  print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2);                                  print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2);
383                                  $sth->{'insert_files'}->execute(@data);  
384                                  if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {                                  if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {
385                                          $new_dirs++;                                          $new_dirs++;
386                                          print STDERR " dir\n" if ($debug >= 2);                                          print STDERR " dir\n" if ($debug >= 2);
# Line 349  sub recurseDir($$$$$$$$) { Line 389  sub recurseDir($$$$$$$$) {
389                                          print STDERR " file\n" if ($debug >= 2);                                          print STDERR " file\n" if ($debug >= 2);
390                                  }                                  }
391                          }                          }
                         $beenThere->{$key}++;  
392    
393                          if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {                          if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {
394                                  $nr_dirs++;                                  $nr_dirs++;

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

  ViewVC Help
Powered by ViewVC 1.1.26