PDA

View Full Version : How To Setup A Cron Job to Copy Previous Month Apache Log Files To A Backup Directory


MrCompTech
8th December 2011, 20:33
ISPConfig3 & FC13 - How To Setup A Cron Job to Copy Previous Month Apache Log Files To A Backup Directory (http://mrcomptech.com/index.php/83-linux/bash-shell/229-ispconfig3-a-fc13-how-to-setup-a-cron-job-to-copy-previous-month-apache-log-files-to-a-backup-directory)

Desired goal: to maintain an ongoing archive of the http access log files. The standard log file rotation only keeps the last 30 days as part of the log rotation process, log files older than this are deleted. This came about as I wanted to be able to perform offline analysis of the log files after the month had ended, but found out that if I didn't manually move the backup files (*.gz) on the first or second of the month then they would start getting deleted.

Script:
#!/bin/bash
BackupDir=logbackup
cd ~/log
for f in *-access.log.gz
do
filename=$f
done
cd ~
HomeDir=$PWD
BackupYear=${f:0:4}
BackupMonth=${f:4:2}

# Test to see if the main backup directory exists if not then create it
if [ ! -d $HomeDir/$BackupDir ]; then
mkdir $HomeDir/$BackupDir
fi

# Test to see if the backup directory for the year exists, if not then create it.
if [ ! -d $HomeDir/$BackupDir/$BackupYear ]; then
mkdir $HomeDir/$BackupDir/$BackupYear
fi

# Test to see if the backup directory for the month exists, if not then create it.
if [ ! -d $HomeDir/$BackupDir/$BackupYear/$BackupMonth ]; then
mkdir $HomeDir/$BackupDir/$BackupYear/$BackupMonth
fi

#Step #02 - command to be executed:
cp ~/log/*-access.log.gz ~/$BackupDir/$BackupYear/$BackupMonth/

# end of script

More details at MrCompTech.com (http://MrCompTech.com)