1 |
#!/usr/bin/perl -w |
#!/usr/bin/perl -w |
2 |
|
|
3 |
|
use strict; |
4 |
|
|
5 |
=head1 NAME |
=head1 NAME |
6 |
|
|
7 |
BackupPC_recover_from_increments |
BackupPC_recover_from_increments |
26 |
use lib "/data/backuppc/lib"; |
use lib "/data/backuppc/lib"; |
27 |
use BackupPC::Lib; |
use BackupPC::Lib; |
28 |
|
|
29 |
|
my $host = 'restore'; |
30 |
|
|
31 |
# connect to BackupPC_server |
# connect to BackupPC_server |
32 |
|
|
33 |
die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) ); |
die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) ); |
43 |
|
|
44 |
my $TopDir = $bpc->TopDir(); |
my $TopDir = $bpc->TopDir(); |
45 |
|
|
46 |
print Dumper(\%Conf); |
#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 |
# create restore host configuration |
55 |
|
|
56 |
my $restore_path = "$TopDir/$temp/restore.tar.gz"; |
my $restore_path = "$Conf{InstallDir}/$Conf{GzipTempDir}/${host}-restore.tar.gz"; |
57 |
|
|
58 |
my $conf_restore = <<'_END_OF_CONF_'; |
my $conf_restore = <<'_END_OF_CONF_'; |
59 |
|
|
64 |
|
|
65 |
# fake ping when restoring |
# fake ping when restoring |
66 |
$Conf{PingCmd} = '$pingPath -c 1 localhost', |
$Conf{PingCmd} = '$pingPath -c 1 localhost', |
67 |
|
$Conf{ClientNameAlias} = 'localhost'; |
68 |
|
|
69 |
$Conf{TarClientCmd} = 'zcat __restore_path__'; |
$Conf{TarClientCmd} = 'zcat __restore_path__'; |
70 |
|
|
74 |
|
|
75 |
$conf_restore =~ s/__restore_path__/$restore_path/gs; |
$conf_restore =~ s/__restore_path__/$restore_path/gs; |
76 |
|
|
77 |
my $config_file = "$bpc->{TopDir}/conf/restore.pl"; |
my $config_file = "$bpc->{TopDir}/conf/${host}.pl"; |
78 |
|
|
79 |
open(my $host_fh, '>', $config_file) || die "can't open $config_file: $!"; |
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: $!"; |
print $host_fh $conf_restore || die "can't write configuration in $config_file: $!"; |
85 |
sub restore_increment { |
sub restore_increment { |
86 |
my $path = shift || die "need path!"; |
my $path = shift || die "need path!"; |
87 |
|
|
88 |
|
if ($path !~ m/\.tar\.gz$/i) { |
89 |
|
print "skipping $path, not .tar.gz increment\n"; |
90 |
|
return; |
91 |
|
} |
92 |
|
|
93 |
print "working on $path\n"; |
print "working on $path\n"; |
94 |
|
|
95 |
if (-e $restore_path) { |
if (-e $restore_path) { |
96 |
unlink $restore_path || die "can't remove $restore_path: $!\n"; |
unlink $restore_path || die "can't remove $restore_path: $!\n"; |
97 |
} |
} |
98 |
link $path, $restore_path || die "can't create link $path -> $restore_path: $!\n"; |
symlink $path, $restore_path || die "can't create link $path -> $restore_path: $!\n"; |
99 |
|
|
100 |
$bpc->ServerMesg("log User backuppc started restore of $path"); |
my $user = $host_info->{$host}->{user} || die "can't get user for host $host"; |
101 |
|
|
102 |
my $full = 0; |
$bpc->ServerMesg("log User $user started recovery from increment $path"); |
103 |
my $r = $bpc->ServerMesg("backup restore restore backuppc $full"); |
|
104 |
print "backup --> $r"; |
my @backups = $bpc->BackupInfoRead( $host ); |
105 |
|
|
106 |
|
my $full = 1; |
107 |
|
foreach my $b (@backups) { |
108 |
|
$full = 0 if ($b->{type} eq 'full'); |
109 |
|
} |
110 |
|
|
111 |
|
my $r = $bpc->ServerMesg("backup $host $host $user $full"); |
112 |
|
print "backup ", $full ? 'full' : 'incremental', " --> $r"; |
113 |
|
die $r if ($r =~ m/^error/); |
114 |
|
|
115 |
# Status_backup_in_progress |
# Status_backup_in_progress |
116 |
# Status_idle |
# Status_idle |
124 |
eval "$s"; |
eval "$s"; |
125 |
} |
} |
126 |
$state = $Status{restore}->{state}; |
$state = $Status{restore}->{state}; |
127 |
|
|
128 |
|
die $state if ($state =~ m/^error/); |
129 |
|
|
130 |
if ($state ne $last_state) { |
if ($state ne $last_state) { |
131 |
print "\n$state"; #, Dumper($Status{restore}); |
print "\n$state"; #, Dumper($Status{restore}); |
132 |
} else { |
} else { |
148 |
restore_increment( $File::Find::name ); |
restore_increment( $File::Find::name ); |
149 |
}, follow => 0 }, $restore_inc); |
}, follow => 0 }, $restore_inc); |
150 |
|
|
151 |
} elsif (-f $restore_inc && $restore_inc =~ m/\.tar\.gz$/i) { |
} elsif (-f $restore_inc) { |
152 |
restore_increment( $restore_inc ); |
restore_increment( $restore_inc ); |
153 |
} else { |
} else { |
154 |
warn "skipped: $restore_inc, not directory or .tar.gz increment\n"; |
warn "skipped: $restore_inc, not file or directory\n"; |
155 |
} |
} |
156 |
|
|
157 |
} |
} |