r10309@llin: dpavlin | 2006-03-07 11:48:22 +0100
[BackupPC.git] / bin / BackupPC_recover_from_increments
index 867934c..d8e505a 100755 (executable)
@@ -1,8 +1,24 @@
 #!/usr/bin/perl -w
 
-# quick hack to create BackupPC pool out of increments
+use strict;
 
-# 2006-02-07 Dobrica Pavlinusic <dpavlin@rot13.org>
+=head1 NAME
+
+BackupPC_recover_from_increments
+
+=head1 DESCRIPTION
+
+quick hack to create BackupPC pool out of increments
+
+=head1 SYNOPSYS
+
+ BackupPC_recover_from_increments /restore/from/directory/ [/path/to/increment/to/restore.tar.gz ... ]
+
+=head1 HISTORY
+
+2006-02-07 Dobrica Pavlinusic <dpavlin@rot13.org>
+
+=cut
 
 use File::Find;
 use Data::Dumper;
@@ -10,13 +26,12 @@ use Data::Dumper;
 use lib "/data/backuppc/lib";
 use BackupPC::Lib;
 
-
-my $restore_path = './temp/restore.tar.gz';
+my $host = 'restore';
 
 # connect to BackupPC_server
 
 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
-my %Conf   = $bpc->Conf();
+my %Conf = $bpc->Conf();
 
 $bpc->ChildInit();
 
@@ -26,8 +41,49 @@ if ( $err ) {
     exit(1);
 }
 
-sub increment {
-       my $path = $File::Find::name;
+my $TopDir = $bpc->TopDir();
+
+#print Dumper(\%Conf);
+
+# 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});
+
+# create restore host configuration
+
+my $restore_path = "$Conf{InstallDir}/$Conf{GzipTempDir}/${host}-restore.tar.gz";
+
+my $conf_restore = <<'_END_OF_CONF_';
+
+$Conf{XferMethod} = 'tar';
+
+$Conf{TarFullArgs} = 'echo "full backups are not supported in restore!" ; exit 1';
+$Conf{TarIncrArgs} = '';
+
+# fake ping when restoring
+$Conf{PingCmd} = '$pingPath -c 1 localhost',
+$Conf{ClientNameAlias} = 'localhost';
+
+$Conf{TarClientCmd} = 'zcat __restore_path__';
+
+1;
+
+_END_OF_CONF_
+
+$conf_restore =~ s/__restore_path__/$restore_path/gs;
+
+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: $!";
+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!";
 
        print "working on $path\n";
 
@@ -36,16 +92,19 @@ sub increment {
        }
        link $path, $restore_path || die "can't create link $path -> $restore_path: $!\n";
 
-       $bpc->ServerMesg("log User backuppc started restore of $restore_path");
+       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 $full = 0;
-       my $r = $bpc->ServerMesg("backup restore restore backuppc $full");
-       print "backup --> $r\n";
+       my $r = $bpc->ServerMesg("backup $host $host $user $full");
+       print "backup --> $r";
+       die $r if ($r =~ m/^error/);
 
        # Status_backup_in_progress
        # Status_idle
 
-       my $state = 'unknown';
+       my ($state,$last_state) = ('','x');
 
        while ($state ne 'Status_idle') {
                my $s = $bpc->ServerMesg("status hosts");
@@ -54,10 +113,36 @@ sub increment {
                        eval "$s";
                }
                $state = $Status{restore}->{state};
-               print "# $state\n"; #, Dumper($Status{restore});
+
+               die $state if ($state =~ m/^error/);
+
+               if ($state ne $last_state) {
+                       print "\n$state"; #, Dumper($Status{restore});
+               } else {
+                       print ".";
+               }
+               $last_state = $state;
                sleep 1;
        }
+       print "\n";
 }
 
-find({ wanted => \&increment, follow => 0 }, './temp');
+# now, start restore
+
+foreach my $restore_inc (@ARGV) {
+
+       if (-d $restore_inc) {
+
+               find({ wanted => sub {
+                       restore_increment( $File::Find::name );
+               }, follow => 0 }, $restore_inc);
+
+       } elsif (-f $restore_inc && $restore_inc =~ m/\.tar\.gz$/i) {
+               restore_increment( $restore_inc );
+       } else {
+               warn "skipped: $restore_inc, not directory or .tar.gz increment\n";
+       }
+
+}
 
+#unlink $config_file || die "can't remove $config_file: $!";