#!/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'; # 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 my $bpc = BackupPC::Lib->new(undef, undef, 1) || die; 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"; 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__'; # 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__'; _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: $!"; 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_increments { foreach my $path (@_) { next unless ($path); # skip 0 element which is undef print "extracting $path\n"; my $cmd = "cd $inc_tmp_dir && tar xfz $path"; system($cmd) == 0 or die "can't execute: $cmd -- $?\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"; 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 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 { extract_filename( $File::Find::name ); }, follow => 0 }, $restore_inc); } elsif (-f $restore_inc) { extract_filename( $restore_inc ); } else { 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);