803adcc6934be0972b5bdabfc0d1d9ffc87537f2
[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 USE_VERSION_SUFFIXES="@USE_VERSION_SUFFIXES@"
46 if test "$USE_VERSION_SUFFIXES" = "yes"; then
47         SUF="-@VERSION@"
48 else
49         SUF=
50 fi
51
52 if [ $# -lt 1 ]
53 then
54         echo `_ 'Usage: %s config [host [disk...]...]' "$0"`  1>&2
55         exit 1
56 fi
57
58 exit_status=0;
59
60 conf=$1
61 if [ ! -d $confdir/$conf ]; then
62     echo `_ '%s: could not find directory %s' "amdump$SUF" "$confdir/$conf"` 1>&2
63     exit 1
64 fi
65 shift
66
67 cd $confdir/$conf || exit 1
68
69 logdir=`amgetconf$SUF $conf logdir "$@"`
70 [ $? -ne 0 ]  && exit 1
71 errfile=$logdir/amdump
72 tapecycle=`amgetconf$SUF $conf tapecycle "$@"`
73 [ $? -ne 0 ]  && exit 1
74 dumpuser=`amgetconf$SUF $conf dumpuser "$@"`
75 [ $? -ne 0 ]  && exit 1
76
77 runuser=`{ whoami ; } 2>/dev/null`
78 if [ $? -ne 0 ]; then
79         idinfo=`{ id ; } 2>/dev/null`
80         if [ $? -ne 0 ]; then
81                 runuser=${LOGNAME:-"??unknown??"}
82         else
83                 runuser=`echo $idinfo | sed -e 's/).*//' -e 's/^.*(//'`
84         fi
85 fi
86
87 if [ $runuser != $dumpuser ]; then
88         echo `_ '%s: must be run as user %s, not %s' "$0" "$dumpuser" "$runuser"` 1>&2
89         exit 1
90 fi
91
92 if test -f hold; then
93         echo `_ '%s: waiting for hold file to be removed' "$0"` 1>&2
94         while test -f hold; do
95                 sleep 60
96         done
97 fi
98
99 if test -f $errfile || test -f $logdir/log; then
100         echo `_ '%s: amdump or amflush is already running, or you must run amcleanup' "$0"` 1>&2
101         exit 1
102 fi
103
104 umask 077
105
106 exit_code=0
107 # Plan and drive the dumps.
108 #exec </dev/null >$errfile 2>&1
109 touch $errfile
110 exit_code=$?
111 [ $exit_code -ne 0 ] && exit_status=$exit_code
112 exec </dev/null 2>>$errfile 1>&2
113 exit_code=$?
114 [ $exit_code -ne 0 ] && exit_status=$exit_code
115
116 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'`
117
118 #date=%a %b %e %H:%M:%S %Z %Y
119 date=`echo $gdate |sed -e "s/AAAAA.*$//"`
120
121 #date_datestamp="%Y%m%d"
122 date_datestamp=`echo $gdate |sed -e "s/^.*AAAAA//;s/BBBBB.*$//"`
123
124 #date_starttime="%Y%m%d%H%M%S"
125 date_starttime=`echo $gdate |sed -e "s/^.*BBBBB//;s/CCCCC.*$//"`
126
127 #date_locale_independent=%Y-%m-%d %H:%M:%S %Z
128 date_locale_independent=`echo $gdate |sed -e "s/^.*CCCCC//"`
129
130 printf '%s: start at %s\n' "amdump" "$date"
131 printf '%s: datestamp %s\n' "amdump" "$date_datestamp"
132 printf '%s: starttime %s\n' "amdump" "$date_starttime"
133 printf '%s: starttime-locale-independent %s\n' "amdump" "$date_locale_independent"
134
135 $amlibexecdir/planner$SUF $conf --starttime $date_starttime "$@" | $amlibexecdir/driver$SUF $conf "$@"
136 exit_code=$?
137 [ $exit_code -ne 0 ] && exit_status=$exit_code
138 printf '%s: end at %s\n' "amdump" "`date`"
139
140 # Send out a report on the dumps.
141 $sbindir/amreport$SUF $conf "$@"
142 exit_code=$?
143 [ $exit_code -ne 0 ] && exit_status=$exit_code
144
145 # Roll the log file to its datestamped name.
146 $amlibexecdir/amlogroll$SUF $conf "$@"
147 exit_code=$?
148 [ $exit_code -ne 0 ] && exit_status=$exit_code
149
150 # Trim the log file to those for dumps that still exist.
151 $amlibexecdir/amtrmlog$SUF $conf "$@"
152 exit_code=$?
153 [ $exit_code -ne 0 ] && exit_status=$exit_code
154
155 # Trim the index file to those for dumps that still exist.
156 $amlibexecdir/amtrmidx$SUF $conf "$@"
157 exit_code=$?
158 [ $exit_code -ne 0 ] && exit_status=$exit_code
159
160 # Keep a debug log through the tapecycle plus a couple of days.
161 maxdays=`expr $tapecycle + 2`
162 days=1
163 # First, find out the last existing errfile,
164 # to avoid ``infinite'' loops if tapecycle is infinite
165 while [ $days -lt $maxdays ] && [ -f $errfile.$days ]; do
166         days=`expr $days + 1`
167 done
168 # Now, renumber the existing log files
169 while [ $days -ge 2 ]; do
170         ndays=`expr $days - 1`
171         mv $errfile.$ndays $errfile.$days
172         exit_code=$?
173         echo $exit_code
174         [ $exit_code -ne 0 ] && exit_status=$exit_code
175         days=$ndays
176 done
177 mv $errfile $errfile.1
178 exit_code=$?
179 [ $exit_code -ne 0 ] && exit_status=$exit_code
180
181 exit $exit_status