Lots of changes:
[BackupPC.git] / bin / BackupPC_archiveHost
1 #!/bin/perl
2 #=============================================================
3 #
4 # BackupPC_archiveHost: Archive files for a single host
5 #
6 # DESCRIPTION
7 #
8 #   Usage: BackupPC_archiveHost tarCreatePath splitPath parPath host bkupNum \
9 #               compPath fileExt splitSize outLoc parFile share
10 #
11 #   This script is run for each host to create an archive.
12 #
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.
17 #
18 # AUTHOR
19 #   Craig Barratt  <cbarratt@users.sourceforge.net>
20 #
21 # COPYRIGHT
22 #   Copyright (C) 2001-2003  Craig Barratt
23 #
24 #   This program is free software; you can redistribute it and/or modify
25 #   it under the terms of the GNU General Public License as published by
26 #   the Free Software Foundation; either version 2 of the License, or
27 #   (at your option) any later version.
28 #
29 #   This program is distributed in the hope that it will be useful,
30 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
31 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32 #   GNU General Public License for more details.
33 #
34 #   You should have received a copy of the GNU General Public License
35 #   along with this program; if not, write to the Free Software
36 #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
37 #
38 #========================================================================
39 #
40 # Version 2.1.0_CVS, released 8 Feb 2004.
41 #
42 # See http://backuppc.sourceforge.net.
43 #
44 #========================================================================
45
46 use strict;
47 use File::Path;
48 use lib "/usr/local/BackupPC/lib";
49 use BackupPC::Lib;
50
51 #
52 # Pick up the command-line arguments
53 #
54 if ( @ARGV != 11 ) {
55     print <<EOF;
56 Usage: $0 tarCreatePath splitPath parPath host bkupNum \\
57           compPath fileExt splitSize outLoc parFile share
58 EOF
59     exit(1);
60 }
61 my $tarCreate    = $ARGV[0];
62 my $splitPath    = $ARGV[1];
63 my $parPath      = $ARGV[2];
64 my $host         = $ARGV[3];
65 my $bkupNum      = $ARGV[4];
66 my $compPath     = $ARGV[5];
67 my $fileExt      = $ARGV[6];
68 my $splitSize    = $ARGV[7];
69 my $outLoc       = $ARGV[8];
70 my $parfile      = $ARGV[9];
71 my $share        = $ARGV[10];
72
73 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
74
75 #
76 # Make sure the specified programs are executable
77 #
78 foreach my $prog ( ($tarCreate, $compPath, $splitPath, $parPath) ) {
79     next if ( $prog eq "" || -x $prog );
80     print("Error: $prog is not an executable program\n");
81     exit(1);
82 }
83 my $mesg = "Writing tar archive for host $host, backup #$bkupNum";
84
85 #
86 # Build the command we will run
87 #
88 $share  = $bpc->shellEscape($share);
89 $host   = $bpc->shellEscape($host);
90 my $cmd = "$tarCreate -t -h $host -n $bkupNum -s $share . ";
91 $cmd   .= "| $compPath " if ( $compPath ne "cat" && $compPath ne "" );
92 if ( -b $outLoc || -c $outLoc || -f $outLoc ) {
93     #
94     # Output file is a device or a regular file, so don't use split
95     #
96     $cmd  .= ">> $outLoc";
97     $mesg .= " to $outLoc";
98 } else {
99     mkpath($outLoc) if ( !-d $outLoc );
100     if ( !-d $outLoc ) {
101         print("Error: unable to create output directory $outLoc\n");
102         exit(1);
103     }
104     if ( $splitSize && -x $splitPath ) {
105         $cmd  .= "| $splitPath -b $splitSize - $outLoc/$host.$bkupNum.tar$fileExt.";
106         $mesg .= ", split to output files $outLoc/$host.$bkupNum.tar$fileExt.*";
107     } else {
108         $cmd  .= "> $outLoc/$host.$bkupNum.tar$fileExt";
109         $mesg .= " to output file $outLoc/$host.$bkupNum.tar$fileExt";
110     }
111 }
112 print("$mesg\n");
113
114 #
115 # Run the command
116 #
117 my $ret = system($cmd);
118 if ( $ret ) {
119     print("Error: $tarCreate, compress or split failed\n");
120     exit(1);
121 }
122
123 #
124 # Run optional parity file generation (only if the output is a directory,
125 # ie: not a tape device).
126 #
127 if ( -d $outLoc && -x $parPath ) {
128     print("Running $parPath to create parity files\n");
129     $ret = system("$parPath a -n $parfile $outLoc/$host.$bkupNum.tar$fileExt.par $outLoc/$host.$bkupNum.tar$fileExt.*");
130     if ( $ret ) {
131         print("Error: $parPath failed\n");
132         exit(1);
133     }
134 }