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 Data::Dumper; |
25 |
|
26 |
use lib "/data/backuppc/lib"; |
27 |
use BackupPC::Lib; |
28 |
|
29 |
my $host = 'restore'; |
30 |
|
31 |
# connect to BackupPC_server |
32 |
|
33 |
die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) ); |
34 |
my %Conf = $bpc->Conf(); |
35 |
|
36 |
$bpc->ChildInit(); |
37 |
|
38 |
my $err = $bpc->ServerConnect($Conf{ServerHost}, $Conf{ServerPort}); |
39 |
if ( $err ) { |
40 |
print("Can't connect to server ($err)\n"); |
41 |
exit(1); |
42 |
} |
43 |
|
44 |
my $TopDir = $bpc->TopDir(); |
45 |
|
46 |
#print Dumper(\%Conf); |
47 |
|
48 |
# check if host exists |
49 |
|
50 |
my $host_info = $bpc->HostInfoRead( $host ); |
51 |
print Dumper($host_info, $bpc->HostInfoRead( 'localhost' )); |
52 |
die "host '$host' is not found, please add it to config/hosts configuration file\n" unless ($host_info->{$host}); |
53 |
|
54 |
# create restore host configuration |
55 |
|
56 |
my $restore_path = "$Conf{InstallDir}/$Conf{GzipTempDir}/${host}-restore.tar.gz"; |
57 |
|
58 |
my $conf_restore = <<'_END_OF_CONF_'; |
59 |
|
60 |
$Conf{XferMethod} = 'tar'; |
61 |
|
62 |
$Conf{TarFullArgs} = 'echo "full backups are not supported in restore!" ; exit 1'; |
63 |
$Conf{TarIncrArgs} = ''; |
64 |
|
65 |
# fake ping when restoring |
66 |
$Conf{PingCmd} = '$pingPath -c 1 localhost', |
67 |
$Conf{ClientNameAlias} = 'localhost'; |
68 |
|
69 |
$Conf{TarClientCmd} = 'zcat __restore_path__'; |
70 |
|
71 |
1; |
72 |
|
73 |
_END_OF_CONF_ |
74 |
|
75 |
$conf_restore =~ s/__restore_path__/$restore_path/gs; |
76 |
|
77 |
my $config_file = "$bpc->{TopDir}/conf/${host}.pl"; |
78 |
|
79 |
open(my $host_fh, '>', $config_file) || die "can't open $config_file: $!"; |
80 |
print $host_fh $conf_restore || die "can't write configuration in $config_file: $!"; |
81 |
close($host_fh) || die "can't close $config_file: $!"; |
82 |
|
83 |
warn "written config:\n$conf_restore\n"; |
84 |
|
85 |
sub restore_increment { |
86 |
my $path = shift || die "need path!"; |
87 |
|
88 |
print "working on $path\n"; |
89 |
|
90 |
if (-e $restore_path) { |
91 |
unlink $restore_path || die "can't remove $restore_path: $!\n"; |
92 |
} |
93 |
link $path, $restore_path || die "can't create link $path -> $restore_path: $!\n"; |
94 |
|
95 |
my $user = $host_info->{$host}->{user} || die "can't get user for host $host"; |
96 |
|
97 |
$bpc->ServerMesg("log User $user started recovery from increment $path"); |
98 |
|
99 |
my $full = 0; |
100 |
my $r = $bpc->ServerMesg("backup $host $host $user $full"); |
101 |
print "backup --> $r"; |
102 |
die $r if ($r =~ m/^error/); |
103 |
|
104 |
# Status_backup_in_progress |
105 |
# Status_idle |
106 |
|
107 |
my ($state,$last_state) = ('','x'); |
108 |
|
109 |
while ($state ne 'Status_idle') { |
110 |
my $s = $bpc->ServerMesg("status hosts"); |
111 |
my %Status; |
112 |
{ |
113 |
eval "$s"; |
114 |
} |
115 |
$state = $Status{restore}->{state}; |
116 |
|
117 |
die $state if ($state =~ m/^error/); |
118 |
|
119 |
if ($state ne $last_state) { |
120 |
print "\n$state"; #, Dumper($Status{restore}); |
121 |
} else { |
122 |
print "."; |
123 |
} |
124 |
$last_state = $state; |
125 |
sleep 1; |
126 |
} |
127 |
print "\n"; |
128 |
} |
129 |
|
130 |
# now, start restore |
131 |
|
132 |
foreach my $restore_inc (@ARGV) { |
133 |
|
134 |
if (-d $restore_inc) { |
135 |
|
136 |
find({ wanted => sub { |
137 |
restore_increment( $File::Find::name ); |
138 |
}, follow => 0 }, $restore_inc); |
139 |
|
140 |
} elsif (-f $restore_inc && $restore_inc =~ m/\.tar\.gz$/i) { |
141 |
restore_increment( $restore_inc ); |
142 |
} else { |
143 |
warn "skipped: $restore_inc, not directory or .tar.gz increment\n"; |
144 |
} |
145 |
|
146 |
} |
147 |
|
148 |
#unlink $config_file || die "can't remove $config_file: $!"; |