fix: 修复日志输出干扰函数返回值的问题

问题描述:
- backup_folders() 和 backup_mysql() 函数使用 echo 返回文件路径
- 但 log_info 也输出到 stdout,导致主函数捕获了所有日志而非路径
- 最终 merge_backups 收到空参数,显示"没有需要打包的文件"

修复内容:
- 将所有日志输出重定向到 stderr (>&2)
- 简化 tar 命令的输出处理逻辑
- 确保函数返回值只包含文件路径

影响文件:
- bin/backup.sh - log() 函数添加 >&2 重定向
- bin/cleanup.sh - 所有 log 函数添加 >&2 重定向
This commit is contained in:
Wang Defa
2025-12-25 15:40:36 +08:00
parent 4d00283654
commit 1ea8d681ee
2 changed files with 6 additions and 9 deletions

View File

@@ -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

View File

@@ -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
}
###############################################################################