2 #=============================================================
4 # BackupPC_archiveHost: Archive files for a single host
8 # Usage: BackupPC_archiveHost tarCreatePath splitPath parPath host bkupNum \
9 # compPath fileExt splitSize outLoc parFile share
11 # This script is run for each host to create an archive.
13 # This script is executed by BackupPC_archive, based on the setting
14 # of $Conf{ArchiveClientCmd}. This script can be copied and modified
15 # for site-specific behavior. Update $Conf{ArchiveClientCmd} to point
16 # at your customized archive script.
19 # Craig Barratt <cbarratt@users.sourceforge.net>
23 # Copyright (C) 2001-2009 Craig Barratt
25 # This program is free software; you can redistribute it and/or modify
26 # it under the terms of the GNU General Public License as published by
27 # the Free Software Foundation; either version 2 of the License, or
28 # (at your option) any later version.
30 # This program is distributed in the hope that it will be useful,
31 # but WITHOUT ANY WARRANTY; without even the implied warranty of
32 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 # GNU General Public License for more details.
35 # You should have received a copy of the GNU General Public License
36 # along with this program; if not, write to the Free Software
37 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
39 #========================================================================
41 # Version 3.2.0, released 31 Jul 2010.
43 # See http://backuppc.sourceforge.net.
45 #========================================================================
49 use lib "/usr/local/BackupPC/lib";
53 # Pick up the command-line arguments
57 Usage: $0 tarCreatePath splitPath parPath host bkupNum \\
58 compPath fileExt splitSize outLoc parFile share
62 my $tarCreate = $ARGV[0];
63 my $splitPath = $ARGV[1];
64 my $parPath = $ARGV[2];
66 my $bkupNum = $ARGV[4];
67 my $compPath = $ARGV[5];
68 my $fileExt = $ARGV[6];
69 my $splitSize = $ARGV[7];
70 my $outLoc = $ARGV[8];
71 my $parfile = $ARGV[9];
72 my $share = $ARGV[10];
74 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
77 # Make sure the specified programs are executable
79 foreach my $prog ( ($tarCreate, $compPath, $splitPath, $parPath) ) {
80 if ( $prog =~ /[][;&()<>{}|^\n\r\t *\$\\'"`?]/ ) {
81 print("Error: executable path $prog contains suspicious characters\n");
84 next if ( $prog eq "" || -x $prog );
85 print("Error: $prog is not an executable program\n");
88 my $mesg = "Writing tar archive for host $host, backup #$bkupNum";
91 # Build the command we will run
93 $share = $bpc->shellEscape($share);
94 $host = $bpc->shellEscape($host);
95 $bkupNum = $bpc->shellEscape($bkupNum);
96 $fileExt = $bpc->shellEscape($fileExt);
97 $splitSize = $bpc->shellEscape($splitSize);
98 $parfile = $bpc->shellEscape($parfile);
99 my $outLocE = $bpc->shellEscape($outLoc);
102 # We prefer to use /bin/csh because the exit status of a pipeline
103 # is non-zero if any command is non zero. In contrast, /bin/sh
104 # and /bin/bash use the convention that the exit status is just
105 # the exit status of the last command of the pipeline.
108 if ( -x "/bin/csh" ) {
109 @shell = ("/bin/csh", "-cf");
110 } elsif ( -x "/bin/sh" ) {
111 @shell = ("/bin/sh", "-c");
113 print("Error: Can't find executable /bin/csh or /bin/sh\n");
116 my $cmd = "$tarCreate -t -h $host -n $bkupNum -s $share . ";
117 $cmd .= "| $compPath " if ( $compPath ne "cat"
118 && $compPath ne "/bin/cat"
119 && $compPath ne "" );
120 if ( -b $outLoc || -c $outLoc || -f $outLoc ) {
122 # Output file is a device or a regular file, so don't use split
124 $cmd .= ">> $outLocE";
125 $mesg .= " to $outLoc";
127 mkpath($outLoc) if ( !-d $outLoc );
129 print("Error: unable to create output directory $outLoc\n");
132 if ( $splitSize > 0 && -x $splitPath ) {
133 $cmd .= "| $splitPath -b $splitSize - $outLocE/$host.$bkupNum.tar$fileExt.";
134 $mesg .= ", split to output files $outLocE/$host.$bkupNum.tar$fileExt.*";
136 $cmd .= "> $outLocE/$host.$bkupNum.tar$fileExt";
137 $mesg .= " to output file $outLocE/$host.$bkupNum.tar$fileExt";
145 my $ret = system(@shell, $cmd);
147 print("Executing: @shell $cmd\n");
148 print("Error: $tarCreate, compress or split failed\n");
153 # Run optional parity file generation (only if the output is a directory,
154 # ie: not a tape device).
156 if ( -d $outLoc && -x $parPath ) {
157 if ( length($parfile) ) {
158 print("Running $parPath to create parity files\n");
159 my $parCmd = "$parPath c -r$parfile $outLocE/$host.$bkupNum.tar$fileExt.par2 $outLocE/$host.$bkupNum.tar$fileExt*";
160 $ret = system($parCmd);
162 print("Executing: $parCmd\n");
163 print("Error: $parPath failed\n");