40 |
|
|
41 |
my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n"; |
my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n"; |
42 |
my $user = $Conf{SearchUser} || ''; |
my $user = $Conf{SearchUser} || ''; |
43 |
|
my $index_path = $Conf{HyperEstraierIndex}; |
44 |
|
|
45 |
my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 }); |
my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 }); |
46 |
|
|
47 |
my %opt; |
my %opt; |
48 |
|
|
49 |
if ( !getopts("cdm:v:", \%opt ) ) { |
if ( !getopts("cdm:v:i", \%opt ) ) { |
50 |
print STDERR <<EOF; |
print STDERR <<EOF; |
51 |
usage: $0 [-c|-d] [-m num] [-v|-v level] |
usage: $0 [-c|-d] [-m num] [-v|-v level] [-i] |
52 |
|
|
53 |
Options: |
Options: |
54 |
-c create database on first use |
-c create database on first use |
55 |
-d delete database before import |
-d delete database before import |
56 |
-m num import just num increments for one host |
-m num import just num increments for one host |
57 |
-v num set verbosity (debug) level (default $debug) |
-v num set verbosity (debug) level (default $debug) |
58 |
|
-i update HyperEstraier full text index |
59 |
EOF |
EOF |
60 |
exit 1; |
exit 1; |
61 |
} |
} |
62 |
|
|
63 |
|
if ($opt{v}) { |
64 |
|
print "Debug level at $opt{v}\n"; |
65 |
|
$debug = $opt{v}; |
66 |
|
} |
67 |
|
|
68 |
|
#---- subs ---- |
69 |
|
|
70 |
|
sub fmt_time { |
71 |
|
my $t = shift || return; |
72 |
|
my $out = ""; |
73 |
|
my ($ss,$mm,$hh) = gmtime($t); |
74 |
|
$out .= "${hh}h" if ($hh); |
75 |
|
$out .= sprintf("%02d:%02d", $mm,$ss); |
76 |
|
return $out; |
77 |
|
} |
78 |
|
|
79 |
|
sub curr_time { |
80 |
|
return strftime($t_fmt,localtime()); |
81 |
|
} |
82 |
|
|
83 |
|
#---- /subs ---- |
84 |
|
|
85 |
|
## update index ## |
86 |
|
if ($opt{i}) { |
87 |
|
|
88 |
|
print curr_time," updating HyperEstraier: files"; |
89 |
|
|
90 |
|
my $t = time(); |
91 |
|
|
92 |
|
my $sth = $dbh->prepare(qq{ |
93 |
|
SELECT |
94 |
|
files.id AS fid, |
95 |
|
hosts.name AS hname, |
96 |
|
shares.name AS sname, |
97 |
|
-- shares.share AS sharename, |
98 |
|
files.backupnum AS backupnum, |
99 |
|
-- files.name AS filename, |
100 |
|
files.path AS filepath, |
101 |
|
files.date AS date, |
102 |
|
files.type AS filetype, |
103 |
|
files.size AS size, |
104 |
|
files.shareid AS shareid, |
105 |
|
backups.date AS backup_date |
106 |
|
FROM files |
107 |
|
INNER JOIN shares ON files.shareID=shares.ID |
108 |
|
INNER JOIN hosts ON hosts.ID = shares.hostID |
109 |
|
INNER JOIN backups ON backups.num = files.backupNum and backups.hostID = hosts.ID AND backups.shareID = shares.ID |
110 |
|
}); |
111 |
|
|
112 |
|
$sth->execute(); |
113 |
|
my $results = $sth->rows; |
114 |
|
|
115 |
|
my $dot = int($results / 15); |
116 |
|
|
117 |
|
print " $results ($dot/#)"; |
118 |
|
|
119 |
|
sub fmt_date { |
120 |
|
my $t = shift || return; |
121 |
|
my $iso = BackupPC::Lib::timeStamp($t); |
122 |
|
$iso =~ s/\s/T/; |
123 |
|
return $iso; |
124 |
|
} |
125 |
|
|
126 |
|
my $i = 0; |
127 |
|
my $max = int($results / $dot); |
128 |
|
|
129 |
|
$index_path = $TopDir . '/' . $index_path; |
130 |
|
$index_path =~ s#//#/#g; |
131 |
|
|
132 |
|
print " index $index_path..."; |
133 |
|
use HyperEstraier; |
134 |
|
my $db = HyperEstraier::Database->new(); |
135 |
|
$db->open($index_path, $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT); |
136 |
|
|
137 |
|
|
138 |
|
while (my $row = $sth->fetchrow_hashref()) { |
139 |
|
|
140 |
|
# create a document object |
141 |
|
my $doc = HyperEstraier::Document->new; |
142 |
|
|
143 |
|
# add attributes to the document object |
144 |
|
$doc->add_attr('@uri', 'file:///' . $row->{'fid'}); |
145 |
|
|
146 |
|
foreach my $c (@{ $sth->{NAME} }) { |
147 |
|
$doc->add_attr($c, $row->{$c}) if ($row->{$c}); |
148 |
|
} |
149 |
|
|
150 |
|
#$doc->add_attr('@cdate', fmt_date($row->{'date'})); |
151 |
|
|
152 |
|
# add the body text to the document object |
153 |
|
my $path = $row->{'filepath'}; |
154 |
|
$doc->add_text($path); |
155 |
|
$path =~ s/(.)/$1 /g; |
156 |
|
$doc->add_hidden_text($path); |
157 |
|
|
158 |
|
print STDERR $doc->dump_draft,"\n" if ($debug > 1); |
159 |
|
|
160 |
|
# register the document object to the database |
161 |
|
$db->put_doc($doc, $HyperEstraier::Database::PDCLEAN); |
162 |
|
|
163 |
|
$i++; |
164 |
|
if ($i % $dot == 0) { |
165 |
|
print "$max "; |
166 |
|
$max--; |
167 |
|
} |
168 |
|
|
169 |
|
} |
170 |
|
|
171 |
|
print "sync"; |
172 |
|
$db->sync(); |
173 |
|
print " close"; |
174 |
|
$db->close(); |
175 |
|
|
176 |
|
my $dur = (time() - $t) || 1; |
177 |
|
printf(" [%.2f/s dur: %s]\n", |
178 |
|
( $results / $dur ), |
179 |
|
fmt_time($dur) |
180 |
|
); |
181 |
|
|
182 |
|
exit; |
183 |
|
} |
184 |
|
|
185 |
###################################create tables############################3 |
###################################create tables############################3 |
186 |
|
|
187 |
if ($opt{c}) { |
if ($opt{c}) { |
284 |
$dbh->commit; |
$dbh->commit; |
285 |
} |
} |
286 |
|
|
|
if ($opt{v}) { |
|
|
print "Debug level at $opt{v}\n"; |
|
|
$debug = $opt{v}; |
|
|
} |
|
|
|
|
287 |
#################################INSERT VALUES############################# |
#################################INSERT VALUES############################# |
288 |
|
|
289 |
# get hosts |
# get hosts |
318 |
VALUES (?,?,?,?,?,?,?) |
VALUES (?,?,?,?,?,?,?) |
319 |
}); |
}); |
320 |
|
|
|
sub fmt_time { |
|
|
my $t = shift || return; |
|
|
my $out = ""; |
|
|
my ($ss,$mm,$hh) = gmtime($t); |
|
|
$out .= "${hh}h" if ($hh); |
|
|
$out .= sprintf("%02d:%02d", $mm,$ss); |
|
|
return $out; |
|
|
} |
|
|
|
|
321 |
foreach my $host_key (keys %{$hosts}) { |
foreach my $host_key (keys %{$hosts}) { |
322 |
|
|
323 |
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"; |
333 |
$hostID = $dbh->last_insert_id(undef,undef,'hosts',undef); |
$hostID = $dbh->last_insert_id(undef,undef,'hosts',undef); |
334 |
} |
} |
335 |
|
|
336 |
print("host ".$hosts->{$host_key}->{'host'}.": "); |
print "host ".$hosts->{$host_key}->{'host'}.": "; |
337 |
|
|
338 |
# get backups for a host |
# get backups for a host |
339 |
my @backups = $bpc->BackupInfoRead($hostname); |
my @backups = $bpc->BackupInfoRead($hostname); |
340 |
print scalar @backups, " increments\n"; |
my $incs = scalar @backups; |
341 |
|
print "$incs increments\n"; |
342 |
|
|
343 |
my $inc_nr = 0; |
my $inc_nr = 0; |
344 |
|
$beenThere = {}; |
345 |
|
|
346 |
foreach my $backup (@backups) { |
foreach my $backup (@backups) { |
347 |
|
|
351 |
my $backupNum = $backup->{'num'}; |
my $backupNum = $backup->{'num'}; |
352 |
my @backupShares = (); |
my @backupShares = (); |
353 |
|
|
354 |
print $hosts->{$host_key}->{'host'}, |
printf("%-10s %2d/%-2d #%-2d %s %5s/%5s files (date: %s dur: %s)\n", |
355 |
"\t#$backupNum\t", $backup->{type} || '?', " ", |
$hosts->{$host_key}->{'host'}, |
356 |
$backup->{nFilesNew} || '?', "/", $backup->{nFiles} || '?', |
$inc_nr, $incs, $backupNum, |
357 |
" files (date: ", |
$backup->{type} || '?', |
358 |
|
$backup->{nFilesNew} || '?', $backup->{nFiles} || '?', |
359 |
strftime($t_fmt,localtime($backup->{startTime})), |
strftime($t_fmt,localtime($backup->{startTime})), |
360 |
" dur: ", |
fmt_time($backup->{endTime} - $backup->{startTime}) |
361 |
fmt_time($backup->{endTime} - $backup->{startTime}), |
); |
|
")\n"; |
|
362 |
|
|
363 |
my $files = BackupPC::View->new($bpc, $hostname, \@backups, 1); |
my $files = BackupPC::View->new($bpc, $hostname, \@backups, 1); |
364 |
foreach my $share ($files->shareList($backupNum)) { |
foreach my $share ($files->shareList($backupNum)) { |
373 |
next if ($count > 0); |
next if ($count > 0); |
374 |
|
|
375 |
# dump some log |
# dump some log |
376 |
print strftime($t_fmt,localtime())," ", $share; |
print curr_time," ", $share; |
377 |
|
|
378 |
my ($f, $nf, $d, $nd, $size) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID); |
my ($f, $nf, $d, $nd, $size) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID); |
379 |
|
|
440 |
my @data = @_; |
my @data = @_; |
441 |
shift @data; |
shift @data; |
442 |
|
|
443 |
my ($key, $shareID,undef,$name,$path,undef,$date,undef,$size) = @_; |
my ($key, $shareID,undef,$name,$path,$date,undef,$size) = @_; |
444 |
|
|
445 |
return $beenThere->{$key} if (defined($beenThere->{$key})); |
return $beenThere->{$key} if (defined($beenThere->{$key})); |
446 |
|
|
448 |
SELECT 1 FROM files |
SELECT 1 FROM files |
449 |
WHERE shareID = ? and |
WHERE shareID = ? and |
450 |
path = ? and |
path = ? and |
|
name = ? and |
|
451 |
date = ? and |
date = ? and |
452 |
size = ? |
size = ? |
453 |
|
LIMIT 1 |
454 |
}); |
}); |
455 |
|
|
456 |
my @param = ($shareID,$path,$name,$date,$size); |
my @param = ($shareID,$path,$date,$size); |
457 |
$sth->{file_in_db}->execute(@param); |
$sth->{file_in_db}->execute(@param); |
458 |
my $rows = $sth->{file_in_db}->rows; |
my $rows = $sth->{file_in_db}->rows; |
459 |
print STDERR "## found_in_db ",( $rows ? '+' : '-' ), join(" ",@param), "\n" if ($debug >= 3); |
print STDERR "## found_in_db($shareID,$path,$date,$size) ",( $rows ? '+' : '-' ), join(" ",@param), "\n" if ($debug >= 3); |
460 |
|
|
461 |
$beenThere->{$key}++; |
$beenThere->{$key}++; |
462 |
|
|
503 |
$filesInBackup->{$path_key}->{'size'} |
$filesInBackup->{$path_key}->{'size'} |
504 |
)); |
)); |
505 |
|
|
506 |
|
my $found; |
507 |
if (! defined($beenThere->{$key}) && ! found_in_db($key, @data)) { |
if (! defined($beenThere->{$key}) && ! ($found = found_in_db($key, @data)) ) { |
508 |
print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2); |
print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2); |
509 |
|
|
510 |
if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) { |
if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) { |
511 |
$new_dirs++; |
$new_dirs++ unless ($found); |
512 |
print STDERR " dir\n" if ($debug >= 2); |
print STDERR " dir\n" if ($debug >= 2); |
513 |
} else { |
} else { |
514 |
$new_files++; |
$new_files++ unless ($found); |
515 |
print STDERR " file\n" if ($debug >= 2); |
print STDERR " file\n" if ($debug >= 2); |
516 |
} |
} |
517 |
$size += $filesInBackup->{$path_key}->{'size'} || 0; |
$size += $filesInBackup->{$path_key}->{'size'} || 0; |