#!/usr/bin/perl -w use strict; =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 =cut use File::Find; use File::Path; use Data::Dumper; use lib "/data/backuppc/lib"; use BackupPC::Lib; my $host = 'restore'; my $share = '/etc'; # connect to BackupPC_server die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) ); my %Conf = $bpc->Conf(); $bpc->ChildInit(); my $err = $bpc->ServerConnect($Conf{ServerHost}, $Conf{ServerPort}); if ( $err ) { print("Can't connect to server ($err)\n"); exit(1); } 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}); # take care of temporary directory for increments my $inc_tmp_dir = $Conf{IncrementTempDir} || die "need working directory in IncrementTempDir\n"; rmtree($inc_tmp_dir) if (-e $inc_tmp_dir); mkpath($inc_tmp_dir); print "# using $inc_tmp_dir for increment scratch space"; # create restore host configuration my $conf_restore = <<'_END_OF_CONF_'; $Conf{XferMethod} = 'tar'; $Conf{TarShareName} = '__share__'; # 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; #$Conf{TarClientCmd} = ''; #$Conf{TarFullArgs} = 'gzip -cdv __restore_path__'; $Conf{TarClientCmd} = '$tarPath -c -v -f - -C __inc_tmp_dir__ --totals'; 1; _END_OF_CONF_ $conf_restore =~ s/__share__/$share/gs; $conf_restore =~ s/__inc_tmp_dir__/$inc_tmp_dir/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!"; if ($path !~ m/\.tar\.gz$/i) { print "# skipping $path, not .tar.gz increment\n"; return; } print "restoring $path\n"; 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 my ($state,$last_state) = ('','x'); while ($state ne 'Status_idle') { my $s = $bpc->ServerMesg("status hosts"); my %Status; { eval "$s"; } $state = $Status{restore}->{state}; 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"; } # 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_increment( $restore_inc ); } else { 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);