/[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 162 by dpavlin, Mon Oct 10 13:39:14 2005 UTC revision 228 by dpavlin, Mon Oct 24 16:41:13 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/;
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::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 19  my $tarIncCreate = $path .= 'BackupPC_ta Line 26  my $tarIncCreate = $path .= 'BackupPC_ta
26    
27  die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate);  die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate);
28    
29  my $debug = 0;  my $bin;
30    foreach my $c (qw/gzip split/) {
31            $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    
40  $|=1;  $|=1;
41    
42  my $start_t = time();  my $start_t = time();
# Line 56  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 64  select Line 163  select
163          backups.id as backup_id,          backups.id as backup_id,
164          hosts.name as host,          hosts.name as host,
165          shares.name as share,          shares.name as share,
166          backups.num as num          backups.num as num,
167            inc_size,
168            parts
169  from backups  from backups
170          join shares on backups.hostid = shares.hostid          join shares on backups.hostid = shares.hostid
171                  and shares.id = backups.shareid                  and shares.id = backups.shareid
172          join hosts on shares.hostid = hosts.id          join hosts on shares.hostid = hosts.id
173  where inc_size < 0 and not inc_deleted  where not inc_deleted
174  order by backups.date  order by backups.date
175    
176  } );  } );
177    
178  $sth->execute();  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_size = $dbh->prepare(qq{ update backups set inc_size = ? 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'});
         print curr_time, sprintf(" %s:%s %-3d ", $row->{'host'}, $row->{'share'}, $row->{'num'}), " -> $tar_file ";  
   
         my $t = time();  
   
         my $cmd = qq{$tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} | gzip -9 > $tar_dir/$tar_file};  
         print STDERR "## $cmd\n" if ($debug);  
189    
190          system($cmd) == 0 or die "failed: $?";          # this will return -1 if file doesn't exist
191            my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file);
192    
193          my $size = (stat( "$tar_dir/$tar_file" ))[7];          print curr_time, " $curr_backup/$num_backups ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file";
194            $curr_backup++;
195    
196          print " dur: ",fmt_time(time() - $t)," $size bytes\n";          my $t = time();
197    
198          $sth_inc_size->execute($size, $row->{'backup_id'});          # re-create archive?
199            if ($row->{'inc_size'} == -1 || $size == -1 ||
200                    $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);
205    
206                    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];
211            }
212    
213            if ($size > $MIN_TAR_SIZE) {
214    
215                    my $max_size = $Conf{'MaxArchiveSize'} || die "problem with MaxArchieSize parametar";
216                    $max_size *= 1024;      # convert to bytes
217    
218                    # maximum file size on ISO image is 4Gb
219                    # this will require Linux kernel 2.6.8 or newer
220                    my $max_iso_file_size = 2^32 - 2048;
221                    if ( $max_size > $max_iso_file_size ) {
222                            $max_size = $max_iso_file_size;
223                    }
224    
225                    my $parts = int( ($size + $max_size - 1) / $max_size );
226    
227                    if (-d "$tar_dir/$tar_file" && $parts != $row->{'parts'}) {
228                            print " join";
229                            tar_join("$tar_dir/$tar_file");
230                    }
231    
232                    if ($size > $max_size && ! -d "$tar_dir/$tar_file") {
233                            print " split/$parts";
234                            my $in = my $out = "$tar_dir/$tar_file";
235                            $out .= '.tmp';
236                            rename $in, $out || die "can't rename $in: $!";
237                            mkdir $in || die "can't mkdir $in: $!";
238    
239                            my $suffix_len = length("$parts");
240                            system("$bin->{'split'} -d -b $max_size -a $suffix_len $out $in/part") == 0 or die "can't split $out: $?";
241                            unlink $out || die "can't unlink $out: $!";
242                    }
243    
244                    $sth_inc_size->execute($size, $parts, $row->{'backup_id'});
245                    $sth_inc_deleted->execute(0, $row->{'backup_id'});
246    
247                    printf(" %1.2f MB", ($size / 1024 / 1024));
248    
249            } else {
250                    $sth_inc_deleted->execute(1, $row->{'backup_id'});
251                    unlink "$tar_dir/$tar_file" || die "can't delete $tar_dir/$tar_file: $!\n";
252                    print " EMPTY";
253            }
254            print ", dur: ",fmt_time(time() - $t), "\n";
255    
256          $dbh->commit;          $dbh->commit;
257    

Legend:
Removed from v.162  
changed lines
  Added in v.228

  ViewVC Help
Powered by ViewVC 1.1.26