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; |
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 ) { |
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:v", \%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 |
} |
} |
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 |
); |
); |
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) |
shareID integer not null references shares(id), |
99 |
|
size integer not null, |
100 |
|
PRIMARY KEY(hostID, num, shareID) |
101 |
); |
); |
102 |
}); |
}); |
103 |
|
|
104 |
|
#do_index('backups_hostid,num_unique'); |
105 |
|
|
106 |
$dbh->do(qq{ |
$dbh->do(qq{ |
107 |
create table dvds ( |
create table dvds ( |
108 |
ID INTEGER PRIMARY KEY, |
ID SERIAL PRIMARY KEY, |
109 |
num INTEGER NOT NULL, |
num INTEGER NOT NULL, |
110 |
name VARCHAR(255) NOT NULL, |
name VARCHAR(255) NOT NULL, |
111 |
mjesto VARCHAR(255) |
mjesto VARCHAR(255) |
114 |
|
|
115 |
$dbh->do(qq{ |
$dbh->do(qq{ |
116 |
create table files ( |
create table files ( |
117 |
ID INTEGER NOT NULL PRIMARY KEY, |
ID SERIAL PRIMARY KEY, |
118 |
shareID INTEGER NOT NULL references shares(id), |
shareID INTEGER NOT NULL references shares(id), |
119 |
backupNum INTEGER NOT NULL references backups(num), |
backupNum INTEGER NOT NULL, |
120 |
name VARCHAR(255) NOT NULL, |
name VARCHAR(255) NOT NULL, |
121 |
path VARCHAR(255) NOT NULL, |
path VARCHAR(255) NOT NULL, |
122 |
fullpath VARCHAR(255) NOT NULL, |
date integer NOT NULL, |
|
date TIMESTAMP NOT NULL, |
|
123 |
type INTEGER NOT NULL, |
type INTEGER NOT NULL, |
124 |
size INTEGER NOT NULL, |
size INTEGER NOT NULL, |
125 |
dvdid INTEGER references dvds(id) |
dvdid INTEGER references dvds(id) |
126 |
); |
); |
127 |
}); |
}); |
128 |
|
|
129 |
print "creating indexes...\n"; |
print "creating indexes:"; |
130 |
|
|
131 |
foreach my $index (qw( |
foreach my $index (qw( |
132 |
hosts_name |
hosts_name |
140 |
files_date |
files_date |
141 |
files_size |
files_size |
142 |
)) { |
)) { |
143 |
my ($table,$col) = split(/_/, $index); |
print " $index"; |
144 |
$dbh->do(qq{ create index $index on $table($col) }); |
do_index($index); |
145 |
} |
} |
146 |
|
print "...\n"; |
147 |
|
|
148 |
|
$dbh->commit; |
149 |
|
|
150 |
} |
} |
151 |
|
|
152 |
if ($opt{d}) { |
if ($opt{d}) { |
153 |
print "deleting "; |
print "deleting "; |
154 |
foreach my $table (qw(hosts shares files dvds backups)) { |
foreach my $table (qw(files dvds backups shares hosts)) { |
155 |
print "$table "; |
print "$table "; |
156 |
$dbh->do(qq{ DELETE FROM $table }); |
$dbh->do(qq{ DELETE FROM $table }); |
157 |
} |
} |
158 |
print " done...\n"; |
print " done...\n"; |
159 |
|
|
160 |
|
$dbh->commit; |
161 |
} |
} |
162 |
|
|
163 |
if ($opt{v}) { |
if ($opt{v}) { |
182 |
SELECT ID FROM hosts WHERE name=? |
SELECT ID FROM hosts WHERE name=? |
183 |
}); |
}); |
184 |
|
|
185 |
$sth->{backups_broj} = $dbh->prepare(qq{ |
$sth->{backups_count} = $dbh->prepare(qq{ |
186 |
SELECT COUNT(*) |
SELECT COUNT(*) |
187 |
FROM backups |
FROM backups |
188 |
WHERE hostID=? AND num=? |
WHERE hostID=? AND num=? AND shareid=? |
189 |
}); |
}); |
190 |
|
|
191 |
$sth->{insert_backups} = $dbh->prepare(qq{ |
$sth->{insert_backups} = $dbh->prepare(qq{ |
192 |
INSERT INTO backups (hostID, num, date, type) |
INSERT INTO backups (hostID, num, date, type, shareid, size) |
193 |
VALUES (?,?,?,?) |
VALUES (?,?,?,?,?,?) |
194 |
}); |
}); |
195 |
|
|
196 |
$sth->{insert_files} = $dbh->prepare(qq{ |
$sth->{insert_files} = $dbh->prepare(qq{ |
197 |
INSERT INTO files |
INSERT INTO files |
198 |
(shareID, backupNum, name, path, fullpath, date, type, size) |
(shareID, backupNum, name, path, date, type, size) |
199 |
VALUES (?,?,?,?,?,?,?,?) |
VALUES (?,?,?,?,?,?,?) |
200 |
}); |
}); |
201 |
|
|
202 |
|
sub fmt_time { |
203 |
|
my $t = shift || return; |
204 |
|
my $out = ""; |
205 |
|
my ($ss,$mm,$hh) = gmtime($t); |
206 |
|
$out .= "${hh}h" if ($hh); |
207 |
|
$out .= sprintf("%02d:%02d", $mm,$ss); |
208 |
|
return $out; |
209 |
|
} |
210 |
|
|
211 |
foreach my $host_key (keys %{$hosts}) { |
foreach my $host_key (keys %{$hosts}) { |
212 |
|
|
213 |
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"; |
220 |
$hosts->{$host_key}->{'ip'} |
$hosts->{$host_key}->{'ip'} |
221 |
); |
); |
222 |
|
|
223 |
$hostID = $dbh->func('last_insert_rowid'); |
$hostID = $dbh->last_insert_id(undef,undef,'hosts',undef); |
224 |
} |
} |
225 |
|
|
226 |
print("host ".$hosts->{$host_key}->{'host'}.": "); |
print "host ".$hosts->{$host_key}->{'host'}.": "; |
227 |
|
|
228 |
# get backups for a host |
# get backups for a host |
229 |
my @backups = $bpc->BackupInfoRead($hostname); |
my @backups = $bpc->BackupInfoRead($hostname); |
230 |
print scalar @backups, " increments\n"; |
my $incs = scalar @backups; |
231 |
|
print "$incs increments\n"; |
232 |
|
|
233 |
my $inc_nr = 0; |
my $inc_nr = 0; |
234 |
|
$beenThere = {}; |
235 |
|
|
236 |
foreach my $backup (@backups) { |
foreach my $backup (@backups) { |
237 |
|
|
238 |
$inc_nr++; |
$inc_nr++; |
239 |
last if ($opt{m} && $inc_nr > $opt{m}); |
last if ($opt{m} && $inc_nr > $opt{m}); |
240 |
|
|
241 |
my $backupNum = $backup->{'num'}; |
my $backupNum = $backup->{'num'}; |
242 |
my @backupShares = (); |
my @backupShares = (); |
243 |
|
|
244 |
print $hosts->{$host_key}->{'host'}, "\t#$backupNum\n"; |
printf("%-10s %2d/%-2d #%-2d %s %5s/%5s files (date: %s dur: %s)\n", |
245 |
|
$hosts->{$host_key}->{'host'}, |
246 |
$sth->{backups_broj}->execute($hostID, $backupNum); |
$inc_nr, $incs, $backupNum, |
247 |
my ($broj) = $sth->{backups_broj}->fetchrow_array(); |
$backup->{type} || '?', |
248 |
next if ($broj > 0); |
$backup->{nFilesNew} || '?', $backup->{nFiles} || '?', |
249 |
|
strftime($t_fmt,localtime($backup->{startTime})), |
250 |
|
fmt_time($backup->{endTime} - $backup->{startTime}) |
251 |
|
); |
252 |
|
|
253 |
my $files = BackupPC::View->new($bpc, $hostname, \@backups, 1); |
my $files = BackupPC::View->new($bpc, $hostname, \@backups, 1); |
254 |
foreach my $share ($files->shareList($backupNum)) { |
foreach my $share ($files->shareList($backupNum)) { |
255 |
|
|
256 |
my $t = time(); |
my $t = time(); |
257 |
|
|
|
print strftime($t_fmt,localtime())," ", $share; |
|
258 |
$shareID = getShareID($share, $hostID, $hostname); |
$shareID = getShareID($share, $hostID, $hostname); |
259 |
|
|
260 |
my ($f, $nf, $d, $nd) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID); |
$sth->{backups_count}->execute($hostID, $backupNum, $shareID); |
261 |
printf(" %d/%d files %d/%d dirs [%.2f/s]\n", |
my ($count) = $sth->{backups_count}->fetchrow_array(); |
262 |
$nf, $f, $nd, $d, |
# skip if allready in database! |
263 |
( ($f+$d) / ((time() - $t) || 1) ) |
next if ($count > 0); |
264 |
|
|
265 |
|
# dump some log |
266 |
|
print strftime($t_fmt,localtime())," ", $share; |
267 |
|
|
268 |
|
my ($f, $nf, $d, $nd, $size) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID); |
269 |
|
|
270 |
|
$sth->{insert_backups}->execute( |
271 |
|
$hostID, |
272 |
|
$backupNum, |
273 |
|
$backup->{'endTime'}, |
274 |
|
$backup->{'type'}, |
275 |
|
$shareID, |
276 |
|
$size, |
277 |
); |
); |
278 |
|
|
279 |
|
print " commit"; |
280 |
$dbh->commit(); |
$dbh->commit(); |
|
} |
|
281 |
|
|
282 |
$sth->{insert_backups}->execute( |
my $dur = (time() - $t) || 1; |
283 |
$hostID, |
printf(" %d/%d files %d/%d dirs %0.2f MB [%.2f/s dur: %s]\n", |
284 |
$backupNum, |
$nf, $f, $nd, $d, |
285 |
$backup->{'endTime'}, |
($size / 1024 / 1024), |
286 |
$backup->{'type'} |
( ($f+$d) / $dur ), |
287 |
); |
fmt_time($dur) |
288 |
$dbh->commit(); |
); |
289 |
|
} |
290 |
|
|
291 |
} |
} |
292 |
} |
} |
294 |
$dbh->commit(); |
$dbh->commit(); |
295 |
$dbh->disconnect(); |
$dbh->disconnect(); |
296 |
|
|
297 |
|
print "total duration: ",fmt_time(time() - $start_t),"\n"; |
298 |
|
|
299 |
$pidfile->remove; |
$pidfile->remove; |
300 |
|
|
301 |
sub getShareID() { |
sub getShareID() { |
322 |
$drop_down =~ s#//+#/#g; |
$drop_down =~ s#//+#/#g; |
323 |
|
|
324 |
$sth->{insert_share}->execute($hostID,$share, $drop_down ,undef); |
$sth->{insert_share}->execute($hostID,$share, $drop_down ,undef); |
325 |
return $dbh->func('last_insert_rowid'); |
return $dbh->last_insert_id(undef,undef,'shares',undef); |
326 |
} |
} |
327 |
|
|
328 |
sub found_in_db { |
sub found_in_db { |
329 |
|
|
330 |
my ($shareID,undef,$name,$path,undef,$date,undef,$size) = @_; |
my @data = @_; |
331 |
|
shift @data; |
332 |
|
|
333 |
|
my ($key, $shareID,undef,$name,$path,undef,$date,undef,$size) = @_; |
334 |
|
|
335 |
|
return $beenThere->{$key} if (defined($beenThere->{$key})); |
336 |
|
|
337 |
$sth->{file_in_db} ||= $dbh->prepare(qq{ |
$sth->{file_in_db} ||= $dbh->prepare(qq{ |
338 |
SELECT count(*) FROM files |
SELECT 1 FROM files |
339 |
WHERE shareID = ? and |
WHERE shareID = ? and |
340 |
path = ? and |
path = ? and |
|
name = ? and |
|
341 |
date = ? and |
date = ? and |
342 |
size = ? |
size = ? |
343 |
}); |
}); |
344 |
|
|
345 |
my @param = ($shareID,$path,$name,$date,$size); |
my @param = ($shareID,$path,$date,$size); |
346 |
$sth->{file_in_db}->execute(@param); |
$sth->{file_in_db}->execute(@param); |
347 |
my ($rows) = $sth->{file_in_db}->fetchrow_array(); |
my $rows = $sth->{file_in_db}->rows; |
348 |
# print STDERR ( $rows ? '+' : '-' ), join(" ",@param), "\n"; |
print STDERR "## found_in_db ",( $rows ? '+' : '-' ), join(" ",@param), "\n" if ($debug >= 3); |
349 |
|
|
350 |
|
$beenThere->{$key}++; |
351 |
|
|
352 |
|
$sth->{'insert_files'}->execute(@data) unless ($rows); |
353 |
return $rows; |
return $rows; |
354 |
} |
} |
355 |
|
|
361 |
|
|
362 |
my ($bpc, $hostname, $files, $backupNum, $share, $dir, $shareID) = @_; |
my ($bpc, $hostname, $files, $backupNum, $share, $dir, $shareID) = @_; |
363 |
|
|
364 |
print STDERR "recurse($hostname,$backupNum,$share,$dir,$shareID)\n" if ($debug >= 1); |
print STDERR "\nrecurse($hostname,$backupNum,$share,$dir,$shareID)\n" if ($debug >= 1); |
365 |
|
|
366 |
my ($nr_files, $new_files, $nr_dirs, $new_dirs) = (0,0,0,0); |
my ($nr_files, $new_files, $nr_dirs, $new_dirs, $size) = (0,0,0,0,0); |
367 |
|
|
368 |
{ # scope |
{ # scope |
369 |
my @stack; |
my @stack; |
370 |
|
|
371 |
|
print STDERR "# dirAttrib($backupNum, $share, $dir)\n" if ($debug >= 2); |
372 |
my $filesInBackup = $files->dirAttrib($backupNum, $share, $dir); |
my $filesInBackup = $files->dirAttrib($backupNum, $share, $dir); |
373 |
|
|
374 |
# first, add all the entries in current directory |
# first, add all the entries in current directory |
375 |
foreach my $path_key (keys %{$filesInBackup}) { |
foreach my $path_key (keys %{$filesInBackup}) { |
376 |
|
print STDERR "# file ",Dumper($filesInBackup->{$path_key}),"\n" if ($debug >= 3); |
377 |
my @data = ( |
my @data = ( |
378 |
$shareID, |
$shareID, |
379 |
$backupNum, |
$backupNum, |
380 |
$path_key, |
$path_key, |
381 |
$filesInBackup->{$path_key}->{'relPath'}, |
$filesInBackup->{$path_key}->{'relPath'}, |
|
$filesInBackup->{$path_key}->{'fullPath'}, |
|
|
# $filesInBackup->{$path_key}->{'sharePathM'}, |
|
382 |
$filesInBackup->{$path_key}->{'mtime'}, |
$filesInBackup->{$path_key}->{'mtime'}, |
383 |
$filesInBackup->{$path_key}->{'type'}, |
$filesInBackup->{$path_key}->{'type'}, |
384 |
$filesInBackup->{$path_key}->{'size'} |
$filesInBackup->{$path_key}->{'size'} |
392 |
$filesInBackup->{$path_key}->{'size'} |
$filesInBackup->{$path_key}->{'size'} |
393 |
)); |
)); |
394 |
|
|
395 |
|
my $found; |
396 |
if (! $beenThere->{$key} && ! found_in_db(@data)) { |
if (! defined($beenThere->{$key}) && ! ($found = found_in_db($key, @data)) ) { |
397 |
print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2); |
print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2); |
398 |
$sth->{'insert_files'}->execute(@data); |
|
399 |
if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) { |
if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) { |
400 |
$new_dirs++; |
$new_dirs++ unless ($found); |
401 |
print STDERR " dir\n" if ($debug >= 2); |
print STDERR " dir\n" if ($debug >= 2); |
402 |
} else { |
} else { |
403 |
$new_files++; |
$new_files++ unless ($found); |
404 |
print STDERR " file\n" if ($debug >= 2); |
print STDERR " file\n" if ($debug >= 2); |
405 |
} |
} |
406 |
|
$size += $filesInBackup->{$path_key}->{'size'} || 0; |
407 |
} |
} |
|
$beenThere->{$key}++; |
|
408 |
|
|
409 |
if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) { |
if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) { |
410 |
$nr_dirs++; |
$nr_dirs++; |
428 |
print STDERR "## STACK ",join(", ", @stack),"\n" if ($debug >= 2); |
print STDERR "## STACK ",join(", ", @stack),"\n" if ($debug >= 2); |
429 |
|
|
430 |
while ( my $dir = shift @stack ) { |
while ( my $dir = shift @stack ) { |
431 |
my ($f,$nf,$d,$nd) = recurseDir($bpc, $hostname, $files, $backupNum, $share, $dir, $shareID); |
my ($f,$nf,$d,$nd, $s) = recurseDir($bpc, $hostname, $files, $backupNum, $share, $dir, $shareID); |
432 |
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); |
433 |
$nr_files += $f; |
$nr_files += $f; |
434 |
$new_files += $nf; |
$new_files += $nf; |
435 |
$nr_dirs += $d; |
$nr_dirs += $d; |
436 |
$new_dirs += $nd; |
$new_dirs += $nd; |
437 |
|
$size += $s; |
438 |
} |
} |
439 |
} |
} |
440 |
|
|
441 |
return ($nr_files, $new_files, $nr_dirs, $new_dirs); |
return ($nr_files, $new_files, $nr_dirs, $new_dirs, $size); |
442 |
} |
} |
443 |
|
|