#复制错误日志到指定的目录,并使用时间对错误日志进行命名,随时监控备份的错误日志数量,如果备份的错误日志超过了7天,便删除最早那天的备份文件,保证的备份的错误日志不超过7份。其中使用到了if判断语句,判断备份数量是否大于7,如果判断为真,则执行rm。
#!/bin/bash
sj=$(date +%F)
cp /var/log/nginx/error.log /nginx.error.log.bf/$sj
bfsl=$(ls -l /nginx.error.log.bf |wc -l)
wj=$(ls -t /nginx.error.log.bf/ |tail -1)
if
test $bfsl -gt 7;
then
rm -f /nginx.error.log.bf/$wj
fi
#脚本结合Crond计划任务一起使用,可以在/etc/crontab文件中配置定时任务,每天进行一次备份,例如:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
30 12 * * * root /root/dsrw.sh