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 # this option will cleanup tar dump before import of each increment
34 # WARNING: this will create increments which contain only new files, not
35 # state of share in that particular moment! (it's fast, though)
36 my $cleanup_before_increment = 0;
38 # this option will probably create wrong increments, but it's here for
39 # testing and comparison. In short, don't use it!
40 my $restore_via_temp_dir = 1;
42 # connect to BackupPC_server
44 my $bpc = BackupPC::Lib->new(undef, undef, 1) || die;
45 my %Conf = $bpc->Conf();
49 my $err = $bpc->ServerConnect($Conf{ServerHost}, $Conf{ServerPort});
51 print("Can't connect to server ($err)\n");
55 my $TopDir = $bpc->TopDir();
57 #print Dumper(\%Conf);
59 # check if host exists
61 my $host_info = $bpc->HostInfoRead( $host );
62 #print Dumper($host_info, $bpc->HostInfoRead( 'localhost' ));
63 die "host '$host' is not found, please add it to config/hosts configuration file\n" unless ($host_info->{$host});
65 # take care of temporary directory for increments
67 my $inc_tmp_dir = $Conf{IncrementTempDir} || die "need working directory in IncrementTempDir\n";
69 sub cleanup_inc_temp_dir {
70 rmtree($inc_tmp_dir) if (-e $inc_tmp_dir);
74 print "# using $inc_tmp_dir for increment scratch space";
75 cleanup_inc_temp_dir() if (! $cleanup_before_increment);
77 my $restore_path = "$Conf{InstallDir}/$Conf{GzipTempDir}/${host}-restore.tar.gz";
79 # create restore host configuration
81 my $conf_restore = <<'_END_OF_CONF_';
83 $Conf{XferMethod} = 'tar';
84 $Conf{TarShareName} = '__share__';
88 # work-around for Backup aborted because of CorrectHostCheck
89 $Conf{FixedIPNetBiosNameCheck} = 0;
90 $Conf{NmbLookupCmd} = '';
91 $Conf{ClientNameAlias} = 'localhost';
93 #$Conf{TarIncrArgs} = '';
94 #$Conf{ClientTimeout} = 600;
98 $conf_restore .= <<'_END_OF_CONF_' if (! $restore_via_temp_dir);
100 #$Conf{TarClientCmd} = '';
101 #$Conf{TarFullArgs} = 'gzip -cdv __restore_path__';
103 # force BackupPC_tarExtract to produce output of tar -c -v -f - --totals
104 # so that we can just pipe tar into it!
105 $Conf{tarExtractEmulateTotals} = 1;
106 $Conf{TarClientCmd} = 'zcat __restore_path__';
110 $conf_restore .= <<'_END_OF_CONF_' if ($restore_via_temp_dir);
112 $Conf{TarClientCmd} = '$tarPath -c -v -f - -C __inc_tmp_dir__ --totals';
116 $conf_restore =~ s/__share__/$share/gs;
117 $conf_restore =~ s/__inc_tmp_dir__/$inc_tmp_dir/gs;
118 $conf_restore =~ s/__restore_path__/$restore_path/gs;
120 $conf_restore .= "\n1;\n";
122 my $config_file = "$bpc->{TopDir}/conf/${host}.pl";
124 open(my $host_fh, '>', $config_file) || die "can't open $config_file: $!";
125 print $host_fh $conf_restore || die "can't write configuration in $config_file: $!";
126 close($host_fh) || die "can't close $config_file: $!";
128 warn "written config:\n$conf_restore\n";
130 sub restore_increments {
132 foreach my $path (@_) {
133 next unless ($path); # skip 0 element which is undef
135 print "extracting $path\n";
137 my $cmd = "cd $inc_tmp_dir && tar xfz $path";
138 system($cmd) == 0 or die "can't execute: $cmd -- $?\n";
142 # print "using $path to create increment\n";
144 # if (-e $restore_path) {
145 # unlink $restore_path || die "can't remove $restore_path: $!\n";
147 # symlink $path, $restore_path || die "can't create link $path -> $restore_path: $!\n";
149 print "starting import into BackupPC pool\n";
151 my $user = $host_info->{$host}->{user} || die "can't get user for host $host";
153 my @backups = $bpc->BackupInfoRead( $host );
156 foreach my $b (@backups) {
157 $full = 0 if ($b->{type} eq 'full');
160 my $r = $bpc->ServerMesg("backup $host $host $user $full");
161 print "backup ", $full ? 'full' : 'incremental', " --> $r";
162 die $r if ($r =~ m/^error/);
164 # Status_backup_in_progress
167 my ($state,$last_state) = ('','x');
169 while ($state ne 'Status_idle') {
170 my $s = $bpc->ServerMesg("status hosts");
175 $state = $Status{restore}->{state};
177 die $state if ($state =~ m/^error/);
179 if ($state ne $last_state) {
180 print "\n$state"; #, Dumper($Status{restore});
184 $last_state = $state;
194 foreach my $restore_inc (@ARGV) {
196 sub extract_filename {
197 my $path = shift || die "no path?";
198 if ($path =~ m#^(.*)/(\w+)_(\w+)_(\d+)\.tar\.gz$#) {
199 my ($path,$host,$share,$num) = ($1,$2,$3,$4);
200 $increments->{$host}->{$share}->{$num}->[1] = "${path}/${host}_${share}_${num}.tar.gz";
201 } elsif ($path =~ m#^(.*)/(\w+)_(\w+)_(\d+)/(\d+)\.tar.gz$#) {
202 my ($path,$host,$share,$num,$part) = ($1,$2,$3,$4,$5);
203 $increments->{$host}->{$share}->{$num}->[$part] = "${path}/${host}_${share}_${num}/${part}.tar.gz";
205 print "# skipped: $path\n";
209 if (-d $restore_inc) {
211 find({ wanted => sub {
212 extract_filename( $File::Find::name );
213 }, follow => 0 }, $restore_inc);
215 } elsif (-f $restore_inc) {
216 extract_filename( $restore_inc );
218 warn "skipped: $restore_inc, not file or directory\n";
223 print Dumper($increments);
225 cleanup_inc_temp_dir();
227 foreach my $host (sort keys %{ $increments }) {
228 foreach my $share (sort keys %{ $increments->{$host} }) {
229 foreach my $num (sort keys %{ $increments->{$host}->{$share} }) {
230 print "# about to restore $host $share $num\n";
231 my @parts = @{ $increments->{$host}->{$share}->{$num} };
232 my $msg = "started recovery of ${host}:${share}#${num} with " . $#parts . " parts";
235 $bpc->ServerMesg("log $msg");
237 restore_increments( @parts );
243 #unlink $config_file || die "can't remove $config_file: $!";
245 rmtree($inc_tmp_dir) if (-e $inc_tmp_dir);