/[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 201 by dpavlin, Fri Oct 14 14:02:51 2005 UTC revision 256 by dpavlin, Mon Dec 12 20:59:53 2005 UTC
# Line 6  use lib "__INSTALLDIR__/lib"; Line 6  use lib "__INSTALLDIR__/lib";
6  use DBI;  use DBI;
7  use BackupPC::Lib;  use BackupPC::Lib;
8  use BackupPC::View;  use BackupPC::View;
9    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;
16    use Algorithm::Diff;
17    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  my $path = abs_path($0);  my $path = abs_path($0);
26  $path =~ s#/[^/]+$#/#;  $path =~ s#/[^/]+$#/#;
# Line 20  my $tarIncCreate = $path .= 'BackupPC_ta Line 29  my $tarIncCreate = $path .= 'BackupPC_ta
29  die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate);  die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate);
30    
31  my $bin;  my $bin;
32  foreach my $c (qw/gzip split/) {  foreach my $c (qw/gzip md5sum/) {
33          $bin->{$c} = which($c) || die "$0 needs $c, install it\n";          $bin->{$c} = which($c) || die "$0 needs $c, install it\n";
34  }  }
35    
36    my %opt;
37    getopts("cd", \%opt );
38    
39    my $debug = $opt{d};
40    my $check = $opt{c} && print STDERR "NOTICE: tar archive check forced\n";
41    
 my $debug = 0;  
42  $|=1;  $|=1;
43    
44  my $start_t = time();  my $start_t = time();
45    
46  my $t_fmt = '%Y-%m-%d %H:%M:%S';  my $t_fmt = '%Y-%m-%d %H:%M:%S';
47    
 my $hosts;  
 my $bpc = BackupPC::Lib->new || die;  
 my %Conf = $bpc->Conf();  
 my $TopDir = $bpc->TopDir();  
 my $beenThere = {};  
   
48  my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";  my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";
49  my $user = $Conf{SearchUser} || '';  my $user = $Conf{SearchUser} || '';
50    
# Line 62  sub curr_time { Line 69  sub curr_time {
69          return strftime($t_fmt,localtime());          return strftime($t_fmt,localtime());
70  }  }
71    
72    sub tar_check($$$$) {
73            my ($host,$share,$num,$filename) = @_;
74    
75            if ($debug) {
76                    print STDERR " {{ CHECK: ${host}:${share}#${num} and $filename";
77            } else {
78                    print " check";
79            }
80    
81            my @tar_parts;
82    
83            if (-d "$tar_dir/$filename") {
84                    print STDERR " multi-part" if ($opt{d});
85                    opendir(my $dir, "$tar_dir/$filename") || die "can't readdir $tar_dir/$filename: $!";
86                    @tar_parts = map { my $p = $_; $p =~ s#^#${filename}/#; $p } grep { !/^\./ && !/md5/ && -f "$tar_dir/$filename/$_" } readdir($dir);
87                    closedir($dir);
88            } else {
89                    push @tar_parts, "${filename}.tar.gz";
90            }
91    
92            print " [parts: ",join(", ", @tar_parts),"]" if ($opt{d});
93    
94            my $same = 1;
95            my @tar_files;
96    
97            print " reading";
98    
99            foreach my $tarfilename (@tar_parts) {
100    
101                    print STDERR " $tarfilename" if ($debug);
102    
103                    my $path = "$tar_dir/$tarfilename";
104                    my $md5 = $path;
105                    $md5 =~ s/\.tar\.gz$/.md5/ || die "can't create md5 filename from $md5";
106                    if (! -e $md5) {
107                            print ", creating md5";
108                            system( $bin->{md5sum} . " $path > $md5") == 0 or die "can't create md5 $path: $!";
109                    }
110    
111                    open(my $fh, "gzip -cd $tar_dir/$tarfilename |") or die "can't open $tar_dir/$tarfilename: $!";
112                    binmode($fh);
113                    my $tar = Archive::Tar::Streamed->new($fh);
114    
115                    my $total_size = 0;
116    
117                    while(my $entry = $tar->next) {
118                            push @tar_files, $entry->name;
119                            $total_size += $entry->size;
120                    }
121    
122                    if ($total_size > $Conf{MaxArchiveFileSize}) {
123                            print STDERR " part too big $total_size > $Conf{MaxArchiveFileSize} }}" if ($debug);
124                            $same = 0;
125                            last;
126                    } elsif ($total_size > $Conf{MaxArchiveSize}) {
127                            print STDERR " part bigger than media $total_size > $Conf{MaxArchiveSize} }}" if ($debug);
128                            $same = 0;
129                            last;
130                    }
131            }
132    
133            # short-cut and exit;
134            return $same unless($same);
135    
136            @tar_files = sort @tar_files;
137            print STDERR " ",($#tar_files + 1), " files" if ($debug);
138    
139            print STDERR ", database" if ($debug);
140    
141            my $sth = $dbh->prepare(qq{
142                    SELECT path,type
143                    FROM files
144                    JOIN shares on shares.id = shareid
145                    JOIN hosts on hosts.id = shares.hostid
146                    WHERE hosts.name = ? and shares.name = ? and backupnum = ?
147            });
148            $sth->execute($host, $share, $num);
149            my @db_files;
150            while( my $row = $sth->fetchrow_hashref ) {
151    
152                    my $path = $row->{'path'} || die "no path?";
153                    $path =~ s#^/#./#;
154                    $path .= '/' if ($row->{'type'} == BPC_FTYPE_DIR);
155                    push @db_files, $path;
156            }
157    
158            print STDERR " ",($#db_files + 1), " files, diff" if ($debug);
159    
160            @db_files = sort @db_files;
161    
162            if ($#tar_files != $#db_files) {
163                    $same = 0;
164                    print STDERR " NUMBER" if ($debug);
165            } else {
166                    my $diff = Algorithm::Diff->new(\@tar_files, \@db_files);
167                    while ( $diff->Next() ) {
168                            next if $diff->Same();
169                            $same = 0;
170                            print "< $_\n" for $diff->Items(1);
171                            print "> $_\n" for $diff->Items(2);
172                    }
173            }
174    
175            print " ",($same ? 'ok' : 'DIFFERENT');
176            print STDERR " }} " if ($debug);
177    
178            return $same;
179    }
180    
181    
182  #----- main  #----- main
183    
184  my $sth = $dbh->prepare( qq{  my $sth = $dbh->prepare( qq{
# Line 83  order by backups.date Line 200  order by backups.date
200  } );  } );
201    
202  $sth->execute();  $sth->execute();
203    my $num_backups = $sth->rows;
204  my $sth_inc_size = $dbh->prepare(qq{ update backups set inc_size = ?, parts = ? where id = ? });  my $curr_backup = 1;
 my $sth_inc_deleted = $dbh->prepare(qq{ update backups set inc_deleted = ? where id = ? });  
   
 %BackupPC::SearchLib::Conf = %Conf;  
205    
206  while (my $row = $sth->fetchrow_hashref) {  while (my $row = $sth->fetchrow_hashref) {
207          my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});          my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
# Line 95  while (my $row = $sth->fetchrow_hashref) Line 209  while (my $row = $sth->fetchrow_hashref)
209          # this will return -1 if file doesn't exist          # this will return -1 if file doesn't exist
210          my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file);          my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file);
211    
212          print curr_time, " ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file";          print "# size: $size backup.size: ", $row->{inc_size},"\n" if ($opt{d});
   
         my $t = time();  
   
         # re-create archive?  
         if ($row->{'inc_size'} == -1 || $size == -1 || $row->{'inc_size'} != $size) {  
                 my $cmd = qq{rm -Rf $tar_dir/$tar_file && $tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} | $bin->{'gzip'} -9 > $tar_dir/$tar_file};  
                 print STDERR "## $cmd\n" if ($debug);  
   
                 system($cmd) == 0 or die "failed: $?";  
           
                 $size = (stat( "$tar_dir/$tar_file" ))[7];  
         }  
   
         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 );  
213    
214                  if (-d "$tar_dir/$tar_file" && $parts != $row->{'parts'}) {          if ( $row->{'inc_size'} != -1 && $size != -1 && $row->{'inc_size'} == $size) {
215                          print " join";                  if ($check) {
216                            tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, $tar_file) && next;
217                          my $in = my $out = "$tar_dir/$tar_file";                  } else {
218                          $out .= '.tmp';                          next;
   
                         # 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: $?";  
219                  }                  }
220            }
221    
222                  if ($size > $max_size && ! -d "$tar_dir/$tar_file") {          print curr_time, " $curr_backup/$num_backups ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file";
223                          print " split/$parts";          $curr_backup++;
                         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: $!";  
                 }  
224    
225                  $sth_inc_size->execute($size, $parts, $row->{'backup_id'});          my $t = time();
                 $sth_inc_deleted->execute(0, $row->{'backup_id'});  
226    
227                  printf(" %1.2f MB", ($size / 1024 / 1024));          # re-create archive?
228            my $cmd = qq{ $tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} -f };
229            print STDERR "## $cmd\n" if ($debug);
230    
231          } else {          if (system($cmd) != 0) {
232                  $sth_inc_deleted->execute(1, $row->{'backup_id'});                  print STDERR " FAILED";
                 unlink "$tar_dir/$tar_file" || die "can't delete $tar_dir/$tar_file: $!\n";  
                 print " EMPTY";  
233          }          }
234    
235          print ", dur: ",fmt_time(time() - $t), "\n";          print ", dur: ",fmt_time(time() - $t), "\n";
236    
237          $dbh->commit;          $dbh->commit;

Legend:
Removed from v.201  
changed lines
  Added in v.256

  ViewVC Help
Powered by ViewVC 1.1.26