/[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 280 by dpavlin, Wed Dec 14 10:40:10 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          if ($debug) {          my $t = time();
104                  print STDERR " {{ CHECK: ${host}:${share}#${num} and $filename";          print curr_time, " check $host:$share#$num -> $filename";
105          } else {  
106                  print " check";          sub check_part {
107                    my ($host, $share, $num, $part_nr, $tar_size, $size, $md5, $items) = @_;
108                    my $backup_id = get_backup_id($host, $share, $num);
109                    my $sth_md5 = $dbh->prepare(qq{
110                            select
111                                    id, tar_size, size, md5, items
112                            from backup_parts
113                            where backup_id = ? and part_nr = ?
114                    });
115    
116                    $sth_md5->execute($backup_id, $part_nr);
117    
118                    if (my $row = $sth_md5->fetchrow_hashref) {
119                            return if (
120                                    $row->{tar_size} >= $tar_size &&
121                                    $row->{size} == $size &&
122                                    $row->{md5} eq $md5 &&
123                                    $row->{items} == $items
124                            );
125                            print ", deleting invalid backup_parts $row->{id}";
126                            $dbh->do(qq{ delete from backup_parts where id = $row->{id} });
127                    }
128                    print ", inserting new";
129                    my $sth_insert = $dbh->prepare(qq{
130                            insert into backup_parts (
131                                    backup_id,
132                                    part_nr,
133                                    tar_size,
134                                    size,
135                                    md5,
136                                    items
137                            ) values (?,?,?,?,?,?)
138                    });
139    
140                    $sth_insert->execute($backup_id, $part_nr, $tar_size, $size, $md5, $items);
141                    $dbh->commit;
142          }          }
143    
144          my @tar_parts;          my @tar_parts;
145    
146          if (-d "$tar_dir/$filename") {          if (-d "$tar_dir/$filename") {
147                  print STDERR " multi-part" if ($opt{d});                  print ", multi-part";
148                  opendir(my $dir, "$tar_dir/$filename") || die "can't readdir $tar_dir/$filename: $!";                  opendir(my $dir, "$tar_dir/$filename") || die "can't readdir $tar_dir/$filename: $!";
149                  @tar_parts = map { my $p = $_; $p =~ s#^#${filename}/#; $p } grep { !/^\./ && !/md5/ && -f "$tar_dir/$filename/$_" } readdir($dir);                  @tar_parts = map { my $p = $_; $p =~ s#^#${filename}/#; $p } grep { !/^\./ && !/md5/ && -f "$tar_dir/$filename/$_" } readdir($dir);
150                  closedir($dir);                  closedir($dir);
# Line 92  sub tar_check($$$$) { Line 152  sub tar_check($$$$) {
152                  push @tar_parts, "${filename}.tar.gz";                  push @tar_parts, "${filename}.tar.gz";
153          }          }
154    
155            print " [parts: ",join(", ", @tar_parts),"]" if ($opt{d});
156    
157            my $same = 1;
158          my @tar_files;          my @tar_files;
159    
160          print " [parts: ",join(", ", @tar_parts),"]" if ($opt{d});          my $backup_part;
161    
162          print " reading";          print " reading" if ($opt{d});
163    
164          foreach my $tarfilename (@tar_parts) {          foreach my $tarfilename (@tar_parts) {
165    
166                  print STDERR " $tarfilename" if ($debug);                  print "\n\t- $tarfilename";
167    
168                  my $path = "$tar_dir/$tarfilename";                  my $size = (stat( "$tar_dir/$tarfilename" ))[7] || die "can't stat $tar_dir/$tarfilename";
169                  my $md5 = $path;  
170                  $md5 =~ s/\.tar\.gz$/.md5/ || die "can't create md5 filename from $md5";                  if ($size > $Conf{MaxArchiveSize}) {
171                  if (! -e $md5) {                          print ", part bigger than media $size > $Conf{MaxArchiveSize}\n";
172                          print ", creating md5";                          return 0;
                         system( $bin->{md5sum} . " $path > $md5") == 0 or die "can't create md5 $path: $!";  
173                  }                  }
174    
175                    print ", $size bytes";
176    
177                    my $path = "$tar_dir/$tarfilename";
178    
179                  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: $!";
180                  binmode($fh);                  binmode($fh);
181                  my $tar = Archive::Tar::Streamed->new($fh);                  my $tar = Archive::Tar::Streamed->new($fh);
182    
183                    my $tar_size = 0;
184                    my $items = 0;
185    
186                  while(my $entry = $tar->next) {                  while(my $entry = $tar->next) {
187                          push @tar_files, $entry->name;                          push @tar_files, $entry->name;
188                            $items++;
189                            $tar_size += $entry->size;
190    
191                            if ($tar_size > $Conf{MaxArchiveFileSize}) {
192                                    print ", part $tarfilename is too big $tar_size > $Conf{MaxArchiveFileSize}\n";
193                                    return 0;
194                            }
195    
196                    }
197    
198                    print ", $items items";
199    
200                    if ($tar_size == 0 && $items == 0) {
201                            print ", EMPTY tar\n";
202    
203                            my $backup_id = get_backup_id($host, $share, $num);
204    
205                            my $sth_inc_deleted = $dbh->prepare(qq{
206                                    update backups set
207                                            inc_deleted = true
208                                    where id = ?
209                            });
210                            $sth_inc_deleted->execute($backup_id);
211    
212                            $dbh->commit;
213    
214                            return 1;
215                  }                  }
216    
217                    # fix tar_size for tars without any files
218                    $tar_size ||= 512 * $items;
219    
220    
221                    #
222                    # check if md5 exists, and if not, create one
223                    #
224    
225                    my $md5_path = $path;
226                    $md5_path =~ s/\.tar\.gz$/.md5/ || die "can't create md5 filename from $md5_path";
227                    if (! -e $md5_path || -z $md5_path) {
228                            print ", creating md5";
229                            system( $bin->{md5sum} . " $path > $md5_path") == 0 or die "can't create md5 $path: $!";
230                    } else {
231                            ## FIXME check if existing md5 is valid
232                    }
233    
234                    my $md5 = read_file( $md5_path ) || die "can't read md5sum file $md5_path: $!";
235                    $md5 =~ s#\s.*$##;
236    
237                    # extract part number from filename
238                    my $part_nr = 1;
239                    $part_nr = $1 if ($tarfilename =~ m#/(\d+)\.tar\.gz#);
240    
241                    #
242                    # finally, check if backup_parts table in database is valid
243                    #
244    
245                    check_part($host, $share, $num, $part_nr, $tar_size, $size, $md5, $items);
246          }          }
247    
248          @tar_files = sort @tar_files;          # short-cut and exit;
249          print STDERR " ",($#tar_files + 1), " files" if ($debug);          return $same unless($same);
250    
251          print STDERR ", database" if ($debug);          @tar_files = sort @tar_files;
252            print "\n\t",($#tar_files + 1), " tar files";
253    
254          my $sth = $dbh->prepare(qq{          my $sth = $dbh->prepare(qq{
255                  SELECT path,type                  SELECT path,type
# Line 141  sub tar_check($$$$) { Line 268  sub tar_check($$$$) {
268                  push @db_files, $path;                  push @db_files, $path;
269          }          }
270    
271          print STDERR " ",($#db_files + 1), " files, diff" if ($debug);          print " ",($#db_files + 1), " database files, diff";
272    
273          @db_files = sort @db_files;          @db_files = sort @db_files;
274    
         my $same = 1;  
275          if ($#tar_files != $#db_files) {          if ($#tar_files != $#db_files) {
276                  $same = 0;                  $same = 0;
277                  print STDERR " NUMBER" if ($debug);                  print " NUMBER";
278          } else {          } else {
279                  my $diff = Algorithm::Diff->new(\@tar_files, \@db_files);                  my $diff = Algorithm::Diff->new(\@tar_files, \@db_files);
280                  while ( $diff->Next() ) {                  while ( $diff->Next() ) {
# Line 159  sub tar_check($$$$) { Line 285  sub tar_check($$$$) {
285                  }                  }
286          }          }
287    
288          print " ",($same ? 'ok' : 'DIFFERENT');          print " ",($same ? 'ok' : 'DIFFERENT'),
289          print STDERR " }} " if ($debug);                  ", dur: ",fmt_time(time() - $t), "\n";
290    
291          return $same;          return $same;
292  }  }
# Line 191  my $num_backups = $sth->rows; Line 317  my $num_backups = $sth->rows;
317  my $curr_backup = 1;  my $curr_backup = 1;
318    
319  while (my $row = $sth->fetchrow_hashref) {  while (my $row = $sth->fetchrow_hashref) {
320    
321            $curr_backup++;
322    
323          my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});          my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
324    
325          # this will return -1 if file doesn't exist          # this will return -1 if file doesn't exist
# Line 198  while (my $row = $sth->fetchrow_hashref) Line 327  while (my $row = $sth->fetchrow_hashref)
327    
328          print "# size: $size backup.size: ", $row->{inc_size},"\n" if ($opt{d});          print "# size: $size backup.size: ", $row->{inc_size},"\n" if ($opt{d});
329    
330          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) {
331                  next;                  if ($check) {
332                            tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, $tar_file) && next;
333                    } else {
334                            next;
335                    }
336          }          }
337    
338          print curr_time, " $curr_backup/$num_backups ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file";          print curr_time, " creating $curr_backup/$num_backups ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file";
         $curr_backup++;  
339    
340          my $t = time();          my $t = time();
341    

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

  ViewVC Help
Powered by ViewVC 1.1.26