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; |
24 |
} elsif ($pidfile->pid ne $$) { |
} elsif ($pidfile->pid ne $$) { |
25 |
$pidfile->remove; |
$pidfile->remove; |
26 |
$pidfile = new File::Pid; |
$pidfile = new File::Pid; |
|
$pidfile->write; |
|
|
print STDERR "$0 using pid ",$pidfile->pid," file ",$pidfile->file,"\n"; |
|
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'; |
my $t_fmt = '%Y-%m-%d %H:%M:%S'; |
32 |
|
|
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(); |
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++; |