根据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 - 从特定时间点开始

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

相关推荐
大树8816 小时前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠16 小时前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质16 小时前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
Inhand陈工17 小时前
基于台达PLC与映翰通IG502的智慧水产养殖精准投喂与远程运维解决方案
运维·人工智能·物联网·阿里云·信息与通信
Alsn8617 小时前
等待学习-学习目录:Docker 容器安全攻防
学习·安全·docker
酣大智18 小时前
ARP代理--工作原理
运维·网络·arp·arp代理
shushangyun_18 小时前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
施努卡机器视觉19 小时前
SNK施努卡侧滑门锁上滑轮总成自动化装配线,从零件到组件,全流程精密制造方案
运维·自动化·制造
AC赳赳老秦20 小时前
用 OpenClaw 搭建服务器故障应急响应系统,自动处理 80% 常见运维故障
android·运维·服务器·python·rxjava·deepseek·openclaw
2601_9618752420 小时前
决战申论100题2026|最新|范文
linux·容器·centos·debian·ssh·fabric·vagrant