Debian Codegraph @

archiveweblogs.sh

raw symbols SCIP
1 #!/bin/bash
2 # archiveweblogs.sh v1.0
3
4 # Troy Engel <[email protected]>
5 # Slightly modified by document author.
6 # Used with permission.
7 #
8 # This script will preserve the normally rotated and
9 #+ thrown away weblogs from a default RedHat/Apache installation.
10 # It will save the files with a date/time stamp in the filename,
11 #+ bzipped, to a given directory.
12 #
13 # Run this from crontab nightly at an off hour,
14 #+ as bzip2 can suck up some serious CPU on huge logs:
15 # 0 2 * * * /opt/sbin/archiveweblogs.sh
16
17
18 PROBLEM=66
19
20 # Set this to your backup dir.
21 BKP_DIR=/opt/backups/weblogs
22
23 # Default Apache/RedHat stuff
24 LOG_DAYS="4 3 2 1"
25 LOG_DIR=/var/log/httpd
26 LOG_FILES="access_log error_log"
27
28 # Default RedHat program locations
29 LS=/bin/ls
30 MV=/bin/mv
31 ID=/usr/bin/id
32 CUT=/bin/cut
33 COL=/usr/bin/column
34 BZ2=/usr/bin/bzip2
35
36 # Are we root?
37 USER=`$ID -u`
38 if [ "X$USER" != "X0" ]; then
39 echo "PANIC: Only root can run this script!"
40 exit $PROBLEM
41 fi
42
43 # Backup dir exists/writable?
44 if [ ! -x $BKP_DIR ]; then
45 echo "PANIC: $BKP_DIR doesn't exist or isn't writable!"
46 exit $PROBLEM
47 fi
48
49 # Move, rename and bzip2 the logs
50 for logday in $LOG_DAYS; do
51 for logfile in $LOG_FILES; do
52 MYFILE="$LOG_DIR/$logfile.$logday"
53 if [ -w $MYFILE ]; then
54 DTS=`$LS -lgo --time-style=+%Y%m%d $MYFILE | $COL -t | $CUT -d ' ' -f7`
55 $MV $MYFILE $BKP_DIR/$logfile.$DTS
56 $BZ2 $BKP_DIR/$logfile.$DTS
57 else
58 # Only spew an error if the file exits (ergo non-writable).
59 if [ -f $MYFILE ]; then
60 echo "ERROR: $MYFILE not writable. Skipping."
61 fi
62 fi
63 done
64 done
65
66 exit 0