From 31f111d812d0e0123a70e26f550c2e489507fbe7 Mon Sep 17 00:00:00 2001 From: Wang Defa <2-wangdefa@users.noreply.gitlab.bcde.io> Date: Thu, 25 Dec 2025 15:49:14 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20tar=20=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=E8=BE=93=E5=87=BA=E5=B9=B2=E6=89=B0=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E8=BF=94=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() 函数使用 echo 返回文件路径 - 但 tar 命令的 2>&1 将 stderr 重定向到 stdout - tar 的警告信息(如 "Removing leading '/' from member names") 被 $(backup_folders) 捕获到 folders_tar 变量 - 导致 merge_backups() 收到的不是文件路径而是 tar 输出 - 最终备份文件中缺少 folders 目录 修复方案: - 将 tar 命令改为 2>&1 >&2 - 先将 stderr 重定向到 stdout (2>&1) - 再将合并后的 stdout 重定向到 stderr (>&2) - 确保 tar 的所有输出都到 stderr,不干扰函数返回值 影响: - 修复后文件夹备份会正确包含在最终备份文件中 - 用户可以正常恢复文件夹数据 --- bin/backup.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bin/backup.sh b/bin/backup.sh index d4eba8c..7dc32a5 100755 --- a/bin/backup.sh +++ b/bin/backup.sh @@ -209,7 +209,8 @@ backup_folders() { # 执行打包 log_info "开始打包文件夹..." log_info "排除规则: ${exclude_args}" - if eval "tar -czf '${folders_tar}' ${exclude_args} ${tar_sources}" 2>&1; then + # 将 tar 输出重定向到 stderr,避免干扰函数返回值 + if eval "tar -czf '${folders_tar}' ${exclude_args} ${tar_sources}" 2>&1 >&2; then local tar_size=$(du -h "${folders_tar}" | cut -f1) log_info "文件夹备份完成: ${folders_tar} (大小: ${tar_size})" echo "${folders_tar}"