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 |
|
## update index ## |
69 |
|
if ($opt{i}) { |
70 |
|
|
71 |
|
print "updating HyperEstraier files "; |
72 |
|
|
73 |
|
my $sth = $dbh->prepare(qq{ |
74 |
|
SELECT |
75 |
|
files.id AS fid, |
76 |
|
hosts.name AS hname, |
77 |
|
shares.name AS sname, |
78 |
|
shares.share AS sharename, |
79 |
|
files.backupNum AS backupNum, |
80 |
|
files.name AS filename, |
81 |
|
files.path AS filepath, |
82 |
|
files.date AS date, |
83 |
|
files.type AS filetype, |
84 |
|
files.size AS size, |
85 |
|
files.shareid AS shareid |
86 |
|
FROM files |
87 |
|
INNER JOIN shares ON files.shareID=shares.ID |
88 |
|
INNER JOIN hosts ON hosts.ID = shares.hostID |
89 |
|
INNER JOIN backups ON backups.num = files.backupNum and backups.hostID = hosts.ID AND backups.shareID = shares.ID |
90 |
|
}); |
91 |
|
|
92 |
|
$sth->execute(); |
93 |
|
|
94 |
|
my $dot = int($sth->rows / 15); |
95 |
|
|
96 |
|
print $sth->rows, " files ($dot/#) "; |
97 |
|
|
98 |
|
sub fmt_date { |
99 |
|
my $t = shift || return; |
100 |
|
my $iso = BackupPC::Lib::timeStamp($t); |
101 |
|
$iso =~ s/\s/T/; |
102 |
|
return $iso; |
103 |
|
} |
104 |
|
|
105 |
|
my $i = 0; |
106 |
|
my $max = int($sth->rows / $dot); |
107 |
|
|
108 |
|
$index_path = $TopDir . '/' . $index_path; |
109 |
|
$index_path =~ s#//#/#g; |
110 |
|
|
111 |
|
print "index $index_path..."; |
112 |
|
use HyperEstraier; |
113 |
|
my $db = HyperEstraier::Database->new(); |
114 |
|
$db->open($index_path, $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT); |
115 |
|
|
116 |
|
|
117 |
|
while (my $row = $sth->fetchrow_hashref()) { |
118 |
|
|
119 |
|
# create a document object |
120 |
|
my $doc = HyperEstraier::Document->new; |
121 |
|
|
122 |
|
# add attributes to the document object |
123 |
|
$doc->add_attr('@uri', 'file:///' . $row->{'fid'}); |
124 |
|
|
125 |
|
foreach my $c (qw/fid hname sname sharename backupNum filename filepath shareid/) { |
126 |
|
$doc->add_attr($c, $row->{$c}) if ($row->{$c}); |
127 |
|
} |
128 |
|
|
129 |
|
$doc->add_attr('date', fmt_date($row->{'date'})); |
130 |
|
|
131 |
|
# add the body text to the document object |
132 |
|
my $path = $row->{'filepath'}; |
133 |
|
$doc->add_text($path); |
134 |
|
$path =~ s/(.)/$1 /g; |
135 |
|
$doc->add_hidden_text($path); |
136 |
|
|
137 |
|
print STDERR $doc->dump_draft,"\n" if ($debug > 1); |
138 |
|
|
139 |
|
# register the document object to the database |
140 |
|
$db->put_doc($doc, $HyperEstraier::Database::PDCLEAN); |
141 |
|
|
142 |
|
$i++; |
143 |
|
if ($i % $dot == 0) { |
144 |
|
print "$max "; |
145 |
|
$max--; |
146 |
|
} |
147 |
|
|
148 |
|
} |
149 |
|
|
150 |
|
print "sync"; |
151 |
|
$db->sync(); |
152 |
|
print " close\n"; |
153 |
|
$db->close(); |
154 |
|
|
155 |
|
exit; |
156 |
|
} |
157 |
|
|
158 |
###################################create tables############################3 |
###################################create tables############################3 |
159 |
|
|
160 |
if ($opt{c}) { |
if ($opt{c}) { |
257 |
$dbh->commit; |
$dbh->commit; |
258 |
} |
} |
259 |
|
|
|
if ($opt{v}) { |
|
|
print "Debug level at $opt{v}\n"; |
|
|
$debug = $opt{v}; |
|
|
} |
|
|
|
|
260 |
#################################INSERT VALUES############################# |
#################################INSERT VALUES############################# |
261 |
|
|
262 |
# get hosts |
# get hosts |
422 |
my @data = @_; |
my @data = @_; |
423 |
shift @data; |
shift @data; |
424 |
|
|
425 |
my ($key, $shareID,undef,$name,$path,undef,$date,undef,$size) = @_; |
my ($key, $shareID,undef,$name,$path,$date,undef,$size) = @_; |
426 |
|
|
427 |
return $beenThere->{$key} if (defined($beenThere->{$key})); |
return $beenThere->{$key} if (defined($beenThere->{$key})); |
428 |
|
|
432 |
path = ? and |
path = ? and |
433 |
date = ? and |
date = ? and |
434 |
size = ? |
size = ? |
435 |
|
LIMIT 1 |
436 |
}); |
}); |
437 |
|
|
438 |
my @param = ($shareID,$path,$date,$size); |
my @param = ($shareID,$path,$date,$size); |
439 |
$sth->{file_in_db}->execute(@param); |
$sth->{file_in_db}->execute(@param); |
440 |
my $rows = $sth->{file_in_db}->rows; |
my $rows = $sth->{file_in_db}->rows; |
441 |
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); |
442 |
|
|
443 |
$beenThere->{$key}++; |
$beenThere->{$key}++; |
444 |
|
|
485 |
$filesInBackup->{$path_key}->{'size'} |
$filesInBackup->{$path_key}->{'size'} |
486 |
)); |
)); |
487 |
|
|
488 |
|
my $found; |
489 |
if (! defined($beenThere->{$key}) && ! found_in_db($key, @data)) { |
if (! defined($beenThere->{$key}) && ! ($found = found_in_db($key, @data)) ) { |
490 |
print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2); |
print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2); |
491 |
|
|
492 |
if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) { |
if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) { |
493 |
$new_dirs++; |
$new_dirs++ unless ($found); |
494 |
print STDERR " dir\n" if ($debug >= 2); |
print STDERR " dir\n" if ($debug >= 2); |
495 |
} else { |
} else { |
496 |
$new_files++; |
$new_files++ unless ($found); |
497 |
print STDERR " file\n" if ($debug >= 2); |
print STDERR " file\n" if ($debug >= 2); |
498 |
} |
} |
499 |
$size += $filesInBackup->{$path_key}->{'size'} || 0; |
$size += $filesInBackup->{$path_key}->{'size'} || 0; |