Skip to content

Instantly share code, notes, and snippets.

@christiannageby
Created February 21, 2018 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christiannageby/d09ab68dfb8ecdf0e53a901197112c26 to your computer and use it in GitHub Desktop.
Save christiannageby/d09ab68dfb8ecdf0e53a901197112c26 to your computer and use it in GitHub Desktop.
# README: This script is a log-archive tool wich compresses the logs in to weekly-archives and puts the Weekly archives in to monthly archives.
# README: The script also deletes the montly archives after a 6 months
# README: -------------------------------------------------------------------
# README: How the script Works:
# README: Setup two Cron-jobs one weekly whch runs the script without any parameters
# README: And a monthly wich contains the parameter 'Monthly'.
#!/bin/bash
# Change directory to where the logs is stored (/storage/logs/apache/error)
cd /storage/logs/apache/error/
# Determine if the script is a weekly rotation or a monthly.
# If the a commandline parameter is set assume there is a monthly rotation.
if [$1 == '']
then # Weekly Run
zip -R /storage/logs/apache/error/archive/log-$(date + '%Y-%m-%d').zip '*.log' #Compress all logs into weekly archive
rm -rf *.log # Remove all logs that has been compressed
else # Monthly Run
cd storage/ # Change into storage directory
zip log-$(date +'%Y-%M').zip log-$(date + '%Y-%m')-*.zip #ZIP all weekly logs to a monthly log
rm log-$(date -d "-6 month" +'%Y')*.zip # remove all logs older than 6 months
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment