腾讯云服务器nginx日志归档备份

# cat /etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily
rotate 7
missingok
notifempty
dateext
sharedscripts
postrotate
    /usr/sbin/nginx -s reopen
endscript
}
 

归档脚本

#!/bin/bash
#首先进行日志切割
/usr/sbin/logrotate -f /etc/logrotate.d/nginx
if [ $? == 0 ];then
#日志切割完成后,进行日志压缩
    cd /var/log/nginx || find . -name '*.log-*' -type f -exec tar -czvf '{}.tar.gz' '{}' --remove-files \;
    if [ $? == 0 ];then
        #日志压缩完成后,进行本地文件上传到对象存储归档,清理本地文件释放空间
        current_date=`date +%Y-%m-%d`
        ls /var/log/nginx/*.tar.gz | while read line; do
            cd /cos_backup || python upload.py $line $current_date
        done
    fi
else
    exit 0
fi
 

你可能感兴趣的:(脚本)