根据docker服务保存日志脚本,时间可选版本

bash 复制代码
#!/bin/bash

# 获取docker ps输出,提取容器ID和名称,并存入数组
container_ids=($(docker ps -q))
container_names=($(docker ps --format '{{.Names}}'))

# 检查是否有运行中的容器
if [ ${#container_ids[@]} -eq 0 ]; then
    echo "No running containers found."
    exit 1
fi

# 显示容器列表供用户选择
echo "Running containers:"
for i in "${!container_ids[@]}"; do
    echo "[$i] ${container_names[$i]} (${container_ids[$i]})"
done

# 请求用户选择容器
read -p "Enter the index of the container to view logs: " choice

# 验证用户输入是否为有效数字
re='^[0-9]+$'
if ! [[ $choice =~ $re ]]; then
   echo "Error: Invalid input, please enter a number."
   exit 1
fi

# 验证用户选择是否在范围内
if (( choice < 0 || choice >= ${#container_ids[@]} )); then
    echo "Error: Index out of range."
    exit 1
fi

# 获取用户选择的容器ID和名称
selected_container_id="${container_ids[$choice]}"
selected_container_name="${container_names[$choice]}"

# 功能选择菜单
echo ""
echo "Select an option:"
echo "[1] View logs in terminal"
echo "[2] Save logs to file"
read -p "Enter your choice (1 or 2): " option

case $option in
    1)
        # 查看日志功能
        read -p "Enter the number of lines to display (default is 1000): " lines
        lines=${lines:-1000}

        read -p "Enter a keyword to search for in the logs (leave empty for no search): " keyword

        if [ -z "$keyword" ]; then
            docker logs -f --tail "$lines" "$selected_container_id"
        else
            docker logs -f --tail "$lines" "$selected_container_id" | grep "$keyword"
        fi
        ;;
    2)
        # 保存日志功能
        echo ""
        echo "Save log options:"
        echo "[1] Last 2 days (default)"
        echo "[2] Last 24 hours"
        echo "[3] Last 7 days"
        echo "[4] Custom hours"
        echo "[5] All logs (since container start)"
        read -p "Enter your choice (1-5): " time_option

        case $time_option in
            1|"")
                # 最近2天 = 48小时
                since_time="48h"
                since_display="2 days (48 hours)"
                ;;
            2)
                since_time="24h"
                since_display="24 hours"
                ;;
            3)
                since_time="168h"
                since_display="7 days (168 hours)"
                ;;
            4)
                read -p "Enter number of hours: " custom_hours
                since_time="${custom_hours}h"
                since_display="${custom_hours} hours"
                ;;
            5)
                since_time=""
                since_display="all logs (since container start)"
                ;;
            *)
                echo "Invalid option, using default (2 days)"
                since_time="48h"
                since_display="2 days (48 hours)"
                ;;
        esac

        # 生成文件名
        timestamp=$(date +"%Y%m%d_%H%M%S")
        filename="${selected_container_name}_logs_${timestamp}.log"
        
        echo "Saving logs for container: $selected_container_name"
        echo "Time range: $since_display"
        echo "File: $filename"
        echo ""

        # 保存日志(根据是否指定时间范围使用不同的命令)
        if [ -z "$since_time" ]; then
            docker logs "$selected_container_id" > "$filename"
        else
            docker logs --since "$since_time" "$selected_container_id" > "$filename"
        fi
        
        if [ $? -eq 0 ]; then
            echo "Logs successfully saved to: $filename"
            echo "File size: $(du -h "$filename" | cut -f1)"
            echo "Lines saved: $(wc -l < "$filename")"
        else
            echo "Error: Failed to save logs"
            # 清理可能创建的空文件
            if [ -f "$filename" ] && [ ! -s "$filename" ]; then
                rm "$filename"
            fi
            exit 1
        fi
        ;;
    *)
        echo "Invalid option selected"
        exit 1
        ;;
esac

正确的时间格式:Docker logs 的 --since 参数支持以下格式:

10m - 10分钟

2h - 2小时

30h - 30小时

2007-01-02T15:04:05 - 具体时间戳

提供多个预设选项:

2天 = 48小时

24小时

7天 = 168小时

自定义小时数

所有日志

更好的错误处理:如果保存失败,会清理空文件

Docker logs --since 支持的格式:

10m - 10分钟前到现在

1h30m - 1小时30分钟前到现在

2h - 2小时前到现在

2013-01-02T13:23:37 - 从特定时间点开始

现在脚本应该能正常工作,不会出现时间格式错误了!

相关推荐
张忠琳5 分钟前
【NVIDIA】NVIDIA k8s-device-plugin v0.19.3 资源管理器模块深度分析之三
云原生·容器·架构·kubernetes·nvidia
图件制作GIS,储量计算10 分钟前
未来已到,抓住机遇
运维·服务器
CIO_Alliance27 分钟前
AI+iPaaS解决方案:跨系统业务流程自动化助力企业AI化转型
运维·人工智能·自动化
dok121 小时前
Johannes 《Linux内核模块与设备驱动开发:编写Linux驱动程序》(4) devicefile 设备号相关
linux·运维·服务器
毛驴赶鹿1 小时前
低配置服务器怎么搭建监控?Komari Docker部署与公网访问完整教程
运维·服务器·docker
Fu2067211 小时前
结课项目考试
linux·运维·服务器
MXsoft6182 小时前
## 信创运维2.0的“最后一公里”——国产设备“能用但不好用”,问题出在哪?
运维
Chengbei112 小时前
云安全漏洞挖掘SKILL、一站式云漏洞挖掘工具,支持S3爆破、IMDS探测、K8s检测与AK/SK权限利用
前端·人工智能·网络安全·云原生·容器·kubernetes·系统安全
公众号:fuwuqiBMC3 小时前
(转自“服务器BMC”)服务器BMC芯片——AST1840
运维·服务器·fpga开发
Ai拆代码的曹操3 小时前
K8s 调度器 predicate 阶段揭秘:为什么你的 Pod 总是堆在同一台机器
后端·容器