Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /bin/bash
- # Clears Logfiles (example HTTPD-Logs) and stores them for a certain amount in archive
- # How to call this script?
- #
- # You can call this script like this:
- # ./logClear.sh /pathtodir/ - Will cleanup the files and store them by default expire time
- # ./logClear.sh /pathtodir/ 2 - Will cleanup the files and store them 2 Days
- # ./logClear.sh /pathtodir/ 0 - Will cleanup the files and DON'T store them
- # ./logClear.sh /pathtodir/ -1 - Will cleanup the files and store them FOREVER
- #
- # Syntax: ./logClear.sh LOGDIR [Optional STORE_TIME_DAYS]
- # Settings
- default_expire_time=6 # Days - 0=turns archiving off - -1=Files will saved forever
- subdir="" # If you have in every directory a same named log folder, you can set it up here
- archive_dir="archive/" # Archiv dir relative to logdir
- logext="log" # File-Extension of the logfile (mostly .txt > "txt" or .log > "log")
- echo "Starting logClear v0.0.1 by Petschko [peter-91@hotmail.de]..."
- # Check if there is a Parameter
- if [ $# -gt 0 ]
- then
- echo "[Info]: Start Script..."
- # Setup vars
- logdir="${1}${subdir}"
- archivedir="${logdir}${archive_dir}"
- delete_after=$default_expire_time
- date=$(date +%Y-%m-%d_%H-%M-%S)
- # Check expire time and if archiving is on
- archiv_on="yes"
- if [ $default_expire_time -eq 0 ]
- then
- archiv_on="no"
- fi
- # Check if there is an other parameter
- if [ $# -gt 1 ]
- then
- if [ $2 -eq 0 ]
- then
- archiv_on="no"
- else
- archiv_on="yes"
- fi
- # Overwrite delete time
- delete_after=$2
- fi
- # Exists File
- if [ -d $logdir ]
- then
- echo "[Info]: Directory logdir found."
- # Check if this script can read
- if [ ! -r $logdir ]
- then
- echo "[Warning]: Directory $logdir is not readable!"
- fi
- # Check if this script can write
- if [ ! -w $logdir ]
- then
- echo "[Error]: Directory $logdir is not writeable!"
- echo "[Info]: Exiting..."
- exit
- fi
- if [ "$archiv_on" = "yes" ];
- then
- # Check if Archive exists if not create it
- echo "[Info]: Checking Archive Directory... (${archivedir})"
- if [ ! -d $archivedir ]
- then
- echo "[Warning]: Directory $archivedir not found! Try to create it..."
- mkdir $archivedir
- # Check if it was created
- if [ ! -d $archivedir ]
- then
- echo "[Error]: Can't create Directory ${archivedir}... (Missing Permissions maybe?)"
- echo "[Info]: Exiting..."
- exit
- fi
- echo "[Info]: Directory successfully created!"
- else
- echo "[Info]: Archive found!"
- fi
- # Check Archive Directory for read
- if [ ! -r $archivedir ]
- then
- echo "[Warning]: Directory $archivedir is not readable!"
- fi
- # Check if this script can write
- if [ ! -w $archivedir ]
- then
- echo "[Error]: Directory $archivedir is not writeable!"
- echo "[Info]: Exiting..."
- exit
- fi
- else
- echo "[Info]: Skipping Archive-Checks - It's turned off"
- fi
- # All prechecks done
- # Switch WorkDir
- echo "[Info]: Change Workdir to ${logdir}"
- cd $logdir
- # Reading files
- echo "[Info]: Reading Files..."
- for logfile in ${logdir}*.${logext}
- do
- clearfile="yes"
- fname=$(basename "$logfile")
- echo "[Info]: Processing file ${fname}";
- if [ "$archiv_on" = "yes" ]
- then
- backupfilename="${archivedir}${date}_${fname}.tar.gz"
- # Compress file and save it to Archive
- if [ -r $logfile ]
- then
- # Don't save empty files
- if [ $(stat -c %s $logfile) -gt 0 ]
- then
- echo "[Info]: Backing up file ${logfile} as TAR.GZ..."
- tar -czf $backupfilename $fname
- # Check if new file exist
- if [ ! -f $backupfilename ]
- then
- clearfile="no"
- echo "[Warning]: Can't create backup file! Will keep logfile."
- fi
- else
- echo "[Notice]: File ${logfile} was not saved as a Backup. It's empty!"
- fi
- else
- echo "[Warning]: File ${logfile} is not readable!"
- fi
- fi
- # Clearfile if all is done
- if [ $clearfile = "yes" ]
- then
- if [ -w $logfile ]
- then
- > $logfile
- else
- echo "[Warning]: File ${logfile} is not writeable! (Can't clear it)"
- fi
- fi
- done
- echo "[Info]: Step 1/2 done."
- # Done clearing and backup files
- # -------------------------------
- # Start delete old files
- if [ $archiv_on = "yes" ]
- then
- # Change workdir to archive
- echo "[Info]: Change Workdir to ${archivedir}"
- cd $archivedir
- echo "[Info]: Reading Archiv files..."
- if [ ! $delete_after -lt 0 ]
- then
- # Delete old files
- for archivfile in ${archivedir}*.tar.gz
- do
- fname=$(basename "$archivfile")
- echo "[Info]: Checking file ${fname}..."
- # Get file date
- file_year=${fname:0:4}
- file_mon=${fname:5:2}
- file_day=${fname:8:2}
- file_timestamp=$(date +%s -d "${file_year}-${file_mon}-${file_day}")
- if [ $(( $file_timestamp + $delete_after * 86400 )) -lt $(date +%s) ]
- then
- # Delete file if it reach a specific age
- echo "[Notice]: Delete File ${archivfile} (Date expired)..."
- if [ -w $archivfile ]
- then
- rm $archivfile
- # Check if it is really deleted
- if [ -f $archivfile ]
- then
- echo "[Warning]: Can't delete ${archivfile}... Skip it"
- fi
- else
- echo "[Warning]: File ${archivfile} is not writeable! (Can't delete it)"
- fi
- fi
- done
- else
- echo "[Notice]: Delete-Archiv-Service is disabled. Skipping..."
- fi
- else
- echo "[Notice]: Archiv-Service is turned off. Skip this step"
- fi
- # Done Message
- echo "[Info]: Step 2/2 done."
- echo "[Info]: All done! :)"
- else
- echo "[Error]: Directory $logdir doesn't exists!"
- fi
- else
- echo "[Error]: Please specify a Directory!"
- echo "----------------------------------"
- echo "Usage: logClear.sh PATH [Optional DELETE_AFTER_X_DAYS]"
- echo ""
- echo "Directory-PATH has to end with an /!"
- echo ""
- echo "Delete after x Days means, that you can specify a delete date after x days."
- echo "0 turns archiving off (Direct delete on call)"
- echo "-1 (Will not delete Archived Files)"
- echo "If you ignore this parameter it will use the default delete time"
- echo "---------------------------------"
- fi
- echo "[Info]: Exiting..."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement