2 #============================================================= -*-perl-*-
4 # BackupPC_tarIncCreate: create a tar archive of an existing incremental dump
9 # Usage: BackupPC_tarIncCreate [options]
14 # -h host Host from which the tar archive is created.
15 # -n dumpNum Dump number from which the tar archive is created.
16 # A negative number means relative to the end (eg -1
17 # means the most recent dump, -2 2nd most recent etc).
18 # -s shareName Share name from which the tar archive is created.
21 # -t print summary totals
22 # -r pathRemove path prefix that will be replaced with pathAdd
23 # -p pathAdd new path prefix
24 # -b BLOCKS BLOCKS x 512 bytes per record (default 20; same as tar)
25 # -w writeBufSz write buffer size (default 1MB)
27 # The -h, -n and -s options specify which dump is used to generate
28 # the tar archive. The -r and -p options can be used to relocate
29 # the paths in the tar archive so extracted files can be placed
30 # in a location different from their original location.
33 # Craig Barratt <cbarratt@users.sourceforge.net>
34 # Ivan Klaric <iklaric@gmail.com>
35 # Dobrica Pavlinusic <dpavlin@rot13.org>
38 # Copyright (C) 2001-2003 Craig Barratt
40 # This program is free software; you can redistribute it and/or modify
41 # it under the terms of the GNU General Public License as published by
42 # the Free Software Foundation; either version 2 of the License, or
43 # (at your option) any later version.
45 # This program is distributed in the hope that it will be useful,
46 # but WITHOUT ANY WARRANTY; without even the implied warranty of
47 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
48 # GNU General Public License for more details.
50 # You should have received a copy of the GNU General Public License
51 # along with this program; if not, write to the Free Software
52 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
54 #========================================================================
56 # Version 2.1.0, released 20 Jun 2004.
58 # See http://backuppc.sourceforge.net.
60 #========================================================================
64 use lib "/usr/local/BackupPC/lib";
69 use BackupPC::Attrib qw(:all);
70 use BackupPC::FileZIO;
73 use Time::HiRes qw/time/;
74 use POSIX qw/strftime/;
77 use Data::Dumper; ### FIXME
79 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
80 my $TopDir = $bpc->TopDir();
81 my $BinDir = $bpc->BinDir();
82 my %Conf = $bpc->Conf();
83 %BackupPC::Search::Conf = %Conf;
85 my $in_backup_increment;
88 if ( !getopts("th:n:p:r:s:b:w:vdf", \%opts) ) {
92 -h host host from which the tar archive is created
93 -n dumpNum dump number from which the tar archive is created
94 A negative number means relative to the end (eg -1
95 means the most recent dump, -2 2nd most recent etc).
96 -s shareName share name from which the tar archive is created
99 -t print summary totals
100 -r pathRemove path prefix that will be replaced with pathAdd
101 -p pathAdd new path prefix
102 -b BLOCKS BLOCKS x 512 bytes per record (default 20; same as tar)
103 -w writeBufSz write buffer size (default 1048576 = 1MB)
104 -f overwrite existing parts
111 if ( $opts{h} !~ /^([\w\.\s-]+)$/ ) {
112 die "$0: bad host name '$opts{h}'\n";
116 if ( $opts{n} !~ /^(-?\d+)$/ ) {
117 die "$0: bad dump number '$opts{n}'\n";
122 foreach my $c (qw/gzip md5sum tee/) {
123 $bin->{$c} = which($c) || die "$0 needs $c, install it\n";
126 my @Backups = $bpc->BackupInfoRead($Host);
132 my $current_tar_size = 0;
133 my $total_increment_size = 0;
136 $Num = $Backups[@Backups + $Num]{num} if ( -@Backups <= $Num && $Num < 0 );
137 for ( $i = 0 ; $i < @Backups ; $i++ ) {
138 last if ( $Backups[$i]{num} == $Num );
140 if ( $i >= @Backups ) {
141 die "$0: bad backup number $Num for host $Host\n";
144 my $PathRemove = $1 if ( $opts{r} =~ /(.+)/ );
145 my $PathAdd = $1 if ( $opts{p} =~ /(.+)/ );
146 if ( $opts{s} !~ /^([\w\s\.\/\$-]+)$/ && $opts{s} ne "*" ) {
147 die "$0: bad share name '$opts{s}'\n";
149 our $ShareName = $opts{s};
150 our $view = BackupPC::View->new($bpc, $Host, \@Backups);
154 my $dsn = $Conf{SearchDSN};
155 my $db_user = $Conf{SearchUser} || '';
157 my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 0} );
159 my $sth_inc_size = $dbh->prepare(qq{
165 my $sth_backup_parts = $dbh->prepare(qq{
166 insert into backup_parts (
173 ) values (?,?,?,?,?,?)
177 # This constant and the line of code below that uses it are borrowed
178 # from Archive::Tar. Thanks to Calle Dybedahl and Stephen Zander.
181 # Archive::Tar is Copyright 1997 Calle Dybedahl. All rights reserved.
182 # Copyright 1998 Stephen Zander. All rights reserved.
185 = 'a100 a8 a8 a8 a12 a12 A8 a1 a100 a6 a2 a32 a32 a8 a8 a155 x12';
186 my $tar_header_length = 512;
188 my $BufSize = $opts{w} || 1048576; # 1MB or 2^20
190 my $WriteBufSz = ($opts{b} || 20) * $tar_header_length;
192 my(%UidCache, %GidCache);
193 my(%HardLinkExtraFiles, @HardLinks);
196 # Write out all the requested files/directories
199 my $max_file_size = $Conf{'ArchiveChunkSize'} || die "problem with MaxArchiveFileSize parametar";
201 my $tar_dir = $Conf{GzipTempDir};
202 die "problem with $tar_dir, check GzipTempDir in configuration\n" unless (-d $tar_dir && -w $tar_dir);
204 my $tar_file = BackupPC::Search::getGzipName($Host, $ShareName, $Num) || die "can't getGzipName($Host, $ShareName, $Num)";
206 my $tar_path_final = $tar_dir . '/' . $tar_file;
207 my $tar_path = $tar_path_final . '.tmp';
209 $tar_path =~ s#//#/#g;
211 my $sth = $dbh->prepare(qq{
215 JOIN shares on shares.id = shareid
216 JOIN hosts on hosts.id = shares.hostid
217 WHERE hosts.name = ? and shares.name = ? and backups.num = ?
219 $sth->execute($Host, $ShareName, $Num);
220 my ($backup_id) = $sth->fetchrow_array;
224 # delete exising backup_parts
225 my $sth_delete_backup_parts = $dbh->prepare(qq{
226 delete from backup_parts
229 $sth_delete_backup_parts->execute($backup_id);
232 print STDERR "backup_id: $backup_id working dir: $tar_dir, max uncompressed size $max_file_size bytes, tar $tar_file\n" if ($opts{d});
234 if (-e $tar_path_final) {
236 rmtree $tar_path_final || die "can't remove $tar_path_final: $!";
238 die "$tar_path_final allready exists\n";
245 my $items_in_part = 0;
251 return if ($current_tar_size == 0);
253 print STDERR "\n\t+ $part:";
256 # Finish with two null 512 byte headers,
257 # and then round out a full block.
259 my $data = "\0" x ($tar_header_length * 2);
260 TarWrite($fh, \$data);
261 TarWrite($fh, undef);
263 close($fh) || die "can't close archive part $part: $!";
265 my $file = $tar_path . '/' . $part;
267 my $md5 = read_file( $file . '.md5' ) || die "can't read md5sum file ${file}.md5";
270 my $size = (stat( $file . '.tar.gz' ))[7] || die "can't stat ${file}.tar.gz";
272 print "$file, $size bytes, $items_in_part items";
274 $sth_backup_parts->execute(
283 $total_increment_size += $size;
289 print STDERR "# rename $from -> $to\n" if ($opts{d});
290 rename $from, $to || die "can't move $from -> $to: $!\n";
294 print STDERR " single" if ($opts{v});
295 move("${tar_path}/1.tar.gz", "${tar_path_final}.tar.gz");
296 move("${tar_path}/1.md5", "${tar_path_final}.md5");
297 rmtree $tar_path or die "can't remove temporary dir $tar_path: $!";
299 print STDERR " [last]" if ($opts{v});
300 move("${tar_path}", "${tar_path_final}");
302 # if this archive was single part, remove it
303 foreach my $suffix (qw/.tar.gz .md5/) {
304 my $path = $tar_path_final . $suffix;
305 unlink $path if (-e $path);
309 $sth_inc_size->execute(
310 $total_increment_size,
314 print "\n\ttotal $total_increment_size bytes";
323 # if this is first part, create directory
327 print STDERR "# deleting existing $tar_path\n" if ($opts{d});
330 mkdir($tar_path) || die "can't create directory $tar_path: $!";
333 print STDERR "ABORTED: cleanup temp dir ";
339 $SIG{'INT'} = \&abort_cleanup;
340 $SIG{'QUIT'} = \&abort_cleanup;
341 $SIG{'__DIE__'} = \&abort_cleanup;
345 my $file = $tar_path . '/' . $part;
348 # create comprex pipe which will pass output through gzip
349 # for compression, create file on disk using tee
350 # and pipe same output to md5sum to create checksum
353 my $cmd = '| ' . $bin->{'gzip'} . ' ' . $Conf{GzipLevel} . ' ' .
354 '| ' . $bin->{'tee'} . ' ' . $file . '.tar.gz' . ' ' .
355 '| ' . $bin->{'md5sum'} . ' - > ' . $file . '.md5';
357 print STDERR "## $cmd\n" if ($opts{d});
359 open($fh, $cmd) or die "can't open $cmd: $!";
362 $current_tar_size = 0;
368 if (seedCache($Host, $ShareName, $Num)) {
369 archiveWrite($fh, '/');
370 archiveWriteHardLinks($fh);
371 new_tar_part( close => 1 );
373 print STDERR "NOTE: no files found for $Host:$ShareName, increment $Num\n" if ($opts{v});
374 # remove temporary files if there are no files
377 my $sth = $dbh->prepare(qq{
378 update backups set inc_size = 0, inc_deleted = true
381 $sth->execute($backup_id);
386 # print out totals if requested
389 print STDERR "Done: $FileCnt files, $ByteCnt bytes, $DirCnt dirs,",
390 " $SpecialCnt specials, $ErrorCnt errors\n";
392 if ( $ErrorCnt && !$FileCnt && !$DirCnt ) {
394 # Got errors, with no files or directories; exit with non-zero
397 die "got errors or no files\n";
400 $sth_inc_size->finish;
401 $sth_backup_parts->finish;
403 $dbh->commit || die "can't commit changes to database";
408 ###########################################################################
410 ###########################################################################
414 my($fh, $dir, $tarPathOverride) = @_;
416 if ( $dir =~ m{(^|/)\.\.(/|$)} ) {
417 print(STDERR "$0: bad directory '$dir'\n");
421 $dir = "/" if ( $dir eq "." );
422 #print(STDERR "calling find with $Num, $ShareName, $dir\n");
424 if ( $view->find($Num, $ShareName, $dir, 0, \&TarWriteFile,
425 $fh, $tarPathOverride) < 0 ) {
426 print(STDERR "$0: bad share or directory '$ShareName/$dir'\n");
433 # Write out any hardlinks (if any)
435 sub archiveWriteHardLinks
438 foreach my $hdr ( @HardLinks ) {
440 if ( defined($PathRemove)
441 && substr($hdr->{linkname}, 0, length($PathRemove)+1)
442 eq ".$PathRemove" ) {
443 substr($hdr->{linkname}, 0, length($PathRemove)+1) = ".$PathAdd";
445 TarWriteFileInfo($fh, $hdr);
448 %HardLinkExtraFiles = ();
455 $UidCache{$uid} = (getpwuid($uid))[0] if ( !exists($UidCache{$uid}) );
456 return $UidCache{$uid};
463 $GidCache{$gid} = (getgrgid($gid))[0] if ( !exists($GidCache{$gid}) );
464 return $GidCache{$gid};
469 my($fh, $dataRef) = @_;
472 if ( !defined($dataRef) ) {
474 # do flush by padding to a full $WriteBufSz
476 my $data = "\0" x ($WriteBufSz - length($WriteBuf));
480 # poor man's tell :-)
481 $current_tar_size += length($$dataRef);
483 if ( length($WriteBuf) + length($$dataRef) < $WriteBufSz ) {
485 # just buffer and return
487 $WriteBuf .= $$dataRef;
490 my $done = $WriteBufSz - length($WriteBuf);
491 if ( syswrite($fh, $WriteBuf . substr($$dataRef, 0, $done))
493 die "Unable to write to output file ($!)\n";
495 while ( $done + $WriteBufSz <= length($$dataRef) ) {
496 if ( syswrite($fh, substr($$dataRef, $done, $WriteBufSz))
498 die "Unable to write to output file ($!)\n";
500 $done += $WriteBufSz;
502 $WriteBuf = substr($$dataRef, $done);
509 if ( $size % $tar_header_length ) {
510 my $data = "\0" x ($tar_header_length - ($size % $tar_header_length));
511 TarWrite($fh, \$data);
519 $hdr->{uname} = UidLookup($hdr->{uid}) if ( !defined($hdr->{uname}) );
520 $hdr->{gname} = GidLookup($hdr->{gid}) if ( !defined($hdr->{gname}) );
521 my $devmajor = defined($hdr->{devmajor}) ? sprintf("%07o", $hdr->{devmajor})
523 my $devminor = defined($hdr->{devminor}) ? sprintf("%07o", $hdr->{devminor})
526 if ( $hdr->{size} >= 2 * 65536 * 65536 ) {
528 # GNU extension for files >= 8GB: send size in big-endian binary
530 $sizeStr = pack("c4 N N", 0x80, 0, 0, 0,
531 $hdr->{size} / (65536 * 65536),
532 $hdr->{size} % (65536 * 65536));
533 } elsif ( $hdr->{size} >= 1 * 65536 * 65536 ) {
535 # sprintf octal only handles up to 2^32 - 1
537 $sizeStr = sprintf("%03o", $hdr->{size} / (1 << 24))
538 . sprintf("%08o", $hdr->{size} % (1 << 24));
540 $sizeStr = sprintf("%011o", $hdr->{size});
542 my $data = pack($tar_pack_header,
543 substr($hdr->{name}, 0, 99),
544 sprintf("%07o", $hdr->{mode}),
545 sprintf("%07o", $hdr->{uid}),
546 sprintf("%07o", $hdr->{gid}),
548 sprintf("%011o", $hdr->{mtime}),
549 "", #checksum field - space padded by pack("A8")
551 substr($hdr->{linkname}, 0, 99),
552 $hdr->{magic} || 'ustar ',
553 $hdr->{version} || ' ',
560 substr($data, 148, 7) = sprintf("%06o\0", unpack("%16C*",$data));
561 TarWrite($fh, \$data);
569 # Handle long link names (symbolic links)
571 if ( length($hdr->{linkname}) > 99 ) {
573 my $data = $hdr->{linkname} . "\0";
574 $h{name} = "././\@LongLink";
576 $h{size} = length($data);
577 TarWriteHeader($fh, \%h);
578 TarWrite($fh, \$data);
579 TarWritePad($fh, length($data));
582 # Handle long file names
584 if ( length($hdr->{name}) > 99 ) {
586 my $data = $hdr->{name} . "\0";
587 $h{name} = "././\@LongLink";
589 $h{size} = length($data);
590 TarWriteHeader($fh, \%h);
591 TarWrite($fh, \$data);
592 TarWritePad($fh, length($data));
594 TarWriteHeader($fh, $hdr);
598 # seed cache of files in this increment
601 my ($host, $share, $dumpNo) = @_;
603 print STDERR curr_time(), "$host:$share #$dumpNo" if ($opts{v});
607 JOIN shares on shares.id = shareid
608 JOIN hosts on hosts.id = shares.hostid
609 WHERE hosts.name = ? and shares.name = ? and backupnum = ?
612 my $sth = $dbh->prepare($sql);
613 $sth->execute($host, $share, $dumpNo);
614 my $count = $sth->rows;
615 print STDERR " $count items, parts:" if ($opts{v});
616 while (my $row = $sth->fetchrow_arrayref) {
617 #print STDERR "+ ", $row->[0],"\n";
618 $in_backup_increment->{ $row->[0] } = $row->[1];
627 # calculate overhad for one file in tar
629 sub tar_overhead($) {
630 my $name = shift || '';
632 # header, padding of file and two null blocks at end
633 my $len = 4 * $tar_header_length;
635 # if filename is longer than 99 chars subtract blocks for
637 if ( length($name) > 99 ) {
638 $len += int( ( length($name) + $tar_header_length ) / $tar_header_length ) * $tar_header_length;
649 my($hdr, $fh, $tarPathOverride) = @_;
651 my $tarPath = $hdr->{relPath};
652 $tarPath = $tarPathOverride if ( defined($tarPathOverride) );
654 $tarPath =~ s{//+}{/}g;
656 #print STDERR "? $tarPath\n" if ($opts{d});
657 my $size = $in_backup_increment->{$tarPath};
658 return unless (defined($size));
660 # is this file too large to fit into ArchiveChunkSize?
662 if ( ($current_tar_size + tar_overhead($tarPath) + $size) > $max_file_size ) {
663 print STDERR "# tar file $current_tar_size + $tar_header_length + $size > $max_file_size, splitting\n" if ($opts{d});
667 #print STDERR "A $tarPath [$size] tell: $current_tar_size\n" if ($opts{d});
670 if ( defined($PathRemove)
671 && substr($tarPath, 0, length($PathRemove)) eq $PathRemove ) {
672 substr($tarPath, 0, length($PathRemove)) = $PathAdd;
674 $tarPath = "./" . $tarPath if ( $tarPath !~ /^\.\// );
675 $tarPath =~ s{//+}{/}g;
676 $hdr->{name} = $tarPath;
678 if ( $hdr->{type} == BPC_FTYPE_DIR ) {
680 # Directory: just write the header
682 $hdr->{name} .= "/" if ( $hdr->{name} !~ m{/$} );
683 TarWriteFileInfo($fh, $hdr);
685 } elsif ( $hdr->{type} == BPC_FTYPE_FILE ) {
687 # Regular file: write the header and file
689 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});
690 if ( !defined($f) ) {
691 print(STDERR "Unable to open file $hdr->{fullPath}\n");
695 # do we need to split file?
696 if ($hdr->{size} < $max_file_size) {
697 TarWriteFileInfo($fh, $hdr);
699 while ( $f->read(\$data, $BufSize) > 0 ) {
700 TarWrite($fh, \$data);
701 $size += length($data);
704 TarWritePad($fh, $size);
708 my $full_size = $hdr->{size};
709 my $orig_name = $hdr->{name};
710 my $max_part_size = $max_file_size - tar_overhead($hdr->{name});
712 my $parts = int(($full_size + $max_part_size - 1) / $max_part_size);
713 print STDERR "# splitting $orig_name [$full_size bytes] into $parts parts\n" if ($opts{d});
714 foreach my $subpart ( 1 .. $parts ) {
716 if ($subpart < $parts) {
717 $hdr->{size} = $max_part_size;
719 $hdr->{size} = $full_size % $max_part_size;
721 $hdr->{name} = $orig_name . '/' . $subpart;
722 print STDERR "## creating part $subpart ",$hdr->{name}, " [", $hdr->{size}," bytes]\n";
724 TarWriteFileInfo($fh, $hdr);
727 for ( 1 .. int($hdr->{size} / $BufSize) ) {
728 my $r_size = $f->read(\$data, $BufSize);
729 die "expected $BufSize bytes read, got $r_size bytes!" if ($r_size != $BufSize);
730 TarWrite($fh, \$data);
731 $size += length($data);
734 my $size_left = $hdr->{size} % $BufSize;
735 my $r_size = $f->read(\$data, $size_left);
736 die "expected $size_left bytes last read, got $r_size bytes!" if ($r_size != $size_left);
738 TarWrite($fh, \$data);
739 $size += length($data);
740 TarWritePad($fh, $size);
746 $ByteCnt += $full_size;
749 } elsif ( $hdr->{type} == BPC_FTYPE_HARDLINK ) {
751 # Hardlink file: either write a hardlink or the complete file
752 # depending upon whether the linked-to file will be written
755 # Start by reading the contents of the link.
757 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});
758 if ( !defined($f) ) {
759 print(STDERR "Unable to open file $hdr->{fullPath}\n");
764 while ( $f->read(\$data, $BufSize) > 0 ) {
765 $hdr->{linkname} .= $data;
769 my $name = $hdr->{linkname};
771 if ( $HardLinkExtraFiles{$name} ) {
773 # Target file will be or was written, so just remember
774 # the hardlink so we can dump it later.
776 push(@HardLinks, $hdr);
780 # Have to dump the original file. Just call the top-level
781 # routine, so that we save the hassle of dealing with
782 # mangling, merging and attributes.
784 $HardLinkExtraFiles{$hdr->{linkname}} = 1;
785 archiveWrite($fh, $hdr->{linkname}, $hdr->{name});
787 } elsif ( $hdr->{type} == BPC_FTYPE_SYMLINK ) {
789 # Symbolic link: read the symbolic link contents into the header
790 # and write the header.
792 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});
793 if ( !defined($f) ) {
794 print(STDERR "Unable to open symlink file $hdr->{fullPath}\n");
799 while ( $f->read(\$data, $BufSize) > 0 ) {
800 $hdr->{linkname} .= $data;
804 TarWriteFileInfo($fh, $hdr);
806 } elsif ( $hdr->{type} == BPC_FTYPE_CHARDEV
807 || $hdr->{type} == BPC_FTYPE_BLOCKDEV
808 || $hdr->{type} == BPC_FTYPE_FIFO ) {
810 # Special files: for char and block special we read the
811 # major and minor numbers from a plain file.
813 if ( $hdr->{type} != BPC_FTYPE_FIFO ) {
814 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0,
817 if ( !defined($f) || $f->read(\$data, $BufSize) < 0 ) {
818 print(STDERR "Unable to open/read char/block special file"
819 . " $hdr->{fullPath}\n");
820 $f->close if ( defined($f) );
825 if ( $data =~ /(\d+),(\d+)/ ) {
826 $hdr->{devmajor} = $1;
827 $hdr->{devminor} = $2;
831 TarWriteFileInfo($fh, $hdr);
834 print(STDERR "Got unknown type $hdr->{type} for $hdr->{name}\n");
839 my $t_fmt = '%Y-%m-%d %H:%M:%S';
841 return strftime($t_fmt,localtime());