/[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 158 by dpavlin, Mon Oct 10 13:39:10 2005 UTC revision 230 by dpavlin, Tue Oct 25 09:30:52 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/;
 use File::Pid;  
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;
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    # cludge: minimum .tar.gz size
26    my $MIN_TAR_SIZE = 80;
27    
28  my $path = abs_path($0);  my $path = abs_path($0);
29  $path =~ s#/[^/]+$#/#;  $path =~ s#/[^/]+$#/#;
# Line 19  my $tarIncCreate = $path .= 'BackupPC_ta Line 31  my $tarIncCreate = $path .= 'BackupPC_ta
31    
32  die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate);  die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate);
33    
34  my $debug = 1;  my $bin;
35    foreach my $c (qw/gzip split/) {
36            $bin->{$c} = which($c) || die "$0 needs $c, install it\n";
37    }
38    
39    my %opt;
40    getopts("cd", \%opt );
41    
42    my $debug = $opt{d};
43    my $check = $opt{c} && print STDERR "NOTICE: tar archive check forced\n";
44    
45  $|=1;  $|=1;
46    
47  my $start_t = time();  my $start_t = time();
48    
49  my $t_fmt = '%Y-%m-%d %H:%M:%S';  my $t_fmt = '%Y-%m-%d %H:%M:%S';
50    
 my $hosts;  
 my $bpc = BackupPC::Lib->new || die;  
 my %Conf = $bpc->Conf();  
 my $TopDir = $bpc->TopDir();  
 my $beenThere = {};  
   
51  my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";  my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";
52  my $user = $Conf{SearchUser} || '';  my $user = $Conf{SearchUser} || '';
53    
# Line 56  sub curr_time { Line 72  sub curr_time {
72          return strftime($t_fmt,localtime());          return strftime($t_fmt,localtime());
73  }  }
74    
75    sub tar_join($) {
76            my $filename = shift;
77    
78            my $in = my $out = $filename;
79            $out .= '.tmp';
80    
81            # FIXME I should really order parts manually!
82            system("cat $in/part* > $out && rm -Rf $in && mv $out $in") == 0 or die "can't join $in: $?";
83    
84    }
85    
86    sub tar_check($$$$) {
87            my ($host,$share,$num,$filename) = @_;
88    
89            if ($debug) {
90                    print STDERR " {{ CHECK: ${host}:${share}#${num} and $filename";
91            } else {
92                    print " check";
93            }
94    
95            if (-d $filename) {
96                    print STDERR ", joining";
97                    tar_join($filename);
98            }
99    
100            print STDERR ", opening" if ($debug);
101            open(my $fh, "gzip -cd $filename |") or die "can't open $filename: $!";
102            binmode($fh);
103            my $tar = Archive::Tar::Streamed->new($fh);
104    
105            print STDERR ", tar" if ($debug);
106            my @tar_files;
107            while(my $entry = $tar->next) {
108                    push @tar_files, $entry->name;
109            }
110            @tar_files = sort @tar_files;
111            print STDERR " ",($#tar_files + 1), " files" if ($debug);
112    
113            print STDERR ", database" if ($debug);
114    
115            my $sth = $dbh->prepare(qq{
116                    SELECT path,type
117                    FROM files
118                    JOIN shares on shares.id = shareid
119                    JOIN hosts on hosts.id = shares.hostid
120                    WHERE hosts.name = ? and shares.name = ? and backupnum = ?
121            });
122            $sth->execute($host, $share, $num);
123            my @db_files;
124            while( my $row = $sth->fetchrow_hashref ) {
125    
126                    my $path = $row->{'path'} || die "no path?";
127                    $path =~ s#^/#./#;
128                    $path .= '/' if ($row->{'type'} == BPC_FTYPE_DIR);
129                    push @db_files, $path;
130            }
131    
132            print STDERR " ",($#db_files + 1), " files, diff" if ($debug);
133    
134            @db_files = sort @db_files;
135    
136            my $same = 1;
137            if ($#tar_files != $#db_files) {
138                    $same = 0;
139                    print STDERR " NUMBER" if ($debug);
140            } else {
141                    my $diff = Algorithm::Diff->new(\@tar_files, \@db_files);
142                    while ( $diff->Next() ) {
143                            next if $diff->Same();
144                            $same = 0;
145                            print "< $_\n" for $diff->Items(1);
146                            print "> $_\n" for $diff->Items(2);
147                    }
148            }
149    
150            print " ",($same ? 'ok' : 'DIFFERENT');
151            print STDERR " }} " if ($debug);
152    
153            return $same;
154    }
155    
156    
157  #----- main  #----- main
158    
159  my $sth = $dbh->prepare( qq{  my $sth = $dbh->prepare( qq{
# Line 64  select Line 162  select
162          backups.id as backup_id,          backups.id as backup_id,
163          hosts.name as host,          hosts.name as host,
164          shares.name as share,          shares.name as share,
165          backups.num as num          backups.num as num,
166            inc_size,
167            parts
168  from backups  from backups
169          join shares on backups.hostid = shares.hostid          join shares on backups.hostid = shares.hostid
170                  and shares.id = backups.shareid                  and shares.id = backups.shareid
171          join hosts on shares.hostid = hosts.id          join hosts on shares.hostid = hosts.id
172  where inc_size < 0 and not inc_deleted  where not inc_deleted
173  order by backups.date  order by backups.date
174    
175  } );  } );
176    
177  $sth->execute();  my $sth_inc_size = $dbh->prepare(qq{ update backups set inc_size = ?, parts = ? where id = ? });
178    my $sth_inc_deleted = $dbh->prepare(qq{ update backups set inc_deleted = ? where id = ? });
179    
 my $sth_inc_size = $dbh->prepare(qq{ update backups set inc_size = ? where id = ? });  
180    
181  %BackupPC::SearchLib::Conf = %Conf;  $sth->execute();
182    my $num_backups = $sth->rows;
183    my $curr_backup = 1;
184    
185  while (my $row = $sth->fetchrow_hashref) {  while (my $row = $sth->fetchrow_hashref) {
186          my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});          my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
         print curr_time, sprintf(" %s:%s %-3d ", $row->{'host'}, $row->{'share'}, $row->{'num'}), " -> $tar_file ";  
187    
188          my $t = time();          # this will return -1 if file doesn't exist
189            my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file);
190    
191          my $cmd = qq{$tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} | gzip -9 > $tar_dir/$tar_file};          print curr_time, " $curr_backup/$num_backups ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file";
192          print STDERR "## $cmd\n" if ($debug);          $curr_backup++;
193    
194          system($cmd) == 0 or die "failed: $?";          my $t = time();
   
         my $size = (stat( "$tar_dir/$tar_file" ))[7];  
   
         print fmt_time(time() - $t)," $size bytes\n";  
195    
196          $sth_inc_size->execute($size, $row->{'backup_id'});          # re-create archive?
197            if ($row->{'inc_size'} == -1 || $size == -1 ||
198                    $row->{'inc_size'} != $size ||
199                    $check && ! tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, "$tar_dir/$tar_file")
200            ) {
201                    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};
202                    print STDERR "## $cmd\n" if ($debug);
203    
204                    system($cmd) == 0 or die "failed: $?";
205    
206                    rename("${tar_dir}/${tar_file}.tmp", "$tar_dir/$tar_file") or die "can't rename $tar_dir/$tar_file: $!";
207    
208                    $size = (stat( "$tar_dir/$tar_file" ))[7];
209            }
210    
211            if ($size > $MIN_TAR_SIZE) {
212    
213                    my $max_size = $Conf{'MaxArchiveSize'} || die "problem with MaxArchiveSize parametar";
214                    $max_size *= 1024;      # convert to bytes
215    
216                    my $max_file_size = $Conf{'MaxArchiveFileSize'} || die "problem with MaxArchiveFileSize parametar";
217                    $max_file_size *= 1024; # bytes
218    
219                    if ($max_file_size > $max_size) {
220                            warn "MaxArchiveFileSize ($max_file_size) is bigger than MaxArchiveSize ($max_size)\n";
221                            $max_file_size = $max_size;
222                    }
223    
224                    # maximum file size on ISO image is 4Gb
225                    # this will require Linux kernel 2.6.8 or newer
226                    if ( $max_size > $max_file_size ) {
227                            $max_size = $max_file_size;
228                    }
229    
230                    my $parts = int( ($size + $max_size - 1) / $max_size );
231    
232                    if (-d "$tar_dir/$tar_file" && $parts != $row->{'parts'}) {
233                            print " join";
234                            tar_join("$tar_dir/$tar_file");
235                    }
236    
237                    if ($size > $max_size && ! -d "$tar_dir/$tar_file") {
238                            print " split/$parts";
239                            my $in = my $out = "$tar_dir/$tar_file";
240                            $out .= '.tmp';
241                            rename $in, $out || die "can't rename $in: $!";
242                            mkdir $in || die "can't mkdir $in: $!";
243    
244                            my $suffix_len = length("$parts");
245                            system("$bin->{'split'} -d -b $max_size -a $suffix_len $out $in/part") == 0 or die "can't split $out: $?";
246                            unlink $out || die "can't unlink $out: $!";
247                    }
248    
249                    $sth_inc_size->execute($size, $parts, $row->{'backup_id'});
250                    $sth_inc_deleted->execute(0, $row->{'backup_id'});
251    
252                    printf(" %1.2f MB", ($size / 1024 / 1024));
253    
254            } else {
255                    $sth_inc_deleted->execute(1, $row->{'backup_id'});
256                    unlink "$tar_dir/$tar_file" || die "can't delete $tar_dir/$tar_file: $!\n";
257                    print " EMPTY";
258            }
259            print ", dur: ",fmt_time(time() - $t), "\n";
260    
261          $dbh->commit;          $dbh->commit;
262    

Legend:
Removed from v.158  
changed lines
  Added in v.230

  ViewVC Help
Powered by ViewVC 1.1.26