- fixed a typo that caused incPartsUpdate not to work properly
[BackupPC.git] / bin / BackupPC_recover_from_increments
index d8e505a..543b67c 100755 (executable)
@@ -21,16 +21,27 @@ 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;
+
+# this option will probably create wrong increments, but it's here for
+# testing and comparison. In short, don't use it!
+my $restore_via_temp_dir = 1;
 
 # connect to BackupPC_server
 
-die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
+my $bpc = BackupPC::Lib->new(undef, undef, 1) || die;
 my %Conf = $bpc->Conf();
 
 $bpc->ChildInit();
@@ -48,32 +59,66 @@ my $TopDir = $bpc->TopDir();
 # check if host exists
 
 my $host_info = $bpc->HostInfoRead( $host );
-print Dumper($host_info, $bpc->HostInfoRead( 'localhost' ));
+#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});
 
-# create restore host configuration
+# take care of temporary directory for increments
+
+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);
 
 my $restore_path = "$Conf{InstallDir}/$Conf{GzipTempDir}/${host}-restore.tar.gz";
 
+# 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} = '';
-
-# fake ping when restoring
-$Conf{PingCmd} = '$pingPath -c 1 localhost',
+# disable ping
+$Conf{PingCmd} = '';
+# work-around for Backup aborted because of CorrectHostCheck
+$Conf{FixedIPNetBiosNameCheck} = 0;
+$Conf{NmbLookupCmd} = '';
 $Conf{ClientNameAlias} = 'localhost';
 
+#$Conf{TarIncrArgs} = '';
+#$Conf{ClientTimeout} = 600;
+
+_END_OF_CONF_
+
+$conf_restore .= <<'_END_OF_CONF_' if (! $restore_via_temp_dir);
+
+#$Conf{TarClientCmd} = '';
+#$Conf{TarFullArgs} = 'gzip -cdv __restore_path__';
+
+# force BackupPC_tarExtract to produce output of tar -c -v -f - --totals
+# so that we can just pipe tar into it!
+$Conf{tarExtractEmulateTotals} = 1;
 $Conf{TarClientCmd} = 'zcat __restore_path__';
 
-1;
+_END_OF_CONF_
+
+$conf_restore .= <<'_END_OF_CONF_' if ($restore_via_temp_dir);
+
+$Conf{TarClientCmd} = '$tarPath -c -v -f - -C __inc_tmp_dir__ --totals';
 
 _END_OF_CONF_
 
+$conf_restore =~ s/__share__/$share/gs;
+$conf_restore =~ s/__inc_tmp_dir__/$inc_tmp_dir/gs;
 $conf_restore =~ s/__restore_path__/$restore_path/gs;
 
+$conf_restore .= "\n1;\n";
+
 my $config_file = "$bpc->{TopDir}/conf/${host}.pl";
 
 open(my $host_fh, '>', $config_file) || die "can't open $config_file: $!";
@@ -82,23 +127,38 @@ close($host_fh) || die "can't close $config_file: $!";
 
 warn "written config:\n$conf_restore\n";
 
-sub restore_increment {
-       my $path = shift || die "need path!";
+sub restore_increments {
 
-       print "working on $path\n";
+       foreach my $path (@_) {
+               next unless ($path);    # skip 0 element which is undef
 
-       if (-e $restore_path) {
-               unlink $restore_path || die "can't remove $restore_path: $!\n";
+               print "extracting $path\n";
+
+               my $cmd = "cd $inc_tmp_dir && tar xfz $path";
+               system($cmd) == 0 or die "can't execute: $cmd -- $?\n";
+       
        }
-       link $path, $restore_path || die "can't create link $path -> $restore_path: $!\n";
+
+#      print "using $path to create increment\n";
+#
+#      if (-e $restore_path) {
+#              unlink $restore_path || die "can't remove $restore_path: $!\n";
+#      }
+#      symlink $path, $restore_path || die "can't create link $path -> $restore_path: $!\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 $full = 0;
        my $r = $bpc->ServerMesg("backup $host $host $user $full");
-       print "backup --> $r";
+       print "backup ", $full ? 'full' : 'incremental', " --> $r";
        die $r if ($r =~ m/^error/);
 
        # Status_backup_in_progress
@@ -129,20 +189,57 @@ sub restore_increment {
 
 # now, start restore
 
+my $increments;
+
 foreach my $restore_inc (@ARGV) {
 
+       sub extract_filename {
+               my $path = shift || die "no path?";
+               if ($path =~ m#^(.*)/(\w+)_(\w+)_(\d+)\.tar\.gz$#) {
+                       my ($path,$host,$share,$num) = ($1,$2,$3,$4);
+                       $increments->{$host}->{$share}->{$num}->[1] = "${path}/${host}_${share}_${num}.tar.gz";
+               } elsif ($path =~ m#^(.*)/(\w+)_(\w+)_(\d+)/(\d+)\.tar.gz$#) {
+                       my ($path,$host,$share,$num,$part) = ($1,$2,$3,$4,$5);
+                       $increments->{$host}->{$share}->{$num}->[$part] = "${path}/${host}_${share}_${num}/${part}.tar.gz";
+               } else {
+                       print "# skipped: $path\n";
+               }
+       }
+
        if (-d $restore_inc) {
 
                find({ wanted => sub {
-                       restore_increment( $File::Find::name );
+                       extract_filename( $File::Find::name );
                }, follow => 0 }, $restore_inc);
 
-       } elsif (-f $restore_inc && $restore_inc =~ m/\.tar\.gz$/i) {
-               restore_increment( $restore_inc );
+       } elsif (-f $restore_inc) {
+               extract_filename( $restore_inc );
        } else {
-               warn "skipped: $restore_inc, not directory or .tar.gz increment\n";
+               warn "skipped: $restore_inc, not file or directory\n";
        }
 
 }
 
+print Dumper($increments);
+
+cleanup_inc_temp_dir();
+
+foreach my $host (sort keys %{ $increments }) {
+       foreach my $share (sort keys %{ $increments->{$host} }) {
+               foreach my $num (sort keys %{ $increments->{$host}->{$share} }) {
+                       print "# about to restore $host $share $num\n";
+                       my @parts = @{ $increments->{$host}->{$share}->{$num} };
+                       my $msg = "started recovery of ${host}:${share}#${num} with " . $#parts . " parts";
+
+                       print "LOG: $msg\n";
+                       $bpc->ServerMesg("log $msg");
+
+                       restore_increments( @parts );
+               }
+       }
+}
+               
+
 #unlink $config_file || die "can't remove $config_file: $!";
+
+rmtree($inc_tmp_dir) if (-e $inc_tmp_dir);