Imported Upstream version 3.1.0
[debian/amanda] / server-src / amdump.sh
1 #!@SHELL@
2 #
3 # Amanda, The Advanced Maryland Automatic Network Disk Archiver
4 # Copyright (c) 1991-1998 University of Maryland at College Park
5 # All Rights Reserved.
6 #
7 # Permission to use, copy, modify, distribute, and sell this software and its
8 # documentation for any purpose is hereby granted without fee, provided that
9 # the above copyright notice appear in all copies and that both that
10 # copyright notice and this permission notice appear in supporting
11 # documentation, and that the name of U.M. not be used in advertising or
12 # publicity pertaining to distribution of the software without specific,
13 # written prior permission.  U.M. makes no representations about the
14 # suitability of this software for any purpose.  It is provided "as is"
15 # without express or implied warranty.
16 #
17 # U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
19 # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21 # OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 #
24 # Author: James da Silva, Systems Design and Analysis Group
25 #                          Computer Science Department
26 #                          University of Maryland at College Park
27 #
28
29 #
30 # amdump: Manage running one night's Amanda dump run.
31 #
32
33 prefix="@prefix@"
34 exec_prefix="@exec_prefix@"
35 sbindir="@sbindir@"
36 amlibexecdir="@amlibexecdir@"
37 . "${amlibexecdir}/amanda-sh-lib.sh"
38
39 confdir=@CONFIG_DIR@
40
41 # add sbin and ucb dirs
42 PATH="$PATH:/usr/sbin:/sbin:/usr/ucb"
43 export PATH
44
45 if [ $# -lt 1 ]
46 then
47         echo `_ 'Usage: %s config [host [disk...]...]' "$0"`  1>&2
48         exit 1
49 fi
50
51 exit_status=0;
52
53 conf=$1
54 if [ ! -d $confdir/$conf ]; then
55     echo `_ '%s: could not find directory %s' "amdump" "$confdir/$conf"` 1>&2
56     exit 1
57 fi
58 shift
59
60 cd $confdir/$conf || exit 1
61
62 logdir=`amgetconf $conf logdir "$@"`
63 [ $? -ne 0 ]  && exit 1
64 errfile=$logdir/amdump
65 tapecycle=`amgetconf $conf tapecycle "$@"`
66 [ $? -ne 0 ]  && exit 1
67 dumpuser=`amgetconf $conf dumpuser "$@"`
68 [ $? -ne 0 ]  && exit 1
69
70 runuser=`{ whoami ; } 2>/dev/null`
71 if [ $? -ne 0 ]; then
72         idinfo=`{ id ; } 2>/dev/null`
73         if [ $? -ne 0 ]; then
74                 runuser=${LOGNAME:-"??unknown??"}
75         else
76                 runuser=`echo $idinfo | sed -e 's/).*//' -e 's/^.*(//'`
77         fi
78 fi
79
80 if [ $runuser != $dumpuser ]; then
81         echo `_ '%s: must be run as user %s, not %s' "$0" "$dumpuser" "$runuser"` 1>&2
82         exit 1
83 fi
84
85 if test -f hold; then
86         echo `_ '%s: waiting for hold file to be removed' "$0"` 1>&2
87         while test -f hold; do
88                 sleep 60
89         done
90 fi
91
92 if test -f $errfile || test -f $logdir/log; then
93         amcleanup -p $conf
94 fi
95
96 gdate=`date +'%a %b %e %H:%M:%S %Z %YAAAAA%Y%m%dBBBBB%Y%m%d%H%M%SCCCCC%Y-%m-%d %H:%M:%S %Z'`
97
98 #date=%a %b %e %H:%M:%S %Z %Y
99 date=`echo $gdate |sed -e "s/AAAAA.*$//"`
100
101 #date_datestamp="%Y%m%d"
102 date_datestamp=`echo $gdate |sed -e "s/^.*AAAAA//;s/BBBBB.*$//"`
103
104 #date_starttime="%Y%m%d%H%M%S"
105 date_starttime=`echo $gdate |sed -e "s/^.*BBBBB//;s/CCCCC.*$//"`
106
107 #date_locale_independent=%Y-%m-%d %H:%M:%S %Z
108 date_locale_independent=`echo $gdate |sed -e "s/^.*CCCCC//"`
109
110 if test -f $errfile || test -f $logdir/log; then
111         process_name=`grep "^INFO .* .* pid " $logdir/log | head -n 1 | awk '{print $2}'`
112         echo `_ '%s: %s is already running, or you must run amcleanup' "$0" "${process_name}"` 1>&2
113         echo "INFO amdump amdump pid $$" > $logdir/log.$$
114         echo "START driver date $date_starttime" >> $logdir/log.$$
115         echo "ERROR amdump " `_ '%s is already running, or you must run amcleanup' "${process_name}"` >> $logdir/log.$$
116         $sbindir/amreport $conf --from-amdump -l $logdir/log.$$ "$@"
117         rm -f $logdir/log.$$
118         exit 1;
119 fi
120
121 umask 077
122
123 echo "INFO amdump amdump pid $$" > $logdir/log
124 exit_code=0
125 # Plan and drive the dumps.
126 #exec </dev/null >$errfile 2>&1
127 touch $errfile
128 exit_code=$?
129 [ $exit_code -ne 0 ] && exit_status=$exit_code
130 exec </dev/null 2>>$errfile 1>&2
131 exit_code=$?
132 [ $exit_code -ne 0 ] && exit_status=$exit_code
133
134 printf '%s: start at %s\n' "amdump" "$date"
135 printf '%s: datestamp %s\n' "amdump" "$date_datestamp"
136 printf '%s: starttime %s\n' "amdump" "$date_starttime"
137 printf '%s: starttime-locale-independent %s\n' "amdump" "$date_locale_independent"
138
139 if [ ! -x $amlibexecdir/planner ]; then
140     echo "ERROR amdump Can't execute $amlibexecdir/planner" >> $logdir/log
141 fi
142 if [ ! -x $amlibexecdir/driver ]; then
143     echo "ERROR amdump Can't execute $amlibexecdir/driver" >> $logdir/log
144 fi
145
146 # shells don't do well with handling exit values from pipelines, so we emulate
147 # a pipeline in perl, in such a way that we can combine both exit statuses in a
148 # kind of logical "OR".
149 @PERL@ - $amlibexecdir/planner $amlibexecdir/driver $conf $date_starttime "$@" <<'EOPERL'
150 use IPC::Open3;
151 use POSIX qw(WIFEXITED WEXITSTATUS);
152 my ($planner, $driver, $conf, $date_starttime, @args) = @ARGV;
153
154 open3("</dev/null", \*PIPE, ">&STDERR", $planner, $conf, '--starttime', $date_starttime, @args)
155     or die "Could not exec $planner: $!";
156 open3("<&PIPE", ">&STDOUT", ">&STDERR", $driver, $conf, @args)
157     or die "Could not exec $driver: $!";
158
159 my $first_bad_exit = 0;
160 for (my $i = 0; $i < 2; $i++) {
161     my $dead = wait();
162     die("Error waiting: $!") if ($dead <= 0);
163     my $exit = WIFEXITED($?)? WEXITSTATUS($?) : 1;
164     $first_bad_exit = $exit if ($exit && !$first_bad_exit)
165 }
166 exit $first_bad_exit;
167 EOPERL
168 exit_code=$?
169 [ $exit_code -ne 0 ] && exit_status=$exit_code
170 printf '%s: end at %s\n' "amdump" "`date`"
171
172 # Send out a report on the dumps.
173 $sbindir/amreport $conf --from-amdump "$@"
174 exit_code=$?
175 [ $exit_code -ne 0 ] && exit_status=$exit_code
176
177 # Roll the log file to its datestamped name.
178 $amlibexecdir/amlogroll $conf "$@"
179 exit_code=$?
180 [ $exit_code -ne 0 ] && exit_status=$exit_code
181
182 # Trim the log file to those for dumps that still exist.
183 $amlibexecdir/amtrmlog $conf "$@"
184 exit_code=$?
185 [ $exit_code -ne 0 ] && exit_status=$exit_code
186
187 # Trim the index file to those for dumps that still exist.
188 $amlibexecdir/amtrmidx $conf "$@"
189 exit_code=$?
190 [ $exit_code -ne 0 ] && exit_status=$exit_code
191
192 # Keep a debug log through the tapecycle plus a couple of days.
193 maxdays=`expr $tapecycle + 2`
194 days=1
195 # First, find out the last existing errfile,
196 # to avoid ``infinite'' loops if tapecycle is infinite
197 while [ $days -lt $maxdays ] && [ -f $errfile.$days ]; do
198         days=`expr $days + 1`
199 done
200 # Now, renumber the existing log files
201 while [ $days -ge 2 ]; do
202         ndays=`expr $days - 1`
203         mv $errfile.$ndays $errfile.$days
204         exit_code=$?
205         [ $exit_code -ne 0 ] && exit_status=$exit_code
206         days=$ndays
207 done
208 mv $errfile $errfile.1
209 exit_code=$?
210 [ $exit_code -ne 0 ] && exit_status=$exit_code
211
212 exit $exit_status