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 output write buffer size in 512-byte blocks (default 20; same as tar)
25 # -w readBufSz buffer size for reading files (default 1048576 = 1MB)
26 # -e charset charset for encoding file names (default: value of
27 # $Conf{ClientCharset} when backup was done)
28 # -l just print a file listing; don't generate an archive
29 # -L just print a detailed file listing; don't generate an archive
31 # The -h, -n and -s options specify which dump is used to generate
32 # the tar archive. The -r and -p options can be used to relocate
33 # the paths in the tar archive so extracted files can be placed
34 # in a location different from their original location.
37 # Craig Barratt <cbarratt@users.sourceforge.net>
40 # Copyright (C) 2001-2009 Craig Barratt
42 # This program is free software; you can redistribute it and/or modify
43 # it under the terms of the GNU General Public License as published by
44 # the Free Software Foundation; either version 2 of the License, or
45 # (at your option) any later version.
47 # This program is distributed in the hope that it will be useful,
48 # but WITHOUT ANY WARRANTY; without even the implied warranty of
49 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50 # GNU General Public License for more details.
52 # You should have received a copy of the GNU General Public License
53 # along with this program; if not, write to the Free Software
54 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
56 #========================================================================
58 # Version 3.2.0, released 31 Jul 2010.
60 # See http://backuppc.sourceforge.net.
62 #========================================================================
66 use lib "/usr/local/BackupPC/lib";
69 use Encode qw/from_to/;
71 use BackupPC::Attrib qw(:all);
72 use BackupPC::FileZIO;
75 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
79 if ( !getopts("Llte:h:n:p:r:s:b:w:i", \%opts) || @ARGV < 1 ) {
81 usage: $0 [options] files/directories...
83 -h host host from which the tar archive is created
84 -n dumpNum dump number from which the tar archive is created
85 A negative number means relative to the end (eg -1
86 means the most recent dump, -2 2nd most recent etc).
87 -s shareName share name from which the tar archive is created
90 -t print summary totals
91 -r pathRemove path prefix that will be replaced with pathAdd
92 -p pathAdd new path prefix
93 -b BLOCKS output write buffer size in 512-byte blocks (default 20; same as tar)
94 -w readBufSz buffer size for reading files (default 1048576 = 1MB)
95 -e charset charset for encoding file names (default: value of
96 \$Conf{ClientCharset} when backup was done)
97 -l just print a file listing; don't generate an archive
98 -L just print a detailed file listing; don't generate an archive
99 -i create incremental tar dump with just new files
104 if ( $opts{h} !~ /^([\w\.\s-]+)$/
105 || $opts{h} =~ m{(^|/)\.\.(/|$)} ) {
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 $Charset = $Backups[$i]{charset};
135 $Charset = $opts{e} if ( $opts{e} ne "" );
137 my $PathRemove = $1 if ( $opts{r} =~ /(.+)/ );
138 my $PathAdd = $1 if ( $opts{p} =~ /(.+)/ );
139 if ( $opts{s} =~ m{(^|/)\.\.(/|$)} ) {
140 print(STDERR "$0: bad share name '$opts{s}'\n");
144 our $ShareName = $opts{s};
145 our $view = BackupPC::View->new($bpc, $Host, \@Backups, $opts{i} ? { only_first => 1 } : {});
148 # This constant and the line of code below that uses it are borrowed
149 # from Archive::Tar. Thanks to Calle Dybedahl and Stephen Zander.
152 # Archive::Tar is Copyright 1997 Calle Dybedahl. All rights reserved.
153 # Copyright 1998 Stephen Zander. All rights reserved.
156 = 'a100 a8 a8 a8 a12 a12 A8 a1 a100 a6 a2 a32 a32 a8 a8 a155 x12';
157 my $tar_header_length = 512;
159 my $BufSize = $opts{w} || 1048576; # 1MB or 2^20
161 my $WriteBufSz = ($opts{b} || 20) * $tar_header_length;
163 my(%UidCache, %GidCache);
164 my(%HardLinkExtraFiles, @HardLinks);
167 # Write out all the requested files/directories
171 if ( $ShareName eq "*" ) {
172 my $PathRemoveOrig = $PathRemove;
173 my $PathAddOrig = $PathAdd;
174 foreach $ShareName ( $view->shareList($Num) ) {
175 #print(STDERR "Doing share ($ShareName)\n");
176 $PathRemove = "/" if ( !defined($PathRemoveOrig) );
177 ($PathAdd = "/$ShareName/$PathAddOrig") =~ s{//+}{/}g;
178 foreach my $dir ( @ARGV ) {
179 archiveWrite($fh, $dir);
181 archiveWriteHardLinks($fh);
184 foreach my $dir ( @ARGV ) {
185 archiveWrite($fh, $dir);
187 archiveWriteHardLinks($fh);
190 if ( !$opts{l} && !$opts{L} ) {
192 # Finish with two null 512 byte headers, and then round out a full
195 my $data = "\0" x ($tar_header_length * 2);
196 TarWrite($fh, \$data);
197 TarWrite($fh, undef);
201 # print out totals if requested
204 print STDERR "Done: $FileCnt files, $ByteCnt bytes, $DirCnt dirs,",
205 " $SpecialCnt specials, $ErrorCnt errors\n";
207 if ( $ErrorCnt && !$FileCnt && !$DirCnt ) {
209 # Got errors, with no files or directories; exit with non-zero
216 ###########################################################################
218 ###########################################################################
222 my($fh, $dir, $tarPathOverride) = @_;
224 if ( $dir =~ m{(^|/)\.\.(/|$)} ) {
225 print(STDERR "$0: bad directory '$dir'\n");
229 $dir = "/" if ( $dir eq "." );
230 #print(STDERR "calling find with $Num, $ShareName, $dir\n");
231 if ( $view->find($Num, $ShareName, $dir, 0, \&TarWriteFile,
232 $fh, $tarPathOverride) < 0 ) {
233 print(STDERR "$0: bad share or directory '$ShareName/$dir'\n");
240 # Write out any hardlinks (if any)
242 sub archiveWriteHardLinks
245 foreach my $hdr ( @HardLinks ) {
247 my $name = $hdr->{linkname};
249 if ( defined($HardLinkExtraFiles{$name}) ) {
250 $hdr->{linkname} = $HardLinkExtraFiles{$name};
252 if ( defined($PathRemove)
253 && substr($hdr->{linkname}, 0, length($PathRemove)+1)
254 eq ".$PathRemove" ) {
255 substr($hdr->{linkname}, 0, length($PathRemove)+1) = ".$PathAdd";
257 TarWriteFileInfo($fh, $hdr);
260 %HardLinkExtraFiles = ();
267 $UidCache{$uid} = (getpwuid($uid))[0] if ( !exists($UidCache{$uid}) );
268 return $UidCache{$uid};
275 $GidCache{$gid} = (getgrgid($gid))[0] if ( !exists($GidCache{$gid}) );
276 return $GidCache{$gid};
281 my($fh, $dataRef) = @_;
283 if ( !defined($dataRef) ) {
285 # do flush by padding to a full $WriteBufSz
287 my $data = "\0" x ($WriteBufSz - length($WriteBuf));
290 if ( length($WriteBuf) + length($$dataRef) < $WriteBufSz ) {
292 # just buffer and return
294 $WriteBuf .= $$dataRef;
297 my $done = $WriteBufSz - length($WriteBuf);
298 if ( syswrite($fh, $WriteBuf . substr($$dataRef, 0, $done))
300 print(STDERR "Unable to write to output file ($!)\n");
303 while ( $done + $WriteBufSz <= length($$dataRef) ) {
304 if ( syswrite($fh, substr($$dataRef, $done, $WriteBufSz))
306 print(STDERR "Unable to write to output file ($!)\n");
309 $done += $WriteBufSz;
311 $WriteBuf = substr($$dataRef, $done);
318 if ( $size % $tar_header_length ) {
319 my $data = "\0" x ($tar_header_length - ($size % $tar_header_length));
320 TarWrite($fh, \$data);
328 $hdr->{uname} = UidLookup($hdr->{uid}) if ( !defined($hdr->{uname}) );
329 $hdr->{gname} = GidLookup($hdr->{gid}) if ( !defined($hdr->{gname}) );
330 my $devmajor = defined($hdr->{devmajor}) ? sprintf("%07o", $hdr->{devmajor})
332 my $devminor = defined($hdr->{devminor}) ? sprintf("%07o", $hdr->{devminor})
335 if ( $hdr->{size} >= 2 * 65536 * 65536 ) {
337 # GNU extension for files >= 8GB: send size in big-endian binary
339 $sizeStr = pack("c4 N N", 0x80, 0, 0, 0,
340 $hdr->{size} / (65536 * 65536),
341 $hdr->{size} % (65536 * 65536));
342 } elsif ( $hdr->{size} >= 1 * 65536 * 65536 ) {
344 # sprintf octal only handles up to 2^32 - 1
346 $sizeStr = sprintf("%03o", $hdr->{size} / (1 << 24))
347 . sprintf("%08o", $hdr->{size} % (1 << 24));
349 $sizeStr = sprintf("%011o", $hdr->{size});
351 my $data = pack($tar_pack_header,
352 substr($hdr->{name}, 0, 99),
353 sprintf("%07o", $hdr->{mode}),
354 sprintf("%07o", $hdr->{uid}),
355 sprintf("%07o", $hdr->{gid}),
357 sprintf("%011o", $hdr->{mtime}),
358 "", #checksum field - space padded by pack("A8")
360 substr($hdr->{linkname}, 0, 99),
361 $hdr->{magic} || 'ustar ',
362 $hdr->{version} || ' ',
369 substr($data, 148, 7) = sprintf("%06o\0", unpack("%16C*",$data));
370 TarWrite($fh, \$data);
378 # Convert path names to requested (eg: client) charset
380 if ( $Charset ne "" ) {
381 from_to($hdr->{name}, "utf8", $Charset);
382 from_to($hdr->{linkname}, "utf8", $Charset);
386 print($hdr->{name} . "\n");
388 } elsif ( $opts{L} ) {
389 my $owner = "$hdr->{uid}/$hdr->{gid}";
391 my $name = $hdr->{name};
393 if ( $hdr->{type} == BPC_FTYPE_SYMLINK
394 || $hdr->{type} == BPC_FTYPE_HARDLINK ) {
395 $name .= " -> $hdr->{linkname}";
399 printf("%6o %9s %11.0f %s\n",
408 # Handle long link names (symbolic links)
410 if ( length($hdr->{linkname}) > 99 ) {
412 my $data = $hdr->{linkname} . "\0";
413 $h{name} = "././\@LongLink";
415 $h{size} = length($data);
416 TarWriteHeader($fh, \%h);
417 TarWrite($fh, \$data);
418 TarWritePad($fh, length($data));
422 # Handle long file names
424 if ( length($hdr->{name}) > 99 ) {
426 my $data = $hdr->{name} . "\0";
427 $h{name} = "././\@LongLink";
429 $h{size} = length($data);
430 TarWriteHeader($fh, \%h);
431 TarWrite($fh, \$data);
432 TarWritePad($fh, length($data));
434 TarWriteHeader($fh, $hdr);
442 my($hdr, $fh, $tarPathOverride) = @_;
444 my $tarPath = $hdr->{relPath};
445 $tarPath = $tarPathOverride if ( defined($tarPathOverride) );
447 $tarPath =~ s{//+}{/}g;
448 if ( defined($PathRemove)
449 && substr($tarPath, 0, length($PathRemove)) eq $PathRemove ) {
450 substr($tarPath, 0, length($PathRemove)) = $PathAdd;
452 $tarPath = "./" . $tarPath if ( $tarPath !~ /^\.\// );
453 $tarPath =~ s{//+}{/}g;
454 $hdr->{name} = $tarPath;
456 if ( $hdr->{type} == BPC_FTYPE_DIR ) {
458 # Directory: just write the header
460 $hdr->{name} .= "/" if ( $hdr->{name} !~ m{/$} );
461 TarWriteFileInfo($fh, $hdr);
463 } elsif ( $hdr->{type} == BPC_FTYPE_FILE ) {
466 # Regular file: write the header and file
468 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});
469 if ( !defined($f) ) {
470 print(STDERR "Unable to open file $hdr->{fullPath}\n");
474 TarWriteFileInfo($fh, $hdr);
475 if ( $opts{l} || $opts{L} ) {
476 $size = $hdr->{size};
478 while ( $f->read(\$data, $BufSize) > 0 ) {
479 if ( $size + length($data) > $hdr->{size} ) {
480 print(STDERR "Error: truncating $hdr->{fullPath} to"
481 . " $hdr->{size} bytes\n");
482 $data = substr($data, 0, $hdr->{size} - $size);
485 TarWrite($fh, \$data);
486 $size += length($data);
489 if ( $size != $hdr->{size} ) {
490 print(STDERR "Error: padding $hdr->{fullPath} to $hdr->{size}"
491 . " bytes from $size bytes\n");
493 while ( $size < $hdr->{size} ) {
494 my $len = $hdr->{size} - $size;
495 $len = $BufSize if ( $len > $BufSize );
497 TarWrite($fh, \$data);
501 TarWritePad($fh, $size);
505 } elsif ( $hdr->{type} == BPC_FTYPE_HARDLINK ) {
507 # Hardlink file: either write a hardlink or the complete file
508 # depending upon whether the linked-to file will be written
511 # Start by reading the contents of the link.
513 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});
514 if ( !defined($f) ) {
515 print(STDERR "Unable to open file $hdr->{fullPath}\n");
520 while ( $f->read(\$data, $BufSize) > 0 ) {
521 $hdr->{linkname} .= $data;
525 # Check @ARGV and the list of hardlinked files we have explicity
526 # dumped to see if we have dumped this file or not
529 my $name = $hdr->{linkname};
531 if ( defined($HardLinkExtraFiles{$name}) ) {
534 foreach my $arg ( @ARGV ) {
535 $arg = "/" if ( $arg eq "." );
538 $done = 1 if ( $name eq $arg || $name =~ /^\Q$arg\// || $arg eq "" );
543 # Target file will be or was written, so just remember
544 # the hardlink so we can dump it later.
546 push(@HardLinks, $hdr);
550 # Have to dump the original file. Just call the top-level
551 # routine, so that we save the hassle of dealing with
552 # mangling, merging and attributes.
554 my $name = $hdr->{linkname};
556 $HardLinkExtraFiles{$name} = $hdr->{name};
557 archiveWrite($fh, $name, $hdr->{name});
559 } elsif ( $hdr->{type} == BPC_FTYPE_SYMLINK ) {
561 # Symbolic link: read the symbolic link contents into the header
562 # and write the header.
564 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});
565 if ( !defined($f) ) {
566 print(STDERR "Unable to open symlink file $hdr->{fullPath}\n");
571 while ( $f->read(\$data, $BufSize) > 0 ) {
572 $hdr->{linkname} .= $data;
576 TarWriteFileInfo($fh, $hdr);
578 } elsif ( $hdr->{type} == BPC_FTYPE_CHARDEV
579 || $hdr->{type} == BPC_FTYPE_BLOCKDEV
580 || $hdr->{type} == BPC_FTYPE_FIFO ) {
582 # Special files: for char and block special we read the
583 # major and minor numbers from a plain file.
585 if ( $hdr->{type} != BPC_FTYPE_FIFO ) {
586 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0,
589 if ( !defined($f) || $f->read(\$data, $BufSize) < 0 ) {
590 print(STDERR "Unable to open/read char/block special file"
591 . " $hdr->{fullPath}\n");
592 $f->close if ( defined($f) );
597 if ( $data =~ /(\d+),(\d+)/ ) {
598 $hdr->{devmajor} = $1;
599 $hdr->{devminor} = $2;
603 TarWriteFileInfo($fh, $hdr);
605 } elsif ( $hdr->{type} == BPC_FTYPE_SOCKET
606 || $hdr->{type} == BPC_FTYPE_UNKNOWN ) {
608 # ignore these two file types - these are dynamic file types created
609 # by applications as needed
612 print(STDERR "Got unknown type $hdr->{type} for $hdr->{name}\n");