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>
27 use lib "/data/backuppc/lib";
33 # connect to BackupPC_server
35 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
36 my %Conf = $bpc->Conf();
40 my $err = $bpc->ServerConnect($Conf{ServerHost}, $Conf{ServerPort});
42 print("Can't connect to server ($err)\n");
46 my $TopDir = $bpc->TopDir();
48 #print Dumper(\%Conf);
50 # check if host exists
52 my $host_info = $bpc->HostInfoRead( $host );
53 #print Dumper($host_info, $bpc->HostInfoRead( 'localhost' ));
54 die "host '$host' is not found, please add it to config/hosts configuration file\n" unless ($host_info->{$host});
56 # take care of temporary directory for increments
58 my $inc_tmp_dir = $Conf{IncrementTempDir} || die "need working directory in IncrementTempDir\n";
59 rmtree($inc_tmp_dir) if (-e $inc_tmp_dir);
63 print "# using $inc_tmp_dir for increment scratch space";
65 # create restore host configuration
67 my $conf_restore = <<'_END_OF_CONF_';
69 $Conf{XferMethod} = 'tar';
70 $Conf{TarShareName} = '__share__';
74 # work-around for Backup aborted because of CorrectHostCheck
75 $Conf{FixedIPNetBiosNameCheck} = 0;
76 $Conf{NmbLookupCmd} = '';
77 $Conf{ClientNameAlias} = 'localhost';
79 #$Conf{TarIncrArgs} = '';
80 #$Conf{ClientTimeout} = 600;
81 #$Conf{TarClientCmd} = '';
82 #$Conf{TarFullArgs} = 'gzip -cdv __restore_path__';
84 $Conf{TarClientCmd} = '$tarPath -c -v -f - -C __inc_tmp_dir__ --totals';
90 $conf_restore =~ s/__share__/$share/gs;
91 $conf_restore =~ s/__inc_tmp_dir__/$inc_tmp_dir/gs;
93 my $config_file = "$bpc->{TopDir}/conf/${host}.pl";
95 open(my $host_fh, '>', $config_file) || die "can't open $config_file: $!";
96 print $host_fh $conf_restore || die "can't write configuration in $config_file: $!";
97 close($host_fh) || die "can't close $config_file: $!";
99 warn "written config:\n$conf_restore\n";
101 sub restore_increment {
102 my $path = shift || die "need path!";
104 if ($path !~ m/\.tar\.gz$/i) {
105 print "# skipping $path, not .tar.gz increment\n";
109 print "restoring $path\n";
111 my $cmd = "cd $inc_tmp_dir && tar xfz $path";
112 system($cmd) == 0 or die "can't execute: $cmd -- $?\n";
114 print "starting import into BackupPC pool\n";
116 my $user = $host_info->{$host}->{user} || die "can't get user for host $host";
118 $bpc->ServerMesg("log User $user started recovery from increment $path");
120 my @backups = $bpc->BackupInfoRead( $host );
123 foreach my $b (@backups) {
124 $full = 0 if ($b->{type} eq 'full');
127 my $r = $bpc->ServerMesg("backup $host $host $user $full");
128 print "backup ", $full ? 'full' : 'incremental', " --> $r";
129 die $r if ($r =~ m/^error/);
131 # Status_backup_in_progress
134 my ($state,$last_state) = ('','x');
136 while ($state ne 'Status_idle') {
137 my $s = $bpc->ServerMesg("status hosts");
142 $state = $Status{restore}->{state};
144 die $state if ($state =~ m/^error/);
146 if ($state ne $last_state) {
147 print "\n$state"; #, Dumper($Status{restore});
151 $last_state = $state;
159 foreach my $restore_inc (@ARGV) {
161 if (-d $restore_inc) {
163 find({ wanted => sub {
164 restore_increment( $File::Find::name );
165 }, follow => 0 }, $restore_inc);
167 } elsif (-f $restore_inc) {
168 restore_increment( $restore_inc );
170 warn "skipped: $restore_inc, not file or directory\n";
175 #unlink $config_file || die "can't remove $config_file: $!";
177 rmtree($inc_tmp_dir) if (-e $inc_tmp_dir);