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.0, released 28 Jan 2007.
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}/pc/$host.pl";
252 return "$s->{TopDir}/pc/$host/config.pl"
253 if ( -f "$s->{TopDir}/pc/$host/config.pl" );
254 return "$s->{ConfDir}/$host.pl"
255 if ( $host ne "config" && -f "$s->{ConfDir}/$host.pl" );
256 return "$s->{ConfDir}/pc/$host.pl";
263 my($ret, $mesg, $config, @configs);
269 my $configPath = $s->ConfigPath($host);
271 push(@configs, $configPath) if ( -f $configPath );
272 foreach $config ( @configs ) {
274 if ( !defined($ret = do $config) && ($! || $@) ) {
275 $mesg = "Couldn't open $config: $!" if ( $! );
276 $mesg = "Couldn't execute $config: $@" if ( $@ );
277 $mesg =~ s/[\n\r]+//;
278 return ($mesg, $conf);
280 %$conf = ( %$conf, %Conf );
283 # Promote BackupFilesOnly and BackupFilesExclude to hashes
285 foreach my $param qw(BackupFilesOnly BackupFilesExclude) {
286 next if ( !defined($conf->{$param}) || ref($conf->{$param}) eq "HASH" );
287 $conf->{$param} = [ $conf->{$param} ]
288 if ( ref($conf->{$param}) ne "ARRAY" );
289 $conf->{$param} = { "*" => $conf->{$param} };
293 # Handle backward compatibility with defunct BlackoutHourBegin,
294 # BlackoutHourEnd, and BlackoutWeekDays parameters.
296 if ( defined($conf->{BlackoutHourBegin}) ) {
297 push(@{$conf->{BlackoutPeriods}},
299 hourBegin => $conf->{BlackoutHourBegin},
300 hourEnd => $conf->{BlackoutHourEnd},
301 weekDays => $conf->{BlackoutWeekDays},
304 delete($conf->{BlackoutHourBegin});
305 delete($conf->{BlackoutHourEnd});
306 delete($conf->{BlackoutWeekDays});
309 return (undef, $conf);
314 my($s, $host, $newConf) = @_;
316 my $configPath = $s->ConfigPath($host);
318 my($err, $contents) = $s->ConfigFileMerge("$configPath", $newConf);
319 if ( defined($err) ) {
325 return $s->TextFileWrite($configPath, $contents);
331 my($s, $inFile, $newConf) = @_;
333 my($contents, $skipExpr, $fakeVar);
338 # Match existing settings in current config file
341 || return ("ConfigFileMerge: can't open/read $inFile", undef);
345 if ( /^\s*\$Conf\{([^}]*)\}\s*=(.*)/ ) {
347 $skipExpr = "\$fakeVar = $2\n";
348 if ( exists($newConf->{$var}) ) {
349 my $d = Data::Dumper->new([$newConf->{$var}], [*value]);
352 my $value = $d->Dump;
353 $value =~ s/(.*)\n/$1;\n/s;
354 $contents .= "\$Conf{$var} = " . $value;
357 } elsif ( defined($skipExpr) ) {
362 if ( defined($skipExpr)
363 && ($skipExpr =~ /^\$fakeVar = *<</
364 || $skipExpr =~ /;[\n\r]*$/) ) {
366 # if we have a complete expression, then we are done
367 # skipping text from the original config file.
369 $skipExpr = $1 if ( $skipExpr =~ /(.*)/s );
371 $skipExpr = undef if ( $@ eq "" );
378 # Add new entries not matched in current config file
380 foreach my $var ( sort(keys(%$newConf)) ) {
381 next if ( $done->{$var} );
382 my $d = Data::Dumper->new([$newConf->{$var}], [*value]);
385 my $value = $d->Dump;
386 $value =~ s/(.*)\n/$1;\n/s;
387 $contents .= "\$Conf{$var} = " . $value;
390 return (undef, $contents);
394 # Return the mtime of the config file
399 return (stat($s->ConfigPath()))[9];
403 # Returns information from the host file in $s->{ConfDir}/hosts.
404 # With no argument a ref to a hash of hosts is returned. Each
405 # hash contains fields as specified in the hosts file. With an
406 # argument a ref to a single hash is returned with information
407 # for just that host.
412 my(%hosts, @hdr, @fld);
413 local(*HOST_INFO, *LOCK);
415 flock(LOCK, LOCK_EX) if open(LOCK, "$s->{ConfDir}/LOCK");
416 if ( !open(HOST_INFO, "$s->{ConfDir}/hosts") ) {
417 print(STDERR "Can't open $s->{ConfDir}/hosts\n");
422 while ( <HOST_INFO> ) {
426 next if ( /^\s*$/ || !/^([\w\.\\-]+\s+.*)/ );
428 # Split on white space, except if preceded by \
429 # using zero-width negative look-behind assertion
430 # (always wanted to use one of those).
432 @fld = split(/(?<!\\)\s+/, $1);
440 if ( defined($host) ) {
441 next if ( lc($fld[0]) ne lc($host) );
442 @{$hosts{lc($fld[0])}}{@hdr} = @fld;
447 @{$hosts{lc($fld[0])}}{@hdr} = @fld;
459 # Writes new hosts information to the hosts file in $s->{ConfDir}/hosts.
460 # With no argument a ref to a hash of hosts is returned. Each
461 # hash contains fields as specified in the hosts file. With an
462 # argument a ref to a single hash is returned with information
463 # for just that host.
468 my($gotHdr, @fld, $hostText, $contents);
471 if ( !open(HOST_INFO, "$s->{ConfDir}/hosts") ) {
472 return "Can't open $s->{ConfDir}/hosts";
474 foreach my $host ( keys(%$hosts) ) {
475 my $name = "$hosts->{$host}{host}";
476 my $rest = "\t$hosts->{$host}{dhcp}"
477 . "\t$hosts->{$host}{user}"
478 . "\t$hosts->{$host}{moreUsers}";
481 $hostText->{$host} = $name . $rest;
484 while ( <HOST_INFO> ) {
486 if ( /^\s*$/ || /^\s*#/ ) {
487 $contents .= $_ . "\n";
491 $contents .= $_ . "\n";
495 @fld = split(/(?<!\\)\s+/, $1);
502 if ( defined($hostText->{$fld[0]}) ) {
503 $contents .= $hostText->{$fld[0]} . "\n";
504 delete($hostText->{$fld[0]});
507 foreach my $host ( sort(keys(%$hostText)) ) {
508 $contents .= $hostText->{$host} . "\n";
509 delete($hostText->{$host});
514 # Write and verify the new host file
516 return $s->TextFileWrite("$s->{ConfDir}/hosts", $contents);
520 # Return the mtime of the hosts file
525 return (stat("$s->{ConfDir}/hosts"))[9];