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; |
my $debug = 0; |
18 |
$|=1; |
$|=1; |
19 |
|
|
20 |
|
my $pidfile = new File::Pid; |
21 |
|
|
22 |
|
if (my $pid = $pidfile->running ) { |
23 |
|
die "$0 already running: $pid\n"; |
24 |
|
} elsif ($pidfile->pid ne $$) { |
25 |
|
$pidfile->remove; |
26 |
|
$pidfile = new File::Pid; |
27 |
|
} |
28 |
|
$pidfile->write; |
29 |
|
print STDERR "$0 using pid ",$pidfile->pid," file ",$pidfile->file,"\n"; |
30 |
|
|
31 |
|
my $t_fmt = '%Y-%m-%d %H:%M:%S'; |
32 |
|
|
33 |
my $hosts; |
my $hosts; |
34 |
my $bpc = BackupPC::Lib->new || die; |
my $bpc = BackupPC::Lib->new || die; |
35 |
my %Conf = $bpc->Conf(); |
my %Conf = $bpc->Conf(); |
42 |
|
|
43 |
my %opt; |
my %opt; |
44 |
|
|
45 |
if ( !getopts("cdm:v", \%opt ) ) { |
if ( !getopts("cdm:v:", \%opt ) ) { |
46 |
print STDERR <<EOF; |
print STDERR <<EOF; |
47 |
usage: $0 [-c|-d] [-m num] |
usage: $0 [-c|-d] [-m num] [-v|-v level] |
48 |
|
|
49 |
Options: |
Options: |
50 |
-c create database on first use |
-c create database on first use |
51 |
-d delete database before import |
-d delete database before import |
52 |
-m num import just num increments for one host |
-m num import just num increments for one host |
53 |
|
-v num set verbosity (debug) level (default $debug) |
54 |
EOF |
EOF |
55 |
exit 1; |
exit 1; |
56 |
} |
} |
205 |
my $inc_nr = 0; |
my $inc_nr = 0; |
206 |
|
|
207 |
foreach my $backup (@backups) { |
foreach my $backup (@backups) { |
208 |
|
|
209 |
$inc_nr++; |
$inc_nr++; |
210 |
last if ($opt{m} && $inc_nr > $opt{m}); |
last if ($opt{m} && $inc_nr > $opt{m}); |
211 |
|
|
212 |
my $backupNum = $backup->{'num'}; |
my $backupNum = $backup->{'num'}; |
213 |
my @backupShares = (); |
my @backupShares = (); |
214 |
|
|
215 |
print $hosts->{$host_key}->{'host'},"\t#$backupNum\n"; |
print $hosts->{$host_key}->{'host'}, |
216 |
|
"\t#$backupNum\t", $backup->{type} || '?', " ", |
217 |
|
$backup->{nFilesNew} || '?', "/", $backup->{nFiles} || '?', |
218 |
|
" files\n"; |
219 |
|
|
220 |
$sth->{backups_broj}->execute($hostID, $backupNum); |
$sth->{backups_broj}->execute($hostID, $backupNum); |
221 |
my ($broj) = $sth->{backups_broj}->fetchrow_array(); |
my ($broj) = $sth->{backups_broj}->fetchrow_array(); |
224 |
my $files = BackupPC::View->new($bpc, $hostname, \@backups, 1); |
my $files = BackupPC::View->new($bpc, $hostname, \@backups, 1); |
225 |
foreach my $share ($files->shareList($backupNum)) { |
foreach my $share ($files->shareList($backupNum)) { |
226 |
|
|
227 |
print "\t$share"; |
my $t = time(); |
228 |
|
|
229 |
|
print strftime($t_fmt,localtime())," ", $share; |
230 |
$shareID = getShareID($share, $hostID, $hostname); |
$shareID = getShareID($share, $hostID, $hostname); |
231 |
|
|
232 |
my ($f, $nf, $d, $nd) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID); |
my ($f, $nf, $d, $nd) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID); |
233 |
print " $nf/$f files $nd/$d dirs\n"; |
printf(" %d/%d files %d/%d dirs [%.2f/s]\n", |
234 |
|
$nf, $f, $nd, $d, |
235 |
|
( ($f+$d) / ((time() - $t) || 1) ) |
236 |
|
); |
237 |
$dbh->commit(); |
$dbh->commit(); |
238 |
} |
} |
239 |
|
|
251 |
$dbh->commit(); |
$dbh->commit(); |
252 |
$dbh->disconnect(); |
$dbh->disconnect(); |
253 |
|
|
254 |
|
$pidfile->remove; |
255 |
|
|
256 |
sub getShareID() { |
sub getShareID() { |
257 |
|
|
258 |
my ($share, $hostID, $hostname) = @_; |
my ($share, $hostID, $hostname) = @_; |
282 |
|
|
283 |
sub found_in_db { |
sub found_in_db { |
284 |
|
|
285 |
my ($shareID,undef,$name,$path,undef,$date,undef,$size) = @_; |
my @data = @_; |
286 |
|
shift @data; |
287 |
|
|
288 |
|
my ($key, $shareID,undef,$name,$path,undef,$date,undef,$size) = @_; |
289 |
|
|
290 |
|
return $beenThere->{$key} if (defined($beenThere->{$key})); |
291 |
|
|
292 |
$sth->{file_in_db} ||= $dbh->prepare(qq{ |
$sth->{file_in_db} ||= $dbh->prepare(qq{ |
293 |
SELECT count(*) FROM files |
SELECT 1 FROM files |
294 |
WHERE shareID = ? and |
WHERE shareID = ? and |
295 |
path = ? and |
path = ? and |
296 |
name = ? and |
name = ? and |
300 |
|
|
301 |
my @param = ($shareID,$path,$name,$date,$size); |
my @param = ($shareID,$path,$name,$date,$size); |
302 |
$sth->{file_in_db}->execute(@param); |
$sth->{file_in_db}->execute(@param); |
303 |
my ($rows) = $sth->{file_in_db}->fetchrow_array(); |
my $rows = $sth->{file_in_db}->rows; |
304 |
# print STDERR ( $rows ? '+' : '-' ), join(" ",@param), "\n"; |
print STDERR "## found_in_db ",( $rows ? '+' : '-' ), join(" ",@param), "\n" if ($debug >= 3); |
305 |
|
|
306 |
|
$beenThere->{$key}++; |
307 |
|
|
308 |
|
$sth->{'insert_files'}->execute(@data) unless ($rows); |
309 |
return $rows; |
return $rows; |
310 |
} |
} |
311 |
|
|
317 |
|
|
318 |
my ($bpc, $hostname, $files, $backupNum, $share, $dir, $shareID) = @_; |
my ($bpc, $hostname, $files, $backupNum, $share, $dir, $shareID) = @_; |
319 |
|
|
320 |
print STDERR "recurse($hostname,$backupNum,$share,$dir,$shareID)\n" if ($debug >= 1); |
print STDERR "\nrecurse($hostname,$backupNum,$share,$dir,$shareID)\n" if ($debug >= 1); |
321 |
|
|
322 |
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); |
323 |
|
|
324 |
{ # scope |
{ # scope |
325 |
my @stack; |
my @stack; |
326 |
|
|
327 |
|
print STDERR "# dirAttrib($backupNum, $share, $dir)\n" if ($debug >= 2); |
328 |
my $filesInBackup = $files->dirAttrib($backupNum, $share, $dir); |
my $filesInBackup = $files->dirAttrib($backupNum, $share, $dir); |
329 |
|
|
330 |
# first, add all the entries in current directory |
# first, add all the entries in current directory |
350 |
)); |
)); |
351 |
|
|
352 |
|
|
353 |
if (! $beenThere->{$key} && ! found_in_db(@data)) { |
if (! defined($beenThere->{$key}) && ! found_in_db($key, @data)) { |
354 |
print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2); |
print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2); |
355 |
$sth->{'insert_files'}->execute(@data); |
|
356 |
if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) { |
if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) { |
357 |
$new_dirs++; |
$new_dirs++; |
358 |
print STDERR " dir\n" if ($debug >= 2); |
print STDERR " dir\n" if ($debug >= 2); |
361 |
print STDERR " file\n" if ($debug >= 2); |
print STDERR " file\n" if ($debug >= 2); |
362 |
} |
} |
363 |
} |
} |
|
$beenThere->{$key}++; |
|
364 |
|
|
365 |
if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) { |
if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) { |
366 |
$nr_dirs++; |
$nr_dirs++; |