scripts/shell/archiveweblogs.sh
2024-03-20 11:28:46 -05:00

62 lines
1.5 KiB
Bash
Executable file

#!/usr/bin/env bash
#
# This script will preserve the normally rotated and
# thrown away weblogs from a default RedHat/Apache installation.
# It will save the files with a date/time stamp in the filename,
# bzipped, to a given directory.
#
# Run this from crontab nightly at an off hour,
# as bzip2 can suck up some serious CPU on huge logs:
# 0 2 * * * /opt/sbin/archiveweblogs.sh
#
# SPDX-License-Identifier: MIT
PROBLEM=66
# Set this to your backup dir.
BKP_DIR=/opt/backups/weblogs
# Default Apache/RedHat stuff
LOG_DAYS="4 3 2 1"
LOG_DIR=/var/log/httpd
LOG_FILES="access_log error_log"
# Default RedHat program locations
LS=/bin/ls
MV=/bin/mv
ID=/usr/bin/id
CUT=/bin/cut
COL=/usr/bin/column
BZ2=/usr/bin/bzip2
# Are we root?
USER=$($ID -u)
if [ "X$USER" != "X0" ]; then
echo "PANIC: Only root can run this script!"
exit $PROBLEM
fi
# Backup dir exists/writable?
if [ ! -x "$BKP_DIR" ]; then
echo "PANIC: $BKP_DIR doesn't exist or isn't writable!"
exit $PROBLEM
fi
# Move, rename and bzip2 the logs
for logday in $LOG_DAYS; do
for logfile in $LOG_FILES; do
MYFILE="$LOG_DIR/$logfile.$logday"
if [ -w "$MYFILE" ]; then
DTS=$($LS -lgo --time-style=+%Y%m%d "$MYFILE" | $COL -t | $CUT -d ' ' -f7)
$MV "$MYFILE" "$BKP_DIR/$logfile.$DTS"
$BZ2 "$BKP_DIR/$logfile.$DTS"
else
# Only spew an error if the file exits (ergo non-writable).
if [ -f "$MYFILE" ]; then
echo "ERROR: $MYFILE not writable. Skipping."
fi
fi
done
done
exit 0