Log Compression Bash Script

  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Log Compression Bash Script as PDF for free.

More details

  • Words: 291
  • Pages: 2
Log compression Bash script From : http://www.stardothosting.com In my experience as a Systems Administrator, it has come up quite often to create a script to rotate and compress rather large log files. These log files could be anything: java logs, apache logs (apache should have its own log rotation built in) and mail logs for example. This script has two modes : daily and monthly. The daily mode is intended to be run daily (obviously) , gzipping the previous days log file. The monthly mode, run monthly (obviously), then tar’s up all the previous month’s gzip files into one big tarball. Note that this script assumes the log filenames are assorted by the filename + date (year/month/day). This can obviously be modified to suit the specific syntax of your log file names. Here is the script : #!/bin/sh # Rotate / compress old logs # Star Dot Hosting yesterday=`date --date='1 day ago' +%Y-%m-%d` lastmonth=`date --date='1 month ago' +%Y-%m` lasttwomonth=`date --date='2 months ago' +%Y-%m` currentmonth=`date "+%Y-%m-%d"` logdir="/path/to/log/directory" logfilename="log-file-name" #gzip yesterdays log if [ "$1" = "daily" ] then gzip $logdir/$logfilename.$yesterday.log exit 0 #tar all last month's logs on the 1st of each month elif [ "$1" = "monthly" ] then tar -C $logdir -cf $logdir/$logfilename.$lastmonth.tar $logdir/ $logfilename.$lastmonth-*.log.gz && rm -f $logdir/$logfilename.$lastmonth-*.log.gz exit 0 else echo "no or invalid arguments given." echo "syntax : ./logcompress.sh daily or ./logcompress.sh monthly" exit 1 fi I simply make two crontab entries : 0 3 * * * /bin/sh /usr/local/bin/logcompress.sh daily 0 5 1 * * /bin/sh /usr/local/bin/logcompress.sh monthly The above entries run the script daily at 3:00am, and monthly on the 1st of every

month at 5:00am, this ensures the script isn’t run at the same time on the 1st as the daily job. That’s it!

Related Documents

Bash
April 2020 13
Bash
May 2020 14
Compression
April 2020 21
Bash
May 2020 11
Bash
October 2019 18