2 #============================================================= -*-perl-*-
4 # BackupPC_tarCreate: create a tar archive of an existing dump
5 # for restore on a client.
9 # Usage: BackupPC_tarCreate [-t] [-h host] [-n dumpNum] [-s shareName]
10 # [-r pathRemove] [-p pathAdd] files/directories...
15 # -h host host from which the tar archive is created
16 # -n dumpNum dump number from which the tar archive is created
17 # -s shareName share name from which the tar archive is created
20 # -t print summary totals
21 # -r pathRemove path prefix that will be replaced with pathAdd
22 # -p pathAdd new path prefix
24 # The -h, -n and -s options specify which dump is used to generate
25 # the tar archive. The -r and -p options can be used to relocate
26 # the paths in the tar archive so extracted files can be placed
27 # in a location different from their original location.
30 # Craig Barratt <cbarratt@users.sourceforge.net>
33 # Copyright (C) 2001-2003 Craig Barratt
35 # This program is free software; you can redistribute it and/or modify
36 # it under the terms of the GNU General Public License as published by
37 # the Free Software Foundation; either version 2 of the License, or
38 # (at your option) any later version.
40 # This program is distributed in the hope that it will be useful,
41 # but WITHOUT ANY WARRANTY; without even the implied warranty of
42 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 # GNU General Public License for more details.
45 # You should have received a copy of the GNU General Public License
46 # along with this program; if not, write to the Free Software
47 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
49 #========================================================================
51 # Version 2.1.0_CVS, released 3 Jul 2003.
53 # See http://backuppc.sourceforge.net.
55 #========================================================================
59 use lib "/usr/local/BackupPC/lib";
63 use BackupPC::Attrib qw(:all);
64 use BackupPC::FileZIO;
67 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
68 my $TopDir = $bpc->TopDir();
69 my $BinDir = $bpc->BinDir();
70 my %Conf = $bpc->Conf();
74 if ( !getopts("th:n:p:r:s:", \%opts) || @ARGV < 1 ) {
75 print(STDERR "usage: $0 [-t] [-h host] [-n dumpNum] [-s shareName]"
76 . " [-r pathRemove] [-p pathAdd]"
77 . " files/directories...\n");
81 if ( $opts{h} !~ /^([\w\.\s-]+)$/ ) {
82 print(STDERR "$0: bad host name '$opts{h}'\n");
87 if ( $opts{n} !~ /^(\d+)$/ ) {
88 print(STDERR "$0: bad dump number '$opts{n}'\n");
93 my @Backups = $bpc->BackupInfoRead($Host);
101 for ( $i = 0 ; $i < @Backups ; $i++ ) {
102 last if ( $Backups[$i]{num} == $Num );
104 if ( $i >= @Backups ) {
105 print(STDERR "$0: bad backup number $Num for host $Host\n");
109 my $PathRemove = $1 if ( $opts{r} =~ /(.+)/ );
110 my $PathAdd = $1 if ( $opts{p} =~ /(.+)/ );
111 if ( $opts{s} !~ /^([\w\s\.\/\$-]+)$/ ) {
112 print(STDERR "$0: bad share name '$opts{s}'\n");
115 my $ShareName = $opts{s};
118 # This constant and the line of code below that uses it are borrowed
119 # from Archive::Tar. Thanks to Calle Dybedahl and Stephen Zander.
122 # Archive::Tar is Copyright 1997 Calle Dybedahl. All rights reserved.
123 # Copyright 1998 Stephen Zander. All rights reserved.
126 = 'a100 a8 a8 a8 a12 a12 A8 a1 a100 a6 a2 a32 a32 a8 a8 a155 x12';
127 my $tar_header_length = 512;
129 my $BufSize = 1048576; # 1MB or 2^20
131 my $WriteBufSz = 20 * $tar_header_length;
133 my(%UidCache, %GidCache);
134 my(%HardLinkExtraFiles, @HardLinks);
137 # Write out all the requested files/directories
141 foreach my $dir ( @ARGV ) {
142 archiveWrite($fh, $dir);
146 # Write out any hardlinks (if any)
148 foreach my $hdr ( @HardLinks ) {
150 if ( defined($PathRemove)
151 && substr($hdr->{linkname}, 0, length($PathRemove)+1)
152 eq ".$PathRemove" ) {
153 substr($hdr->{linkname}, 0, length($PathRemove)+1) = ".$PathAdd";
155 TarWriteFileInfo($fh, $hdr);
159 # Finish with two null 512 byte headers, and then round out a full
162 my $data = "\0" x ($tar_header_length * 2);
163 TarWrite($fh, \$data);
164 TarWrite($fh, undef);
167 # print out totals if requested
170 print STDERR "Done: $FileCnt files, $ByteCnt bytes, $DirCnt dirs,",
171 " $SpecialCnt specials, $ErrorCnt errors\n";
175 ###########################################################################
177 ###########################################################################
181 my($fh, $dir, $tarPathOverride) = @_;
183 my $view = BackupPC::View->new($bpc, $Host, \@Backups);
185 if ( $dir =~ m{(^|/)\.\.(/|$)} ) {
186 print(STDERR "$0: bad directory '$dir'\n");
190 $view->find($Num, $ShareName, $dir, 0, \&TarWriteFile,
191 $fh, $tarPathOverride);
198 $UidCache{$uid} = (getpwuid($uid))[0] if ( !exists($UidCache{$uid}) );
199 return $UidCache{$uid};
206 $GidCache{$gid} = (getgrgid($gid))[0] if ( !exists($GidCache{$gid}) );
207 return $GidCache{$gid};
212 my($fh, $dataRef) = @_;
214 if ( !defined($dataRef) ) {
216 # do flush by padding to a full $WriteBufSz
218 my $data = "\0" x ($WriteBufSz - length($WriteBuf));
221 if ( length($WriteBuf) + length($$dataRef) < $WriteBufSz ) {
223 # just buffer and return
225 $WriteBuf .= $$dataRef;
228 my $done = $WriteBufSz - length($WriteBuf);
229 if ( syswrite($fh, $WriteBuf . substr($$dataRef, 0, $done))
231 print(STDERR "Unable to write to output file ($!)\n");
234 while ( $done + $WriteBufSz <= length($$dataRef) ) {
235 if ( syswrite($fh, substr($$dataRef, $done, $WriteBufSz))
237 print(STDERR "Unable to write to output file ($!)\n");
240 $done += $WriteBufSz;
242 $WriteBuf = substr($$dataRef, $done);
249 if ( $size % $tar_header_length ) {
250 my $data = "\0" x ($tar_header_length - ($size % $tar_header_length));
251 TarWrite($fh, \$data);
259 $hdr->{uname} = UidLookup($hdr->{uid}) if ( !defined($hdr->{uname}) );
260 $hdr->{gname} = GidLookup($hdr->{gid}) if ( !defined($hdr->{gname}) );
261 my $devmajor = defined($hdr->{devmajor}) ? sprintf("%07o", $hdr->{devmajor})
263 my $devminor = defined($hdr->{devminor}) ? sprintf("%07o", $hdr->{devminor})
265 my $data = pack($tar_pack_header,
266 substr($hdr->{name}, 0, 99),
267 sprintf("%07o", $hdr->{mode}),
268 sprintf("%07o", $hdr->{uid}),
269 sprintf("%07o", $hdr->{gid}),
270 sprintf("%011o", $hdr->{size}),
271 sprintf("%011o", $hdr->{mtime}),
272 "", #checksum field - space padded by pack("A8")
274 substr($hdr->{linkname}, 0, 99),
275 $hdr->{magic} || 'ustar ',
276 $hdr->{version} || ' ',
283 substr($data, 148, 7) = sprintf("%06o\0", unpack("%16C*",$data));
284 TarWrite($fh, \$data);
292 # Handle long link names (symbolic links)
294 if ( length($hdr->{linkname}) > 99 ) {
296 my $data = $hdr->{linkname} . "\0";
297 $h{name} = "././\@LongLink";
299 $h{size} = length($data);
300 TarWriteHeader($fh, \%h);
301 TarWrite($fh, \$data);
302 TarWritePad($fh, length($data));
305 # Handle long file names
307 if ( length($hdr->{name}) > 99 ) {
309 my $data = $hdr->{name} . "\0";
310 $h{name} = "././\@LongLink";
312 $h{size} = length($data);
313 TarWriteHeader($fh, \%h);
314 TarWrite($fh, \$data);
315 TarWritePad($fh, length($data));
317 TarWriteHeader($fh, $hdr);
325 my($hdr, $fh, $tarPathOverride) = @_;
327 my $tarPath = $hdr->{relPath};
328 $tarPath = $tarPathOverride if ( defined($tarPathOverride) );
330 if ( defined($PathRemove)
331 && substr($tarPath, 0, length($PathRemove)) eq $PathRemove ) {
332 substr($tarPath, 0, length($PathRemove)) = $PathAdd;
334 $tarPath = "./" . $tarPath if ( $tarPath !~ /^\.\// );
335 $tarPath =~ s{//+}{/}g;
336 $hdr->{name} = $tarPath;
338 if ( $hdr->{type} == BPC_FTYPE_DIR ) {
340 # Directory: just write the header
342 $hdr->{name} .= "/" if ( $hdr->{name} !~ m{/$} );
343 TarWriteFileInfo($fh, $hdr);
345 } elsif ( $hdr->{type} == BPC_FTYPE_FILE ) {
347 # Regular file: write the header and file
349 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});
350 if ( !defined($f) ) {
351 print(STDERR "Unable to open file $hdr->{fullPath}\n");
355 TarWriteFileInfo($fh, $hdr);
357 while ( $f->read(\$data, $BufSize) > 0 ) {
358 TarWrite($fh, \$data);
359 $size += length($data);
362 TarWritePad($fh, $size);
365 } elsif ( $hdr->{type} == BPC_FTYPE_HARDLINK ) {
367 # Hardlink file: either write a hardlink or the complete file
368 # depending upon whether the linked-to file will be written
371 # Start by reading the contents of the link.
373 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});
374 if ( !defined($f) ) {
375 print(STDERR "Unable to open file $hdr->{fullPath}\n");
380 while ( $f->read(\$data, $BufSize) > 0 ) {
381 $hdr->{linkname} .= $data;
385 # Check @ARGV and the list of hardlinked files we have explicity
386 # dumped to see if we have dumped this file or not
389 my $name = $hdr->{linkname};
391 if ( $HardLinkExtraFiles{$name} ) {
394 foreach my $arg ( @ARGV ) {
397 $done = 1 if ( $name eq $arg || $name =~ /^\Q$arg\// );
402 # Target file will be or was written, so just remember
403 # the hardlink so we can dump it later.
405 push(@HardLinks, $hdr);
409 # Have to dump the original file. Just call the top-level
410 # routine, so that we save the hassle of dealing with
411 # mangling, merging and attributes.
413 $HardLinkExtraFiles{$hdr->{linkname}} = 1;
414 archiveWrite($fh, $hdr->{linkname}, $hdr->{name});
416 } elsif ( $hdr->{type} == BPC_FTYPE_SYMLINK ) {
418 # Symbolic link: read the symbolic link contents into the header
419 # and write the header.
421 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});
422 if ( !defined($f) ) {
423 print(STDERR "Unable to open symlink file $hdr->{fullPath}\n");
428 while ( $f->read(\$data, $BufSize) > 0 ) {
429 $hdr->{linkname} .= $data;
433 TarWriteFileInfo($fh, $hdr);
435 } elsif ( $hdr->{type} == BPC_FTYPE_CHARDEV
436 || $hdr->{type} == BPC_FTYPE_BLOCKDEV
437 || $hdr->{type} == BPC_FTYPE_FIFO ) {
439 # Special files: for char and block special we read the
440 # major and minor numbers from a plain file.
442 if ( $hdr->{type} != BPC_FTYPE_FIFO ) {
443 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0,
446 if ( !defined($f) || $f->read(\$data, $BufSize) < 0 ) {
447 print(STDERR "Unable to open/read char/block special file"
448 . " $hdr->{fullPath}\n");
449 $f->close if ( defined($f) );
454 if ( $data =~ /(\d+),(\d+)/ ) {
455 $hdr->{devmajor} = $1;
456 $hdr->{devminor} = $2;
460 TarWriteFileInfo($fh, $hdr);
463 print(STDERR "Got unknown type $hdr->{type} for $hdr->{name}\n");