2 #============================================================= -*-perl-*-
4 # BackupPC_tarCreate: create a tar archive of an existing dump
5 # for restore on a client.
9 # Usage: BackupPC_tarCreate [options] files/directories...
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)
26 # -e charset charset for encoding file names (default: value of
27 # $Conf{ClientCharset} when backup was done)
29 # The -h, -n and -s options specify which dump is used to generate
30 # the tar archive. The -r and -p options can be used to relocate
31 # the paths in the tar archive so extracted files can be placed
32 # in a location different from their original location.
35 # Craig Barratt <cbarratt@users.sourceforge.net>
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 3.0.0alpha, released 23 Jan 2006.
58 # See http://backuppc.sourceforge.net.
60 #========================================================================
64 use lib "/usr/local/BackupPC/lib";
67 use Encode qw/from_to/;
69 use BackupPC::Attrib qw(:all);
70 use BackupPC::FileZIO;
73 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
77 if ( !getopts("te:h:n:p:r:s:b:w:", \%opts) || @ARGV < 1 ) {
79 usage: $0 [options] files/directories...
81 -h host host from which the tar archive is created
82 -n dumpNum dump number from which the tar archive is created
83 A negative number means relative to the end (eg -1
84 means the most recent dump, -2 2nd most recent etc).
85 -s shareName share name from which the tar archive is created
88 -t print summary totals
89 -r pathRemove path prefix that will be replaced with pathAdd
90 -p pathAdd new path prefix
91 -b BLOCKS BLOCKS x 512 bytes per record (default 20; same as tar)
92 -w writeBufSz write buffer size (default 1048576 = 1MB)
93 -e charset charset for encoding file names (default: value of
94 \$Conf{ClientCharset} when backup was done)
99 if ( $opts{h} !~ /^([\w\.\s-]+)$/ ) {
100 print(STDERR "$0: bad host name '$opts{h}'\n");
105 if ( $opts{n} !~ /^(-?\d+)$/ ) {
106 print(STDERR "$0: bad dump number '$opts{n}'\n");
111 my @Backups = $bpc->BackupInfoRead($Host);
119 $Num = $Backups[@Backups + $Num]{num} if ( -@Backups <= $Num && $Num < 0 );
120 for ( $i = 0 ; $i < @Backups ; $i++ ) {
121 last if ( $Backups[$i]{num} == $Num );
123 if ( $i >= @Backups ) {
124 print(STDERR "$0: bad backup number $Num for host $Host\n");
128 my $Charset = $Backups[$i]{charset};
129 $Charset = $opts{e} if ( $opts{e} ne "" );
131 my $PathRemove = $1 if ( $opts{r} =~ /(.+)/ );
132 my $PathAdd = $1 if ( $opts{p} =~ /(.+)/ );
133 if ( $opts{s} !~ /^([\w\s.\/$(){}[\]-]+)$/ && $opts{s} ne "*" ) {
134 print(STDERR "$0: bad share name '$opts{s}'\n");
138 our $ShareName = $opts{s};
139 our $view = BackupPC::View->new($bpc, $Host, \@Backups);
142 # This constant and the line of code below that uses it are borrowed
143 # from Archive::Tar. Thanks to Calle Dybedahl and Stephen Zander.
146 # Archive::Tar is Copyright 1997 Calle Dybedahl. All rights reserved.
147 # Copyright 1998 Stephen Zander. All rights reserved.
150 = 'a100 a8 a8 a8 a12 a12 A8 a1 a100 a6 a2 a32 a32 a8 a8 a155 x12';
151 my $tar_header_length = 512;
153 my $BufSize = $opts{w} || 1048576; # 1MB or 2^20
155 my $WriteBufSz = ($opts{b} || 20) * $tar_header_length;
157 my(%UidCache, %GidCache);
158 my(%HardLinkExtraFiles, @HardLinks);
161 # Write out all the requested files/directories
165 if ( $ShareName eq "*" ) {
166 my $PathRemoveOrig = $PathRemove;
167 my $PathAddOrig = $PathAdd;
168 foreach $ShareName ( $view->shareList($Num) ) {
169 #print(STDERR "Doing share ($ShareName)\n");
170 $PathRemove = "/" if ( !defined($PathRemoveOrig) );
171 ($PathAdd = "/$ShareName/$PathAddOrig") =~ s{//+}{/}g;
172 foreach my $dir ( @ARGV ) {
173 archiveWrite($fh, $dir);
175 archiveWriteHardLinks($fh);
178 foreach my $dir ( @ARGV ) {
179 archiveWrite($fh, $dir);
181 archiveWriteHardLinks($fh);
185 # Finish with two null 512 byte headers, and then round out a full
188 my $data = "\0" x ($tar_header_length * 2);
189 TarWrite($fh, \$data);
190 TarWrite($fh, undef);
193 # print out totals if requested
196 print STDERR "Done: $FileCnt files, $ByteCnt bytes, $DirCnt dirs,",
197 " $SpecialCnt specials, $ErrorCnt errors\n";
199 if ( $ErrorCnt && !$FileCnt && !$DirCnt ) {
201 # Got errors, with no files or directories; exit with non-zero
208 ###########################################################################
210 ###########################################################################
214 my($fh, $dir, $tarPathOverride) = @_;
216 if ( $dir =~ m{(^|/)\.\.(/|$)} ) {
217 print(STDERR "$0: bad directory '$dir'\n");
221 $dir = "/" if ( $dir eq "." );
222 #print(STDERR "calling find with $Num, $ShareName, $dir\n");
223 if ( $view->find($Num, $ShareName, $dir, 0, \&TarWriteFile,
224 $fh, $tarPathOverride) < 0 ) {
225 print(STDERR "$0: bad share or directory '$ShareName/$dir'\n");
232 # Write out any hardlinks (if any)
234 sub archiveWriteHardLinks
237 foreach my $hdr ( @HardLinks ) {
239 my $name = $hdr->{linkname};
241 if ( defined($HardLinkExtraFiles{$name}) ) {
242 $hdr->{linkname} = $HardLinkExtraFiles{$name};
244 if ( defined($PathRemove)
245 && substr($hdr->{linkname}, 0, length($PathRemove)+1)
246 eq ".$PathRemove" ) {
247 substr($hdr->{linkname}, 0, length($PathRemove)+1) = ".$PathAdd";
249 TarWriteFileInfo($fh, $hdr);
252 %HardLinkExtraFiles = ();
259 $UidCache{$uid} = (getpwuid($uid))[0] if ( !exists($UidCache{$uid}) );
260 return $UidCache{$uid};
267 $GidCache{$gid} = (getgrgid($gid))[0] if ( !exists($GidCache{$gid}) );
268 return $GidCache{$gid};
273 my($fh, $dataRef) = @_;
275 if ( !defined($dataRef) ) {
277 # do flush by padding to a full $WriteBufSz
279 my $data = "\0" x ($WriteBufSz - length($WriteBuf));
282 if ( length($WriteBuf) + length($$dataRef) < $WriteBufSz ) {
284 # just buffer and return
286 $WriteBuf .= $$dataRef;
289 my $done = $WriteBufSz - length($WriteBuf);
290 if ( syswrite($fh, $WriteBuf . substr($$dataRef, 0, $done))
292 print(STDERR "Unable to write to output file ($!)\n");
295 while ( $done + $WriteBufSz <= length($$dataRef) ) {
296 if ( syswrite($fh, substr($$dataRef, $done, $WriteBufSz))
298 print(STDERR "Unable to write to output file ($!)\n");
301 $done += $WriteBufSz;
303 $WriteBuf = substr($$dataRef, $done);
310 if ( $size % $tar_header_length ) {
311 my $data = "\0" x ($tar_header_length - ($size % $tar_header_length));
312 TarWrite($fh, \$data);
320 $hdr->{uname} = UidLookup($hdr->{uid}) if ( !defined($hdr->{uname}) );
321 $hdr->{gname} = GidLookup($hdr->{gid}) if ( !defined($hdr->{gname}) );
322 my $devmajor = defined($hdr->{devmajor}) ? sprintf("%07o", $hdr->{devmajor})
324 my $devminor = defined($hdr->{devminor}) ? sprintf("%07o", $hdr->{devminor})
327 if ( $hdr->{size} >= 2 * 65536 * 65536 ) {
329 # GNU extension for files >= 8GB: send size in big-endian binary
331 $sizeStr = pack("c4 N N", 0x80, 0, 0, 0,
332 $hdr->{size} / (65536 * 65536),
333 $hdr->{size} % (65536 * 65536));
334 } elsif ( $hdr->{size} >= 1 * 65536 * 65536 ) {
336 # sprintf octal only handles up to 2^32 - 1
338 $sizeStr = sprintf("%03o", $hdr->{size} / (1 << 24))
339 . sprintf("%08o", $hdr->{size} % (1 << 24));
341 $sizeStr = sprintf("%011o", $hdr->{size});
343 my $data = pack($tar_pack_header,
344 substr($hdr->{name}, 0, 99),
345 sprintf("%07o", $hdr->{mode}),
346 sprintf("%07o", $hdr->{uid}),
347 sprintf("%07o", $hdr->{gid}),
349 sprintf("%011o", $hdr->{mtime}),
350 "", #checksum field - space padded by pack("A8")
352 substr($hdr->{linkname}, 0, 99),
353 $hdr->{magic} || 'ustar ',
354 $hdr->{version} || ' ',
361 substr($data, 148, 7) = sprintf("%06o\0", unpack("%16C*",$data));
362 TarWrite($fh, \$data);
370 # Convert path names to requested (eg: client) charset
372 if ( $Charset ne "" ) {
373 from_to($hdr->{name}, "utf8", $Charset);
374 from_to($hdr->{linkname}, "utf8", $Charset);
378 # Handle long link names (symbolic links)
380 if ( length($hdr->{linkname}) > 99 ) {
382 my $data = $hdr->{linkname} . "\0";
383 $h{name} = "././\@LongLink";
385 $h{size} = length($data);
386 TarWriteHeader($fh, \%h);
387 TarWrite($fh, \$data);
388 TarWritePad($fh, length($data));
392 # Handle long file names
394 if ( length($hdr->{name}) > 99 ) {
396 my $data = $hdr->{name} . "\0";
397 $h{name} = "././\@LongLink";
399 $h{size} = length($data);
400 TarWriteHeader($fh, \%h);
401 TarWrite($fh, \$data);
402 TarWritePad($fh, length($data));
404 TarWriteHeader($fh, $hdr);
412 my($hdr, $fh, $tarPathOverride) = @_;
414 my $tarPath = $hdr->{relPath};
415 $tarPath = $tarPathOverride if ( defined($tarPathOverride) );
417 $tarPath =~ s{//+}{/}g;
418 if ( defined($PathRemove)
419 && substr($tarPath, 0, length($PathRemove)) eq $PathRemove ) {
420 substr($tarPath, 0, length($PathRemove)) = $PathAdd;
422 $tarPath = "./" . $tarPath if ( $tarPath !~ /^\.\// );
423 $tarPath =~ s{//+}{/}g;
424 $hdr->{name} = $tarPath;
426 if ( $hdr->{type} == BPC_FTYPE_DIR ) {
428 # Directory: just write the header
430 $hdr->{name} .= "/" if ( $hdr->{name} !~ m{/$} );
431 TarWriteFileInfo($fh, $hdr);
433 } elsif ( $hdr->{type} == BPC_FTYPE_FILE ) {
435 # Regular file: write the header and file
437 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});
438 if ( !defined($f) ) {
439 print(STDERR "Unable to open file $hdr->{fullPath}\n");
443 TarWriteFileInfo($fh, $hdr);
445 while ( $f->read(\$data, $BufSize) > 0 ) {
446 if ( $size + length($data) > $hdr->{size} ) {
447 print(STDERR "Error: truncating $hdr->{fullPath} to"
448 . " $hdr->{size} bytes\n");
449 $data = substr($data, 0, $hdr->{size} - $size);
452 TarWrite($fh, \$data);
453 $size += length($data);
456 if ( $size != $hdr->{size} ) {
457 print(STDERR "Error: padding $hdr->{fullPath} to $hdr->{size}"
458 . " bytes from $size bytes\n");
460 while ( $size < $hdr->{size} ) {
461 my $len = $hdr->{size} - $size;
462 $len = $BufSize if ( $len > $BufSize );
464 TarWrite($fh, \$data);
468 TarWritePad($fh, $size);
471 } elsif ( $hdr->{type} == BPC_FTYPE_HARDLINK ) {
473 # Hardlink file: either write a hardlink or the complete file
474 # depending upon whether the linked-to file will be written
477 # Start by reading the contents of the link.
479 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});
480 if ( !defined($f) ) {
481 print(STDERR "Unable to open file $hdr->{fullPath}\n");
486 while ( $f->read(\$data, $BufSize) > 0 ) {
487 $hdr->{linkname} .= $data;
491 # Check @ARGV and the list of hardlinked files we have explicity
492 # dumped to see if we have dumped this file or not
495 my $name = $hdr->{linkname};
497 if ( defined($HardLinkExtraFiles{$name}) ) {
500 foreach my $arg ( @ARGV ) {
501 $arg = "/" if ( $arg eq "." );
504 $done = 1 if ( $name eq $arg || $name =~ /^\Q$arg\// );
509 # Target file will be or was written, so just remember
510 # the hardlink so we can dump it later.
512 push(@HardLinks, $hdr);
516 # Have to dump the original file. Just call the top-level
517 # routine, so that we save the hassle of dealing with
518 # mangling, merging and attributes.
520 my $name = $hdr->{linkname};
522 $HardLinkExtraFiles{$name} = $hdr->{name};
523 archiveWrite($fh, $name, $hdr->{name});
525 } elsif ( $hdr->{type} == BPC_FTYPE_SYMLINK ) {
527 # Symbolic link: read the symbolic link contents into the header
528 # and write the header.
530 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});
531 if ( !defined($f) ) {
532 print(STDERR "Unable to open symlink file $hdr->{fullPath}\n");
537 while ( $f->read(\$data, $BufSize) > 0 ) {
538 $hdr->{linkname} .= $data;
542 TarWriteFileInfo($fh, $hdr);
544 } elsif ( $hdr->{type} == BPC_FTYPE_CHARDEV
545 || $hdr->{type} == BPC_FTYPE_BLOCKDEV
546 || $hdr->{type} == BPC_FTYPE_FIFO ) {
548 # Special files: for char and block special we read the
549 # major and minor numbers from a plain file.
551 if ( $hdr->{type} != BPC_FTYPE_FIFO ) {
552 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0,
555 if ( !defined($f) || $f->read(\$data, $BufSize) < 0 ) {
556 print(STDERR "Unable to open/read char/block special file"
557 . " $hdr->{fullPath}\n");
558 $f->close if ( defined($f) );
563 if ( $data =~ /(\d+),(\d+)/ ) {
564 $hdr->{devmajor} = $1;
565 $hdr->{devminor} = $2;
569 TarWriteFileInfo($fh, $hdr);
572 print(STDERR "Got unknown type $hdr->{type} for $hdr->{name}\n");