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