7 |
use BackupPC::View; |
use BackupPC::View; |
8 |
use Data::Dumper; |
use Data::Dumper; |
9 |
use Getopt::Std; |
use Getopt::Std; |
10 |
|
use Time::HiRes qw/time/; |
11 |
|
use File::Pid; |
12 |
|
use POSIX qw/strftime/; |
13 |
use constant BPC_FTYPE_DIR => 5; |
use constant BPC_FTYPE_DIR => 5; |
14 |
|
|
15 |
|
my $debug = 0; |
16 |
$|=1; |
$|=1; |
17 |
|
|
18 |
|
my $pidfile = new File::Pid; |
19 |
|
|
20 |
|
if (my $pid = $pidfile->running ) { |
21 |
|
die "$0 already running: $pid\n"; |
22 |
|
} elsif ($pidfile->pid ne $$) { |
23 |
|
$pidfile->remove; |
24 |
|
$pidfile = new File::Pid; |
25 |
|
} |
26 |
|
$pidfile->write; |
27 |
|
print STDERR "$0 using pid ",$pidfile->pid," file ",$pidfile->file,"\n"; |
28 |
|
|
29 |
|
my $t_fmt = '%Y-%m-%d %H:%M:%S'; |
30 |
|
|
31 |
my $hosts; |
my $hosts; |
32 |
my $bpc = BackupPC::Lib->new || die; |
my $bpc = BackupPC::Lib->new || die; |
33 |
my %Conf = $bpc->Conf(); |
my %Conf = $bpc->Conf(); |
40 |
|
|
41 |
my %opt; |
my %opt; |
42 |
|
|
43 |
if ( !getopts("cdm:", \%opt ) ) { |
if ( !getopts("cdm:v", \%opt ) ) { |
44 |
print STDERR <<EOF; |
print STDERR <<EOF; |
45 |
usage: $0 [-c|-d] [-m num] |
usage: $0 [-c|-d] [-m num] |
46 |
|
|
98 |
create table files ( |
create table files ( |
99 |
ID INTEGER NOT NULL PRIMARY KEY, |
ID INTEGER NOT NULL PRIMARY KEY, |
100 |
shareID INTEGER NOT NULL references shares(id), |
shareID INTEGER NOT NULL references shares(id), |
101 |
backupNum INTEGER NOT NULL references backups(id), |
backupNum INTEGER NOT NULL references backups(num), |
102 |
name VARCHAR(255) NOT NULL, |
name VARCHAR(255) NOT NULL, |
103 |
path VARCHAR(255) NOT NULL, |
path VARCHAR(255) NOT NULL, |
104 |
fullpath VARCHAR(255) NOT NULL, |
fullpath VARCHAR(255) NOT NULL, |
139 |
print " done...\n"; |
print " done...\n"; |
140 |
} |
} |
141 |
|
|
142 |
|
if ($opt{v}) { |
143 |
|
print "Debug level at $opt{v}\n"; |
144 |
|
$debug = $opt{v}; |
145 |
|
} |
146 |
|
|
147 |
#################################INSERT VALUES############################# |
#################################INSERT VALUES############################# |
148 |
|
|
149 |
# get hosts |
# get hosts |
202 |
my $inc_nr = 0; |
my $inc_nr = 0; |
203 |
|
|
204 |
foreach my $backup (@backups) { |
foreach my $backup (@backups) { |
205 |
|
|
206 |
$inc_nr++; |
$inc_nr++; |
207 |
last if ($opt{m} && $inc_nr > $opt{m}); |
last if ($opt{m} && $inc_nr > $opt{m}); |
208 |
|
|
209 |
my $backupNum = $backup->{'num'}; |
my $backupNum = $backup->{'num'}; |
210 |
my @backupShares = (); |
my @backupShares = (); |
211 |
|
|
212 |
print $hosts->{$host_key}->{'host'},"\t$backupNum\n"; |
print $hosts->{$host_key}->{'host'}, |
213 |
|
"\t#$backupNum\t", |
214 |
|
$backup->{nFilesNew} || '?', "/", $backup->{nFiles} || '?', |
215 |
|
" files\n"; |
216 |
|
|
217 |
$sth->{backups_broj}->execute($hostID, $backupNum); |
$sth->{backups_broj}->execute($hostID, $backupNum); |
218 |
my ($broj) = $sth->{backups_broj}->fetchrow_array(); |
my ($broj) = $sth->{backups_broj}->fetchrow_array(); |
219 |
next if ($broj > 0); |
next if ($broj > 0); |
220 |
|
|
221 |
|
my $files = BackupPC::View->new($bpc, $hostname, \@backups, 1); |
222 |
|
foreach my $share ($files->shareList($backupNum)) { |
223 |
|
|
224 |
|
my $t = time(); |
225 |
|
|
226 |
|
print strftime($t_fmt,localtime())," ", $share; |
227 |
|
$shareID = getShareID($share, $hostID, $hostname); |
228 |
|
|
229 |
|
my ($f, $nf, $d, $nd) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID); |
230 |
|
printf(" %d/%d files %d/%d dirs [%.2f/s]\n", |
231 |
|
$nf, $f, $nd, $d, |
232 |
|
( ($f+$d) / ((time() - $t) || 1) ) |
233 |
|
); |
234 |
|
$dbh->commit(); |
235 |
|
} |
236 |
|
|
237 |
$sth->{insert_backups}->execute( |
$sth->{insert_backups}->execute( |
238 |
$hostID, |
$hostID, |
239 |
$backupNum, |
$backupNum, |
242 |
); |
); |
243 |
$dbh->commit(); |
$dbh->commit(); |
244 |
|
|
|
my $files = BackupPC::View->new($bpc, $hostname, \@backups); |
|
|
foreach my $share ($files->shareList($backupNum)) { |
|
|
|
|
|
print "\t$share"; |
|
|
$shareID = getShareID($share, $hostID, $hostname); |
|
|
|
|
|
my ($f, $nf, $d, $nd) = recurseDir($bpc, $hostname, \@backups, $backupNum, $share, "", $shareID); |
|
|
print " $nf/$f files $nd/$d dirs\n"; |
|
|
$dbh->commit(); |
|
|
} |
|
245 |
} |
} |
246 |
} |
} |
247 |
undef $sth; |
undef $sth; |
248 |
$dbh->commit(); |
$dbh->commit(); |
249 |
$dbh->disconnect(); |
$dbh->disconnect(); |
250 |
|
|
251 |
|
$pidfile->remove; |
252 |
|
|
253 |
sub getShareID() { |
sub getShareID() { |
254 |
|
|
255 |
my ($share, $hostID, $hostname) = @_; |
my ($share, $hostID, $hostname) = @_; |
270 |
VALUES (?,?,?,?) |
VALUES (?,?,?,?) |
271 |
}); |
}); |
272 |
|
|
273 |
$sth->{insert_share}->execute($hostID,$share, $hostname . $share,undef); |
my $drop_down = $hostname . '/' . $share; |
274 |
|
$drop_down =~ s#//+#/#g; |
275 |
|
|
276 |
|
$sth->{insert_share}->execute($hostID,$share, $drop_down ,undef); |
277 |
return $dbh->func('last_insert_rowid'); |
return $dbh->func('last_insert_rowid'); |
278 |
} |
} |
279 |
|
|
293 |
my @param = ($shareID,$path,$name,$date,$size); |
my @param = ($shareID,$path,$name,$date,$size); |
294 |
$sth->{file_in_db}->execute(@param); |
$sth->{file_in_db}->execute(@param); |
295 |
my ($rows) = $sth->{file_in_db}->fetchrow_array(); |
my ($rows) = $sth->{file_in_db}->fetchrow_array(); |
296 |
print STDERR ( $rows ? '+' : '-' ), join(" ",@param), "\n"; |
# print STDERR ( $rows ? '+' : '-' ), join(" ",@param), "\n"; |
297 |
return $rows; |
return $rows; |
298 |
} |
} |
299 |
|
|
303 |
#################################################### |
#################################################### |
304 |
sub recurseDir($$$$$$$$) { |
sub recurseDir($$$$$$$$) { |
305 |
|
|
306 |
my ($bpc, $hostname, $backups, $backupNum, $share, $dir, $shareID) = @_; |
my ($bpc, $hostname, $files, $backupNum, $share, $dir, $shareID) = @_; |
307 |
|
|
308 |
my ($nr_files, $new_files, $nr_dirs, $new_dirs) = (0,0,0,0); |
print STDERR "recurse($hostname,$backupNum,$share,$dir,$shareID)\n" if ($debug >= 1); |
309 |
|
|
310 |
my $files = BackupPC::View->new($bpc, $hostname, $backups); |
my ($nr_files, $new_files, $nr_dirs, $new_dirs) = (0,0,0,0); |
|
my $filesInBackup = $files->dirAttrib($backupNum, $share, $dir); |
|
311 |
|
|
312 |
# first, add all the entries in current directory |
{ # scope |
313 |
foreach my $path_key (keys %{$filesInBackup}) { |
my @stack; |
|
my @data = ( |
|
|
$shareID, |
|
|
$backupNum, |
|
|
$path_key, |
|
|
$filesInBackup->{$path_key}->{'relPath'}, |
|
|
$filesInBackup->{$path_key}->{'fullPath'}, |
|
|
# $filesInBackup->{$path_key}->{'sharePathM'}, |
|
|
$filesInBackup->{$path_key}->{'mtime'}, |
|
|
$filesInBackup->{$path_key}->{'type'}, |
|
|
$filesInBackup->{$path_key}->{'size'} |
|
|
); |
|
314 |
|
|
315 |
my $key = join(" ", ( |
my $filesInBackup = $files->dirAttrib($backupNum, $share, $dir); |
|
$shareID, |
|
|
$dir, |
|
|
$path_key, |
|
|
$filesInBackup->{$path_key}->{'mtime'}, |
|
|
$filesInBackup->{$path_key}->{'size'} |
|
|
)); |
|
316 |
|
|
317 |
|
# first, add all the entries in current directory |
318 |
|
foreach my $path_key (keys %{$filesInBackup}) { |
319 |
|
my @data = ( |
320 |
|
$shareID, |
321 |
|
$backupNum, |
322 |
|
$path_key, |
323 |
|
$filesInBackup->{$path_key}->{'relPath'}, |
324 |
|
$filesInBackup->{$path_key}->{'fullPath'}, |
325 |
|
# $filesInBackup->{$path_key}->{'sharePathM'}, |
326 |
|
$filesInBackup->{$path_key}->{'mtime'}, |
327 |
|
$filesInBackup->{$path_key}->{'type'}, |
328 |
|
$filesInBackup->{$path_key}->{'size'} |
329 |
|
); |
330 |
|
|
331 |
|
my $key = join(" ", ( |
332 |
|
$shareID, |
333 |
|
$dir, |
334 |
|
$path_key, |
335 |
|
$filesInBackup->{$path_key}->{'mtime'}, |
336 |
|
$filesInBackup->{$path_key}->{'size'} |
337 |
|
)); |
338 |
|
|
339 |
|
|
340 |
|
if (! $beenThere->{$key} && ! found_in_db(@data)) { |
341 |
|
print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2); |
342 |
|
$sth->{'insert_files'}->execute(@data); |
343 |
|
if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) { |
344 |
|
$new_dirs++; |
345 |
|
print STDERR " dir\n" if ($debug >= 2); |
346 |
|
} else { |
347 |
|
$new_files++; |
348 |
|
print STDERR " file\n" if ($debug >= 2); |
349 |
|
} |
350 |
|
} |
351 |
|
$beenThere->{$key}++; |
352 |
|
|
|
if (! $beenThere->{$key} && ! found_in_db(@data)) { |
|
|
$sth->{'insert_files'}->execute(@data); |
|
|
print STDERR "$key\n"; |
|
353 |
if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) { |
if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) { |
354 |
$new_dirs++; |
$nr_dirs++; |
355 |
|
|
356 |
|
my $full_path = $dir . '/' . $path_key; |
357 |
|
push @stack, $full_path; |
358 |
|
print STDERR "### store to stack: $full_path\n" if ($debug >= 3); |
359 |
|
|
360 |
|
# my ($f,$nf,$d,$nd) = recurseDir($bpc, $hostname, $backups, $backupNum, $share, $path_key, $shareID) unless ($beenThere->{$key}); |
361 |
|
# |
362 |
|
# $nr_files += $f; |
363 |
|
# $new_files += $nf; |
364 |
|
# $nr_dirs += $d; |
365 |
|
# $new_dirs += $nd; |
366 |
|
|
367 |
} else { |
} else { |
368 |
$new_files++; |
$nr_files++; |
369 |
} |
} |
370 |
} |
} |
|
$beenThere->{$key}++; |
|
371 |
|
|
372 |
if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) { |
print STDERR "## STACK ",join(", ", @stack),"\n" if ($debug >= 2); |
|
$nr_dirs++; |
|
|
|
|
|
my ($f,$nf,$d,$nd) = recurseDir($bpc, $hostname, $backups, $backupNum, $share, $path_key, $shareID); |
|
373 |
|
|
374 |
|
while ( my $dir = shift @stack ) { |
375 |
|
my ($f,$nf,$d,$nd) = recurseDir($bpc, $hostname, $files, $backupNum, $share, $dir, $shareID); |
376 |
|
print STDERR "# $dir f: $f nf: $nf d: $d nd: $nd\n" if ($debug >= 1); |
377 |
$nr_files += $f; |
$nr_files += $f; |
378 |
$new_files += $nf; |
$new_files += $nf; |
379 |
$nr_dirs += $d; |
$nr_dirs += $d; |
380 |
$new_dirs += $nd; |
$new_dirs += $nd; |
|
|
|
|
} else { |
|
|
$nr_files++; |
|
381 |
} |
} |
382 |
} |
} |
383 |
|
|