/[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 51 by dpavlin, Sat Aug 20 16:40:11 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;
18  $|=1;  $|=1;
19    
20    my $start_t = time();
21    
22  my $pidfile = new File::Pid;  my $pidfile = new File::Pid;
23    
24  if (my $pid = $pidfile->running ) {  if (my $pid = $pidfile->running ) {
# Line 34  my %Conf = $bpc->Conf(); Line 38  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    
# Line 56  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                    $dbh->do(qq{ create $unique index $index on $table($col) });
69            }
70    
71          print "creating tables...\n";          print "creating tables...\n";
72                
73          $dbh->do(qq{          $dbh->do(qq{
74                  create table hosts (                  create table hosts (
75                          ID      INTEGER         PRIMARY KEY,                          ID      SERIAL          PRIMARY KEY,
76                          name    VARCHAR(30)     NOT NULL,                          name    VARCHAR(30)     NOT NULL,
77                          IP      VARCHAR(15)                          IP      VARCHAR(15)
78                  );                              );            
# Line 68  if ($opt{c}) { Line 80  if ($opt{c}) {
80                                
81          $dbh->do(qq{          $dbh->do(qq{
82                  create table shares (                  create table shares (
83                          ID      INTEGER         PRIMARY KEY,                          ID      SERIAL          PRIMARY KEY,
84                          hostID  INTEGER         NOT NULL references hosts(id),                          hostID  INTEGER         NOT NULL references hosts(id),
85                          name    VARCHAR(30)     NOT NULL,                          name    VARCHAR(30)     NOT NULL,
86                          share   VARCHAR(200)    NOT NULL,                          share   VARCHAR(200)    NOT NULL,
# Line 80  if ($opt{c}) { Line 92  if ($opt{c}) {
92                  create table backups (                  create table backups (
93                          hostID  INTEGER         NOT NULL references hosts(id),                          hostID  INTEGER         NOT NULL references hosts(id),
94                          num     INTEGER         NOT NULL,                          num     INTEGER         NOT NULL,
95                          date    DATE,                          date    integer         NOT NULL,
96                          type    CHAR(1),                          type    CHAR(4)         not null,
97                          PRIMARY KEY(hostID, num)                          PRIMARY KEY(hostID, num)
98                  );                              );            
99          });          });
100    
101            do_index('backups_num_unique');
102    
103          $dbh->do(qq{          $dbh->do(qq{
104                  create table dvds (                  create table dvds (
105                          ID      INTEGER         PRIMARY KEY,                          ID      SERIAL          PRIMARY KEY,
106                          num     INTEGER         NOT NULL,                          num     INTEGER         NOT NULL,
107                          name    VARCHAR(255)    NOT NULL,                          name    VARCHAR(255)    NOT NULL,
108                          mjesto  VARCHAR(255)                          mjesto  VARCHAR(255)
# Line 97  if ($opt{c}) { Line 111  if ($opt{c}) {
111    
112          $dbh->do(qq{              $dbh->do(qq{    
113                  create table files (                  create table files (
114                          ID      INTEGER         NOT NULL PRIMARY KEY,                            ID      SERIAL          PRIMARY KEY,  
115                          shareID INTEGER         NOT NULL references shares(id),                          shareID INTEGER         NOT NULL references shares(id),
116                          backupNum  INTEGER      NOT NULL references backups(num),                          backupNum  INTEGER      NOT NULL references backups(num),
117                          name       VARCHAR(255) NOT NULL,                          name       VARCHAR(255) NOT NULL,
118                          path       VARCHAR(255) NOT NULL,                          path       VARCHAR(255) NOT NULL,
119                          fullpath   VARCHAR(255) NOT NULL,                          fullpath   VARCHAR(255) NOT NULL,
120                          date       TIMESTAMP    NOT NULL,                          date       integer      NOT NULL,
121                          type       INTEGER      NOT NULL,                          type       INTEGER      NOT NULL,
122                          size       INTEGER      NOT NULL,                          size       INTEGER      NOT NULL,
123                          dvdid      INTEGER      references dvds(id)                              dvdid      INTEGER      references dvds(id)    
124                  );                  );
125          });          });
126    
127          print "creating indexes...\n";          print "creating indexes:";
128    
129          foreach my $index (qw(          foreach my $index (qw(
130                  hosts_name                  hosts_name
# Line 124  if ($opt{c}) { Line 138  if ($opt{c}) {
138                  files_date                  files_date
139                  files_size                  files_size
140          )) {          )) {
141                  my ($table,$col) = split(/_/, $index);                  print " $index";
142                  $dbh->do(qq{ create index $index on $table($col) });                  do_index($index);
143          }          }
144            print "...\n";
145    
146            $dbh->commit;
147    
148  }  }
149    
150  if ($opt{d}) {  if ($opt{d}) {
151          print "deleting ";          print "deleting ";
152          foreach my $table (qw(hosts shares files dvds backups)) {          foreach my $table (qw(files dvds backups shares hosts)) {
153                  print "$table ";                  print "$table ";
154                  $dbh->do(qq{ DELETE FROM $table });                  $dbh->do(qq{ DELETE FROM $table });
155          }          }
156          print " done...\n";          print " done...\n";
157    
158            $dbh->commit;
159  }  }
160    
161  if ($opt{v}) {  if ($opt{v}) {
# Line 179  INSERT INTO files Line 197  INSERT INTO files
197          VALUES (?,?,?,?,?,?,?,?)          VALUES (?,?,?,?,?,?,?,?)
198  });  });
199    
200    sub fmt_time {
201            my $t = shift || return;
202            my $out = "";
203            my ($ss,$mm,$hh) = gmtime($t);
204            $out .= "${hh}h" if ($hh);
205            $out .= sprintf("%02d:%02d", $mm,$ss);
206            return $out;
207    }
208    
209  foreach my $host_key (keys %{$hosts}) {  foreach my $host_key (keys %{$hosts}) {
210    
211          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 218  foreach my $host_key (keys %{$hosts}) {
218                          $hosts->{$host_key}->{'ip'}                          $hosts->{$host_key}->{'ip'}
219                  );                  );
220    
221                  $hostID = $dbh->func('last_insert_rowid');                  $hostID = $dbh->last_insert_id(undef,undef,'hosts',undef);
222          }          }
223    
224          print("host ".$hosts->{$host_key}->{'host'}.": ");          print("host ".$hosts->{$host_key}->{'host'}.": ");
# Line 219  foreach my $host_key (keys %{$hosts}) { Line 246  foreach my $host_key (keys %{$hosts}) {
246                  my ($broj) = $sth->{backups_broj}->fetchrow_array();                  my ($broj) = $sth->{backups_broj}->fetchrow_array();
247                  next if ($broj > 0);                  next if ($broj > 0);
248    
249                    $sth->{insert_backups}->execute(
250                            $hostID,
251                            $backupNum,
252                            $backup->{'endTime'},
253                            $backup->{'type'}
254                    );
255                    $dbh->commit();
256    
257                  my $files = BackupPC::View->new($bpc, $hostname, \@backups, 1);                  my $files = BackupPC::View->new($bpc, $hostname, \@backups, 1);
258                  foreach my $share ($files->shareList($backupNum)) {                  foreach my $share ($files->shareList($backupNum)) {
259    
# Line 228  foreach my $host_key (keys %{$hosts}) { Line 263  foreach my $host_key (keys %{$hosts}) {
263                          $shareID = getShareID($share, $hostID, $hostname);                          $shareID = getShareID($share, $hostID, $hostname);
264                                    
265                          my ($f, $nf, $d, $nd) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID);                          my ($f, $nf, $d, $nd) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID);
266                          printf(" %d/%d files %d/%d dirs [%.2f/s]\n",                          my $dur = (time() - $t) || 1;
267                            printf(" %d/%d files %d/%d dirs [%.2f/s dur: %s]\n",
268                                  $nf, $f, $nd, $d,                                  $nf, $f, $nd, $d,
269                                  ( ($f+$d) / ((time() - $t) || 1) )                                  ( ($f+$d) / $dur ),
270                                    fmt_time($dur)
271                          );                          );
272                          $dbh->commit();                          $dbh->commit();
273                  }                  }
274    
                 $sth->{insert_backups}->execute(  
                         $hostID,  
                         $backupNum,  
                         $backup->{'endTime'},  
                         $backup->{'type'}  
                 );  
                 $dbh->commit();  
   
275          }          }
276  }  }
277  undef $sth;  undef $sth;
278  $dbh->commit();  $dbh->commit();
279  $dbh->disconnect();  $dbh->disconnect();
280    
281    print "total duration: ",fmt_time(time() - $start_t),"\n";
282    
283  $pidfile->remove;  $pidfile->remove;
284    
285  sub getShareID() {  sub getShareID() {
# Line 275  sub getShareID() { Line 306  sub getShareID() {
306          $drop_down =~ s#//+#/#g;          $drop_down =~ s#//+#/#g;
307    
308          $sth->{insert_share}->execute($hostID,$share, $drop_down ,undef);          $sth->{insert_share}->execute($hostID,$share, $drop_down ,undef);
309          return $dbh->func('last_insert_rowid');                  return $dbh->last_insert_id(undef,undef,'shares',undef);
310  }  }
311    
312  sub found_in_db {  sub found_in_db {
313    
314          my ($shareID,undef,$name,$path,undef,$date,undef,$size) = @_;          my @data = @_;
315            shift @data;
316    
317            my ($key, $shareID,undef,$name,$path,undef,$date,undef,$size) = @_;
318    
319            return $beenThere->{$key} if (defined($beenThere->{$key}));
320    
321          $sth->{file_in_db} ||= $dbh->prepare(qq{          $sth->{file_in_db} ||= $dbh->prepare(qq{
322                  SELECT count(*) FROM files                  SELECT 1 FROM files
323                  WHERE shareID = ? and                  WHERE shareID = ? and
324                          path = ? and                          path = ? and
325                          name = ? and                          name = ? and
# Line 293  sub found_in_db { Line 329  sub found_in_db {
329    
330          my @param = ($shareID,$path,$name,$date,$size);          my @param = ($shareID,$path,$name,$date,$size);
331          $sth->{file_in_db}->execute(@param);          $sth->{file_in_db}->execute(@param);
332          my ($rows) = $sth->{file_in_db}->fetchrow_array();          my $rows = $sth->{file_in_db}->rows;
333  #       print STDERR ( $rows ? '+' : '-' ), join(" ",@param), "\n";          print STDERR "## found_in_db ",( $rows ? '+' : '-' ), join(" ",@param), "\n" if ($debug >= 3);
334    
335            $beenThere->{$key}++;
336    
337            $sth->{'insert_files'}->execute(@data) unless ($rows);
338          return $rows;          return $rows;
339  }  }
340    
# Line 313  sub recurseDir($$$$$$$$) { Line 353  sub recurseDir($$$$$$$$) {
353          { # scope          { # scope
354                  my @stack;                  my @stack;
355    
356                    print STDERR "# dirAttrib($backupNum, $share, $dir)\n" if ($debug >= 2);
357                  my $filesInBackup = $files->dirAttrib($backupNum, $share, $dir);                  my $filesInBackup = $files->dirAttrib($backupNum, $share, $dir);
358    
359                  # first, add all the entries in current directory                  # first, add all the entries in current directory
# Line 338  sub recurseDir($$$$$$$$) { Line 379  sub recurseDir($$$$$$$$) {
379                          ));                          ));
380    
381    
382                          if (! $beenThere->{$key} && ! found_in_db(@data)) {                          if (! defined($beenThere->{$key}) && ! found_in_db($key, @data)) {
383                                  print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2);                                  print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2);
384                                  $sth->{'insert_files'}->execute(@data);  
385                                  if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {                                  if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {
386                                          $new_dirs++;                                          $new_dirs++;
387                                          print STDERR " dir\n" if ($debug >= 2);                                          print STDERR " dir\n" if ($debug >= 2);
# Line 349  sub recurseDir($$$$$$$$) { Line 390  sub recurseDir($$$$$$$$) {
390                                          print STDERR " file\n" if ($debug >= 2);                                          print STDERR " file\n" if ($debug >= 2);
391                                  }                                  }
392                          }                          }
                         $beenThere->{$key}++;  
393    
394                          if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {                          if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {
395                                  $nr_dirs++;                                  $nr_dirs++;

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

  ViewVC Help
Powered by ViewVC 1.1.26