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