1 #!/usr/local/bin/perl -w
4 use lib "__INSTALLDIR__/lib";
9 use BackupPC::Attrib qw/:all/;
11 use Time::HiRes qw/time/;
12 use POSIX qw/strftime/;
13 use BackupPC::SearchLib;
16 use Archive::Tar::Streamed;
20 # cludge: minimum .tar.gz size
21 my $MIN_TAR_SIZE = 80;
23 my $path = abs_path($0);
24 $path =~ s#/[^/]+$#/#;
25 my $tarIncCreate = $path .= 'BackupPC_tarIncCreate';
27 die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate);
30 foreach my $c (qw/gzip split/) {
31 $bin->{$c} = which($c) || die "$0 needs $c, install it\n";
35 getopts("cd", \%opt );
38 my $check = $opt{c} && print STDERR "NOTICE: tar archive check forced\n";
44 my $t_fmt = '%Y-%m-%d %H:%M:%S';
47 my $bpc = BackupPC::Lib->new || die;
48 my %Conf = $bpc->Conf();
49 my $TopDir = $bpc->TopDir();
52 my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";
53 my $user = $Conf{SearchUser} || '';
55 my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 });
57 my $tar_dir = $Conf{InstallDir}.'/'.$Conf{GzipTempDir};
59 die "problem with $tar_dir, check GzipTempDir in configuration\n" unless (-d $tar_dir && -w $tar_dir);
64 my $t = shift || return;
66 my ($ss,$mm,$hh) = gmtime($t);
67 $out .= "${hh}h" if ($hh);
68 $out .= sprintf("%02d:%02d", $mm,$ss);
73 return strftime($t_fmt,localtime());
79 my $in = my $out = $filename;
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: $?";
88 my ($host,$share,$num,$filename) = @_;
91 print STDERR " {{ CHECK: ${host}:${share}#${num} and $filename";
97 print STDERR ", joining";
101 print STDERR ", opening" if ($debug);
102 open(my $fh, "gzip -cd $filename |") or die "can't open $filename: $!";
104 my $tar = Archive::Tar::Streamed->new($fh);
106 print STDERR ", tar" if ($debug);
108 while(my $entry = $tar->next) {
109 push @tar_files, $entry->name;
111 @tar_files = sort @tar_files;
112 print STDERR " ",($#tar_files + 1), " files" if ($debug);
114 print STDERR ", database" if ($debug);
116 my $sth = $dbh->prepare(qq{
119 JOIN shares on shares.id = shareid
120 JOIN hosts on hosts.id = shares.hostid
121 WHERE hosts.name = ? and shares.name = ? and backupnum = ?
123 $sth->execute($host, $share, $num);
125 while( my $row = $sth->fetchrow_hashref ) {
127 my $path = $row->{'path'} || die "no path?";
129 $path .= '/' if ($row->{'type'} == BPC_FTYPE_DIR);
130 push @db_files, $path;
133 print STDERR " ",($#db_files + 1), " files, diff" if ($debug);
135 @db_files = sort @db_files;
138 if ($#tar_files != $#db_files) {
140 print STDERR " NUMBER" if ($debug);
142 my $diff = Algorithm::Diff->new(\@tar_files, \@db_files);
143 while ( $diff->Next() ) {
144 next if $diff->Same();
146 print "< $_\n" for $diff->Items(1);
147 print "> $_\n" for $diff->Items(2);
151 print " ",($same ? 'ok' : 'DIFFERENT');
152 print STDERR " }} " if ($debug);
160 my $sth = $dbh->prepare( qq{
163 backups.id as backup_id,
165 shares.name as share,
170 join shares on backups.hostid = shares.hostid
171 and shares.id = backups.shareid
172 join hosts on shares.hostid = hosts.id
173 where not inc_deleted
174 order by backups.date
178 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 = ? });
181 %BackupPC::SearchLib::Conf = %Conf;
184 my $num_backups = $sth->rows;
187 while (my $row = $sth->fetchrow_hashref) {
188 my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
190 # this will return -1 if file doesn't exist
191 my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file);
193 print curr_time, " $curr_backup/$num_backups ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file";
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")
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);
206 system($cmd) == 0 or die "failed: $?";
208 rename("${tar_dir}/${tar_file}.tmp", "$tar_dir/$tar_file") or die "can't rename $tar_dir/$tar_file: $!";
210 $size = (stat( "$tar_dir/$tar_file" ))[7];
213 if ($size > $MIN_TAR_SIZE) {
215 my $max_size = $Conf{'MaxArchiveSize'} || die "problem with MaxArchieSize parametar";
216 $max_size *= 1024; # convert to bytes
218 my $parts = int( ($size + $max_size - 1) / $max_size );
220 if (-d "$tar_dir/$tar_file" && $parts != $row->{'parts'}) {
222 tar_join("$tar_dir/$tar_file");
225 if ($size > $max_size && ! -d "$tar_dir/$tar_file") {
226 print " split/$parts";
227 my $in = my $out = "$tar_dir/$tar_file";
229 rename $in, $out || die "can't rename $in: $!";
230 mkdir $in || die "can't mkdir $in: $!";
232 my $suffix_len = length("$parts");
233 system("$bin->{'split'} -d -b $max_size -a $suffix_len $out $in/part") == 0 or die "can't split $out: $?";
234 unlink $out || die "can't unlink $out: $!";
237 $sth_inc_size->execute($size, $parts, $row->{'backup_id'});
238 $sth_inc_deleted->execute(0, $row->{'backup_id'});
240 printf(" %1.2f MB", ($size / 1024 / 1024));
243 $sth_inc_deleted->execute(1, $row->{'backup_id'});
244 unlink "$tar_dir/$tar_file" || die "can't delete $tar_dir/$tar_file: $!\n";
247 print ", dur: ",fmt_time(time() - $t), "\n";