12 |
use File::Pid; |
use File::Pid; |
13 |
use POSIX qw/strftime/; |
use POSIX qw/strftime/; |
14 |
use BackupPC::SearchLib; |
use BackupPC::SearchLib; |
15 |
|
use Cwd qw/abs_path/; |
16 |
|
|
17 |
use constant BPC_FTYPE_DIR => 5; |
use constant BPC_FTYPE_DIR => 5; |
18 |
use constant EST_CHUNK => 100000; |
use constant EST_CHUNK => 100000; |
19 |
|
|
20 |
|
# daylight saving time change offset for 1h |
21 |
|
my $dst_offset = 60 * 60; |
22 |
|
|
23 |
my $debug = 0; |
my $debug = 0; |
24 |
$|=1; |
$|=1; |
25 |
|
|
26 |
my $start_t = time(); |
my $start_t = time(); |
27 |
|
|
28 |
my $pidfile = new File::Pid; |
my $pid_path = abs_path($0); |
29 |
|
$pid_path =~ s/\W+/_/g; |
30 |
|
|
31 |
|
my $pidfile = new File::Pid({ |
32 |
|
file => "/tmp/$pid_path", |
33 |
|
}); |
34 |
|
|
35 |
if (my $pid = $pidfile->running ) { |
if (my $pid = $pidfile->running ) { |
36 |
die "$0 already running: $pid\n"; |
die "$0 already running: $pid\n"; |
38 |
$pidfile->remove; |
$pidfile->remove; |
39 |
$pidfile = new File::Pid; |
$pidfile = new File::Pid; |
40 |
} |
} |
|
$pidfile->write; |
|
41 |
print STDERR "$0 using pid ",$pidfile->pid," file ",$pidfile->file,"\n"; |
print STDERR "$0 using pid ",$pidfile->pid," file ",$pidfile->file,"\n"; |
42 |
|
$pidfile->write; |
43 |
|
|
44 |
my $t_fmt = '%Y-%m-%d %H:%M:%S'; |
my $t_fmt = '%Y-%m-%d %H:%M:%S'; |
45 |
|
|
52 |
my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n"; |
my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n"; |
53 |
my $user = $Conf{SearchUser} || ''; |
my $user = $Conf{SearchUser} || ''; |
54 |
|
|
55 |
my $use_hest = $Conf{HyperEstraierIndex}; |
my $index_node_url = $Conf{HyperEstraierIndex}; |
|
my ($index_path, $index_node_url) = BackupPC::SearchLib::getHyperEstraier_url($use_hest); |
|
56 |
|
|
57 |
my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 }); |
my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 }); |
58 |
|
|
59 |
my %opt; |
my %opt; |
60 |
|
|
61 |
if ( !getopts("cdm:v:ij", \%opt ) ) { |
if ( !getopts("cdm:v:ijf", \%opt ) ) { |
62 |
print STDERR <<EOF; |
print STDERR <<EOF; |
63 |
usage: $0 [-c|-d] [-m num] [-v|-v level] [-i] |
usage: $0 [-c|-d] [-m num] [-v|-v level] [-i|-j|-f] |
64 |
|
|
65 |
Options: |
Options: |
66 |
-c create database on first use |
-c create database on first use |
69 |
-v num set verbosity (debug) level (default $debug) |
-v num set verbosity (debug) level (default $debug) |
70 |
-i update Hyper Estraier full text index |
-i update Hyper Estraier full text index |
71 |
-j update full text, don't check existing files |
-j update full text, don't check existing files |
72 |
|
-f don't do anything with full text index |
73 |
|
|
74 |
Option -j is variation on -i. It will allow faster initial creation |
Option -j is variation on -i. It will allow faster initial creation |
75 |
of full-text index from existing database. |
of full-text index from existing database. |
76 |
|
|
77 |
|
Option -f will create database which is out of sync with full text index. You |
78 |
|
will have to re-run $0 with -i to fix it. |
79 |
|
|
80 |
EOF |
EOF |
81 |
exit 1; |
exit 1; |
82 |
} |
} |
84 |
if ($opt{v}) { |
if ($opt{v}) { |
85 |
print "Debug level at $opt{v}\n"; |
print "Debug level at $opt{v}\n"; |
86 |
$debug = $opt{v}; |
$debug = $opt{v}; |
87 |
|
} elsif ($opt{f}) { |
88 |
|
print "WARNING: disabling full-text index update. You need to re-run $0 -j !\n"; |
89 |
|
$index_node_url = undef; |
90 |
} |
} |
91 |
|
|
92 |
#---- subs ---- |
#---- subs ---- |
126 |
|
|
127 |
my $skip_check = $opt{j} && print STDERR "Skipping check for existing files -- this should be used only with initital import\n"; |
my $skip_check = $opt{j} && print STDERR "Skipping check for existing files -- this should be used only with initital import\n"; |
128 |
|
|
129 |
unless ($use_hest) { |
unless (defined($index_node_url)) { |
130 |
print STDERR "HyperEstraier support not enabled in configuration\n"; |
print STDERR "HyperEstraier support not enabled in configuration\n"; |
131 |
|
$index_node_url = 0; |
132 |
return; |
return; |
133 |
} |
} |
134 |
|
|
135 |
print curr_time," updating HyperEstraier:"; |
print curr_time," updating Hyper Estraier:"; |
136 |
|
|
137 |
my $t = time(); |
my $t = time(); |
138 |
|
|
139 |
my $offset = 0; |
my $offset = 0; |
140 |
my $added = 0; |
my $added = 0; |
141 |
|
|
142 |
print " opening index $use_hest"; |
print " opening index $index_node_url"; |
143 |
if ($index_path) { |
if ($index_node_url) { |
144 |
$hest_db = HyperEstraier::Database->new(); |
$hest_node ||= Search::Estraier::Node->new($index_node_url); |
|
$hest_db->open($TopDir . $index_path, $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT); |
|
|
print " directly"; |
|
|
} elsif ($index_node_url) { |
|
|
$hest_node ||= HyperEstraier::Node->new($index_node_url); |
|
145 |
$hest_node->set_auth('admin', 'admin'); |
$hest_node->set_auth('admin', 'admin'); |
146 |
print " via node URL"; |
print " via node URL"; |
147 |
} else { |
} else { |
148 |
die "don't know how to use HyperEstraier Index $use_hest"; |
die "don't know how to use Hyper Estraier Index $index_node_url"; |
149 |
} |
} |
|
print " increment is " . EST_CHUNK . " files:"; |
|
150 |
|
|
151 |
my $results = 0; |
my $results = 0; |
152 |
|
|
194 |
if ($results == 0) { |
if ($results == 0) { |
195 |
print " - no new files\n"; |
print " - no new files\n"; |
196 |
last; |
last; |
197 |
|
} else { |
198 |
|
print " - $results files: "; |
199 |
} |
} |
200 |
|
|
201 |
sub fmt_date { |
sub fmt_date { |
216 |
} |
} |
217 |
|
|
218 |
# create a document object |
# create a document object |
219 |
my $doc = HyperEstraier::Document->new; |
my $doc = Search::Estraier::Document->new; |
220 |
|
|
221 |
# add attributes to the document object |
# add attributes to the document object |
222 |
$doc->add_attr('@uri', $uri); |
$doc->add_attr('@uri', $uri); |
223 |
|
|
224 |
foreach my $c (@{ $sth->{NAME} }) { |
foreach my $c (@{ $sth->{NAME} }) { |
225 |
$doc->add_attr($c, $row->{$c}) if ($row->{$c}); |
$doc->add_attr($c, $row->{$c}) if (defined($row->{$c})); |
226 |
} |
} |
227 |
|
|
228 |
#$doc->add_attr('@cdate', fmt_date($row->{'date'})); |
#$doc->add_attr('@cdate', fmt_date($row->{'date'})); |
236 |
print STDERR $doc->dump_draft,"\n" if ($debug > 1); |
print STDERR $doc->dump_draft,"\n" if ($debug > 1); |
237 |
|
|
238 |
# register the document object to the database |
# register the document object to the database |
239 |
if ($hest_db) { |
if ($hest_node) { |
|
$hest_db->put_doc($doc, $HyperEstraier::Database::PDCLEAN); |
|
|
} elsif ($hest_node) { |
|
240 |
$hest_node->put_doc($doc); |
$hest_node->put_doc($doc); |
241 |
} else { |
} else { |
242 |
die "not supported"; |
die "not supported"; |
245 |
} |
} |
246 |
|
|
247 |
print " $added"; |
print " $added"; |
|
$hest_db->sync() if ($index_path); |
|
248 |
|
|
249 |
$offset += EST_CHUNK; |
$offset += EST_CHUNK; |
250 |
|
|
251 |
} while ($results == EST_CHUNK); |
} while ($results == EST_CHUNK); |
252 |
|
|
|
if ($index_path) { |
|
|
print ", close"; |
|
|
$hest_db->close(); |
|
|
} |
|
|
|
|
253 |
my $dur = (time() - $t) || 1; |
my $dur = (time() - $t) || 1; |
254 |
printf(" [%.2f/s dur: %s]\n", |
printf(" [%.2f/s dur: %s]\n", |
255 |
( $added / $dur ), |
( $added / $dur ), |
261 |
|
|
262 |
|
|
263 |
## update index ## |
## update index ## |
264 |
if (($opt{i} || $opt{j} || ($index_path && ! -e $TopDir . $index_path)) && !$opt{c}) { |
if ( ( $opt{i} || $opt{j} ) && !$opt{c} ) { |
265 |
# update all |
# update all |
266 |
print "force update of HyperEstraier index "; |
print "force update of Hyper Estraier index "; |
|
print "importing existing data" unless (-e $TopDir . $index_path); |
|
267 |
print "by -i flag" if ($opt{i}); |
print "by -i flag" if ($opt{i}); |
268 |
print "by -j flag" if ($opt{j}); |
print "by -j flag" if ($opt{j}); |
269 |
print "\n"; |
print "\n"; |
438 |
|
|
439 |
$sth->{insert_backups} = $dbh->prepare(qq{ |
$sth->{insert_backups} = $dbh->prepare(qq{ |
440 |
INSERT INTO backups (hostID, num, date, type, shareid, size) |
INSERT INTO backups (hostID, num, date, type, shareid, size) |
441 |
VALUES (?,?,?,?,?,?) |
VALUES (?,?,?,?,?,-1) |
442 |
|
}); |
443 |
|
|
444 |
|
$sth->{update_backups_size} = $dbh->prepare(qq{ |
445 |
|
UPDATE backups SET size = ? |
446 |
|
WHERE hostID = ? and num = ? and date = ? and type =? and shareid = ? |
447 |
}); |
}); |
448 |
|
|
449 |
$sth->{insert_files} = $dbh->prepare(qq{ |
$sth->{insert_files} = $dbh->prepare(qq{ |
452 |
VALUES (?,?,?,?,?,?,?) |
VALUES (?,?,?,?,?,?,?) |
453 |
}); |
}); |
454 |
|
|
455 |
foreach my $host_key (keys %{$hosts}) { |
my @hosts = keys %{$hosts}; |
456 |
|
my $host_nr = 0; |
457 |
|
|
458 |
|
foreach my $host_key (@hosts) { |
459 |
|
|
460 |
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"; |
461 |
|
|
470 |
$hostID = $dbh->last_insert_id(undef,undef,'hosts',undef); |
$hostID = $dbh->last_insert_id(undef,undef,'hosts',undef); |
471 |
} |
} |
472 |
|
|
473 |
print "host ".$hosts->{$host_key}->{'host'}.": "; |
$host_nr++; |
474 |
|
print "host ", $hosts->{$host_key}->{'host'}, " [", |
475 |
|
$host_nr, "/", ($#hosts + 1), "]: "; |
476 |
|
|
477 |
# get backups for a host |
# get backups for a host |
478 |
my @backups = $bpc->BackupInfoRead($hostname); |
my @backups = $bpc->BackupInfoRead($hostname); |
514 |
# dump some log |
# dump some log |
515 |
print curr_time," ", $share; |
print curr_time," ", $share; |
516 |
|
|
|
my ($f, $nf, $d, $nd, $size) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID); |
|
|
|
|
517 |
$sth->{insert_backups}->execute( |
$sth->{insert_backups}->execute( |
518 |
$hostID, |
$hostID, |
519 |
$backupNum, |
$backupNum, |
520 |
$backup->{'endTime'}, |
$backup->{'endTime'}, |
521 |
substr($backup->{'type'},0,4), |
substr($backup->{'type'},0,4), |
522 |
$shareID, |
$shareID, |
|
$size, |
|
523 |
); |
); |
524 |
|
|
525 |
print " commit"; |
my ($f, $nf, $d, $nd, $size) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID); |
526 |
$dbh->commit(); |
|
527 |
|
eval { |
528 |
|
$sth->{update_backups_size}->execute( |
529 |
|
$size, |
530 |
|
$hostID, |
531 |
|
$backupNum, |
532 |
|
$backup->{'endTime'}, |
533 |
|
substr($backup->{'type'},0,4), |
534 |
|
$shareID, |
535 |
|
); |
536 |
|
print " commit"; |
537 |
|
$dbh->commit(); |
538 |
|
}; |
539 |
|
if ($@) { |
540 |
|
print " rollback"; |
541 |
|
$dbh->rollback(); |
542 |
|
} |
543 |
|
|
544 |
my $dur = (time() - $t) || 1; |
my $dur = (time() - $t) || 1; |
545 |
printf(" %d/%d files %d/%d dirs %0.2f MB [%.2f/s dur: %s]\n", |
printf(" %d/%d files %d/%d dirs %0.2f MB [%.2f/s dur: %s]\n", |
602 |
SELECT 1 FROM files |
SELECT 1 FROM files |
603 |
WHERE shareID = ? and |
WHERE shareID = ? and |
604 |
path = ? and |
path = ? and |
605 |
date = ? and |
size = ? and |
606 |
size = ? |
( date = ? or date = ? or date = ? ) |
607 |
LIMIT 1 |
LIMIT 1 |
608 |
}); |
}); |
609 |
|
|
610 |
my @param = ($shareID,$path,$date,$size); |
my @param = ($shareID,$path,$size,$date, $date-$dst_offset, $date+$dst_offset); |
611 |
$sth->{file_in_db}->execute(@param); |
$sth->{file_in_db}->execute(@param); |
612 |
my $rows = $sth->{file_in_db}->rows; |
my $rows = $sth->{file_in_db}->rows; |
613 |
print STDERR "## found_in_db($shareID,$path,$date,$size) ",( $rows ? '+' : '-' ), join(" ",@param), "\n" if ($debug >= 3); |
print STDERR "## found_in_db($shareID,$path,$date,$size) ",( $rows ? '+' : '-' ), join(" ",@param), "\n" if ($debug >= 3); |
657 |
$filesInBackup->{$path_key}->{'size'} |
$filesInBackup->{$path_key}->{'size'} |
658 |
)); |
)); |
659 |
|
|
660 |
|
my $key_dst_prev = join(" ", ( |
661 |
|
$shareID, |
662 |
|
$dir, |
663 |
|
$path_key, |
664 |
|
$filesInBackup->{$path_key}->{'mtime'} - $dst_offset, |
665 |
|
$filesInBackup->{$path_key}->{'size'} |
666 |
|
)); |
667 |
|
|
668 |
|
my $key_dst_next = join(" ", ( |
669 |
|
$shareID, |
670 |
|
$dir, |
671 |
|
$path_key, |
672 |
|
$filesInBackup->{$path_key}->{'mtime'} + $dst_offset, |
673 |
|
$filesInBackup->{$path_key}->{'size'} |
674 |
|
)); |
675 |
|
|
676 |
my $found; |
my $found; |
677 |
if (! defined($beenThere->{$key}) && ! ($found = found_in_db($key, @data)) ) { |
if ( |
678 |
|
! defined($beenThere->{$key}) && |
679 |
|
! defined($beenThere->{$key_dst_prev}) && |
680 |
|
! defined($beenThere->{$key_dst_next}) && |
681 |
|
! ($found = found_in_db($key, @data)) |
682 |
|
) { |
683 |
print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2); |
print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2); |
684 |
|
|
685 |
if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) { |
if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) { |