/[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 254 by dpavlin, Mon Dec 12 16:07:27 2005 UTC revision 265 by dpavlin, Tue Dec 13 00:10:48 2005 UTC
# Line 15  use File::Which; Line 15  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    use File::Slurp;
19    
20  my $bpc = BackupPC::Lib->new || die "can't create BackupPC::Lib";  my $bpc = BackupPC::Lib->new || die "can't create BackupPC::Lib";
21  my %Conf = $bpc->Conf();  my %Conf = $bpc->Conf();
# Line 22  my %Conf = $bpc->Conf(); Line 23  my %Conf = $bpc->Conf();
23  use BackupPC::SearchLib;  use BackupPC::SearchLib;
24  %BackupPC::SearchLib::Conf = %Conf;  %BackupPC::SearchLib::Conf = %Conf;
25    
 # cludge: minimum .tar.gz size  
 my $MIN_TAR_SIZE = 80;  
   
26  my $path = abs_path($0);  my $path = abs_path($0);
27  $path =~ s#/[^/]+$#/#;  $path =~ s#/[^/]+$#/#;
28  my $tarIncCreate = $path .= 'BackupPC_tarIncCreate';  my $tarIncCreate = $path .= 'BackupPC_tarIncCreate';
# Line 72  sub curr_time { Line 70  sub curr_time {
70          return strftime($t_fmt,localtime());          return strftime($t_fmt,localtime());
71  }  }
72    
73    my $hsn_cache;
74    
75    sub get_backup_id($$$) {
76            my ($host, $share, $num) = @_;
77    
78            my $key = "$host $share $num";
79            return $hsn_cache->{$key} if ($hsn_cache->{$key});
80    
81            my $sth = $dbh->prepare(qq{
82                    SELECT
83                            backups.id
84                    FROM backups
85                    INNER JOIN shares       ON backups.shareID=shares.ID
86                    INNER JOIN hosts        ON backups.hostID = hosts.ID
87                    where hosts.name = ? and shares.name = ? and backups.num = ?
88            });
89            $sth->execute($host, $share, $num);
90            my ($id) = $sth->fetchrow_array;
91    
92            $hsn_cache->{"$host $share $num"} = $id;
93    
94            print STDERR "# $host $share $num == $id\n" if ($opt{d});
95    
96            return $id;
97    }
98    
99    
100  sub tar_check($$$$) {  sub tar_check($$$$) {
101          my ($host,$share,$num,$filename) = @_;          my ($host,$share,$num,$filename) = @_;
102    
103            sub check_part {
104                    my ($host, $share, $num, $part_nr, $tar_size, $size, $md5, $items) = @_;
105                    my $backup_id = get_backup_id($host, $share, $num);
106                    my $sth_md5 = $dbh->prepare(qq{
107                            select
108                                    id, tar_size, size, md5, items
109                            from backup_parts
110                            where backup_id = ? and part_nr = ?
111                    });
112    
113                    $sth_md5->execute($backup_id, $part_nr);
114    
115                    if (my $row = $sth_md5->fetchrow_hashref) {
116                            return if (
117                                    $row->{tar_size} >= $tar_size &&
118                                    $row->{size} == $size &&
119                                    $row->{md5} eq $md5 &&
120                                    $row->{items} == $items
121                            );
122                            print STDERR "# deleting invalid row $row->{id}\n" if ($opt{d});
123                            $dbh->do(qq{ delete from backup_parts where id = $row->{id} });
124                    }
125                    print STDERR "# inserting new backup_part row\n";
126                    my $sth_insert = $dbh->prepare(qq{
127                            insert into backup_parts (
128                                    backup_id,
129                                    part_nr,
130                                    tar_size,
131                                    size,
132                                    md5,
133                                    items
134                            ) values (?,?,?,?,?,?)
135                    });
136    
137                    $sth_insert->execute($backup_id, $part_nr, $tar_size, $size, $md5, $items);
138                    $dbh->commit;
139            }
140    
141          if ($debug) {          if ($debug) {
142                  print STDERR " {{ CHECK: ${host}:${share}#${num} and $filename";                  print STDERR " {{ CHECK: ${host}:${share}#${num} and $filename";
143          } else {          } else {
# Line 92  sub tar_check($$$$) { Line 155  sub tar_check($$$$) {
155                  push @tar_parts, "${filename}.tar.gz";                  push @tar_parts, "${filename}.tar.gz";
156          }          }
157    
158            print " [parts: ",join(", ", @tar_parts),"]" if ($opt{d});
159    
160            my $same = 1;
161          my @tar_files;          my @tar_files;
162    
163          print " [parts: ",join(", ", @tar_parts),"]" if ($opt{d});          my $backup_part;
164    
165          print " reading";          print " reading";
166    
# Line 103  sub tar_check($$$$) { Line 169  sub tar_check($$$$) {
169                  print STDERR " $tarfilename" if ($debug);                  print STDERR " $tarfilename" if ($debug);
170    
171                  my $path = "$tar_dir/$tarfilename";                  my $path = "$tar_dir/$tarfilename";
172                  my $md5 = $path;                  my $md5_path = $path;
173                  $md5 =~ s/\.tar\.gz$/.md5/ || die "can't create md5 filename from $md5";                  $md5_path =~ s/\.tar\.gz$/.md5/ || die "can't create md5 filename from $md5_path";
174                  if (! -e $md5) {                  if (! -e $md5_path) {
175                          print ", creating md5";                          print ", creating md5";
176                          system( $bin->{md5sum} . " $path > $md5") == 0 or die "can't create md5 $path: $!";                          system( $bin->{md5sum} . " $path > $md5_path") == 0 or die "can't create md5 $path: $!";
177                  }                  }
178    
179                    my $md5 = read_file( $md5_path ) || die "can't read md5sum file $md5_path: $!";
180                    $md5 =~ s#\s.*$##;
181    
182                    my $part_nr = 1;
183                    $part_nr = $1 if ($tarfilename =~ m#/(\d+)\.tar\.gz#);
184    
185                    my $size = (stat( "$tar_dir/$tarfilename" ))[7] || die "can't stat $tar_dir/$tarfilename";
186    
187                  open(my $fh, "gzip -cd $tar_dir/$tarfilename |") or die "can't open $tar_dir/$tarfilename: $!";                  open(my $fh, "gzip -cd $tar_dir/$tarfilename |") or die "can't open $tar_dir/$tarfilename: $!";
188                  binmode($fh);                  binmode($fh);
189                  my $tar = Archive::Tar::Streamed->new($fh);                  my $tar = Archive::Tar::Streamed->new($fh);
190    
191                    my $tar_size = 0;
192                    my $items = 0;
193    
194                  while(my $entry = $tar->next) {                  while(my $entry = $tar->next) {
195                          push @tar_files, $entry->name;                          push @tar_files, $entry->name;
196                            $items++;
197                            $tar_size += $entry->size;
198                  }                  }
199    
200                    if ($tar_size > $Conf{MaxArchiveFileSize}) {
201                            print STDERR " part too big $tar_size > $Conf{MaxArchiveFileSize} }}" if ($debug);
202                            $same = 0;
203                            last;
204                    } elsif ($size > $Conf{MaxArchiveSize}) {
205                            print STDERR " part bigger than media $size > $Conf{MaxArchiveSize} }}" if ($debug);
206                            $same = 0;
207                            last;
208                    }
209    
210                    check_part($host, $share, $num, $part_nr, $tar_size, $size, $md5, $items);
211          }          }
212    
213            # short-cut and exit;
214            return $same unless($same);
215    
216          @tar_files = sort @tar_files;          @tar_files = sort @tar_files;
217          print STDERR " ",($#tar_files + 1), " files" if ($debug);          print STDERR " ",($#tar_files + 1), " files" if ($debug);
218    
# Line 145  sub tar_check($$$$) { Line 239  sub tar_check($$$$) {
239    
240          @db_files = sort @db_files;          @db_files = sort @db_files;
241    
         my $same = 1;  
242          if ($#tar_files != $#db_files) {          if ($#tar_files != $#db_files) {
243                  $same = 0;                  $same = 0;
244                  print STDERR " NUMBER" if ($debug);                  print STDERR " NUMBER" if ($debug);
# Line 198  while (my $row = $sth->fetchrow_hashref) Line 291  while (my $row = $sth->fetchrow_hashref)
291    
292          print "# size: $size backup.size: ", $row->{inc_size},"\n" if ($opt{d});          print "# size: $size backup.size: ", $row->{inc_size},"\n" if ($opt{d});
293    
294          if ( $row->{'inc_size'} != -1 && $size != -1 && $row->{'inc_size'} == $size && ( $check && tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, $tar_file) || 1) ) {          if ( $row->{'inc_size'} != -1 && $size != -1 && $row->{'inc_size'} == $size) {
295                  next;                  if ($check) {
296                            tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, $tar_file) && next;
297                    } else {
298                            next;
299                    }
300          }          }
301    
302          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";

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

  ViewVC Help
Powered by ViewVC 1.1.26