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

Diff of /trunk/bin/BackupPC_incPartsUpdate

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 216 by dpavlin, Sun Oct 16 17:41:10 2005 UTC revision 254 by dpavlin, Mon Dec 12 16:07:27 2005 UTC
# Line 10  use BackupPC::Attrib qw/:all/; Line 10  use BackupPC::Attrib qw/:all/;
10  use Data::Dumper;  use Data::Dumper;
11  use Time::HiRes qw/time/;  use Time::HiRes qw/time/;
12  use POSIX qw/strftime/;  use POSIX qw/strftime/;
 use BackupPC::SearchLib;  
13  use Cwd qw/abs_path/;  use Cwd qw/abs_path/;
14  use File::Which;  use File::Which;
15  use Archive::Tar::Streamed;  use Archive::Tar::Streamed;
16  use Algorithm::Diff;  use Algorithm::Diff;
17  use Getopt::Std;  use Getopt::Std;
18    
19    my $bpc = BackupPC::Lib->new || die "can't create BackupPC::Lib";
20    my %Conf = $bpc->Conf();
21    
22    use BackupPC::SearchLib;
23    %BackupPC::SearchLib::Conf = %Conf;
24    
25    # cludge: minimum .tar.gz size
26    my $MIN_TAR_SIZE = 80;
27    
28  my $path = abs_path($0);  my $path = abs_path($0);
29  $path =~ s#/[^/]+$#/#;  $path =~ s#/[^/]+$#/#;
30  my $tarIncCreate = $path .= 'BackupPC_tarIncCreate';  my $tarIncCreate = $path .= 'BackupPC_tarIncCreate';
# Line 24  my $tarIncCreate = $path .= 'BackupPC_ta Line 32  my $tarIncCreate = $path .= 'BackupPC_ta
32  die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate);  die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate);
33    
34  my $bin;  my $bin;
35  foreach my $c (qw/gzip split/) {  foreach my $c (qw/gzip md5sum/) {
36          $bin->{$c} = which($c) || die "$0 needs $c, install it\n";          $bin->{$c} = which($c) || die "$0 needs $c, install it\n";
37  }  }
38    
# Line 40  my $start_t = time(); Line 48  my $start_t = time();
48    
49  my $t_fmt = '%Y-%m-%d %H:%M:%S';  my $t_fmt = '%Y-%m-%d %H:%M:%S';
50    
 my $hosts;  
 my $bpc = BackupPC::Lib->new || die;  
 my %Conf = $bpc->Conf();  
 my $TopDir = $bpc->TopDir();  
 my $beenThere = {};  
   
51  my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";  my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";
52  my $user = $Conf{SearchUser} || '';  my $user = $Conf{SearchUser} || '';
53    
# Line 70  sub curr_time { Line 72  sub curr_time {
72          return strftime($t_fmt,localtime());          return strftime($t_fmt,localtime());
73  }  }
74    
 sub tar_join($) {  
         my $filename = shift;  
   
         my $in = my $out = $filename;  
         $out .= '.tmp';  
   
         # FIXME I should really order parts manually!  
         system("cat $in/part* > $out && rm -Rf $in && mv $out $in") == 0 or die "can't join $in: $?";  
   
 }  
   
75  sub tar_check($$$$) {  sub tar_check($$$$) {
76          my ($host,$share,$num,$filename) = @_;          my ($host,$share,$num,$filename) = @_;
77    
# Line 90  sub tar_check($$$$) { Line 81  sub tar_check($$$$) {
81                  print " check";                  print " check";
82          }          }
83    
84          if (-d $filename) {          my @tar_parts;
                 print STDERR ", joining";  
                 tar_join($filename);  
         }  
85    
86          print STDERR ", opening" if ($debug);          if (-d "$tar_dir/$filename") {
87          open(my $fh, "gzip -cd $filename |") or die "can't open $filename: $!";                  print STDERR " multi-part" if ($opt{d});
88          binmode($fh);                  opendir(my $dir, "$tar_dir/$filename") || die "can't readdir $tar_dir/$filename: $!";
89          my $tar = Archive::Tar::Streamed->new($fh);                  @tar_parts = map { my $p = $_; $p =~ s#^#${filename}/#; $p } grep { !/^\./ && !/md5/ && -f "$tar_dir/$filename/$_" } readdir($dir);
90                    closedir($dir);
91            } else {
92                    push @tar_parts, "${filename}.tar.gz";
93            }
94    
         print STDERR ", tar" if ($debug);  
95          my @tar_files;          my @tar_files;
96          while(my $entry = $tar->next) {  
97                  push @tar_files, $entry->name;          print " [parts: ",join(", ", @tar_parts),"]" if ($opt{d});
98    
99            print " reading";
100    
101            foreach my $tarfilename (@tar_parts) {
102    
103                    print STDERR " $tarfilename" if ($debug);
104    
105                    my $path = "$tar_dir/$tarfilename";
106                    my $md5 = $path;
107                    $md5 =~ s/\.tar\.gz$/.md5/ || die "can't create md5 filename from $md5";
108                    if (! -e $md5) {
109                            print ", creating md5";
110                            system( $bin->{md5sum} . " $path > $md5") == 0 or die "can't create md5 $path: $!";
111                    }
112    
113                    open(my $fh, "gzip -cd $tar_dir/$tarfilename |") or die "can't open $tar_dir/$tarfilename: $!";
114                    binmode($fh);
115                    my $tar = Archive::Tar::Streamed->new($fh);
116    
117                    while(my $entry = $tar->next) {
118                            push @tar_files, $entry->name;
119                    }
120          }          }
121    
122          @tar_files = sort @tar_files;          @tar_files = sort @tar_files;
123          print STDERR " ",($#tar_files + 1), " files" if ($debug);          print STDERR " ",($#tar_files + 1), " files" if ($debug);
124    
# Line 172  order by backups.date Line 186  order by backups.date
186    
187  } );  } );
188    
 my $sth_inc_size = $dbh->prepare(qq{ update backups set inc_size = ?, parts = ? where id = ? });  
 my $sth_inc_deleted = $dbh->prepare(qq{ update backups set inc_deleted = ? where id = ? });  
   
 %BackupPC::SearchLib::Conf = %Conf;  
   
189  $sth->execute();  $sth->execute();
190  my $num_backups = $sth->rows;  my $num_backups = $sth->rows;
191  my $curr_backup = 1;  my $curr_backup = 1;
# Line 187  while (my $row = $sth->fetchrow_hashref) Line 196  while (my $row = $sth->fetchrow_hashref)
196          # this will return -1 if file doesn't exist          # this will return -1 if file doesn't exist
197          my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file);          my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file);
198    
199            print "# size: $size backup.size: ", $row->{inc_size},"\n" if ($opt{d});
200    
201            if ( $row->{'inc_size'} != -1 && $size != -1 && $row->{'inc_size'} == $size && ( $check && tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, $tar_file) || 1) ) {
202                    next;
203            }
204    
205          print curr_time, " $curr_backup/$num_backups ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file";          print curr_time, " $curr_backup/$num_backups ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file";
206          $curr_backup++;          $curr_backup++;
207    
208          my $t = time();          my $t = time();
209    
210          # re-create archive?          # re-create archive?
211          if ($row->{'inc_size'} == -1 || $size == -1 ||          my $cmd = qq{ $tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} -f };
212                  $row->{'inc_size'} != $size ||          print STDERR "## $cmd\n" if ($debug);
                 $check && ! tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, "$tar_dir/$tar_file")  
         ) {  
                 my $cmd = qq{rm -Rf $tar_dir/$tar_file && $tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} | $bin->{'gzip'} $Conf{GzipLevel} > ${tar_dir}/${tar_file}.tmp};  
                 print STDERR "## $cmd\n" if ($debug);  
   
                 system($cmd) == 0 or die "failed: $?";  
   
                 rename("${tar_dir}/${tar_file}.tmp", "$tar_dir/$tar_file") or die "can't rename $tar_dir/$tar_file: $!";  
213    
214                  $size = (stat( "$tar_dir/$tar_file" ))[7];          if (system($cmd) != 0) {
215                    print STDERR " FAILED";
216          }          }
217    
         if ($size > 45) {  
   
                 my $max_size = $Conf{'MaxArchiveSize'} || die "problem with MaxArchieSize parametar";  
                 $max_size *= 1024;      # convert to bytes  
   
                 my $parts = int( ($size + $max_size - 1) / $max_size );  
   
                 if (-d "$tar_dir/$tar_file" && $parts != $row->{'parts'}) {  
                         print " join";  
                         tar_join("$tar_dir/$tar_file");  
                 }  
   
                 if ($size > $max_size && ! -d "$tar_dir/$tar_file") {  
                         print " split/$parts";  
                         my $in = my $out = "$tar_dir/$tar_file";  
                         $out .= '.tmp';  
                         rename $in, $out || die "can't rename $in: $!";  
                         mkdir $in || die "can't mkdir $in: $!";  
   
                         my $suffix_len = length("$parts");  
                         system("$bin->{'split'} -d -b $max_size -a $suffix_len $out $in/part") == 0 or die "can't split $out: $?";  
                         unlink $out || die "can't unlink $out: $!";  
                 }  
   
                 $sth_inc_size->execute($size, $parts, $row->{'backup_id'});  
                 $sth_inc_deleted->execute(0, $row->{'backup_id'});  
   
                 printf(" %1.2f MB", ($size / 1024 / 1024));  
   
         } else {  
                 $sth_inc_deleted->execute(1, $row->{'backup_id'});  
                 unlink "$tar_dir/$tar_file" || die "can't delete $tar_dir/$tar_file: $!\n";  
                 print " EMPTY";  
         }  
218          print ", dur: ",fmt_time(time() - $t), "\n";          print ", dur: ",fmt_time(time() - $t), "\n";
219    
220          $dbh->commit;          $dbh->commit;

Legend:
Removed from v.216  
changed lines
  Added in v.254

  ViewVC Help
Powered by ViewVC 1.1.26