7 BackupPC_recover_from_increments
11 quick hack to create BackupPC pool out of increments
15 BackupPC_recover_from_increments /restore/from/directory/ [/path/to/increment/to/restore.tar.gz ... ]
19 2006-02-07 Dobrica Pavlinusic <dpavlin@rot13.org>
26 use lib "/data/backuppc/lib";
31 # connect to BackupPC_server
33 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
34 my %Conf = $bpc->Conf();
38 my $err = $bpc->ServerConnect($Conf{ServerHost}, $Conf{ServerPort});
40 print("Can't connect to server ($err)\n");
44 my $TopDir = $bpc->TopDir();
46 #print Dumper(\%Conf);
48 # check if host exists
50 my $host_info = $bpc->HostInfoRead( $host );
51 print Dumper($host_info, $bpc->HostInfoRead( 'localhost' ));
52 die "host '$host' is not found, please add it to config/hosts configuration file\n" unless ($host_info->{$host});
54 # create restore host configuration
56 my $restore_path = "$Conf{InstallDir}/$Conf{GzipTempDir}/${host}-restore.tar.gz";
58 my $conf_restore = <<'_END_OF_CONF_';
60 $Conf{XferMethod} = 'tar';
62 $Conf{TarFullArgs} = 'echo "full backups are not supported in restore!" ; exit 1';
63 $Conf{TarIncrArgs} = '';
65 # fake ping when restoring
66 $Conf{PingCmd} = '$pingPath -c 1 localhost',
67 $Conf{ClientNameAlias} = 'localhost';
69 $Conf{TarClientCmd} = 'zcat __restore_path__';
75 $conf_restore =~ s/__restore_path__/$restore_path/gs;
77 my $config_file = "$bpc->{TopDir}/conf/${host}.pl";
79 open(my $host_fh, '>', $config_file) || die "can't open $config_file: $!";
80 print $host_fh $conf_restore || die "can't write configuration in $config_file: $!";
81 close($host_fh) || die "can't close $config_file: $!";
83 warn "written config:\n$conf_restore\n";
85 sub restore_increment {
86 my $path = shift || die "need path!";
88 print "working on $path\n";
90 if (-e $restore_path) {
91 unlink $restore_path || die "can't remove $restore_path: $!\n";
93 link $path, $restore_path || die "can't create link $path -> $restore_path: $!\n";
95 my $user = $host_info->{$host}->{user} || die "can't get user for host $host";
97 $bpc->ServerMesg("log User $user started recovery from increment $path");
100 my $r = $bpc->ServerMesg("backup $host $host $user $full");
101 print "backup --> $r";
102 die $r if ($r =~ m/^error/);
104 # Status_backup_in_progress
107 my ($state,$last_state) = ('','x');
109 while ($state ne 'Status_idle') {
110 my $s = $bpc->ServerMesg("status hosts");
115 $state = $Status{restore}->{state};
117 die $state if ($state =~ m/^error/);
119 if ($state ne $last_state) {
120 print "\n$state"; #, Dumper($Status{restore});
124 $last_state = $state;
132 foreach my $restore_inc (@ARGV) {
134 if (-d $restore_inc) {
136 find({ wanted => sub {
137 restore_increment( $File::Find::name );
138 }, follow => 0 }, $restore_inc);
140 } elsif (-f $restore_inc && $restore_inc =~ m/\.tar\.gz$/i) {
141 restore_increment( $restore_inc );
143 warn "skipped: $restore_inc, not directory or .tar.gz increment\n";
148 #unlink $config_file || die "can't remove $config_file: $!";