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 |
# connect to BackupPC_server |
39 |
|
40 |
die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) ); |
41 |
my %Conf = $bpc->Conf(); |
42 |
|
43 |
$bpc->ChildInit(); |
44 |
|
45 |
my $err = $bpc->ServerConnect($Conf{ServerHost}, $Conf{ServerPort}); |
46 |
if ( $err ) { |
47 |
print("Can't connect to server ($err)\n"); |
48 |
exit(1); |
49 |
} |
50 |
|
51 |
my $TopDir = $bpc->TopDir(); |
52 |
|
53 |
#print Dumper(\%Conf); |
54 |
|
55 |
# check if host exists |
56 |
|
57 |
my $host_info = $bpc->HostInfoRead( $host ); |
58 |
#print Dumper($host_info, $bpc->HostInfoRead( 'localhost' )); |
59 |
die "host '$host' is not found, please add it to config/hosts configuration file\n" unless ($host_info->{$host}); |
60 |
|
61 |
# take care of temporary directory for increments |
62 |
|
63 |
my $inc_tmp_dir = $Conf{IncrementTempDir} || die "need working directory in IncrementTempDir\n"; |
64 |
|
65 |
sub cleanup_inc_temp_dir { |
66 |
rmtree($inc_tmp_dir) if (-e $inc_tmp_dir); |
67 |
mkpath($inc_tmp_dir); |
68 |
} |
69 |
|
70 |
print "# using $inc_tmp_dir for increment scratch space"; |
71 |
cleanup_inc_temp_dir() if (! $cleanup_before_increment); |
72 |
|
73 |
# create restore host configuration |
74 |
|
75 |
my $conf_restore = <<'_END_OF_CONF_'; |
76 |
|
77 |
$Conf{XferMethod} = 'tar'; |
78 |
$Conf{TarShareName} = '__share__'; |
79 |
|
80 |
# disable ping |
81 |
$Conf{PingCmd} = ''; |
82 |
# work-around for Backup aborted because of CorrectHostCheck |
83 |
$Conf{FixedIPNetBiosNameCheck} = 0; |
84 |
$Conf{NmbLookupCmd} = ''; |
85 |
$Conf{ClientNameAlias} = 'localhost'; |
86 |
|
87 |
#$Conf{TarIncrArgs} = ''; |
88 |
#$Conf{ClientTimeout} = 600; |
89 |
#$Conf{TarClientCmd} = ''; |
90 |
#$Conf{TarFullArgs} = 'gzip -cdv __restore_path__'; |
91 |
|
92 |
$Conf{TarClientCmd} = '$tarPath -c -v -f - -C __inc_tmp_dir__ --totals'; |
93 |
|
94 |
1; |
95 |
|
96 |
_END_OF_CONF_ |
97 |
|
98 |
$conf_restore =~ s/__share__/$share/gs; |
99 |
$conf_restore =~ s/__inc_tmp_dir__/$inc_tmp_dir/gs; |
100 |
|
101 |
my $config_file = "$bpc->{TopDir}/conf/${host}.pl"; |
102 |
|
103 |
open(my $host_fh, '>', $config_file) || die "can't open $config_file: $!"; |
104 |
print $host_fh $conf_restore || die "can't write configuration in $config_file: $!"; |
105 |
close($host_fh) || die "can't close $config_file: $!"; |
106 |
|
107 |
warn "written config:\n$conf_restore\n"; |
108 |
|
109 |
sub restore_increment { |
110 |
my $path = shift || die "need path!"; |
111 |
|
112 |
if ($path !~ m/\.tar\.gz$/i) { |
113 |
print "# skipping $path, not .tar.gz increment\n"; |
114 |
return; |
115 |
} |
116 |
|
117 |
print "restoring $path\n"; |
118 |
|
119 |
cleanup_inc_temp_dir() if ($cleanup_before_increment); |
120 |
|
121 |
my $cmd = "cd $inc_tmp_dir && tar xfz $path"; |
122 |
system($cmd) == 0 or die "can't execute: $cmd -- $?\n"; |
123 |
|
124 |
print "starting import into BackupPC pool\n"; |
125 |
|
126 |
my $user = $host_info->{$host}->{user} || die "can't get user for host $host"; |
127 |
|
128 |
$bpc->ServerMesg("log User $user started recovery from increment $path"); |
129 |
|
130 |
my @backups = $bpc->BackupInfoRead( $host ); |
131 |
|
132 |
my $full = 1; |
133 |
foreach my $b (@backups) { |
134 |
$full = 0 if ($b->{type} eq 'full'); |
135 |
} |
136 |
|
137 |
my $r = $bpc->ServerMesg("backup $host $host $user $full"); |
138 |
print "backup ", $full ? 'full' : 'incremental', " --> $r"; |
139 |
die $r if ($r =~ m/^error/); |
140 |
|
141 |
# Status_backup_in_progress |
142 |
# Status_idle |
143 |
|
144 |
my ($state,$last_state) = ('','x'); |
145 |
|
146 |
while ($state ne 'Status_idle') { |
147 |
my $s = $bpc->ServerMesg("status hosts"); |
148 |
my %Status; |
149 |
{ |
150 |
eval "$s"; |
151 |
} |
152 |
$state = $Status{restore}->{state}; |
153 |
|
154 |
die $state if ($state =~ m/^error/); |
155 |
|
156 |
if ($state ne $last_state) { |
157 |
print "\n$state"; #, Dumper($Status{restore}); |
158 |
} else { |
159 |
print "."; |
160 |
} |
161 |
$last_state = $state; |
162 |
sleep 1; |
163 |
} |
164 |
print "\n"; |
165 |
} |
166 |
|
167 |
# now, start restore |
168 |
|
169 |
foreach my $restore_inc (@ARGV) { |
170 |
|
171 |
if (-d $restore_inc) { |
172 |
|
173 |
find({ wanted => sub { |
174 |
restore_increment( $File::Find::name ); |
175 |
}, follow => 0 }, $restore_inc); |
176 |
|
177 |
} elsif (-f $restore_inc) { |
178 |
restore_increment( $restore_inc ); |
179 |
} else { |
180 |
warn "skipped: $restore_inc, not file or directory\n"; |
181 |
} |
182 |
|
183 |
} |
184 |
|
185 |
#unlink $config_file || die "can't remove $config_file: $!"; |
186 |
|
187 |
rmtree($inc_tmp_dir) if (-e $inc_tmp_dir); |