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-2009 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.2.0, released 31 Jul 2010.
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/);
88 # Default the version field. Prior to 3.0.0 the xferMethod
89 # field is empty, so we use that to figure out the version.
91 for ( my $i = 0 ; $i < @Backups ; $i++ ) {
92 next if ( $Backups[$i]{version} ne "" );
93 if ( $Backups[$i]{xferMethod} eq "" ) {
94 $Backups[$i]{version} = "2.1.2";
96 $Backups[$i]{version} = "3.0.0";
104 my($s, $host, @Backups) = @_;
105 my($i, $contents, $fileOk);
108 # Generate the file contents
110 for ( $i = 0 ; $i < @Backups ; $i++ ) {
111 my %b = %{$Backups[$i]};
112 $contents .= join("\t", @b{@{$s->{BackupFields}}}) . "\n";
118 return $s->TextFileWrite("$s->{TopDir}/pc/$host/backups", $contents);
124 local(*RESTORE_INFO, *LOCK);
127 flock(LOCK, LOCK_EX) if open(LOCK, "$s->{TopDir}/pc/$host/LOCK");
128 if ( open(RESTORE_INFO, "$s->{TopDir}/pc/$host/restores") ) {
129 binmode(RESTORE_INFO);
130 while ( <RESTORE_INFO> ) {
132 next if ( !/^(\d+.*)/ );
134 @{$Restores[@Restores]}{@{$s->{RestoreFields}}} = split(/\t/);
144 my($s, $host, @Restores) = @_;
145 local(*RESTORE_INFO, *LOCK);
146 my($i, $contents, $fileOk);
149 # Generate the file contents
151 for ( $i = 0 ; $i < @Restores ; $i++ ) {
152 my %b = %{$Restores[$i]};
153 $contents .= join("\t", @b{@{$s->{RestoreFields}}}) . "\n";
159 return $s->TextFileWrite("$s->{TopDir}/pc/$host/restores", $contents);
165 local(*ARCHIVE_INFO, *LOCK);
168 flock(LOCK, LOCK_EX) if open(LOCK, "$s->{TopDir}/pc/$host/LOCK");
169 if ( open(ARCHIVE_INFO, "$s->{TopDir}/pc/$host/archives") ) {
170 binmode(ARCHIVE_INFO);
171 while ( <ARCHIVE_INFO> ) {
173 next if ( !/^(\d+.*)/ );
175 @{$Archives[@Archives]}{@{$s->{ArchiveFields}}} = split(/\t/);
185 my($s, $host, @Archives) = @_;
186 local(*ARCHIVE_INFO, *LOCK);
187 my($i, $contents, $fileOk);
190 # Generate the file contents
192 for ( $i = 0 ; $i < @Archives ; $i++ ) {
193 my %b = %{$Archives[$i]};
194 $contents .= join("\t", @b{@{$s->{ArchiveFields}}}) . "\n";
200 return $s->TextFileWrite("$s->{TopDir}/pc/$host/archives", $contents);
204 # Write a text file as safely as possible. We write to
205 # a new file, verify the file, and the rename the file.
206 # The previous version of the file is renamed with a
211 my($s, $file, $contents) = @_;
215 (my $dir = $file) =~ s{(.+)/(.+)}{$1};
218 eval { mkpath($dir, 0, 0775) };
219 return "TextFileWrite: can't create directory $dir" if ( $@ );
221 if ( open(FD, ">", "$file.new") ) {
228 if ( open(FD, "<", "$file.new") ) {
230 if ( join("", <FD>) ne $contents ) {
231 return "TextFileWrite: Failed to verify $file.new";
241 if ( open(LOCK, "$dir/LOCK") || open(LOCK, ">", "$dir/LOCK") ) {
243 flock(LOCK, LOCK_EX);
246 unlink("$file.old") if ( -f "$file.old" );
247 rename("$file", "$file.old") if ( -f "$file" );
249 unlink("$file") if ( -f "$file" );
251 rename("$file.new", "$file") if ( -f "$file.new" );
252 close(LOCK) if ( $lock );
254 return "TextFileWrite: Failed to write $file.new";
263 return "$s->{ConfDir}/config.pl" if ( !defined($host) );
264 if ( $s->{useFHS} ) {
265 return "$s->{ConfDir}/pc/$host.pl";
267 return "$s->{TopDir}/pc/$host/config.pl"
268 if ( -f "$s->{TopDir}/pc/$host/config.pl" );
269 return "$s->{ConfDir}/$host.pl"
270 if ( $host ne "config" && -f "$s->{ConfDir}/$host.pl" );
271 return "$s->{ConfDir}/pc/$host.pl";
277 my($s, $host, $prevConfig) = @_;
278 my($ret, $mesg, $config, @configs);
283 my $conf = $prevConfig || {};
284 my $configPath = $s->ConfigPath($host);
286 push(@configs, $configPath) if ( -f $configPath );
287 foreach $config ( @configs ) {
289 if ( !defined($ret = do $config) && ($! || $@) ) {
290 $mesg = "Couldn't open $config: $!" if ( $! );
291 $mesg = "Couldn't execute $config: $@" if ( $@ );
292 $mesg =~ s/[\n\r]+//;
293 return ($mesg, $conf);
299 # Promote BackupFilesOnly and BackupFilesExclude to hashes
301 foreach my $param qw(BackupFilesOnly BackupFilesExclude) {
302 next if ( !defined($conf->{$param}) || ref($conf->{$param}) eq "HASH" );
303 $conf->{$param} = [ $conf->{$param} ]
304 if ( ref($conf->{$param}) ne "ARRAY" );
305 $conf->{$param} = { "*" => $conf->{$param} };
309 # Handle backward compatibility with defunct BlackoutHourBegin,
310 # BlackoutHourEnd, and BlackoutWeekDays parameters.
312 if ( defined($conf->{BlackoutHourBegin}) ) {
313 push(@{$conf->{BlackoutPeriods}},
315 hourBegin => $conf->{BlackoutHourBegin},
316 hourEnd => $conf->{BlackoutHourEnd},
317 weekDays => $conf->{BlackoutWeekDays},
320 delete($conf->{BlackoutHourBegin});
321 delete($conf->{BlackoutHourEnd});
322 delete($conf->{BlackoutWeekDays});
325 return (undef, $conf);
330 my($s, $host, $newConf) = @_;
332 my $configPath = $s->ConfigPath($host);
334 my($err, $contents) = $s->ConfigFileMerge("$configPath", $newConf);
335 if ( defined($err) ) {
341 return $s->TextFileWrite($configPath, $contents);
347 my($s, $inFile, $newConf) = @_;
349 my($contents, $skipExpr, $fakeVar);
354 # Match existing settings in current config file
357 || return ("ConfigFileMerge: can't open/read $inFile", undef);
361 if ( /^\s*\$Conf\{([^}]*)\}\s*=(.*)/ ) {
363 $skipExpr = "\$fakeVar = $2\n";
364 if ( exists($newConf->{$var}) ) {
365 my $d = Data::Dumper->new([$newConf->{$var}], [*value]);
368 my $value = $d->Dump;
369 $value =~ s/(.*)\n/$1;\n/s;
370 $contents .= "\$Conf{$var} = " . $value;
373 } elsif ( defined($skipExpr) ) {
378 if ( defined($skipExpr)
379 && ($skipExpr =~ /^\$fakeVar = *<</
380 || $skipExpr =~ /;[\n\r]*$/) ) {
382 # if we have a complete expression, then we are done
383 # skipping text from the original config file.
385 $skipExpr = $1 if ( $skipExpr =~ /(.*)/s );
387 $skipExpr = undef if ( $@ eq "" );
394 # Add new entries not matched in current config file
396 foreach my $var ( sort(keys(%$newConf)) ) {
397 next if ( $done->{$var} );
398 my $d = Data::Dumper->new([$newConf->{$var}], [*value]);
401 my $value = $d->Dump;
402 $value =~ s/(.*)\n/$1;\n/s;
403 $contents .= "\$Conf{$var} = " . $value;
406 return (undef, $contents);
410 # Return the mtime of the config file
415 return (stat($s->ConfigPath()))[9];
419 # Returns information from the host file in $s->{ConfDir}/hosts.
420 # With no argument a ref to a hash of hosts is returned. Each
421 # hash contains fields as specified in the hosts file. With an
422 # argument a ref to a single hash is returned with information
423 # for just that host.
428 my(%hosts, @hdr, @fld);
429 local(*HOST_INFO, *LOCK);
431 flock(LOCK, LOCK_EX) if open(LOCK, "$s->{ConfDir}/LOCK");
432 if ( !open(HOST_INFO, "$s->{ConfDir}/hosts") ) {
433 print(STDERR "Can't open $s->{ConfDir}/hosts\n");
438 while ( <HOST_INFO> ) {
442 next if ( /^\s*$/ || !/^([\w\.\\-]+\s+.*)/ );
444 # Split on white space, except if preceded by \
445 # using zero-width negative look-behind assertion
446 # (always wanted to use one of those).
448 @fld = split(/(?<!\\)\s+/, $1);
456 if ( defined($host) ) {
457 next if ( lc($fld[0]) ne lc($host) );
458 @{$hosts{lc($fld[0])}}{@hdr} = @fld;
463 @{$hosts{lc($fld[0])}}{@hdr} = @fld;
475 # Writes new hosts information to the hosts file in $s->{ConfDir}/hosts.
476 # With no argument a ref to a hash of hosts is returned. Each
477 # hash contains fields as specified in the hosts file. With an
478 # argument a ref to a single hash is returned with information
479 # for just that host.
484 my($gotHdr, @fld, $hostText, $contents);
487 if ( !open(HOST_INFO, "$s->{ConfDir}/hosts") ) {
488 return "Can't open $s->{ConfDir}/hosts";
490 foreach my $host ( keys(%$hosts) ) {
491 my $name = "$hosts->{$host}{host}";
492 my $rest = "\t$hosts->{$host}{dhcp}"
493 . "\t$hosts->{$host}{user}"
494 . "\t$hosts->{$host}{moreUsers}";
497 $hostText->{$host} = $name . $rest;
500 while ( <HOST_INFO> ) {
502 if ( /^\s*$/ || /^\s*#/ ) {
503 $contents .= $_ . "\n";
507 $contents .= $_ . "\n";
511 @fld = split(/(?<!\\)\s+/, $1);
518 if ( defined($hostText->{$fld[0]}) ) {
519 $contents .= $hostText->{$fld[0]} . "\n";
520 delete($hostText->{$fld[0]});
523 foreach my $host ( sort(keys(%$hostText)) ) {
524 $contents .= $hostText->{$host} . "\n";
525 delete($hostText->{$host});
530 # Write and verify the new host file
532 return $s->TextFileWrite("$s->{ConfDir}/hosts", $contents);
536 # Return the mtime of the hosts file
541 return (stat("$s->{ConfDir}/hosts"))[9];