From 1ea8d681ee30926a638b7c4133396609f5e90967 Mon Sep 17 00:00:00 2001 From: Wang Defa <2-wangdefa@users.noreply.gitlab.bcde.io> Date: Thu, 25 Dec 2025 15:40:36 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E8=BE=93=E5=87=BA=E5=B9=B2=E6=89=B0=E5=87=BD=E6=95=B0=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E5=80=BC=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题描述: - backup_folders() 和 backup_mysql() 函数使用 echo 返回文件路径 - 但 log_info 也输出到 stdout,导致主函数捕获了所有日志而非路径 - 最终 merge_backups 收到空参数,显示"没有需要打包的文件" 修复内容: - 将所有日志输出重定向到 stderr (>&2) - 简化 tar 命令的输出处理逻辑 - 确保函数返回值只包含文件路径 影响文件: - bin/backup.sh - log() 函数添加 >&2 重定向 - bin/cleanup.sh - 所有 log 函数添加 >&2 重定向 --- bin/backup.sh | 9 +++------ bin/cleanup.sh | 6 +++--- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/bin/backup.sh b/bin/backup.sh index 8e2b544..38e3933 100755 --- a/bin/backup.sh +++ b/bin/backup.sh @@ -33,7 +33,8 @@ log() { local message="$@" local timestamp=$(date '+%Y-%m-%d %H:%M:%S') - echo -e "[${timestamp}] [${level}] ${message}" + # 输出到 stderr,避免干扰函数返回值 + echo -e "[${timestamp}] [${level}] ${message}" >&2 # 如果配置了日志文件,同时写入文件 if [[ -n "${LOG_FILE}" ]] && [[ "${LOGGING_ENABLED}" == "true" ]]; then @@ -207,11 +208,7 @@ backup_folders() { # 执行打包 log_info "开始打包文件夹..." - eval "tar -czf '${folders_tar}' ${exclude_args} ${tar_sources}" 2>&1 | while read line; do - log_info "$line" - done - - if [[ -f "${folders_tar}" ]]; then + if eval "tar -czf '${folders_tar}' ${exclude_args} ${tar_sources}" 2>&1; then log_info "文件夹备份完成: ${folders_tar}" echo "${folders_tar}" else diff --git a/bin/cleanup.sh b/bin/cleanup.sh index e6c0c3a..c4a1876 100755 --- a/bin/cleanup.sh +++ b/bin/cleanup.sh @@ -25,15 +25,15 @@ NC='\033[0m' ############################################################################### log_info() { - echo -e "${GREEN}[INFO]${NC} $@" + echo -e "${GREEN}[INFO]${NC} $@" >&2 } log_warn() { - echo -e "${YELLOW}[WARN]${NC} $@" + echo -e "${YELLOW}[WARN]${NC} $@" >&2 } log_error() { - echo -e "${RED}[ERROR]${NC} $@" + echo -e "${RED}[ERROR]${NC} $@" >&2 } ###############################################################################