1 #============================================================= -*-perl-*-
3 # BackupPC::Storage::Text package
7 # This library defines a BackupPC::Storage::Text class that implements
8 # BackupPC's persistent state storage (config, host info, backup
9 # and restore info) using text files.
12 # Craig Barratt <cbarratt@users.sourceforge.net>
15 # Copyright (C) 2004 Craig Barratt
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 # GNU General Public License for more details.
27 # You should have received a copy of the GNU General Public License
28 # along with this program; if not, write to the Free Software
29 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #========================================================================
33 # Version 3.0.0alpha, released 23 Jan 2006.
35 # See http://backuppc.sourceforge.net.
37 #========================================================================
39 package BackupPC::Storage::Text;
50 my($flds, $paths) = @_;
64 foreach my $v ( keys(%$paths) ) {
65 $class->{$v} = $paths->{$v};
72 local(*BK_INFO, *LOCK);
75 flock(LOCK, LOCK_EX) if open(LOCK, "$s->{TopDir}/pc/$host/LOCK");
76 if ( open(BK_INFO, "$s->{TopDir}/pc/$host/backups") ) {
80 next if ( !/^(\d+\t(incr|full|partial).*)/ );
82 @{$Backups[@Backups]}{@{$s->{BackupFields}}} = split(/\t/);
92 my($s, $host, @Backups) = @_;
93 my($i, $contents, $fileOk);
96 # Generate the file contents
98 for ( $i = 0 ; $i < @Backups ; $i++ ) {
99 my %b = %{$Backups[$i]};
100 $contents .= join("\t", @b{@{$s->{BackupFields}}}) . "\n";
106 return $s->TextFileWrite("$s->{TopDir}/pc/$host/backups", $contents);
112 local(*RESTORE_INFO, *LOCK);
115 flock(LOCK, LOCK_EX) if open(LOCK, "$s->{TopDir}/pc/$host/LOCK");
116 if ( open(RESTORE_INFO, "$s->{TopDir}/pc/$host/restores") ) {
117 binmode(RESTORE_INFO);
118 while ( <RESTORE_INFO> ) {
120 next if ( !/^(\d+.*)/ );
122 @{$Restores[@Restores]}{@{$s->{RestoreFields}}} = split(/\t/);
132 my($s, $host, @Restores) = @_;
133 local(*RESTORE_INFO, *LOCK);
134 my($i, $contents, $fileOk);
137 # Generate the file contents
139 for ( $i = 0 ; $i < @Restores ; $i++ ) {
140 my %b = %{$Restores[$i]};
141 $contents .= join("\t", @b{@{$s->{RestoreFields}}}) . "\n";
147 return $s->TextFileWrite("$s->{TopDir}/pc/$host/restores", $contents);
153 local(*ARCHIVE_INFO, *LOCK);
156 flock(LOCK, LOCK_EX) if open(LOCK, "$s->{TopDir}/pc/$host/LOCK");
157 if ( open(ARCHIVE_INFO, "$s->{TopDir}/pc/$host/archives") ) {
158 binmode(ARCHIVE_INFO);
159 while ( <ARCHIVE_INFO> ) {
161 next if ( !/^(\d+.*)/ );
163 @{$Archives[@Archives]}{@{$s->{ArchiveFields}}} = split(/\t/);
173 my($s, $host, @Archives) = @_;
174 local(*ARCHIVE_INFO, *LOCK);
175 my($i, $contents, $fileOk);
178 # Generate the file contents
180 for ( $i = 0 ; $i < @Archives ; $i++ ) {
181 my %b = %{$Archives[$i]};
182 $contents .= join("\t", @b{@{$s->{ArchiveFields}}}) . "\n";
188 return $s->TextFileWrite("$s->{TopDir}/pc/$host/archives", $contents);
192 # Write a text file as safely as possible. We write to
193 # a new file, verify the file, and the rename the file.
194 # The previous version of the file is renamed with a
199 my($s, $file, $contents) = @_;
203 (my $dir = $file) =~ s{(.+)/(.+)}{$1};
205 mkpath($dir, 0, 0775) if ( !-d $dir );
206 if ( open(FD, ">", "$file.new") ) {
213 if ( open(FD, "<", "$file.new") ) {
215 if ( join("", <FD>) ne $contents ) {
216 return "TextFileWrite: Failed to verify $file.new";
226 if ( open(LOCK, "$dir/LOCK") || open(LOCK, ">", "$dir/LOCK") ) {
228 flock(LOCK, LOCK_EX);
231 unlink("$file.old") if ( -f "$file.old" );
232 rename("$file", "$file.old") if ( -f "$file" );
234 unlink("$file") if ( -f "$file" );
236 rename("$file.new", "$file") if ( -f "$file.new" );
237 close(LOCK) if ( $lock );
239 return "TextFileWrite: Failed to write $file.new";
248 return "$s->{ConfDir}/config.pl" if ( !defined($host) );
249 if ( $s->{useFHS} ) {
250 return "$s->{ConfDir}/host/$host.pl";
252 return "$s->{ConfDir}/$host.pl"
253 if ( $host ne "config" && -f "$s->{ConfDir}/$host.pl" );
254 return "$s->{TopDir}/pc/$host/config.pl";
261 my($ret, $mesg, $config, @configs);
267 my $configPath = $s->ConfigPath($host);
269 push(@configs, $configPath) if ( -f $configPath );
270 foreach $config ( @configs ) {
272 if ( !defined($ret = do $config) && ($! || $@) ) {
273 $mesg = "Couldn't open $config: $!" if ( $! );
274 $mesg = "Couldn't execute $config: $@" if ( $@ );
275 $mesg =~ s/[\n\r]+//;
276 return ($mesg, $conf);
278 %$conf = ( %$conf, %Conf );
281 # Promote BackupFilesOnly and BackupFilesExclude to hashes
283 foreach my $param qw(BackupFilesOnly BackupFilesExclude) {
284 next if ( !defined($conf->{$param}) || ref($conf->{$param}) eq "HASH" );
285 $conf->{$param} = [ $conf->{$param} ]
286 if ( ref($conf->{$param}) ne "ARRAY" );
287 $conf->{$param} = { "*" => $conf->{$param} };
290 return (undef, $conf);
295 my($s, $host, $newConf) = @_;
297 my $configPath = $s->ConfigPath($host);
299 my($err, $contents) = $s->ConfigFileMerge("$configPath", $newConf);
300 if ( defined($err) ) {
306 return $s->TextFileWrite($configPath, $contents);
312 my($s, $inFile, $newConf) = @_;
322 # Match existing settings in current config file
325 || return ("ConfigFileMerge: can't open/read $inFile", undef);
329 if ( $comment && /^\s*#/ ) {
331 } elsif ( /^\s*\$Conf\{([^}]*)\}\s*=/ ) {
333 if ( exists($newConf->{$var}) ) {
335 my $d = Data::Dumper->new([$newConf->{$var}], [*value]);
338 my $value = $d->Dump;
339 $value =~ s/(.*)\n/$1;\n/s;
340 $contents .= "\$Conf{$var} = " . $value;
343 if ( /^\s*\$Conf\{[^}]*} *= *<<(.*);/
344 || /^\s*\$Conf\{[^}]*} *= *<<'(.*)';/ ) {
349 $skipVar = /^[^#]*;/ ? 0 : 1;
352 } elsif ( $skipVar ) {
353 if ( !defined($endLine) && /^[^#]*;/ ) {
357 if ( defined($endLine) && /^\Q$endLine\E[\n\r]*$/ ) {
371 # Add new entries not matched in current config file
373 foreach my $var ( sort(keys(%$newConf)) ) {
374 next if ( $done->{$var} );
375 my $d = Data::Dumper->new([$newConf->{$var}], [*value]);
378 my $value = $d->Dump;
379 $value =~ s/(.*)\n/$1;\n/s;
380 $contents .= "\$Conf{$var} = " . $value;
383 return (undef, $contents);
387 # Return the mtime of the config file
392 return (stat($s->ConfigPath()))[9];
396 # Returns information from the host file in $s->{ConfDir}/hosts.
397 # With no argument a ref to a hash of hosts is returned. Each
398 # hash contains fields as specified in the hosts file. With an
399 # argument a ref to a single hash is returned with information
400 # for just that host.
405 my(%hosts, @hdr, @fld);
406 local(*HOST_INFO, *LOCK);
408 flock(LOCK, LOCK_EX) if open(LOCK, "$s->{ConfDir}/LOCK");
409 if ( !open(HOST_INFO, "$s->{ConfDir}/hosts") ) {
410 print(STDERR "Can't open $s->{ConfDir}/hosts\n");
415 while ( <HOST_INFO> ) {
419 next if ( /^\s*$/ || !/^([\w\.\\-]+\s+.*)/ );
421 # Split on white space, except if preceded by \
422 # using zero-width negative look-behind assertion
423 # (always wanted to use one of those).
425 @fld = split(/(?<!\\)\s+/, $1);
433 if ( defined($host) ) {
434 next if ( lc($fld[0]) ne lc($host) );
435 @{$hosts{$fld[0]}}{@hdr} = @fld;
440 @{$hosts{$fld[0]}}{@hdr} = @fld;
452 # Writes new hosts information to the hosts file in $s->{ConfDir}/hosts.
453 # With no argument a ref to a hash of hosts is returned. Each
454 # hash contains fields as specified in the hosts file. With an
455 # argument a ref to a single hash is returned with information
456 # for just that host.
461 my($gotHdr, @fld, $hostText, $contents);
464 if ( !open(HOST_INFO, "$s->{ConfDir}/hosts") ) {
465 return "Can't open $s->{ConfDir}/hosts";
467 foreach my $host ( keys(%$hosts) ) {
468 my $name = "$hosts->{$host}{host}";
469 my $rest = "\t$hosts->{$host}{dhcp}"
470 . "\t$hosts->{$host}{user}"
471 . "\t$hosts->{$host}{moreUsers}";
474 $hostText->{$host} = $name . $rest;
477 while ( <HOST_INFO> ) {
479 if ( /^\s*$/ || /^\s*#/ ) {
480 $contents .= $_ . "\n";
484 $contents .= $_ . "\n";
488 @fld = split(/(?<!\\)\s+/, $1);
495 if ( defined($hostText->{$fld[0]}) ) {
496 $contents .= $hostText->{$fld[0]} . "\n";
497 delete($hostText->{$fld[0]});
500 foreach my $host ( sort(keys(%$hostText)) ) {
501 $contents .= $hostText->{$host} . "\n";
502 delete($hostText->{$host});
507 # Write and verify the new host file
509 return $s->TextFileWrite("$s->{ConfDir}/hosts", $contents);
513 # Return the mtime of the hosts file
518 return (stat("$s->{ConfDir}/hosts"))[9];