/[BackupPC]/trunk/bin/BackupPC_recover_from_increments
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/bin/BackupPC_recover_from_increments

Parent Directory Parent Directory | Revision Log Revision Log


Revision 357 - (hide annotations)
Thu Apr 27 09:45:01 2006 UTC (18 years ago) by dpavlin
File size: 3992 byte(s)
 r602@athlon:  dpavlin | 2006-04-27 11:43:41 +0200
 don't display files while extracting, remove IncrementTempDir after finish

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 dpavlin 356 use File::Path;
25 dpavlin 333 use Data::Dumper;
26    
27     use lib "/data/backuppc/lib";
28     use BackupPC::Lib;
29    
30 dpavlin 342 my $host = 'restore';
31 dpavlin 349 my $share = '/etc';
32 dpavlin 342
33 dpavlin 333 # connect to BackupPC_server
34    
35     die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
36 dpavlin 340 my %Conf = $bpc->Conf();
37 dpavlin 333
38     $bpc->ChildInit();
39    
40     my $err = $bpc->ServerConnect($Conf{ServerHost}, $Conf{ServerPort});
41     if ( $err ) {
42     print("Can't connect to server ($err)\n");
43     exit(1);
44     }
45    
46 dpavlin 340 my $TopDir = $bpc->TopDir();
47 dpavlin 333
48 dpavlin 343 #print Dumper(\%Conf);
49 dpavlin 340
50 dpavlin 342 # check if host exists
51    
52 dpavlin 343 my $host_info = $bpc->HostInfoRead( $host );
53 dpavlin 356 #print Dumper($host_info, $bpc->HostInfoRead( 'localhost' ));
54 dpavlin 344 die "host '$host' is not found, please add it to config/hosts configuration file\n" unless ($host_info->{$host});
55 dpavlin 342
56 dpavlin 356 # take care of temporary directory for increments
57    
58     my $inc_tmp_dir = $Conf{IncrementTempDir} || die "need working directory in IncrementTempDir\n";
59     rmtree($inc_tmp_dir) if (-e $inc_tmp_dir);
60    
61     mkpath($inc_tmp_dir);
62    
63     print "# using $inc_tmp_dir for increment scratch space";
64    
65 dpavlin 340 # create restore host configuration
66    
67     my $conf_restore = <<'_END_OF_CONF_';
68    
69     $Conf{XferMethod} = 'tar';
70 dpavlin 349 $Conf{TarShareName} = '__share__';
71 dpavlin 340
72 dpavlin 352 # disable ping
73     $Conf{PingCmd} = '';
74     # work-around for Backup aborted because of CorrectHostCheck
75     $Conf{FixedIPNetBiosNameCheck} = 0;
76     $Conf{NmbLookupCmd} = '';
77 dpavlin 345 $Conf{ClientNameAlias} = 'localhost';
78 dpavlin 340
79 dpavlin 356 #$Conf{TarIncrArgs} = '';
80     #$Conf{ClientTimeout} = 600;
81     #$Conf{TarClientCmd} = '';
82     #$Conf{TarFullArgs} = 'gzip -cdv __restore_path__';
83 dpavlin 340
84 dpavlin 356 $Conf{TarClientCmd} = '$tarPath -c -v -f - -C __inc_tmp_dir__ --totals';
85    
86 dpavlin 340 1;
87    
88     _END_OF_CONF_
89    
90 dpavlin 349 $conf_restore =~ s/__share__/$share/gs;
91 dpavlin 356 $conf_restore =~ s/__inc_tmp_dir__/$inc_tmp_dir/gs;
92 dpavlin 340
93 dpavlin 342 my $config_file = "$bpc->{TopDir}/conf/${host}.pl";
94 dpavlin 340
95     open(my $host_fh, '>', $config_file) || die "can't open $config_file: $!";
96     print $host_fh $conf_restore || die "can't write configuration in $config_file: $!";
97     close($host_fh) || die "can't close $config_file: $!";
98    
99     warn "written config:\n$conf_restore\n";
100    
101     sub restore_increment {
102     my $path = shift || die "need path!";
103    
104 dpavlin 347 if ($path !~ m/\.tar\.gz$/i) {
105 dpavlin 356 print "# skipping $path, not .tar.gz increment\n";
106 dpavlin 347 return;
107     }
108    
109 dpavlin 356 print "restoring $path\n";
110 dpavlin 333
111 dpavlin 357 my $cmd = "cd $inc_tmp_dir && tar xfz $path";
112 dpavlin 356 system($cmd) == 0 or die "can't execute: $cmd -- $?\n";
113 dpavlin 333
114 dpavlin 356 print "starting import into BackupPC pool\n";
115    
116 dpavlin 344 my $user = $host_info->{$host}->{user} || die "can't get user for host $host";
117 dpavlin 333
118 dpavlin 344 $bpc->ServerMesg("log User $user started recovery from increment $path");
119    
120 dpavlin 348 my @backups = $bpc->BackupInfoRead( $host );
121    
122     my $full = 1;
123     foreach my $b (@backups) {
124     $full = 0 if ($b->{type} eq 'full');
125     }
126    
127 dpavlin 344 my $r = $bpc->ServerMesg("backup $host $host $user $full");
128 dpavlin 348 print "backup ", $full ? 'full' : 'incremental', " --> $r";
129 dpavlin 342 die $r if ($r =~ m/^error/);
130 dpavlin 333
131     # Status_backup_in_progress
132     # Status_idle
133    
134 dpavlin 340 my ($state,$last_state) = ('','x');
135 dpavlin 333
136     while ($state ne 'Status_idle') {
137     my $s = $bpc->ServerMesg("status hosts");
138     my %Status;
139     {
140     eval "$s";
141     }
142     $state = $Status{restore}->{state};
143 dpavlin 342
144     die $state if ($state =~ m/^error/);
145    
146 dpavlin 340 if ($state ne $last_state) {
147     print "\n$state"; #, Dumper($Status{restore});
148     } else {
149     print ".";
150     }
151     $last_state = $state;
152 dpavlin 333 sleep 1;
153     }
154 dpavlin 340 print "\n";
155 dpavlin 333 }
156    
157 dpavlin 340 # now, start restore
158 dpavlin 333
159 dpavlin 340 foreach my $restore_inc (@ARGV) {
160    
161     if (-d $restore_inc) {
162    
163     find({ wanted => sub {
164     restore_increment( $File::Find::name );
165     }, follow => 0 }, $restore_inc);
166    
167 dpavlin 347 } elsif (-f $restore_inc) {
168 dpavlin 340 restore_increment( $restore_inc );
169     } else {
170 dpavlin 347 warn "skipped: $restore_inc, not file or directory\n";
171 dpavlin 340 }
172    
173     }
174    
175     #unlink $config_file || die "can't remove $config_file: $!";
176 dpavlin 357
177     rmtree($inc_tmp_dir) if (-e $inc_tmp_dir);

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26