After the project is developed and deployed to the Linux environment, if its own platform does not provide log automatic backup functionality, then we may need to write a script and use the Linux crontab to do the scheduled cleanup and backup logs.
The following are the more common log cleanup scripts and crontab configuration instructions that I have written.
The design of the cleanup script is as follows:
1, the script can be based on the parameters to back up the corresponding directory of the log file
2, the script can match the corresponding date format according to the parameter the log file
3, the script can be parameterized need to back up the cycle (days)
4, the script can be parameterized need to keep the log information in the journal Directory Cycle (days)
5, the script can be parameterized to store the backup LOG directory address
Here's the script source.
#!/bin/bash-e
#date 2015-12-04
#by DUZX
#date 2015-12-23
#by DUZX
#desc fixes a log bug that does not delete the first n days of the day when there is no log
#脚本名称 clean_log.sh
#逻辑说明
#0, initialize parameter data
#1, enter log directory
#2, find yesterday's log files, and Count
#3, determine the number of log files that need to be backed up is greater than or equal to 1
#4, put yesterday's log file tar pack
#5, gzip Compressed tar pack
#6, delete tar packets, and yesterday's log files
#7, clean up the log files of n days
#8, clean up n days of backup log files
#9, Script complete
#参数说明
#$1--The log directory address that needs to be backed up. such as/usr/local/apache/logs
#$2--the directory address where the backup log compression pack is stored. such as/usr/local/apache/logs/baklogs
#$3--The backup log is saved (days) like 30
#$4--The log record of the script operation to store the directory address. such as/usr/local/apache/bin
#$5--date format such as%y%m%d
#$6--keep original log files as 7 days
###########################################
if [!-n ' $ '] | | [!-n ' $ '] | | [!-n ' $ '] | | [!-N "$"] | | [!-N "$"] | | [!-N "$"];
Then
echo "parameter input is missing, please add it and try again." "
echo "The log directory address that needs to be backed up." such as/usr/local/apache/logs "
echo "Stores the directory address of the backup log compression pack. such as/usr/local/apache/logs/baklogs "
echo "Time of backup log saved (days) like 30"
The log record of the Echo script operation resides in the directory address. such as/usr/local/apache/bin "
echo "date format like%y%m%d"
echo "Keep the original log file in days like 7"
Exit 0
Fi
Declare-r today=$ (Date +%y%m%d_%h%m%s)
Declare-r yesterday=$ (date-d ' 1 day ago ' +$5)
Declare-r cleanday=$ (date-d $ ' day ago ' +$5)
Cleanlog=$4/clean_log.log
if [!-D $];
Then
mkdir $4-p
echo "Script log does not exist, create alert log storage directory Success"
Fi
echo "${today}:" >> $cleanlog
echo "1 prepare to back up $yesterday logging" >> $cleanlog
Logdir=$1
Baklogs=$2
Bakdays=$3
echo "2 initialization data Completion" >> $cleanlog
CD ${logdir}
echo "3 Entry log directory" >> $cleanlog
Declare-r filesum=$ (ls *${yesterday}* | wc-l)
echo "4 counts the number of log records yesterday $filesum log Files" >> $cleanlog
if [!-D ${baklogs}];
Then
echo "5 backup log file directory does not exist" >> $cleanlog
mkdir ${baklogs}-P >> $cleanlog
echo "6 Create log file directory $baklogs successful. ">> $cleanlog
Fi
if ["${filesum}"-ge 1];then
echo "7 Start packing log file" >> $cleanlog
TAR-CVF ${yesterday}.log.tar *${yesterday}* >> $cleanlog
echo "8 Package Success" >> $cleanlog
echo "9 starts compressing log file" >> $cleanlog
Gzip ${yesterday}.log.tar >> $cleanlog
echo "10 compression success" >> $cleanlog
echo "11 Start Backup LOG directory" >> $cleanlog
MV ${yesterday}.log.tar.gz $baklogs >> $cleanlog
echo "12 Transfer Compressed log file Success" >> $cleanlog
echo "13 starts to clean up the $ days log file" >> $cleanlog
RM-RF $ (ls-tr *${cleanday}*) >> $cleanlog
echo "14 Clean up the $ prior log file Success" >> $cleanlog
Else
The echo "$ Days ' log is empty. ">> $cleanlog
echo "13 starts to clean up the $ days log file" >> $cleanlog
RM-RF $ (ls-tr *${cleanday}*) >> $cleanlog
echo "14 Clean up the $ prior log file Success" >> $cleanlog
Exit 0
Fi
echo "15 prepare to clean up ${bakdays log backup files before days" >> $cleanlog
CD ${baklogs}
echo "16 into backup directory" >> $cleanlog
Declare-r baksum=$ (ls * | wc-l) >> $cleanlog
if ["${baksum}"-gt ${bakdays}];
Then
Declare-i Delsum=${baksum}-${bakdays}
Declare-r delbakfiles=$ (ls-tr *.gz | sort | head-n ${delsum})
echo "17 counts the backup log files that need to be cleaned as follows:" >> $cleanlog
echo "${delbakfiles}" >> $cleanlog
echo "18 starts cleaning up backup LOG" >> $cleanlog
RM-RF ${delbakfiles}
echo "19 Clean backup Log Success" >> $cleanlog
Else
echo "The number of log files ${baksum}, not exceeding the quota to be cleaned up" >> $cleanlog
Fi
echo "End" >> $cleanlog
############################################
# #脚本完成
The following is the operation permission to configure the SH script
chmod 755 clean_log.sh
Here is the configuration crontab
Access to the Linux operating system
Input CRONTAB-E
Enter crontab edit mode, and type the following (I am defining a scheduled task 1:0 A.M. 0 seconds per day)
0 1 * * */usr/local/apache/bin/clean_log.sh/usr/local/apache/logs/usr/local/apache/logs/baklogs 30/usr/local/apache /bin \%y\%m\%d 7
It should be noted that there are% of the parameters, so you need to use a backslash to handle.