定期备份mysql的log日志文件,保留一个月
将文件压缩为gzip格式,节省空间,备份到/home/shell/myqsl_back/目录下,保留一个月
mysql_backup备份的脚本 #!/bin/bash/usr/local/mysql/bin/mysqldump -uroot -pmingqua1 --all-databases --routines --events --triggers | gzip > /home/shell/mysql_bakup/mysql_bakup_`date '+%Y-%m-%d'`.sql.gzfind /home/shell/mysql_bakup -mtime +30 -name "*.gz" -exec rm -rf {} \;
定期处理nginx的log文件
#!/bin/bash#function:cut nginx log files ##set the path to nginx log fileslog_files_path="/home/wwwlogs/"#log_files_dir=${log_files_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")log_files_dir=$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")#set nginx log files you want to cutlog_files_name=( ggg8.com_access,xxx.com_access,别的省略) #这是从/usr/local/nginx/conf/vhost/ggg8.com.conf xxx.com.conf#set the path to nginx.nginx_sbin="/usr/local/nginx/sbin/nginx"#Set how long you want to savesave_days=30cd $log_files_path#############################################Please do not modify the following script #############################################mkdir -p $log_files_dirlog_files_num=${#log_files_name[@]}#cut nginx log filesfor((i=0;i<$log_files_num;i++));do#mv ${log_files_path}${log_files_name[i]}.log ${log_files_dir}/${log_files_name[i]}_$(date -d "yesterday" +"%Y%m%d").logtar -zcvf ${log_files_dir}/${log_files_name[i]}_$(date -d "yesterday" +"%Y%m%d").tar.gz ${log_files_name[i]}.log#delete sourcerm -rf ${log_files_name[i]}.logdone#delete 30 days ago nginx log filesfind $log_files_path -mtime +$save_days -exec rm -rf {} \;$nginx_sbin -s reload还有一个nginx定期处理脚本文件:#!/bin/bash#desc:把当前日志按日期备份,重新生成第二天的日志文件DATE=`date +%Y%m%d`NGINX_PID=`cat /var/run/nginx.pid`#如果当前Nginx没有运行就退出if [ "$?" != 0 ]then exit 1;fi#nginx 日志所在的路径LOG_PATH='/usr/local/nginx/logs/'LOG_NAME='access.log'mv ${LOG_PATH}${LOG_NAME} ${LOG_PATH}${LOG_NAME}$DATE#删除7天前旧的备份文件function deloldbak(){ olddate=`date +"%Y%m%d" -d "-$1 day"` if [ -e "${LOG_PATH}${LOG_NAME}$olddate" ] then rm -f ${LOG_PATH}${LOG_NAME}$olddate echo "${LOG_PATH}${LOG_NAME}$olddate del OK" fi}#重载nginx配置,重新生成nginx日志文件kill -USR1 $NGINX_PIDif [ "$?" == 0 ]then deloldbak 7 exit 0;fidata -d +"%Y-%m-%d"date -d "yesterday" +"%Y" date -d "yesterday" +"%m"[root@test2 ~]# date +"%Y-%m-%d"2018-03-28[root@test2 ~]# date -d "yesterday" +"%m"03[root@test2 ~]# date -d "yesterday" +"%d"27[root@test2 ~]# date -d "1 day ago" +"%Y-%m-%d"2018-03-27[root@z ~]# date -d "yesterday" +"%Y"2018
crontab -e
mkdir -p /home/shell/
* 0 * * * sh /home/shell/cut_nginx_logs.sh >> /dev/null 2>&1
0 3 * * * sh /home/shell/mysql_bakup.sh >> /dev/null 2>&1