日志备份shell脚本中,需要按照年和月来细分目标文件存储目录,
需要检查目标目录是否存在,避免在跨月,跨年时脚本执行报错
shell脚本名称:dividelog.sh
内容如下:
#原始文件路径
souceLogPath='/usr/local/nginx/logs'
#备份路径前缀
targetSavePathPrefix='/usr/local/nginx_backup/logs'
#目录存在检查,不存在创建
targetSaveFullPath=$targetSavePathPrefix/$(date +%Y)/$(date +%m)
if [ ! -d "$targetSaveFullPath" ]; then
mkdir -p "$targetSaveFullPath"
fi
#处理日志
mv $souceLogPath/access_y.log $targetSaveFullPath/$(date +%Y%m%d)_access_y.log
mv $souceLogPath/access_y_proxy.log $targetSaveFullPath/$(date +%Y%m%d)_access_y_proxy.log
#...
#平滑重启nginx,并重新创建所需的access日志文件
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`