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; |
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; |
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 |
|
|
49 |
if ( !getopts("cdm:v", \%opt ) ) { |
if ( !getopts("cdm:v:", \%opt ) ) { |
50 |
print STDERR <<EOF; |
print STDERR <<EOF; |
51 |
usage: $0 [-c|-d] [-m num] |
usage: $0 [-c|-d] [-m num] [-v|-v level] |
52 |
|
|
53 |
Options: |
Options: |
54 |
-c create database on first use |
-c create database on first use |
55 |
-d delete database before import |
-d delete database before import |
56 |
-m num import just num increments for one host |
-m num import just num increments for one host |
57 |
|
-v num set verbosity (debug) level (default $debug) |
58 |
EOF |
EOF |
59 |
exit 1; |
exit 1; |
60 |
} |
} |
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 |
); |
); |
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, |
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) |
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 |
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}) { |
210 |
$hosts->{$host_key}->{'ip'} |
$hosts->{$host_key}->{'ip'} |
211 |
); |
); |
212 |
|
|
213 |
$hostID = $dbh->func('last_insert_rowid'); |
$hostID = $dbh->last_insert_id(undef,undef,'hosts',undef); |
214 |
} |
} |
215 |
|
|
216 |
print("host ".$hosts->{$host_key}->{'host'}.": "); |
print("host ".$hosts->{$host_key}->{'host'}.": "); |
222 |
my $inc_nr = 0; |
my $inc_nr = 0; |
223 |
|
|
224 |
foreach my $backup (@backups) { |
foreach my $backup (@backups) { |
225 |
|
|
226 |
$inc_nr++; |
$inc_nr++; |
227 |
last if ($opt{m} && $inc_nr > $opt{m}); |
last if ($opt{m} && $inc_nr > $opt{m}); |
228 |
|
|
229 |
my $backupNum = $backup->{'num'}; |
my $backupNum = $backup->{'num'}; |
230 |
my @backupShares = (); |
my @backupShares = (); |
231 |
|
|
232 |
print $hosts->{$host_key}->{'host'}, "\t#$backupNum\n"; |
print $hosts->{$host_key}->{'host'}, |
233 |
|
"\t#$backupNum\t", $backup->{type} || '?', " ", |
234 |
|
$backup->{nFilesNew} || '?', "/", $backup->{nFiles} || '?', |
235 |
|
" files\n"; |
236 |
|
|
237 |
$sth->{backups_broj}->execute($hostID, $backupNum); |
$sth->{backups_broj}->execute($hostID, $backupNum); |
238 |
my ($broj) = $sth->{backups_broj}->fetchrow_array(); |
my ($broj) = $sth->{backups_broj}->fetchrow_array(); |
239 |
next if ($broj > 0); |
next if ($broj > 0); |
240 |
|
|
241 |
|
$sth->{insert_backups}->execute( |
242 |
|
$hostID, |
243 |
|
$backupNum, |
244 |
|
$backup->{'endTime'}, |
245 |
|
$backup->{'type'} |
246 |
|
); |
247 |
|
$dbh->commit(); |
248 |
|
|
249 |
my $files = BackupPC::View->new($bpc, $hostname, \@backups, 1); |
my $files = BackupPC::View->new($bpc, $hostname, \@backups, 1); |
250 |
foreach my $share ($files->shareList($backupNum)) { |
foreach my $share ($files->shareList($backupNum)) { |
251 |
|
|
262 |
$dbh->commit(); |
$dbh->commit(); |
263 |
} |
} |
264 |
|
|
|
$sth->{insert_backups}->execute( |
|
|
$hostID, |
|
|
$backupNum, |
|
|
$backup->{'endTime'}, |
|
|
$backup->{'type'} |
|
|
); |
|
|
$dbh->commit(); |
|
|
|
|
265 |
} |
} |
266 |
} |
} |
267 |
undef $sth; |
undef $sth; |
294 |
$drop_down =~ s#//+#/#g; |
$drop_down =~ s#//+#/#g; |
295 |
|
|
296 |
$sth->{insert_share}->execute($hostID,$share, $drop_down ,undef); |
$sth->{insert_share}->execute($hostID,$share, $drop_down ,undef); |
297 |
return $dbh->func('last_insert_rowid'); |
return $dbh->last_insert_id(undef,undef,'shares',undef); |
298 |
} |
} |
299 |
|
|
300 |
sub found_in_db { |
sub found_in_db { |
301 |
|
|
302 |
my ($shareID,undef,$name,$path,undef,$date,undef,$size) = @_; |
my @data = @_; |
303 |
|
shift @data; |
304 |
|
|
305 |
|
my ($key, $shareID,undef,$name,$path,undef,$date,undef,$size) = @_; |
306 |
|
|
307 |
|
return $beenThere->{$key} if (defined($beenThere->{$key})); |
308 |
|
|
309 |
$sth->{file_in_db} ||= $dbh->prepare(qq{ |
$sth->{file_in_db} ||= $dbh->prepare(qq{ |
310 |
SELECT count(*) FROM files |
SELECT 1 FROM files |
311 |
WHERE shareID = ? and |
WHERE shareID = ? and |
312 |
path = ? and |
path = ? and |
313 |
name = ? and |
name = ? and |
317 |
|
|
318 |
my @param = ($shareID,$path,$name,$date,$size); |
my @param = ($shareID,$path,$name,$date,$size); |
319 |
$sth->{file_in_db}->execute(@param); |
$sth->{file_in_db}->execute(@param); |
320 |
my ($rows) = $sth->{file_in_db}->fetchrow_array(); |
my $rows = $sth->{file_in_db}->rows; |
321 |
# print STDERR ( $rows ? '+' : '-' ), join(" ",@param), "\n"; |
print STDERR "## found_in_db ",( $rows ? '+' : '-' ), join(" ",@param), "\n" if ($debug >= 3); |
322 |
|
|
323 |
|
$beenThere->{$key}++; |
324 |
|
|
325 |
|
$sth->{'insert_files'}->execute(@data) unless ($rows); |
326 |
return $rows; |
return $rows; |
327 |
} |
} |
328 |
|
|
334 |
|
|
335 |
my ($bpc, $hostname, $files, $backupNum, $share, $dir, $shareID) = @_; |
my ($bpc, $hostname, $files, $backupNum, $share, $dir, $shareID) = @_; |
336 |
|
|
337 |
print STDERR "recurse($hostname,$backupNum,$share,$dir,$shareID)\n" if ($debug >= 1); |
print STDERR "\nrecurse($hostname,$backupNum,$share,$dir,$shareID)\n" if ($debug >= 1); |
338 |
|
|
339 |
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); |
340 |
|
|
341 |
{ # scope |
{ # scope |
342 |
my @stack; |
my @stack; |
343 |
|
|
344 |
|
print STDERR "# dirAttrib($backupNum, $share, $dir)\n" if ($debug >= 2); |
345 |
my $filesInBackup = $files->dirAttrib($backupNum, $share, $dir); |
my $filesInBackup = $files->dirAttrib($backupNum, $share, $dir); |
346 |
|
|
347 |
# first, add all the entries in current directory |
# first, add all the entries in current directory |
367 |
)); |
)); |
368 |
|
|
369 |
|
|
370 |
if (! $beenThere->{$key} && ! found_in_db(@data)) { |
if (! defined($beenThere->{$key}) && ! found_in_db($key, @data)) { |
371 |
print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2); |
print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2); |
372 |
$sth->{'insert_files'}->execute(@data); |
|
373 |
if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) { |
if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) { |
374 |
$new_dirs++; |
$new_dirs++; |
375 |
print STDERR " dir\n" if ($debug >= 2); |
print STDERR " dir\n" if ($debug >= 2); |
378 |
print STDERR " file\n" if ($debug >= 2); |
print STDERR " file\n" if ($debug >= 2); |
379 |
} |
} |
380 |
} |
} |
|
$beenThere->{$key}++; |
|
381 |
|
|
382 |
if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) { |
if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) { |
383 |
$nr_dirs++; |
$nr_dirs++; |