2 #============================================================= -*-perl-*-
4 # BackupPC_zipCreate: create a zip archive of an existing dump
5 # for restore on a client.
9 # Usage: BackupPC_zipCreate [-t] [-h host] [-n dumpNum] [-s shareName]
10 # [-r pathRemove] [-p pathAdd] [-c compressionLevel]
11 # files/directories...
16 # -h host host from which the zip archive is created
17 # -n dumpNum dump number from which the zip archive is created
18 # -s shareName share name from which the zip 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 # -c level compression level (default is 0, no compression)
26 # The -h, -n and -s options specify which dump is used to generate
27 # the zip archive. The -r and -p options can be used to relocate
28 # the paths in the zip archive so extracted files can be placed
29 # in a location different from their original location.
32 # Guillaume Filion <gfk@users.sourceforge.net>
33 # Based on Backup_tarCreate by Craig Barratt <cbarratt@users.sourceforge.net>
36 # Copyright (C) 2002 Craig Barratt and Guillaume Filion
38 # This program is free software; you can redistribute it and/or modify
39 # it under the terms of the GNU General Public License as published by
40 # the Free Software Foundation; either version 2 of the License, or
41 # (at your option) any later version.
43 # This program is distributed in the hope that it will be useful,
44 # but WITHOUT ANY WARRANTY; without even the implied warranty of
45 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46 # GNU General Public License for more details.
48 # You should have received a copy of the GNU General Public License
49 # along with this program; if not, write to the Free Software
50 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
52 #========================================================================
54 # Version 2.0.0_CVS, released 3 Feb 2003.
56 # See http://backuppc.sourceforge.net.
58 #========================================================================
61 use lib "/usr/local/BackupPC/lib";
62 use Archive::Zip qw(:ERROR_CODES);
67 use BackupPC::Attrib qw(:all);
68 use BackupPC::FileZIO;
69 use BackupPC::Zip::FileMember;
72 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
73 my $TopDir = $bpc->TopDir();
74 my $BinDir = $bpc->BinDir();
75 my %Conf = $bpc->Conf();
78 getopts("th:n:p:r:s:c:", \%opts);
81 print(STDERR "usage: $0 [-t] [-h host] [-n dumpNum] [-s shareName]"
82 . " [-r pathRemove] [-p pathAdd] [-c compressionLevel]"
83 . " files/directories...\n");
87 if ( $opts{h} !~ /^([\w\.\s-]+)$/ ) {
88 print(STDERR "$0: bad host name '$opts{h}'\n");
93 if ( $opts{n} !~ /^(\d+)$/ ) {
94 print(STDERR "$0: bad dump number '$opts{n}'\n");
98 $opts{c} = 0 if ( $opts{c} eq "" );
99 if ( $opts{c} !~ /^(\d+)$/ ) {
100 print(STDERR "$0: invalid compression level '$opts{c}'. 0=none, 9=max\n");
103 my $compLevel = $opts{c};
105 my @Backups = $bpc->BackupInfoRead($Host);
113 for ( $i = 0 ; $i < @Backups ; $i++ ) {
114 last if ( $Backups[$i]{num} == $Num );
116 if ( $i >= @Backups ) {
117 print(STDERR "$0: bad backup number $Num for host $Host\n");
121 my $PathRemove = $1 if ( $opts{r} =~ /(.+)/ );
122 my $PathAdd = $1 if ( $opts{p} =~ /(.+)/ );
123 if ( $opts{s} !~ /^([\w\s\.\/\$-]+)$/ ) {
124 print(STDERR "$0: bad share name '$opts{s}'\n");
127 my $ShareName = $opts{s};
129 my $BufSize = 1048576; # 1MB or 2^20
130 my(%UidCache, %GidCache);
132 my $fh = new IO::Handle;
133 $fh->fdopen(fileno(STDOUT),"w");
134 my $zipfh = Archive::Zip->new();
136 foreach my $dir ( @ARGV ) {
137 archiveWrite($zipfh, $dir);
142 my($zipfh, $dir, $zipPathOverride) = @_;
144 my $view = BackupPC::View->new($bpc, $Host, \@Backups);
146 if ( $dir =~ m{(^|/)\.\.(/|$)} || $dir !~ /^(.*)$/ ) {
147 print(STDERR "$0: bad directory '$dir'\n");
151 $view->find($Num, $ShareName, $dir, 0, \&ZipWriteFile,
152 $zipfh, $zipPathOverride);
156 print STDERR "Can't write Zip file\n"
157 unless $zipfh->writeToFileHandle($fh, 0) == Archive::Zip::AZ_OK;
160 # print out totals if requested
163 print STDERR "Done: $FileCnt files, $ByteCnt bytes, $DirCnt dirs,",
164 " $SpecialCnt specials ignored, $ErrorCnt errors\n";
168 ###########################################################################
170 ###########################################################################
176 $UidCache{$uid} = (getpwuid($uid))[0] if ( !exists($UidCache{$uid}) );
177 return $UidCache{$uid};
184 $GidCache{$gid} = (getgrgid($gid))[0] if ( !exists($GidCache{$gid}) );
185 return $GidCache{$gid};
193 my($hdr, $zipfh, $zipPathOverride) = @_;
195 my $tarPath = $hdr->{relPath};
196 $tarPath = $zipPathOverride if ( defined($zipPathOverride) );
198 if ( defined($PathRemove)
199 && substr($tarPath, 0, length($PathRemove)) eq $PathRemove ) {
200 substr($tarPath, 0, length($PathRemove)) = $PathAdd;
202 $tarPath = "./" . $tarPath if ( $tarPath !~ /^\.\// );
203 $tarPath =~ s{//+}{/}g;
204 $hdr->{name} = $tarPath;
206 my $zipmember; # Container to hold the file/directory to zip.
208 if ( $hdr->{type} == BPC_FTYPE_DIR ) {
210 # Directory: just write the header
212 $hdr->{name} .= "/" if ( $hdr->{name} !~ m{/$} );
213 $zipmember = Archive::Zip::Member->newDirectoryNamed($hdr->{name});
215 } elsif ( $hdr->{type} == BPC_FTYPE_FILE ) {
217 # Regular file: write the header and file
219 $zipmember = BackupPC::Zip::FileMember->newFromFileNamed(
226 $ByteCnt += $hdr->{size};
227 } elsif ( $hdr->{type} == BPC_FTYPE_HARDLINK ) {
229 # Hardlink file: not supported by Zip, so just make a copy
230 # of the pointed-to file.
232 # Start by reading the contents of the link.
234 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});
235 if ( !defined($f) ) {
236 print(STDERR "Unable to open file $hdr->{fullPath}\n");
241 while ( $f->read(\$data, $BufSize) > 0 ) {
242 $hdr->{linkname} .= $data;
246 # Dump the original file. Just call the top-level
247 # routine, so that we save the hassle of dealing with
248 # mangling, merging and attributes.
250 archiveWrite($zipfh, $hdr->{linkname}, $hdr->{name});
251 } elsif ( $hdr->{type} == BPC_FTYPE_SYMLINK ) {
253 # Symlinks can't be Zipped. 8(
254 # We could zip the pointed-to dir/file (just like hardlink), but we
255 # have to avoid the infinite-loop case of a symlink pointed to a
256 # directory above us. Ignore for now. Could be a comand-line
260 } elsif ( $hdr->{type} == BPC_FTYPE_CHARDEV
261 || $hdr->{type} == BPC_FTYPE_BLOCKDEV
262 || $hdr->{type} == BPC_FTYPE_FIFO ) {
264 # Special files can't be Zipped. 8(
268 print(STDERR "Got unknown type $hdr->{type} for $hdr->{name}\n");
271 return if ( !$zipmember );
273 # Set the attributes and permissions
274 $zipmember->setLastModFileDateTimeFromUnix($hdr->{mtime});
275 $zipmember->unixFileAttributes($hdr->{mode});
276 # Zip files don't accept uid and gid, so we put them in the comment field.
277 $zipmember->fileComment("uid=".$hdr->{uid}." gid=".$hdr->{gid})
278 if ( $hdr->{uid} || $hdr->{gid} );
280 # Specify the compression level for this member
281 $zipmember->desiredCompressionLevel($compLevel) if ($compLevel =~ /[0-9]/);
283 # Finally Zip the member
284 $zipfh->addMember($zipmember);