Featured image of post 容器清理日志脚本

容器清理日志脚本

容器里面按照条件清理日志往往缺少find和xargs等命令,我提供一个可以更纯粹的,能在容器里运行的脚本。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/bash

containers=$(docker ps -q)

# 清理docker容器日志
function clear_container_logs {
    echo "======== start clean docker containers logs ========"
    logs=$(find /var/lib/docker/containers/ -name *-json.log)
    for log in $logs; do
        echo "clean logs : $log"
        cat /dev/null >$log
    done
    echo -e "======== end clean docker containers logs ========\n"
}

# 清理tomcat日志
function clear_tomcat_logs {
    echo "======== start clean tomcat logs ========"
    for c in $containers; do
        echo "ContainerID : $c"
        docker exec $c bash -c "
            log_dir=/srv/tomcat8/logs
            retention_days=7
            now=\$(date +%s)
            cutoff=\$((now - \$retention_days * 86400))
            if [ -d \"\$log_dir\" ]; then
                countfile=\$(ls -l \"\$log_dir\" | wc -l)
                if [ \$countfile -eq 1 ]; then
                    echo \"no logs found\"
                else
                    for log in \"\$log_dir\"/*; do
                        shopt -s globstar
                        if [ -d "\$log" ]; then
                            for logapp in "\$log"/**; do
                                if [ -f \"\$logapp\" ] && [ \$(stat -c '%Y' \"\$logapp\") -lt \$cutoff ]; then
                                    rm -fr \"\$logapp\"
                                    echo \"app logs : \$logapp removed\"
                                fi
                            done
                        fi
                        shopt -u globstar
                        if [ \$(stat -c '%Y' \"\$log\") -lt \$cutoff ]; then
                            rm -f \"\$log\"
                            echo \"tomcat logs : \$log removed\"
                        fi
                    done
                fi
            fi
        "
    done
    echo -e "======== end clean tomcat logs ======== \n"
}

# 清理Nginx日志
function clear_nginx_logs {
    echo "======== start clean nginx logs ========"
    for c in $containers; do
        echo "ContainerID : $c"
        docker exec $c bash -c "
            log_dir=/usr/local/openresty/nginx/logs
            max_size=300000000
            if [ -d \"\$log_dir\" ]; then
                for log in \$log_dir/*; do
                    if [ -f \"\$log\" ] && [ \$(stat -c %s \"\$log\") -gt \$max_size ]; then
                        > \"\$log\"
                        echo \"nginx logs : \$log cleared\"
                    fi
                done
                rm -f \$log_dir/bak/*
            fi
        "
    done
    echo "======== end clean nginx logs ========"
}

echo "======== start clean logs ========"
clear_container_logs
clear_tomcat_logs
clear_nginx_logs
echo "======== end clean logs ========"