Debian Codegraph @

savelog

raw
1 #! /bin/sh
2 # savelog - save a log file
3 # Copyright (C) 1987, 1988 Ronald S. Karr and Landon Curt Noll
4 # Copyright (C) 1992 Ronald S. Karr
5 # Slight modifications by Ian A. Murdock <[email protected]>:
6 # * uses `gzip' rather than `compress'
7 # * doesn't use $savedir; keeps saved log files in the same directory
8 # * reports successful rotation of log files
9 # * for the sake of consistency, files are rotated even if they are
10 # empty
11 # More modifications by Guy Maor <[email protected]>:
12 # * cleanup.
13 # * -p (preserve) option
14 #
15 # usage: savelog [-m mode] [-u user] [-g group] [-t] [-p] [-c cycle]
16 # [-j] [-C] [-d] [-l] [-r rolldir] [-n] [-q] file...
17 # -m mode - chmod log files to mode
18 # -u user - chown log files to user
19 # -g group - chgrp log files to group
20 # -c cycle - save cycle versions of the logfile (default: 7)
21 # -r rolldir- use rolldir instead of . to roll files
22 # -C - force cleanup of cycled logfiles
23 # -d - use standard date for rolling
24 # -D - override date format for -d
25 # -t - touch file
26 # -l - don't compress any log files (default: compress)
27 # -p - preserve mode/user/group of original file
28 # -j - use bzip2 instead of gzip
29 # -J - use xz instead of gzip
30 # -1 .. -9 - compression strength or memory usage (default: 9, except for xz)
31 # -x script - invoke script with rotated log file in $FILE
32 # -n - do not rotate empty files
33 # -q - be quiet
34 # file - log file names
35 #
36 # The savelog command saves and optionally compresses old copies of files.
37 # Older version of 'file' are named:
38 #
39 # 'file'.<number><compress_suffix>
40 #
41 # where <number> is the version number, 0 being the newest. By default,
42 # version numbers > 0 are compressed (unless -l prevents it). The
43 # version number 0 is never compressed on the off chance that a process
44 # still has 'file' opened for I/O.
45 #
46 # if the '-d' option is specified, <number> will be YYMMDDhhmmss
47 #
48 # If the 'file' does not exist and -t was given, it will be created.
49 #
50 # For files that do exist and have lengths greater than zero, the following
51 # actions are performed.
52 #
53 # 1) Version numered files are cycled. That is version 6 is moved to
54 # version 7, version is moved to becomes version 6, ... and finally
55 # version 0 is moved to version 1. Both compressed names and
56 # uncompressed names are cycled, regardless of -t. Missing version
57 # files are ignored.
58 #
59 # 2) The new file.1 is compressed and is changed subject to
60 # the -m, -u and -g flags. This step is skipped if the -t flag
61 # was given.
62 #
63 # 3) The main file is moved to file.0.
64 #
65 # 4) If the -m, -u, -g, -t, or -p flags are given, then the file is
66 # touched into existence subject to the given flags. The -p flag
67 # will preserve the original owner, group, and permissions.
68 #
69 # 5) The new file.0 is changed subject to the -m, -u and -g flags.
70 #
71 # Note: If no -m, -u, -g, -t, or -p is given, then the primary log file is
72 # not created.
73 #
74 # Note: Since the version numbers start with 0, version number <cycle>
75 # is never formed. The <cycle> count must be at least 2.
76 #
77 # Bugs: If a process is still writing to the file.0 and savelog
78 # moved it to file.1 and compresses it, data could be lost.
79 # Smail does not have this problem in general because it
80 # restats files often.
81
82 # common location
83 export PATH=$PATH:/sbin:/bin:/usr/sbin:/usr/bin
84 COMPRESS="gzip"
85 COMPRESS_OPTS="-f"
86 COMPRESS_STRENGTH_DEF="-9";
87 DOT_Z=".gz"
88 DATUM=`date +%Y%m%d%H%M%S`
89
90 # parse args
91 exitcode=0 # no problems to far
92 prog=`basename $0`
93 mode=
94 user=
95 group=
96 touch=
97 forceclean=
98 rolldir=
99 datum=
100 preserve=
101 hookscript=
102 quiet=0
103 rotateifempty=yes
104 count=7
105
106 usage()
107 {
108 echo "Usage: $prog [-m mode] [-u user] [-g group] [-t] [-c cycle] [-p]"
109 echo " [-j] [-C] [-d] [-l] [-r rolldir] [-n] [-q] file ..."
110 echo " -m mode - chmod log files to mode"
111 echo " -u user - chown log files to user"
112 echo " -g group - chgrp log files to group"
113 echo " -c cycle - save cycle versions of the logfile (default: 7)"
114 echo " -r rolldir - use rolldir instead of . to roll files"
115 echo " -C - force cleanup of cycled logfiles"
116 echo " -d - use standard date for rolling"
117 echo " -D - override date format for -d"
118 echo " -t - touch file"
119 echo " -l - don't compress any log files (default: compress)"
120 echo " -p - preserve mode/user/group of original file"
121 echo " -j - use bzip2 instead of gzip"
122 echo " -J - use xz instead of gzip"
123 echo " -1 .. -9 - compression strength or memory usage (default: 9, except for xz)"
124 echo " -x script - invoke script with rotated log file in \$FILE"
125 echo " -n - do not rotate empty files"
126 echo " -q - suppress rotation message"
127 echo " file - log file names"
128 }
129
130
131 fixfile()
132 {
133 if [ -n "$user" ]; then
134 chown -- "$user" "$1"
135 fi
136 if [ -n "$group" ]; then
137 chgrp -- "$group" "$1"
138 fi
139 if [ -n "$mode" ]; then
140 chmod -- "$mode" "$1"
141 fi
142 }
143
144
145 while getopts m:u:g:c:r:CdD:tlphjJ123456789x:nq opt ; do
146 case "$opt" in
147 m) mode="$OPTARG" ;;
148 u) user="$OPTARG" ;;
149 g) group="$OPTARG" ;;
150 c) count="$OPTARG" ;;
151 r) rolldir="$OPTARG" ;;
152 C) forceclean=1 ;;
153 d) datum=1 ;;
154 D) DATUM=$(date +$OPTARG) ;;
155 t) touch=1 ;;
156 j) COMPRESS="bzip2"; COMPRESS_OPTS="-f"; COMPRESS_STRENGTH_DEF="-9"; DOT_Z=".bz2" ;;
157 J) COMPRESS="xz"; COMPRESS_OPTS="-f"; COMPRESS_STRENGTH_DEF=""; DOT_Z=".xz" ;;
158 [1-9]) COMPRESS_STRENGTH="-$opt" ;;
159 x) hookscript="$OPTARG" ;;
160 l) COMPRESS="" ;;
161 p) preserve=1 ;;
162 n) rotateifempty="no" ;;
163 q) quiet=1 ;;
164 h) usage; exit 0 ;;
165 *) usage; exit 1 ;;
166 esac
167 done
168
169 shift $(($OPTIND - 1))
170
171 if [ "$count" -lt 2 ]; then
172 echo "$prog: count must be at least 2" 1>&2
173 exit 2
174 fi
175
176 if [ -n "$COMPRESS" ] && [ -z "$(command -v $COMPRESS)" ]; then
177 echo "$prog: Compression binary not available, please make sure '$COMPRESS' is installed" 1>&2
178 exit 2
179 fi
180
181 if [ -n "$COMPRESS_STRENGTH" ]; then
182 COMPRESS_OPTS="$COMPRESS_OPTS $COMPRESS_STRENGTH"
183 else
184 COMPRESS_OPTS="$COMPRESS_OPTS $COMPRESS_STRENGTH_DEF"
185 fi
186
187 # cycle thru filenames
188 while [ $# -gt 0 ]; do
189
190 # get the filename
191 filename="$1"
192 shift
193
194 # catch bogus files
195 if [ -e "$filename" ] && [ ! -f "$filename" ]; then
196 echo "$prog: $filename is not a regular file" 1>&2
197 exitcode=3
198 continue
199 fi
200
201 # if file does not exist or is empty, and we've been told to not rotate
202 # empty files, create if requested and skip to the next file.
203 if [ ! -s "$filename" ] && [ "$rotateifempty" = "no" ]; then
204 # if -t was given and it does not exist, create it
205 if test -n "$touch" && [ ! -f "$filename" ]; then
206 touch -- "$filename"
207 if [ "$?" -ne 0 ]; then
208 echo "$prog: could not touch $filename" 1>&2
209 exitcode=4
210 continue
211 fi
212 fixfile "$filename"
213 fi
214 continue
215 # otherwise if the file does not exist and we've been told to rotate it
216 # anyway, create an empty file to rotate.
217 elif [ ! -e "$filename" ]; then
218 touch -- "$filename"
219 if [ "$?" -ne 0 ]; then
220 echo "$prog: could not touch $filename" 1>&2
221 exitcode=4
222 continue
223 fi
224 fixfile "$filename"
225 fi
226
227 # be sure that the savedir exists and is writable
228 # (Debian default: $savedir is . and not ./OLD)
229 savedir=`dirname -- "$filename"`
230 if [ -z "$savedir" ]; then
231 savedir=.
232 fi
233 case "$rolldir" in
234 (/*)
235 savedir="$rolldir"
236 ;;
237 (*)
238 savedir="$savedir/$rolldir"
239 ;;
240 esac
241 if [ ! -d "$savedir" ]; then
242 mkdir -p -- "$savedir"
243 if [ "$?" -ne 0 ]; then
244 echo "$prog: could not mkdir $savedir" 1>&2
245 exitcode=5
246 continue
247 fi
248 chmod 0755 -- "$savedir"
249 fi
250 if [ ! -w "$savedir" ]; then
251 echo "$prog: directory $savedir is not writable" 1>&2
252 exitcode=7
253 continue
254 fi
255
256 # determine our uncompressed file names
257 newname=`basename -- "$filename"`
258 newname="$savedir/$newname"
259
260 # cycle the old compressed log files
261 cycle=$(( $count - 1))
262 rm -f -- "$newname.$cycle" "$newname.$cycle$DOT_Z"
263 while [ $cycle -gt 1 ]; do
264 # --cycle
265 oldcycle=$cycle
266 cycle=$(( $cycle - 1 ))
267 # cycle log
268 if [ -f "$newname.$cycle$DOT_Z" ]; then
269 mv -f -- "$newname.$cycle$DOT_Z" \
270 "$newname.$oldcycle$DOT_Z"
271 fi
272 if [ -f "$newname.$cycle" ]; then
273 # file was not compressed. move it anyway
274 mv -f -- "$newname.$cycle" "$newname.$oldcycle"
275 fi
276 done
277
278 # compress the old uncompressed log if needed
279 if [ -f "$newname.0" ]; then
280 if [ -z "$COMPRESS" ]; then
281 newfile="$newname.1"
282 mv -f -- "$newname.0" "$newfile"
283 else
284 newfile="$newname.1$DOT_Z"
285 # $COMPRESS $COMPRESS_OPTS < $newname.0 > $newfile
286 # rm -f $newname.0
287 $COMPRESS $COMPRESS_OPTS "$newname.0"
288 mv -f -- "$newname.0$DOT_Z" "$newfile"
289 fi
290 fixfile "$newfile"
291 fi
292
293 # compress the old uncompressed log if needed
294 if test -n "$datum" && test -n "$COMPRESS"; then
295 $COMPRESS $COMPRESS_OPTS -- "$newname".[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]
296 fi
297
298 # remove old files if so desired
299 if [ -n "$forceclean" ]; then
300 cycle=$(( $count - 1))
301 if [ -z "$COMPRESS" ]; then
302 list=$(ls -t -- $newname.[0-9]* 2>/dev/null | sed -e 1,${cycle}d)
303 if [ -n "$list" ]; then
304 rm -f -- $list
305 fi
306 else
307 list=$(ls -t -- $newname.[0-9]*$DOT_Z 2>/dev/null | sed -e 1,${cycle}d)
308 if [ -n "$list" ]; then
309 rm -f -- $list
310 fi
311 fi
312 fi
313
314 # create new file if needed
315 if [ -n "$preserve" ]; then
316 (umask 077
317 touch -- "$filename.new"
318 chown --reference="$filename" -- "$filename.new"
319 chmod --reference="$filename" -- "$filename.new")
320 filenew=1
321 elif [ -n "$touch$user$group$mode" ]; then
322 touch -- "$filename.new"
323 fixfile "$filename.new"
324 filenew=1
325 fi
326
327 newfilename="$newname.0"
328 # link the file into the file.0 holding place
329 if [ -f "$filename" ]; then
330 if [ -n "$filenew" ]; then
331 if ln -f -- "$filename" "$newfilename"; then
332 mv -f -- "$filename.new" "$filename"
333 else
334 echo "Error hardlinking $filename to $newfilename" >&2
335 exitcode=8
336 continue
337 fi
338 else
339 mv -f -- "$filename" "$newfilename"
340 fi
341 fi
342 [ ! -f "$newfilename" ] && touch -- "$newfilename"
343 fixfile "$newfilename"
344 if [ -n "$datum" ]; then
345 mv -- "$newfilename" "$newname.$DATUM"
346 newfilename="$newname.$DATUM"
347 fi
348
349 if [ -n "$hookscript" ]; then
350 FILE="$newfilename" $SHELL -c "$hookscript" || \
351 {
352 ret=$?
353 test "$quiet" -eq 1 || echo "Hook script failed with exit code $ret." 1>&2
354 }
355 fi
356
357 # report successful rotation
358 test "$quiet" -eq 1 || echo "Rotated \`$filename' at `date`."
359 done
360 exit $exitcode