44# Version: 1.0
55# Date: 2020.6.2
66# Author: hyb
7- # Description: docker nginx 日志切割 、打包、删除过期压缩包脚本,支持多项目切割打包
7+ # Description: nginx[ docker] 容器日志切割 、打包、删除过期压缩包脚本,支持多项目切割打包
88# Notes: 配合 crontab 计划任务,每天零时对日志打包
9- # 0 0 * * * [PATH]/docker_nginx_log_cutting.sh container_name /var/www/project &> /dev/null
9+ # 0 0 * * * [PATH]/docker_nginx_log_cutting.sh container_name project_dir &> /dev/null
1010# ----------------------------------------------------------------------------------------------------
1111
1212# .log 日志文件名
@@ -42,8 +42,8 @@ test -z $nginx_logs_dir && echo ${missing_parameter_tips//[]/nginx_logs_dir} &&
4242test ! -d $nginx_logs_dir && echo " failed, $nginx_logs_dir is not a directory. is the nginx logs directory? " && exit 3
4343# 多项目支持
4444nginx_logs_dirs=($nginx_logs_dir /)
45- for i in $( ls -d $nginx_logs_dir /* / 2> /dev/null) ; do
46- nginx_logs_dirs[${# nginx_logs_dirs[@]} ]=$i
45+ for path in $( ls -d $nginx_logs_dir /* / 2> /dev/null) ; do
46+ nginx_logs_dirs[${# nginx_logs_dirs[@]} ]=$path
4747done
4848
4949# 确保 docker daemon 正在运行
@@ -56,7 +56,7 @@ test $? -ne 0 && echo "failed, the container[$nginx_container_name] was not foun
5656
5757# 确保 container 基于 nginx
5858nginx=$( docker exec $nginx_container_name bash -c " command -v nginx" )
59- test -z " $nginx " && echo " failed, nginx commands were not found in the container. is the nginx container?" && exit 6
59+ test -z $nginx && echo " failed, nginx commands were not found in the container. is the nginx container?" && exit 6
6060
6161# 确保 container 中的 nginx 正在运行
6262nginx_pid_file=" /var/run/nginx.pid"
@@ -67,8 +67,8 @@ test $? -ne 0 && echo "failed, nginx.pid file was not found." && exit 7
6767for path in ${nginx_logs_dirs[@]} ; do
6868 # access.log 处理
6969 # 切割打包
70- mv $path$access_log $path " access-${ when} .log" & > /dev/null && \
71- tar -zcf $path " access-${ when} .tar.gz" -C $path access-${ when} .log --remove-file & > /dev/null
70+ mv $path$access_log ${ path} access-$when .log & > /dev/null && \
71+ tar -zcf ${ path} access-$when .tar.gz -C $path access-$when .log --remove-file & > /dev/null
7272 # 过期处理
7373 if test $? -eq 0; then
7474 access_expires=$(( $(ls $path$access_tgz 2 > / dev/ null | wc - l) - $log_expires ))
@@ -77,8 +77,8 @@ for path in ${nginx_logs_dirs[@]}; do
7777
7878 # error.log 处理
7979 # 切割打包
80- mv $path$error_log $path $" error-${ when} .log" & > /dev/null && \
81- tar -zcf $path " error-${ when} .tar.gz" -C $path error-${ when} .log --remove-file & > /dev/null
80+ mv $path$error_log ${ path} error-$when .log & > /dev/null && \
81+ tar -zcf ${ path} error-$when .tar.gz -C $path error-$when .log --remove-file & > /dev/null
8282 # 过期处理
8383 if test $? -eq 0; then
8484 error_expires=$(( $(ls $path$error_tgz 2 > / dev/ null | wc - l) - $log_expires ))
0 commit comments