1 |
dpavlin |
333 |
#!/usr/bin/perl -w |
2 |
|
|
|
3 |
dpavlin |
342 |
use strict; |
4 |
|
|
|
5 |
dpavlin |
340 |
=head1 NAME |
6 |
dpavlin |
333 |
|
7 |
dpavlin |
340 |
BackupPC_recover_from_increments |
8 |
dpavlin |
333 |
|
9 |
dpavlin |
340 |
=head1 DESCRIPTION |
10 |
|
|
|
11 |
|
|
quick hack to create BackupPC pool out of increments |
12 |
|
|
|
13 |
|
|
=head1 SYNOPSYS |
14 |
|
|
|
15 |
|
|
BackupPC_recover_from_increments /restore/from/directory/ [/path/to/increment/to/restore.tar.gz ... ] |
16 |
|
|
|
17 |
|
|
=head1 HISTORY |
18 |
|
|
|
19 |
|
|
2006-02-07 Dobrica Pavlinusic <dpavlin@rot13.org> |
20 |
|
|
|
21 |
|
|
=cut |
22 |
|
|
|
23 |
dpavlin |
333 |
use File::Find; |
24 |
dpavlin |
356 |
use File::Path; |
25 |
dpavlin |
333 |
use Data::Dumper; |
26 |
|
|
|
27 |
|
|
use lib "/data/backuppc/lib"; |
28 |
|
|
use BackupPC::Lib; |
29 |
|
|
|
30 |
dpavlin |
342 |
my $host = 'restore'; |
31 |
dpavlin |
349 |
my $share = '/etc'; |
32 |
dpavlin |
342 |
|
33 |
dpavlin |
360 |
# 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; |
37 |
|
|
|
38 |
dpavlin |
361 |
# 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; |
41 |
|
|
|
42 |
dpavlin |
333 |
# connect to BackupPC_server |
43 |
|
|
|
44 |
dpavlin |
361 |
my $bpc = BackupPC::Lib->new(undef, undef, 1) || die; |
45 |
dpavlin |
340 |
my %Conf = $bpc->Conf(); |
46 |
dpavlin |
333 |
|
47 |
|
|
$bpc->ChildInit(); |
48 |
|
|
|
49 |
|
|
my $err = $bpc->ServerConnect($Conf{ServerHost}, $Conf{ServerPort}); |
50 |
|
|
if ( $err ) { |
51 |
|
|
print("Can't connect to server ($err)\n"); |
52 |
|
|
exit(1); |
53 |
|
|
} |
54 |
|
|
|
55 |
dpavlin |
340 |
my $TopDir = $bpc->TopDir(); |
56 |
dpavlin |
333 |
|
57 |
dpavlin |
343 |
#print Dumper(\%Conf); |
58 |
dpavlin |
340 |
|
59 |
dpavlin |
342 |
# check if host exists |
60 |
|
|
|
61 |
dpavlin |
343 |
my $host_info = $bpc->HostInfoRead( $host ); |
62 |
dpavlin |
356 |
#print Dumper($host_info, $bpc->HostInfoRead( 'localhost' )); |
63 |
dpavlin |
344 |
die "host '$host' is not found, please add it to config/hosts configuration file\n" unless ($host_info->{$host}); |
64 |
dpavlin |
342 |
|
65 |
dpavlin |
356 |
# take care of temporary directory for increments |
66 |
|
|
|
67 |
|
|
my $inc_tmp_dir = $Conf{IncrementTempDir} || die "need working directory in IncrementTempDir\n"; |
68 |
|
|
|
69 |
dpavlin |
360 |
sub cleanup_inc_temp_dir { |
70 |
|
|
rmtree($inc_tmp_dir) if (-e $inc_tmp_dir); |
71 |
|
|
mkpath($inc_tmp_dir); |
72 |
|
|
} |
73 |
dpavlin |
356 |
|
74 |
|
|
print "# using $inc_tmp_dir for increment scratch space"; |
75 |
dpavlin |
360 |
cleanup_inc_temp_dir() if (! $cleanup_before_increment); |
76 |
dpavlin |
356 |
|
77 |
dpavlin |
361 |
my $restore_path = "$Conf{InstallDir}/$Conf{GzipTempDir}/${host}-restore.tar.gz"; |
78 |
|
|
|
79 |
dpavlin |
340 |
# create restore host configuration |
80 |
|
|
|
81 |
|
|
my $conf_restore = <<'_END_OF_CONF_'; |
82 |
|
|
|
83 |
|
|
$Conf{XferMethod} = 'tar'; |
84 |
dpavlin |
349 |
$Conf{TarShareName} = '__share__'; |
85 |
dpavlin |
340 |
|
86 |
dpavlin |
352 |
# disable ping |
87 |
|
|
$Conf{PingCmd} = ''; |
88 |
|
|
# work-around for Backup aborted because of CorrectHostCheck |
89 |
|
|
$Conf{FixedIPNetBiosNameCheck} = 0; |
90 |
|
|
$Conf{NmbLookupCmd} = ''; |
91 |
dpavlin |
345 |
$Conf{ClientNameAlias} = 'localhost'; |
92 |
dpavlin |
340 |
|
93 |
dpavlin |
356 |
#$Conf{TarIncrArgs} = ''; |
94 |
|
|
#$Conf{ClientTimeout} = 600; |
95 |
dpavlin |
361 |
|
96 |
|
|
_END_OF_CONF_ |
97 |
|
|
|
98 |
|
|
$conf_restore .= <<'_END_OF_CONF_' if (! $restore_via_temp_dir); |
99 |
|
|
|
100 |
dpavlin |
356 |
#$Conf{TarClientCmd} = ''; |
101 |
|
|
#$Conf{TarFullArgs} = 'gzip -cdv __restore_path__'; |
102 |
dpavlin |
340 |
|
103 |
dpavlin |
361 |
# 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__'; |
107 |
|
|
|
108 |
|
|
_END_OF_CONF_ |
109 |
|
|
|
110 |
|
|
$conf_restore .= <<'_END_OF_CONF_' if ($restore_via_temp_dir); |
111 |
|
|
|
112 |
dpavlin |
356 |
$Conf{TarClientCmd} = '$tarPath -c -v -f - -C __inc_tmp_dir__ --totals'; |
113 |
|
|
|
114 |
dpavlin |
340 |
_END_OF_CONF_ |
115 |
|
|
|
116 |
dpavlin |
349 |
$conf_restore =~ s/__share__/$share/gs; |
117 |
dpavlin |
356 |
$conf_restore =~ s/__inc_tmp_dir__/$inc_tmp_dir/gs; |
118 |
dpavlin |
361 |
$conf_restore =~ s/__restore_path__/$restore_path/gs; |
119 |
dpavlin |
340 |
|
120 |
dpavlin |
361 |
$conf_restore .= "\n1;\n"; |
121 |
|
|
|
122 |
dpavlin |
342 |
my $config_file = "$bpc->{TopDir}/conf/${host}.pl"; |
123 |
dpavlin |
340 |
|
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: $!"; |
127 |
|
|
|
128 |
|
|
warn "written config:\n$conf_restore\n"; |
129 |
|
|
|
130 |
dpavlin |
362 |
sub restore_increments { |
131 |
dpavlin |
340 |
|
132 |
dpavlin |
362 |
foreach my $path (@_) { |
133 |
|
|
next unless ($path); # skip 0 element which is undef |
134 |
dpavlin |
347 |
|
135 |
dpavlin |
362 |
print "extracting $path\n"; |
136 |
dpavlin |
333 |
|
137 |
dpavlin |
361 |
my $cmd = "cd $inc_tmp_dir && tar xfz $path"; |
138 |
|
|
system($cmd) == 0 or die "can't execute: $cmd -- $?\n"; |
139 |
|
|
|
140 |
dpavlin |
362 |
} |
141 |
dpavlin |
361 |
|
142 |
dpavlin |
362 |
# print "using $path to create increment\n"; |
143 |
|
|
# |
144 |
|
|
# if (-e $restore_path) { |
145 |
|
|
# unlink $restore_path || die "can't remove $restore_path: $!\n"; |
146 |
|
|
# } |
147 |
|
|
# symlink $path, $restore_path || die "can't create link $path -> $restore_path: $!\n"; |
148 |
dpavlin |
361 |
|
149 |
dpavlin |
356 |
print "starting import into BackupPC pool\n"; |
150 |
|
|
|
151 |
dpavlin |
344 |
my $user = $host_info->{$host}->{user} || die "can't get user for host $host"; |
152 |
dpavlin |
333 |
|
153 |
dpavlin |
348 |
my @backups = $bpc->BackupInfoRead( $host ); |
154 |
|
|
|
155 |
|
|
my $full = 1; |
156 |
|
|
foreach my $b (@backups) { |
157 |
|
|
$full = 0 if ($b->{type} eq 'full'); |
158 |
|
|
} |
159 |
|
|
|
160 |
dpavlin |
344 |
my $r = $bpc->ServerMesg("backup $host $host $user $full"); |
161 |
dpavlin |
348 |
print "backup ", $full ? 'full' : 'incremental', " --> $r"; |
162 |
dpavlin |
342 |
die $r if ($r =~ m/^error/); |
163 |
dpavlin |
333 |
|
164 |
|
|
# Status_backup_in_progress |
165 |
|
|
# Status_idle |
166 |
|
|
|
167 |
dpavlin |
340 |
my ($state,$last_state) = ('','x'); |
168 |
dpavlin |
333 |
|
169 |
|
|
while ($state ne 'Status_idle') { |
170 |
|
|
my $s = $bpc->ServerMesg("status hosts"); |
171 |
|
|
my %Status; |
172 |
|
|
{ |
173 |
|
|
eval "$s"; |
174 |
|
|
} |
175 |
|
|
$state = $Status{restore}->{state}; |
176 |
dpavlin |
342 |
|
177 |
|
|
die $state if ($state =~ m/^error/); |
178 |
|
|
|
179 |
dpavlin |
340 |
if ($state ne $last_state) { |
180 |
|
|
print "\n$state"; #, Dumper($Status{restore}); |
181 |
|
|
} else { |
182 |
|
|
print "."; |
183 |
|
|
} |
184 |
|
|
$last_state = $state; |
185 |
dpavlin |
333 |
sleep 1; |
186 |
|
|
} |
187 |
dpavlin |
340 |
print "\n"; |
188 |
dpavlin |
333 |
} |
189 |
|
|
|
190 |
dpavlin |
340 |
# now, start restore |
191 |
dpavlin |
333 |
|
192 |
dpavlin |
362 |
my $increments; |
193 |
|
|
|
194 |
dpavlin |
340 |
foreach my $restore_inc (@ARGV) { |
195 |
|
|
|
196 |
dpavlin |
362 |
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"; |
204 |
|
|
} else { |
205 |
|
|
print "# skipped: $path\n"; |
206 |
|
|
} |
207 |
|
|
} |
208 |
|
|
|
209 |
dpavlin |
340 |
if (-d $restore_inc) { |
210 |
|
|
|
211 |
|
|
find({ wanted => sub { |
212 |
dpavlin |
362 |
extract_filename( $File::Find::name ); |
213 |
dpavlin |
340 |
}, follow => 0 }, $restore_inc); |
214 |
|
|
|
215 |
dpavlin |
347 |
} elsif (-f $restore_inc) { |
216 |
dpavlin |
362 |
extract_filename( $restore_inc ); |
217 |
dpavlin |
340 |
} else { |
218 |
dpavlin |
347 |
warn "skipped: $restore_inc, not file or directory\n"; |
219 |
dpavlin |
340 |
} |
220 |
|
|
|
221 |
|
|
} |
222 |
|
|
|
223 |
dpavlin |
362 |
print Dumper($increments); |
224 |
|
|
|
225 |
|
|
cleanup_inc_temp_dir(); |
226 |
|
|
|
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"; |
233 |
|
|
|
234 |
|
|
print "LOG: $msg\n"; |
235 |
|
|
$bpc->ServerMesg("log $msg"); |
236 |
|
|
|
237 |
|
|
restore_increments( @parts ); |
238 |
|
|
} |
239 |
|
|
} |
240 |
|
|
} |
241 |
|
|
|
242 |
|
|
|
243 |
dpavlin |
340 |
#unlink $config_file || die "can't remove $config_file: $!"; |
244 |
dpavlin |
357 |
|
245 |
|
|
rmtree($inc_tmp_dir) if (-e $inc_tmp_dir); |