r10862@llin: dpavlin | 2006-04-28 11:07:29 +0200
[BackupPC.git] / bin / BackupPC_recover_from_increments
index 631bff3..5216efa 100755 (executable)
@@ -1,5 +1,7 @@
 #!/usr/bin/perl -w
 
+use strict;
+
 =head1 NAME
 
 BackupPC_recover_from_increments
@@ -19,11 +21,20 @@ quick hack to create BackupPC pool out of increments
 =cut
 
 use File::Find;
+use File::Path;
 use Data::Dumper;
 
 use lib "/data/backuppc/lib";
 use BackupPC::Lib;
 
+my $host = 'restore';
+my $share = '/etc';
+
+# this option will cleanup tar dump before import of each increment
+# WARNING: this will create increments which contain only new files, not
+# state of share in that particular moment! (it's fast, though)
+my $cleanup_before_increment = 0;
+
 # connect to BackupPC_server
 
 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
@@ -39,31 +50,55 @@ if ( $err ) {
 
 my $TopDir = $bpc->TopDir();
 
-print Dumper(\%Conf);
+#print Dumper(\%Conf);
 
-# create restore host configuration
+# check if host exists
+
+my $host_info = $bpc->HostInfoRead( $host );
+#print Dumper($host_info, $bpc->HostInfoRead( 'localhost' ));
+die "host '$host' is not found, please add it to config/hosts configuration file\n" unless ($host_info->{$host});
+
+# take care of temporary directory for increments
 
-my $restore_path = "$TopDir/$temp/restore.tar.gz";
+my $inc_tmp_dir = $Conf{IncrementTempDir} || die "need working directory in IncrementTempDir\n";
+
+sub cleanup_inc_temp_dir {
+       rmtree($inc_tmp_dir) if (-e $inc_tmp_dir);
+       mkpath($inc_tmp_dir);
+}
+
+print "# using $inc_tmp_dir for increment scratch space";
+cleanup_inc_temp_dir() if (! $cleanup_before_increment);
+
+# create restore host configuration
 
 my $conf_restore = <<'_END_OF_CONF_';
 
 $Conf{XferMethod} = 'tar';
+$Conf{TarShareName} = '__share__';
 
-$Conf{TarFullArgs} = 'echo "full backups are not supported in restore!" ; exit 1';
-$Conf{TarIncrArgs} = '';
+# disable ping
+$Conf{PingCmd} = '';
+# work-around for Backup aborted because of CorrectHostCheck
+$Conf{FixedIPNetBiosNameCheck} = 0;
+$Conf{NmbLookupCmd} = '';
+$Conf{ClientNameAlias} = 'localhost';
 
-# fake ping when restoring
-$Conf{PingCmd} = '$pingPath -c 1 localhost',
+#$Conf{TarIncrArgs} = '';
+#$Conf{ClientTimeout} = 600;
+#$Conf{TarClientCmd} = '';
+#$Conf{TarFullArgs} = 'gzip -cdv __restore_path__';
 
-$Conf{TarClientCmd} = 'zcat __restore_path__';
+$Conf{TarClientCmd} = '$tarPath -c -v -f - -C __inc_tmp_dir__ --totals';
 
 1;
 
 _END_OF_CONF_
 
-$conf_restore =~ s/__restore_path__/$restore_path/gs;
+$conf_restore =~ s/__share__/$share/gs;
+$conf_restore =~ s/__inc_tmp_dir__/$inc_tmp_dir/gs;
 
-my $config_file = "$bpc->{TopDir}/conf/restore.pl";
+my $config_file = "$bpc->{TopDir}/conf/${host}.pl";
 
 open(my $host_fh, '>', $config_file) || die "can't open $config_file: $!";
 print $host_fh $conf_restore || die "can't write configuration in $config_file: $!";
@@ -74,18 +109,34 @@ warn "written config:\n$conf_restore\n";
 sub restore_increment {
        my $path = shift || die "need path!";
 
-       print "working on $path\n";
-
-       if (-e $restore_path) {
-               unlink $restore_path || die "can't remove $restore_path: $!\n";
+       if ($path !~ m/\.tar\.gz$/i) {
+               print "# skipping $path, not .tar.gz increment\n";
+               return;
        }
-       link $path, $restore_path || die "can't create link $path -> $restore_path: $!\n";
 
-       $bpc->ServerMesg("log User backuppc started restore of $path");
+       print "restoring $path\n";
 
-       my $full = 0;
-       my $r = $bpc->ServerMesg("backup restore restore backuppc $full");
-       print "backup --> $r";
+       cleanup_inc_temp_dir() if ($cleanup_before_increment);
+
+       my $cmd = "cd $inc_tmp_dir && tar xfz $path";
+       system($cmd) == 0 or die "can't execute: $cmd -- $?\n";
+
+       print "starting import into BackupPC pool\n";
+
+       my $user = $host_info->{$host}->{user} || die "can't get user for host $host";
+
+       $bpc->ServerMesg("log User $user started recovery from increment $path");
+
+       my @backups = $bpc->BackupInfoRead( $host );
+
+       my $full = 1;
+       foreach my $b (@backups) {
+               $full = 0 if ($b->{type} eq 'full');
+       }
+
+       my $r = $bpc->ServerMesg("backup $host $host $user $full");
+       print "backup ", $full ? 'full' : 'incremental', " --> $r";
+       die $r if ($r =~ m/^error/);
 
        # Status_backup_in_progress
        # Status_idle
@@ -99,6 +150,9 @@ sub restore_increment {
                        eval "$s";
                }
                $state = $Status{restore}->{state};
+
+               die $state if ($state =~ m/^error/);
+
                if ($state ne $last_state) {
                        print "\n$state"; #, Dumper($Status{restore});
                } else {
@@ -120,12 +174,14 @@ foreach my $restore_inc (@ARGV) {
                        restore_increment( $File::Find::name );
                }, follow => 0 }, $restore_inc);
 
-       } elsif (-f $restore_inc && $restore_inc =~ m/\.tar\.gz$/i) {
+       } elsif (-f $restore_inc) {
                restore_increment( $restore_inc );
        } else {
-               warn "skipped: $restore_inc, not directory or .tar.gz increment\n";
+               warn "skipped: $restore_inc, not file or directory\n";
        }
 
 }
 
 #unlink $config_file || die "can't remove $config_file: $!";
+
+rmtree($inc_tmp_dir) if (-e $inc_tmp_dir);