/[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 182 by dpavlin, Wed Oct 12 11:15:18 2005 UTC revision 214 by dpavlin, Sun Oct 16 12:33: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;
16    use Archive::Tar;
17    use Algorithm::Diff;
18    use Getopt::Std;
19    
20  my $path = abs_path($0);  my $path = abs_path($0);
21  $path =~ s#/[^/]+$#/#;  $path =~ s#/[^/]+$#/#;
# Line 18  my $tarIncCreate = $path .= 'BackupPC_ta Line 23  my $tarIncCreate = $path .= 'BackupPC_ta
23    
24  die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate);  die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate);
25    
26  my $debug = 0;  my $bin;
27    foreach my $c (qw/gzip split/) {
28            $bin->{$c} = which($c) || die "$0 needs $c, install it\n";
29    }
30    
31    my %opt;
32    getopts("cd", \%opt );
33    
34    my $debug = $opt{d};
35    my $check = $opt{c} && print STDERR "NOTICE: tar archive check forced\n";
36    
37  $|=1;  $|=1;
38    
39  my $start_t = time();  my $start_t = time();
# Line 55  sub curr_time { Line 70  sub curr_time {
70          return strftime($t_fmt,localtime());          return strftime($t_fmt,localtime());
71  }  }
72    
73    sub tar_join($) {
74            my $filename = shift;
75    
76            my $in = my $out = $filename;
77            $out .= '.tmp';
78    
79            # FIXME I should really order parts manually!
80            system("cat $in/part* > $out && rm -Rf $in && mv $out $in") == 0 or die "can't join $in: $?";
81    
82    }
83    
84    sub tar_check($$$$) {
85            my ($host,$share,$num,$filename) = @_;
86    
87            if ($debug) {
88                    print STDERR " {{ CHECK: ${host}:${share}#${num} and $filename";
89            } else {
90                    print " check";
91            }
92    
93            if (-d $filename) {
94                    print STDERR ", joining";
95                    tar_join($filename);
96            }
97    
98            my $tar = Archive::Tar->new;
99            my $comp = 0;
100            $comp = 1 if ($filename =~ m/\.(gz|tgz)$/);
101            print STDERR ", opening" if ($debug);
102            $tar->read($filename, $comp) or die "can't open $filename: $!";
103    
104            print STDERR ", tar" if ($debug);
105            my @tar_files = sort $tar->list_files();
106            print STDERR " ",($#tar_files + 1), " files" if ($debug);
107    
108            print STDERR ", database" if ($debug);
109    
110            my $sth = $dbh->prepare(qq{
111                    SELECT path,type
112                    FROM files
113                    JOIN shares on shares.id = shareid
114                    JOIN hosts on hosts.id = shares.hostid
115                    WHERE hosts.name = ? and shares.name = ? and backupnum = ?
116            });
117            $sth->execute($host, $share, $num);
118            my @db_files;
119            while( my $row = $sth->fetchrow_hashref ) {
120    
121                    my $path = $row->{'path'} || die "no path?";
122                    $path =~ s#^/#./#;
123                    $path .= '/' if ($row->{'type'} == BPC_FTYPE_DIR);
124                    push @db_files, $path;
125            }
126    
127            print STDERR " ",($#db_files + 1), " files, diff" if ($debug);
128    
129            @db_files = sort @db_files;
130    
131            my $same = 1;
132            if ($#tar_files != $#db_files) {
133                    $same = 0;
134                    print STDERR " NUMBER" if ($debug);
135            } else {
136                    my $diff = Algorithm::Diff->new(\@tar_files, \@db_files);
137                    while ( $diff->Next() ) {
138                            next if $diff->Same();
139                            $same = 0;
140                            print "< $_\n" for $diff->Items(1);
141                            print "> $_\n" for $diff->Items(2);
142                    }
143            }
144    
145            print STDERR " ",($same ? 'ok' : 'DIFFERENT');
146            print STDERR " }} " if ($debug);
147    
148            return $same;
149    }
150    
151    
152  #----- main  #----- main
153    
154  my $sth = $dbh->prepare( qq{  my $sth = $dbh->prepare( qq{
# Line 63  select Line 157  select
157          backups.id as backup_id,          backups.id as backup_id,
158          hosts.name as host,          hosts.name as host,
159          shares.name as share,          shares.name as share,
160          backups.num as num          backups.num as num,
161            inc_size,
162            parts
163  from backups  from backups
164          join shares on backups.hostid = shares.hostid          join shares on backups.hostid = shares.hostid
165                  and shares.id = backups.shareid                  and shares.id = backups.shareid
166          join hosts on shares.hostid = hosts.id          join hosts on shares.hostid = hosts.id
167  where inc_size < 0 and not inc_deleted  where not inc_deleted
168  order by backups.date  order by backups.date
169    
170  } );  } );
171    
172  $sth->execute();  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 = ? where id = ? });  
173  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 = ? });
174    
175  %BackupPC::SearchLib::Conf = %Conf;  %BackupPC::SearchLib::Conf = %Conf;
176    
177    $sth->execute();
178    my $num_backups = $sth->rows;
179    my $curr_backup = 1;
180    
181  while (my $row = $sth->fetchrow_hashref) {  while (my $row = $sth->fetchrow_hashref) {
182          my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});          my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
183          print curr_time, " ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file";  
184            # this will return -1 if file doesn't exist
185            my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file);
186    
187            print curr_time, " $curr_backup/$num_backups ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file";
188            $curr_backup++;
189    
190          my $t = time();          my $t = time();
191    
192          my $cmd = qq{$tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} | gzip -9 > $tar_dir/$tar_file};          # re-create archive?
193          print STDERR "## $cmd\n" if ($debug);          if ($row->{'inc_size'} == -1 || $size == -1 ||
194                    $row->{'inc_size'} != $size ||
195                    $check && ! tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, "$tar_dir/$tar_file")
196            ) {
197                    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};
198                    print STDERR "## $cmd\n" if ($debug);
199    
200          system($cmd) == 0 or die "failed: $?";                  system($cmd) == 0 or die "failed: $?";
201    
202          my $size = (stat( "$tar_dir/$tar_file" ))[7];                  rename("${tar_dir}/${tar_file}.tmp", "$tar_dir/$tar_file") or die "can't rename $tar_dir/$tar_file: $!";
203    
204          printf(" %1.2f MB, dur: %s", ($size / 1024 / 1024), fmt_time(time() - $t));                  $size = (stat( "$tar_dir/$tar_file" ))[7];
205            }
206    
207          if ($size > 45) {          if ($size > 45) {
208                  $sth_inc_size->execute($size, $row->{'backup_id'});  
209                    my $max_size = $Conf{'MaxArchiveSize'} || die "problem with MaxArchieSize parametar";
210                    $max_size *= 1024;      # convert to bytes
211    
212                    my $parts = int( ($size + $max_size - 1) / $max_size );
213    
214                    if (-d "$tar_dir/$tar_file" && $parts != $row->{'parts'}) {
215                            print " join";
216                            tar_join("$tar_dir/$tar_file");
217                    }
218    
219                    if ($size > $max_size && ! -d "$tar_dir/$tar_file") {
220                            print " split/$parts";
221                            my $in = my $out = "$tar_dir/$tar_file";
222                            $out .= '.tmp';
223                            rename $in, $out || die "can't rename $in: $!";
224                            mkdir $in || die "can't mkdir $in: $!";
225    
226                            my $suffix_len = length("$parts");
227                            system("$bin->{'split'} -d -b $max_size -a $suffix_len $out $in/part") == 0 or die "can't split $out: $?";
228                            unlink $out || die "can't unlink $out: $!";
229                    }
230    
231                    $sth_inc_size->execute($size, $parts, $row->{'backup_id'});
232                  $sth_inc_deleted->execute(0, $row->{'backup_id'});                  $sth_inc_deleted->execute(0, $row->{'backup_id'});
233    
234                    printf(" %1.2f MB", ($size / 1024 / 1024));
235    
236          } else {          } else {
237                  $sth_inc_deleted->execute(1, $row->{'backup_id'});                  $sth_inc_deleted->execute(1, $row->{'backup_id'});
238                  unlink "$tar_dir/$tar_file" || die "can't delete $tar_dir/$tar_file: $!\n";                  unlink "$tar_dir/$tar_file" || die "can't delete $tar_dir/$tar_file: $!\n";
239                  print " EMPTY";                  print " EMPTY";
240          }          }
241          print "\n";          print ", dur: ",fmt_time(time() - $t), "\n";
242    
243          $dbh->commit;          $dbh->commit;
244    

Legend:
Removed from v.182  
changed lines
  Added in v.214

  ViewVC Help
Powered by ViewVC 1.1.26