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 "__INSTALLDIR__/lib";
69 use BackupPC::Attrib qw(:all);
70 use BackupPC::FileZIO;
72 use BackupPC::SearchLib;
73 use Time::HiRes qw/time/;
74 use POSIX qw/strftime/;
75 use Data::Dumper; ### FIXME
77 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
78 my $TopDir = $bpc->TopDir();
79 my $BinDir = $bpc->BinDir();
80 my %Conf = $bpc->Conf();
82 my $in_backup_increment;
85 if ( !getopts("th:n:p:r:s:b:w:", \%opts) ) {
89 -h host host from which the tar archive is created
90 -n dumpNum dump number from which the tar archive is created
91 A negative number means relative to the end (eg -1
92 means the most recent dump, -2 2nd most recent etc).
93 -s shareName share name from which the tar archive is created
96 -t print summary totals
97 -r pathRemove path prefix that will be replaced with pathAdd
98 -p pathAdd new path prefix
99 -b BLOCKS BLOCKS x 512 bytes per record (default 20; same as tar)
100 -w writeBufSz write buffer size (default 1048576 = 1MB)
105 if ( $opts{h} !~ /^([\w\.\s-]+)$/ ) {
106 print(STDERR "$0: bad host name '$opts{h}'\n");
111 if ( $opts{n} !~ /^(-?\d+)$/ ) {
112 print(STDERR "$0: bad dump number '$opts{n}'\n");
117 my @Backups = $bpc->BackupInfoRead($Host);
125 $Num = $Backups[@Backups + $Num]{num} if ( -@Backups <= $Num && $Num < 0 );
126 for ( $i = 0 ; $i < @Backups ; $i++ ) {
127 last if ( $Backups[$i]{num} == $Num );
129 if ( $i >= @Backups ) {
130 print(STDERR "$0: bad backup number $Num for host $Host\n");
134 my $PathRemove = $1 if ( $opts{r} =~ /(.+)/ );
135 my $PathAdd = $1 if ( $opts{p} =~ /(.+)/ );
136 if ( $opts{s} !~ /^([\w\s\.\/\$-]+)$/ && $opts{s} ne "*" ) {
137 print(STDERR "$0: bad share name '$opts{s}'\n");
140 our $ShareName = $opts{s};
141 our $view = BackupPC::View->new($bpc, $Host, \@Backups);
144 # This constant and the line of code below that uses it are borrowed
145 # from Archive::Tar. Thanks to Calle Dybedahl and Stephen Zander.
148 # Archive::Tar is Copyright 1997 Calle Dybedahl. All rights reserved.
149 # Copyright 1998 Stephen Zander. All rights reserved.
152 = 'a100 a8 a8 a8 a12 a12 A8 a1 a100 a6 a2 a32 a32 a8 a8 a155 x12';
153 my $tar_header_length = 512;
155 my $BufSize = $opts{w} || 1048576; # 1MB or 2^20
157 my $WriteBufSz = ($opts{b} || 20) * $tar_header_length;
159 my(%UidCache, %GidCache);
160 my(%HardLinkExtraFiles, @HardLinks);
163 # Write out all the requested files/directories
168 if (seedCache($Host, $ShareName, $Num)) {
169 archiveWrite($fh, '/');
170 archiveWriteHardLinks($fh);
172 print STDERR "NOTE: no files found for $Host:$ShareName, increment $Num\n";
176 # Finish with two null 512 byte headers, and then round out a full
179 my $data = "\0" x ($tar_header_length * 2);
180 TarWrite($fh, \$data);
181 TarWrite($fh, undef);
184 # print out totals if requested
187 print STDERR "Done: $FileCnt files, $ByteCnt bytes, $DirCnt dirs,",
188 " $SpecialCnt specials, $ErrorCnt errors\n";
190 if ( $ErrorCnt && !$FileCnt && !$DirCnt ) {
192 # Got errors, with no files or directories; exit with non-zero
199 ###########################################################################
201 ###########################################################################
205 my($fh, $dir, $tarPathOverride) = @_;
207 if ( $dir =~ m{(^|/)\.\.(/|$)} ) {
208 print(STDERR "$0: bad directory '$dir'\n");
212 $dir = "/" if ( $dir eq "." );
213 #print(STDERR "calling find with $Num, $ShareName, $dir\n");
215 if ( $view->find($Num, $ShareName, $dir, 0, \&TarWriteFile,
216 $fh, $tarPathOverride) < 0 ) {
217 print(STDERR "$0: bad share or directory '$ShareName/$dir'\n");
224 # Write out any hardlinks (if any)
226 sub archiveWriteHardLinks
229 foreach my $hdr ( @HardLinks ) {
231 if ( defined($PathRemove)
232 && substr($hdr->{linkname}, 0, length($PathRemove)+1)
233 eq ".$PathRemove" ) {
234 substr($hdr->{linkname}, 0, length($PathRemove)+1) = ".$PathAdd";
236 TarWriteFileInfo($fh, $hdr);
239 %HardLinkExtraFiles = ();
246 $UidCache{$uid} = (getpwuid($uid))[0] if ( !exists($UidCache{$uid}) );
247 return $UidCache{$uid};
254 $GidCache{$gid} = (getgrgid($gid))[0] if ( !exists($GidCache{$gid}) );
255 return $GidCache{$gid};
260 my($fh, $dataRef) = @_;
262 if ( !defined($dataRef) ) {
264 # do flush by padding to a full $WriteBufSz
266 my $data = "\0" x ($WriteBufSz - length($WriteBuf));
269 if ( length($WriteBuf) + length($$dataRef) < $WriteBufSz ) {
271 # just buffer and return
273 $WriteBuf .= $$dataRef;
276 my $done = $WriteBufSz - length($WriteBuf);
277 if ( syswrite($fh, $WriteBuf . substr($$dataRef, 0, $done))
279 print(STDERR "Unable to write to output file ($!)\n");
282 while ( $done + $WriteBufSz <= length($$dataRef) ) {
283 if ( syswrite($fh, substr($$dataRef, $done, $WriteBufSz))
285 print(STDERR "Unable to write to output file ($!)\n");
288 $done += $WriteBufSz;
290 $WriteBuf = substr($$dataRef, $done);
297 if ( $size % $tar_header_length ) {
298 my $data = "\0" x ($tar_header_length - ($size % $tar_header_length));
299 TarWrite($fh, \$data);
307 $hdr->{uname} = UidLookup($hdr->{uid}) if ( !defined($hdr->{uname}) );
308 $hdr->{gname} = GidLookup($hdr->{gid}) if ( !defined($hdr->{gname}) );
309 my $devmajor = defined($hdr->{devmajor}) ? sprintf("%07o", $hdr->{devmajor})
311 my $devminor = defined($hdr->{devminor}) ? sprintf("%07o", $hdr->{devminor})
314 if ( $hdr->{size} >= 2 * 65536 * 65536 ) {
316 # GNU extension for files >= 8GB: send size in big-endian binary
318 $sizeStr = pack("c4 N N", 0x80, 0, 0, 0,
319 $hdr->{size} / (65536 * 65536),
320 $hdr->{size} % (65536 * 65536));
321 } elsif ( $hdr->{size} >= 1 * 65536 * 65536 ) {
323 # sprintf octal only handles up to 2^32 - 1
325 $sizeStr = sprintf("%03o", $hdr->{size} / (1 << 24))
326 . sprintf("%08o", $hdr->{size} % (1 << 24));
328 $sizeStr = sprintf("%011o", $hdr->{size});
330 my $data = pack($tar_pack_header,
331 substr($hdr->{name}, 0, 99),
332 sprintf("%07o", $hdr->{mode}),
333 sprintf("%07o", $hdr->{uid}),
334 sprintf("%07o", $hdr->{gid}),
336 sprintf("%011o", $hdr->{mtime}),
337 "", #checksum field - space padded by pack("A8")
339 substr($hdr->{linkname}, 0, 99),
340 $hdr->{magic} || 'ustar ',
341 $hdr->{version} || ' ',
348 substr($data, 148, 7) = sprintf("%06o\0", unpack("%16C*",$data));
349 TarWrite($fh, \$data);
357 # Handle long link names (symbolic links)
359 if ( length($hdr->{linkname}) > 99 ) {
361 my $data = $hdr->{linkname} . "\0";
362 $h{name} = "././\@LongLink";
364 $h{size} = length($data);
365 TarWriteHeader($fh, \%h);
366 TarWrite($fh, \$data);
367 TarWritePad($fh, length($data));
370 # Handle long file names
372 if ( length($hdr->{name}) > 99 ) {
374 my $data = $hdr->{name} . "\0";
375 $h{name} = "././\@LongLink";
377 $h{size} = length($data);
378 TarWriteHeader($fh, \%h);
379 TarWrite($fh, \$data);
380 TarWritePad($fh, length($data));
382 TarWriteHeader($fh, $hdr);
386 # seed cache of files in this increment
389 my ($host, $share, $dumpNo) = @_;
391 my $dsn = $Conf{SearchDSN};
392 my $db_user = $Conf{SearchUser} || '';
394 print STDERR curr_time(), "getting files for $host:$share increment $dumpNo...";
398 JOIN shares on shares.id = shareid
399 JOIN hosts on hosts.id = shares.hostid
400 WHERE hosts.name = ? and shares.name = ? and backupnum = ?
403 my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1} );
404 my $sth = $dbh->prepare($sql);
405 $sth->execute($host, $share, $dumpNo);
406 my $count = $sth->rows;
407 print STDERR " found $count items\n";
408 while (my $row = $sth->fetchrow_arrayref) {
409 print STDERR "+ ", $row->[0],"\n";
410 $in_backup_increment->{ $row->[0] }++;
424 my($hdr, $fh, $tarPathOverride) = @_;
426 my $tarPath = $hdr->{relPath};
427 $tarPath = $tarPathOverride if ( defined($tarPathOverride) );
429 $tarPath =~ s{//+}{/}g;
431 #print STDERR "? $tarPath\n";
432 return unless ($in_backup_increment->{$tarPath});
433 print STDERR "A $tarPath\n";
435 if ( defined($PathRemove)
436 && substr($tarPath, 0, length($PathRemove)) eq $PathRemove ) {
437 substr($tarPath, 0, length($PathRemove)) = $PathAdd;
439 $tarPath = "./" . $tarPath if ( $tarPath !~ /^\.\// );
440 $tarPath =~ s{//+}{/}g;
441 $hdr->{name} = $tarPath;
443 if ( $hdr->{type} == BPC_FTYPE_DIR ) {
445 # Directory: just write the header
449 $hdr->{name} .= "/" if ( $hdr->{name} !~ m{/$} );
450 TarWriteFileInfo($fh, $hdr);
452 } elsif ( $hdr->{type} == BPC_FTYPE_FILE ) {
454 # Regular file: write the header and file
456 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});
457 if ( !defined($f) ) {
458 print(STDERR "Unable to open file $hdr->{fullPath}\n");
462 TarWriteFileInfo($fh, $hdr);
464 while ( $f->read(\$data, $BufSize) > 0 ) {
465 TarWrite($fh, \$data);
466 $size += length($data);
469 TarWritePad($fh, $size);
472 } elsif ( $hdr->{type} == BPC_FTYPE_HARDLINK ) {
474 # Hardlink file: either write a hardlink or the complete file
475 # depending upon whether the linked-to file will be written
478 # Start by reading the contents of the link.
480 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});
481 if ( !defined($f) ) {
482 print(STDERR "Unable to open file $hdr->{fullPath}\n");
487 while ( $f->read(\$data, $BufSize) > 0 ) {
488 $hdr->{linkname} .= $data;
492 my $name = $hdr->{linkname};
494 if ( $HardLinkExtraFiles{$name} ) {
496 # Target file will be or was written, so just remember
497 # the hardlink so we can dump it later.
499 push(@HardLinks, $hdr);
503 # Have to dump the original file. Just call the top-level
504 # routine, so that we save the hassle of dealing with
505 # mangling, merging and attributes.
507 $HardLinkExtraFiles{$hdr->{linkname}} = 1;
508 archiveWrite($fh, $hdr->{linkname}, $hdr->{name});
510 } elsif ( $hdr->{type} == BPC_FTYPE_SYMLINK ) {
512 # Symbolic link: read the symbolic link contents into the header
513 # and write the header.
515 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});
516 if ( !defined($f) ) {
517 print(STDERR "Unable to open symlink file $hdr->{fullPath}\n");
522 while ( $f->read(\$data, $BufSize) > 0 ) {
523 $hdr->{linkname} .= $data;
527 TarWriteFileInfo($fh, $hdr);
529 } elsif ( $hdr->{type} == BPC_FTYPE_CHARDEV
530 || $hdr->{type} == BPC_FTYPE_BLOCKDEV
531 || $hdr->{type} == BPC_FTYPE_FIFO ) {
533 # Special files: for char and block special we read the
534 # major and minor numbers from a plain file.
536 if ( $hdr->{type} != BPC_FTYPE_FIFO ) {
537 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0,
540 if ( !defined($f) || $f->read(\$data, $BufSize) < 0 ) {
541 print(STDERR "Unable to open/read char/block special file"
542 . " $hdr->{fullPath}\n");
543 $f->close if ( defined($f) );
548 if ( $data =~ /(\d+),(\d+)/ ) {
549 $hdr->{devmajor} = $1;
550 $hdr->{devminor} = $2;
554 TarWriteFileInfo($fh, $hdr);
557 print(STDERR "Got unknown type $hdr->{type} for $hdr->{name}\n");
562 my $t_fmt = '%Y-%m-%d %H:%M:%S';
564 return strftime($t_fmt,localtime());