1 |
#!/usr/bin/perl -w |
2 |
|
3 |
=head1 NAME |
4 |
|
5 |
BackupPC_recover_from_increments |
6 |
|
7 |
=head1 DESCRIPTION |
8 |
|
9 |
quick hack to create BackupPC pool out of increments |
10 |
|
11 |
=head1 SYNOPSYS |
12 |
|
13 |
BackupPC_recover_from_increments /restore/from/directory/ [/path/to/increment/to/restore.tar.gz ... ] |
14 |
|
15 |
=head1 HISTORY |
16 |
|
17 |
2006-02-07 Dobrica Pavlinusic <dpavlin@rot13.org> |
18 |
|
19 |
=cut |
20 |
|
21 |
use File::Find; |
22 |
use Data::Dumper; |
23 |
|
24 |
use lib "/data/backuppc/lib"; |
25 |
use BackupPC::Lib; |
26 |
|
27 |
# connect to BackupPC_server |
28 |
|
29 |
die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) ); |
30 |
my %Conf = $bpc->Conf(); |
31 |
|
32 |
$bpc->ChildInit(); |
33 |
|
34 |
my $err = $bpc->ServerConnect($Conf{ServerHost}, $Conf{ServerPort}); |
35 |
if ( $err ) { |
36 |
print("Can't connect to server ($err)\n"); |
37 |
exit(1); |
38 |
} |
39 |
|
40 |
my $TopDir = $bpc->TopDir(); |
41 |
|
42 |
print Dumper(\%Conf); |
43 |
|
44 |
# create restore host configuration |
45 |
|
46 |
my $restore_path = "$TopDir/$temp/restore.tar.gz"; |
47 |
|
48 |
my $conf_restore = <<'_END_OF_CONF_'; |
49 |
|
50 |
$Conf{XferMethod} = 'tar'; |
51 |
|
52 |
$Conf{TarFullArgs} = 'echo "full backups are not supported in restore!" ; exit 1'; |
53 |
$Conf{TarIncrArgs} = ''; |
54 |
|
55 |
# fake ping when restoring |
56 |
$Conf{PingCmd} = '$pingPath -c 1 localhost', |
57 |
|
58 |
$Conf{TarClientCmd} = 'zcat __restore_path__'; |
59 |
|
60 |
1; |
61 |
|
62 |
_END_OF_CONF_ |
63 |
|
64 |
$conf_restore =~ s/__restore_path__/$restore_path/gs; |
65 |
|
66 |
my $config_file = "$bpc->{TopDir}/conf/restore.pl"; |
67 |
|
68 |
open(my $host_fh, '>', $config_file) || die "can't open $config_file: $!"; |
69 |
print $host_fh $conf_restore || die "can't write configuration in $config_file: $!"; |
70 |
close($host_fh) || die "can't close $config_file: $!"; |
71 |
|
72 |
warn "written config:\n$conf_restore\n"; |
73 |
|
74 |
sub restore_increment { |
75 |
my $path = shift || die "need path!"; |
76 |
|
77 |
print "working on $path\n"; |
78 |
|
79 |
if (-e $restore_path) { |
80 |
unlink $restore_path || die "can't remove $restore_path: $!\n"; |
81 |
} |
82 |
link $path, $restore_path || die "can't create link $path -> $restore_path: $!\n"; |
83 |
|
84 |
$bpc->ServerMesg("log User backuppc started restore of $path"); |
85 |
|
86 |
my $full = 0; |
87 |
my $r = $bpc->ServerMesg("backup restore restore backuppc $full"); |
88 |
print "backup --> $r"; |
89 |
|
90 |
# Status_backup_in_progress |
91 |
# Status_idle |
92 |
|
93 |
my ($state,$last_state) = ('','x'); |
94 |
|
95 |
while ($state ne 'Status_idle') { |
96 |
my $s = $bpc->ServerMesg("status hosts"); |
97 |
my %Status; |
98 |
{ |
99 |
eval "$s"; |
100 |
} |
101 |
$state = $Status{restore}->{state}; |
102 |
if ($state ne $last_state) { |
103 |
print "\n$state"; #, Dumper($Status{restore}); |
104 |
} else { |
105 |
print "."; |
106 |
} |
107 |
$last_state = $state; |
108 |
sleep 1; |
109 |
} |
110 |
print "\n"; |
111 |
} |
112 |
|
113 |
# now, start restore |
114 |
|
115 |
foreach my $restore_inc (@ARGV) { |
116 |
|
117 |
if (-d $restore_inc) { |
118 |
|
119 |
find({ wanted => sub { |
120 |
restore_increment( $File::Find::name ); |
121 |
}, follow => 0 }, $restore_inc); |
122 |
|
123 |
} elsif (-f $restore_inc && $restore_inc =~ m/\.tar\.gz$/i) { |
124 |
restore_increment( $restore_inc ); |
125 |
} else { |
126 |
warn "skipped: $restore_inc, not directory or .tar.gz increment\n"; |
127 |
} |
128 |
|
129 |
} |
130 |
|
131 |
#unlink $config_file || die "can't remove $config_file: $!"; |