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 my $path = abs_path($0);
21 $path =~ s#/[^/]+$#/#;
22 my $tarIncCreate = $path .= 'BackupPC_tarIncCreate';
24 die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate);
27 foreach my $c (qw/gzip split/) {
28 $bin->{$c} = which($c) || die "$0 needs $c, install it\n";
32 getopts("cd", \%opt );
35 my $check = $opt{c} && print STDERR "NOTICE: tar archive check forced\n";
41 my $t_fmt = '%Y-%m-%d %H:%M:%S';
44 my $bpc = BackupPC::Lib->new || die;
45 my %Conf = $bpc->Conf();
46 my $TopDir = $bpc->TopDir();
49 my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";
50 my $user = $Conf{SearchUser} || '';
52 my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 });
54 my $tar_dir = $Conf{InstallDir}.'/'.$Conf{GzipTempDir};
56 die "problem with $tar_dir, check GzipTempDir in configuration\n" unless (-d $tar_dir && -w $tar_dir);
61 my $t = shift || return;
63 my ($ss,$mm,$hh) = gmtime($t);
64 $out .= "${hh}h" if ($hh);
65 $out .= sprintf("%02d:%02d", $mm,$ss);
70 return strftime($t_fmt,localtime());
76 my $in = my $out = $filename;
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: $?";
85 my ($host,$share,$num,$filename) = @_;
88 print STDERR " {{ CHECK: ${host}:${share}#${num} and $filename";
94 print STDERR ", joining";
98 print STDERR ", opening" if ($debug);
99 open(my $fh, "gzip -cd $filename |") or die "can't open $filename: $!";
101 my $tar = Archive::Tar::Streamed->new($fh);
103 print STDERR ", tar" if ($debug);
105 while(my $entry = $tar->next) {
106 push @tar_files, $entry->name;
108 @tar_files = sort @tar_files;
109 print STDERR " ",($#tar_files + 1), " files" if ($debug);
111 print STDERR ", database" if ($debug);
113 my $sth = $dbh->prepare(qq{
116 JOIN shares on shares.id = shareid
117 JOIN hosts on hosts.id = shares.hostid
118 WHERE hosts.name = ? and shares.name = ? and backupnum = ?
120 $sth->execute($host, $share, $num);
122 while( my $row = $sth->fetchrow_hashref ) {
124 my $path = $row->{'path'} || die "no path?";
126 $path .= '/' if ($row->{'type'} == BPC_FTYPE_DIR);
127 push @db_files, $path;
130 print STDERR " ",($#db_files + 1), " files, diff" if ($debug);
132 @db_files = sort @db_files;
135 if ($#tar_files != $#db_files) {
137 print STDERR " NUMBER" if ($debug);
139 my $diff = Algorithm::Diff->new(\@tar_files, \@db_files);
140 while ( $diff->Next() ) {
141 next if $diff->Same();
143 print "< $_\n" for $diff->Items(1);
144 print "> $_\n" for $diff->Items(2);
148 print STDERR " ",($same ? 'ok' : 'DIFFERENT');
149 print STDERR " }} " if ($debug);
157 my $sth = $dbh->prepare( qq{
160 backups.id as backup_id,
162 shares.name as share,
167 join shares on backups.hostid = shares.hostid
168 and shares.id = backups.shareid
169 join hosts on shares.hostid = hosts.id
170 where not inc_deleted
171 order by backups.date
175 my $sth_inc_size = $dbh->prepare(qq{ update backups set inc_size = ?, parts = ? where id = ? });
176 my $sth_inc_deleted = $dbh->prepare(qq{ update backups set inc_deleted = ? where id = ? });
178 %BackupPC::SearchLib::Conf = %Conf;
181 my $num_backups = $sth->rows;
184 while (my $row = $sth->fetchrow_hashref) {
185 my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
187 # this will return -1 if file doesn't exist
188 my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file);
190 print curr_time, " $curr_backup/$num_backups ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file";
196 if ($row->{'inc_size'} == -1 || $size == -1 ||
197 $row->{'inc_size'} != $size ||
198 $check && ! tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, "$tar_dir/$tar_file")
200 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};
201 print STDERR "## $cmd\n" if ($debug);
203 system($cmd) == 0 or die "failed: $?";
205 rename("${tar_dir}/${tar_file}.tmp", "$tar_dir/$tar_file") or die "can't rename $tar_dir/$tar_file: $!";
207 $size = (stat( "$tar_dir/$tar_file" ))[7];
212 my $max_size = $Conf{'MaxArchiveSize'} || die "problem with MaxArchieSize parametar";
213 $max_size *= 1024; # convert to bytes
215 my $parts = int( ($size + $max_size - 1) / $max_size );
217 if (-d "$tar_dir/$tar_file" && $parts != $row->{'parts'}) {
219 tar_join("$tar_dir/$tar_file");
222 if ($size > $max_size && ! -d "$tar_dir/$tar_file") {
223 print " split/$parts";
224 my $in = my $out = "$tar_dir/$tar_file";
226 rename $in, $out || die "can't rename $in: $!";
227 mkdir $in || die "can't mkdir $in: $!";
229 my $suffix_len = length("$parts");
230 system("$bin->{'split'} -d -b $max_size -a $suffix_len $out $in/part") == 0 or die "can't split $out: $?";
231 unlink $out || die "can't unlink $out: $!";
234 $sth_inc_size->execute($size, $parts, $row->{'backup_id'});
235 $sth_inc_deleted->execute(0, $row->{'backup_id'});
237 printf(" %1.2f MB", ($size / 1024 / 1024));
240 $sth_inc_deleted->execute(1, $row->{'backup_id'});
241 unlink "$tar_dir/$tar_file" || die "can't delete $tar_dir/$tar_file: $!\n";
244 print ", dur: ",fmt_time(time() - $t), "\n";