/[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 218 by dpavlin, Sun Oct 16 17:48:05 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/;
13  use BackupPC::SearchLib;  use BackupPC::SearchLib;
14  use Cwd qw/abs_path/;  use Cwd qw/abs_path/;
15  use File::Which;  use File::Which;
16    use Archive::Tar::Streamed;
17    use Algorithm::Diff;
18    use Getopt::Std;
19    
20    # cludge: minimum .tar.gz size
21    my $MIN_TAR_SIZE = 80;
22    
23  my $path = abs_path($0);  my $path = abs_path($0);
24  $path =~ s#/[^/]+$#/#;  $path =~ s#/[^/]+$#/#;
# Line 24  foreach my $c (qw/gzip split/) { Line 31  foreach my $c (qw/gzip split/) {
31          $bin->{$c} = which($c) || die "$0 needs $c, install it\n";          $bin->{$c} = which($c) || die "$0 needs $c, install it\n";
32  }  }
33    
34    my %opt;
35    getopts("cd", \%opt );
36    
37    my $debug = $opt{d};
38    my $check = $opt{c} && print STDERR "NOTICE: tar archive check forced\n";
39    
 my $debug = 0;  
40  $|=1;  $|=1;
41    
42  my $start_t = time();  my $start_t = time();
# Line 62  sub curr_time { Line 73  sub curr_time {
73          return strftime($t_fmt,localtime());          return strftime($t_fmt,localtime());
74  }  }
75    
76    sub tar_join($) {
77            my $filename = shift;
78    
79            my $in = my $out = $filename;
80            $out .= '.tmp';
81    
82            # FIXME I should really order parts manually!
83            system("cat $in/part* > $out && rm -Rf $in && mv $out $in") == 0 or die "can't join $in: $?";
84    
85    }
86    
87    sub tar_check($$$$) {
88            my ($host,$share,$num,$filename) = @_;
89    
90            if ($debug) {
91                    print STDERR " {{ CHECK: ${host}:${share}#${num} and $filename";
92            } else {
93                    print " check";
94            }
95    
96            if (-d $filename) {
97                    print STDERR ", joining";
98                    tar_join($filename);
99            }
100    
101            print STDERR ", opening" if ($debug);
102            open(my $fh, "gzip -cd $filename |") or die "can't open $filename: $!";
103            binmode($fh);
104            my $tar = Archive::Tar::Streamed->new($fh);
105    
106            print STDERR ", tar" if ($debug);
107            my @tar_files;
108            while(my $entry = $tar->next) {
109                    push @tar_files, $entry->name;
110            }
111            @tar_files = sort @tar_files;
112            print STDERR " ",($#tar_files + 1), " files" if ($debug);
113    
114            print STDERR ", database" if ($debug);
115    
116            my $sth = $dbh->prepare(qq{
117                    SELECT path,type
118                    FROM files
119                    JOIN shares on shares.id = shareid
120                    JOIN hosts on hosts.id = shares.hostid
121                    WHERE hosts.name = ? and shares.name = ? and backupnum = ?
122            });
123            $sth->execute($host, $share, $num);
124            my @db_files;
125            while( my $row = $sth->fetchrow_hashref ) {
126    
127                    my $path = $row->{'path'} || die "no path?";
128                    $path =~ s#^/#./#;
129                    $path .= '/' if ($row->{'type'} == BPC_FTYPE_DIR);
130                    push @db_files, $path;
131            }
132    
133            print STDERR " ",($#db_files + 1), " files, diff" if ($debug);
134    
135            @db_files = sort @db_files;
136    
137            my $same = 1;
138            if ($#tar_files != $#db_files) {
139                    $same = 0;
140                    print STDERR " NUMBER" if ($debug);
141            } else {
142                    my $diff = Algorithm::Diff->new(\@tar_files, \@db_files);
143                    while ( $diff->Next() ) {
144                            next if $diff->Same();
145                            $same = 0;
146                            print "< $_\n" for $diff->Items(1);
147                            print "> $_\n" for $diff->Items(2);
148                    }
149            }
150    
151            print " ",($same ? 'ok' : 'DIFFERENT');
152            print STDERR " }} " if ($debug);
153    
154            return $same;
155    }
156    
157    
158  #----- main  #----- main
159    
160  my $sth = $dbh->prepare( qq{  my $sth = $dbh->prepare( qq{
# Line 82  order by backups.date Line 175  order by backups.date
175    
176  } );  } );
177    
 $sth->execute();  
   
178  my $sth_inc_size = $dbh->prepare(qq{ update backups set inc_size = ?, parts = ? where id = ? });  my $sth_inc_size = $dbh->prepare(qq{ update backups set inc_size = ?, parts = ? where id = ? });
179  my $sth_inc_deleted = $dbh->prepare(qq{ update backups set inc_deleted = ? where id = ? });  my $sth_inc_deleted = $dbh->prepare(qq{ update backups set inc_deleted = ? where id = ? });
180    
181  %BackupPC::SearchLib::Conf = %Conf;  %BackupPC::SearchLib::Conf = %Conf;
182    
183    $sth->execute();
184    my $num_backups = $sth->rows;
185    my $curr_backup = 1;
186    
187  while (my $row = $sth->fetchrow_hashref) {  while (my $row = $sth->fetchrow_hashref) {
188          my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});          my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
189    
190          # this will return -1 if file doesn't exist          # this will return -1 if file doesn't exist
191          my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file);          my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file);
192    
193          print curr_time, " ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file";          print curr_time, " $curr_backup/$num_backups ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file";
194            $curr_backup++;
195    
196          my $t = time();          my $t = time();
197    
198          # re-create archive?          # re-create archive?
199          if ($row->{'inc_size'} == -1 || $size == -1 || $row->{'inc_size'} != $size) {          if ($row->{'inc_size'} == -1 || $size == -1 ||
200                  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};                  $row->{'inc_size'} != $size ||
201                    $check && ! tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, "$tar_dir/$tar_file")
202            ) {
203                    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};
204                  print STDERR "## $cmd\n" if ($debug);                  print STDERR "## $cmd\n" if ($debug);
205    
206                  system($cmd) == 0 or die "failed: $?";                  system($cmd) == 0 or die "failed: $?";
207            
208                    rename("${tar_dir}/${tar_file}.tmp", "$tar_dir/$tar_file") or die "can't rename $tar_dir/$tar_file: $!";
209    
210                  $size = (stat( "$tar_dir/$tar_file" ))[7];                  $size = (stat( "$tar_dir/$tar_file" ))[7];
211          }          }
212    
213          if ($size > 45) {          if ($size > $MIN_TAR_SIZE) {
214    
215                  my $max_size = $Conf{'MaxArchiveSize'} || die "problem with MaxArchieSize parametar";                  my $max_size = $Conf{'MaxArchiveSize'} || die "problem with MaxArchieSize parametar";
216                  $max_size *= 1024;      # convert to bytes                  $max_size *= 1024;      # convert to bytes
# Line 118  while (my $row = $sth->fetchrow_hashref) Line 219  while (my $row = $sth->fetchrow_hashref)
219    
220                  if (-d "$tar_dir/$tar_file" && $parts != $row->{'parts'}) {                  if (-d "$tar_dir/$tar_file" && $parts != $row->{'parts'}) {
221                          print " join";                          print " join";
222                            tar_join("$tar_dir/$tar_file");
                         my $in = my $out = "$tar_dir/$tar_file";  
                         $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: $?";  
223                  }                  }
224    
225                  if ($size > $max_size && ! -d "$tar_dir/$tar_file") {                  if ($size > $max_size && ! -d "$tar_dir/$tar_file") {

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

  ViewVC Help
Powered by ViewVC 1.1.26