1 |
#!/usr/bin/perl -w |
2 |
|
3 |
use strict; |
4 |
|
5 |
=head1 NAME |
6 |
|
7 |
BackupPC_recover_from_increments |
8 |
|
9 |
=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 |
use File::Find; |
24 |
use File::Path; |
25 |
use Data::Dumper; |
26 |
|
27 |
use lib "/data/backuppc/lib"; |
28 |
use BackupPC::Lib; |
29 |
|
30 |
my $host = 'restore'; |
31 |
my $share = '/etc'; |
32 |
|
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; |
37 |
|
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; |
41 |
|
42 |
# connect to BackupPC_server |
43 |
|
44 |
my $bpc = BackupPC::Lib->new(undef, undef, 1) || die; |
45 |
my %Conf = $bpc->Conf(); |
46 |
|
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 |
my $TopDir = $bpc->TopDir(); |
56 |
|
57 |
#print Dumper(\%Conf); |
58 |
|
59 |
# check if host exists |
60 |
|
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}); |
64 |
|
65 |
# take care of temporary directory for increments |
66 |
|
67 |
my $inc_tmp_dir = $Conf{IncrementTempDir} || die "need working directory in IncrementTempDir\n"; |
68 |
|
69 |
sub cleanup_inc_temp_dir { |
70 |
rmtree($inc_tmp_dir) if (-e $inc_tmp_dir); |
71 |
mkpath($inc_tmp_dir); |
72 |
} |
73 |
|
74 |
print "# using $inc_tmp_dir for increment scratch space"; |
75 |
cleanup_inc_temp_dir() if (! $cleanup_before_increment); |
76 |
|
77 |
my $restore_path = "$Conf{InstallDir}/$Conf{GzipTempDir}/${host}-restore.tar.gz"; |
78 |
|
79 |
# create restore host configuration |
80 |
|
81 |
my $conf_restore = <<'_END_OF_CONF_'; |
82 |
|
83 |
$Conf{XferMethod} = 'tar'; |
84 |
$Conf{TarShareName} = '__share__'; |
85 |
|
86 |
# disable ping |
87 |
$Conf{PingCmd} = ''; |
88 |
# work-around for Backup aborted because of CorrectHostCheck |
89 |
$Conf{FixedIPNetBiosNameCheck} = 0; |
90 |
$Conf{NmbLookupCmd} = ''; |
91 |
$Conf{ClientNameAlias} = 'localhost'; |
92 |
|
93 |
#$Conf{TarIncrArgs} = ''; |
94 |
#$Conf{ClientTimeout} = 600; |
95 |
|
96 |
_END_OF_CONF_ |
97 |
|
98 |
$conf_restore .= <<'_END_OF_CONF_' if (! $restore_via_temp_dir); |
99 |
|
100 |
#$Conf{TarClientCmd} = ''; |
101 |
#$Conf{TarFullArgs} = 'gzip -cdv __restore_path__'; |
102 |
|
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__'; |
107 |
|
108 |
_END_OF_CONF_ |
109 |
|
110 |
$conf_restore .= <<'_END_OF_CONF_' if ($restore_via_temp_dir); |
111 |
|
112 |
$Conf{TarClientCmd} = '$tarPath -c -v -f - -C __inc_tmp_dir__ --totals'; |
113 |
|
114 |
_END_OF_CONF_ |
115 |
|
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; |
119 |
|
120 |
$conf_restore .= "\n1;\n"; |
121 |
|
122 |
my $config_file = "$bpc->{TopDir}/conf/${host}.pl"; |
123 |
|
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 |
sub restore_increment { |
131 |
my $path = shift || die "need path!"; |
132 |
|
133 |
if ($path !~ m/\.tar\.gz$/i) { |
134 |
print "# skipping $path, not .tar.gz increment\n"; |
135 |
return; |
136 |
} |
137 |
|
138 |
if ($restore_via_temp_dir) { |
139 |
|
140 |
print "restoring $path (extracting to create increment)\n"; |
141 |
|
142 |
cleanup_inc_temp_dir() if ($cleanup_before_increment); |
143 |
|
144 |
my $cmd = "cd $inc_tmp_dir && tar xfz $path"; |
145 |
system($cmd) == 0 or die "can't execute: $cmd -- $?\n"; |
146 |
|
147 |
} else { |
148 |
|
149 |
print "using $path to create increment\n"; |
150 |
|
151 |
if (-e $restore_path) { |
152 |
unlink $restore_path || die "can't remove $restore_path: $!\n"; |
153 |
} |
154 |
symlink $path, $restore_path || die "can't create link $path -> $restore_path: $!\n"; |
155 |
|
156 |
|
157 |
} |
158 |
|
159 |
print "starting import into BackupPC pool\n"; |
160 |
|
161 |
my $user = $host_info->{$host}->{user} || die "can't get user for host $host"; |
162 |
|
163 |
$bpc->ServerMesg("log User $user started recovery from increment $path"); |
164 |
|
165 |
my @backups = $bpc->BackupInfoRead( $host ); |
166 |
|
167 |
my $full = 1; |
168 |
foreach my $b (@backups) { |
169 |
$full = 0 if ($b->{type} eq 'full'); |
170 |
} |
171 |
|
172 |
my $r = $bpc->ServerMesg("backup $host $host $user $full"); |
173 |
print "backup ", $full ? 'full' : 'incremental', " --> $r"; |
174 |
die $r if ($r =~ m/^error/); |
175 |
|
176 |
# Status_backup_in_progress |
177 |
# Status_idle |
178 |
|
179 |
my ($state,$last_state) = ('','x'); |
180 |
|
181 |
while ($state ne 'Status_idle') { |
182 |
my $s = $bpc->ServerMesg("status hosts"); |
183 |
my %Status; |
184 |
{ |
185 |
eval "$s"; |
186 |
} |
187 |
$state = $Status{restore}->{state}; |
188 |
|
189 |
die $state if ($state =~ m/^error/); |
190 |
|
191 |
if ($state ne $last_state) { |
192 |
print "\n$state"; #, Dumper($Status{restore}); |
193 |
} else { |
194 |
print "."; |
195 |
} |
196 |
$last_state = $state; |
197 |
sleep 1; |
198 |
} |
199 |
print "\n"; |
200 |
} |
201 |
|
202 |
# now, start restore |
203 |
|
204 |
foreach my $restore_inc (@ARGV) { |
205 |
|
206 |
if (-d $restore_inc) { |
207 |
|
208 |
find({ wanted => sub { |
209 |
restore_increment( $File::Find::name ); |
210 |
}, follow => 0 }, $restore_inc); |
211 |
|
212 |
} elsif (-f $restore_inc) { |
213 |
restore_increment( $restore_inc ); |
214 |
} else { |
215 |
warn "skipped: $restore_inc, not file or directory\n"; |
216 |
} |
217 |
|
218 |
} |
219 |
|
220 |
#unlink $config_file || die "can't remove $config_file: $!"; |
221 |
|
222 |
rmtree($inc_tmp_dir) if (-e $inc_tmp_dir); |