archiveweblogs.sh

来自「Shall高级编程」· Shell 代码 · 共 67 行

SH
67
字号
#!/bin/bash# archiveweblogs.sh v1.0# Troy Engel <tengel@fluid.com># Slightly modified by document author.# Used with permission.##  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.shPROBLEM=66# Set this to your backup dir.BKP_DIR=/opt/backups/weblogs# Default Apache/RedHat stuffLOG_DAYS="4 3 2 1"LOG_DIR=/var/log/httpdLOG_FILES="access_log error_log"# Default RedHat program locationsLS=/bin/lsMV=/bin/mvID=/usr/bin/idCUT=/bin/cutCOL=/usr/bin/columnBZ2=/usr/bin/bzip2# Are we root?USER=`$ID -u`if [ "X$USER" != "X0" ]; then  echo "PANIC: Only root can run this script!"  exit $PROBLEMfi# Backup dir exists/writable?if [ ! -x $BKP_DIR ]; then  echo "PANIC: $BKP_DIR doesn't exist or isn't writable!"  exit $PROBLEMfi# Move, rename and bzip2 the logsfor 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  donedoneexit 0

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?