Remove timestamps from Telegram notifications

This commit is contained in:
Codex
2026-03-17 13:11:03 +08:00
parent 7ab26712d0
commit d60fb93587
3 changed files with 12 additions and 17 deletions
+4 -4
View File
@@ -39,7 +39,7 @@ on_error() {
local tail_log local tail_log
log "ERROR" "脚本异常退出(exit=$exit_code, line=${line_no})." log "ERROR" "脚本异常退出(exit=$exit_code, line=${line_no})."
tail_log="$(tail -n 80 "${LOG_FILE:-./backup.log}" 2>/dev/null || true)" tail_log="$(tail -n 80 "${LOG_FILE:-./backup.log}" 2>/dev/null || true)"
tg_backup_error "$LABEL" "$(date +'%Y-%m-%d %H:%M:%S')" "脚本异常退出(exit=${exit_code}, line=${line_no}" "$tail_log" tg_backup_error "$LABEL" "脚本异常退出(exit=${exit_code}, line=${line_no}" "$tail_log"
exit "$exit_code" exit "$exit_code"
} }
trap on_error ERR trap on_error ERR
@@ -525,7 +525,7 @@ gen_sha
if $DRY_RUN; then if $DRY_RUN; then
log "INFO" "Dry-run 模式:不执行上传。归档:${ARCHIVE_BASE} 大小:${BACKUP_SIZE_HUMAN}" log "INFO" "Dry-run 模式:不执行上传。归档:${ARCHIVE_BASE} 大小:${BACKUP_SIZE_HUMAN}"
log "INFO" "本次总用时:${SECONDS}s" log "INFO" "本次总用时:${SECONDS}s"
tg_backup_dryrun "$LABEL" "$(date +'%Y-%m-%d %H:%M:%S')" "$(printf '%s\n' "${BACKUP_PATHS[@]}")" "$(tg_read_user_excludes "$USER_EXCLUDE_FILE")" "$ARCHIVE_BASE" "$BACKUP_SIZE_HUMAN" "${PACK_SECONDS}s" tg_backup_dryrun "$LABEL" "$(printf '%s\n' "${BACKUP_PATHS[@]}")" "$(tg_read_user_excludes "$USER_EXCLUDE_FILE")" "$ARCHIVE_BASE" "$BACKUP_SIZE_HUMAN" "${PACK_SECONDS}s"
if $SELF_CHECK; then if $SELF_CHECK; then
rm -f "$ARCHIVE_PATH" "$SHA_PATH" || true rm -f "$ARCHIVE_PATH" "$SHA_PATH" || true
log "INFO" "Self-check 模式:已删除本地自检归档与校验文件" log "INFO" "Self-check 模式:已删除本地自检归档与校验文件"
@@ -538,7 +538,7 @@ retry 3 "上传到 OBS" upload || { log "ERROR" "上传失败"; exit 4; }
if ! verify_remote; then if ! verify_remote; then
local_tail_log="$(tail -n 80 "${LOG_FILE:-./backup.log}" 2>/dev/null || true)" local_tail_log="$(tail -n 80 "${LOG_FILE:-./backup.log}" 2>/dev/null || true)"
log "ERROR" "上传后完整性校验失败" log "ERROR" "上传后完整性校验失败"
tg_backup_error "$LABEL" "$(date +'%Y-%m-%d %H:%M:%S')" "上传后完整性校验失败:${ARCHIVE_BASE}" "$local_tail_log" tg_backup_error "$LABEL" "上传后完整性校验失败:${ARCHIVE_BASE}" "$local_tail_log"
exit 5 exit 5
fi fi
@@ -550,6 +550,6 @@ fi
TOTAL_SECS=$SECONDS TOTAL_SECS=$SECONDS
SPEED_HUMAN="$(numfmt --to=iec "$AVG_SPEED")/s" SPEED_HUMAN="$(numfmt --to=iec "$AVG_SPEED")/s"
log "INFO" "完成:归档大小=${BACKUP_SIZE_HUMAN} 打包=${PACK_SECONDS}s 上传=${UP_SECONDS}s 平均速率=${SPEED_HUMAN} 总用时=${TOTAL_SECS}s" log "INFO" "完成:归档大小=${BACKUP_SIZE_HUMAN} 打包=${PACK_SECONDS}s 上传=${UP_SECONDS}s 平均速率=${SPEED_HUMAN} 总用时=${TOTAL_SECS}s"
tg_backup_success "$LABEL" "$(date +'%Y-%m-%d %H:%M:%S')" "$(printf '%s\n' "${BACKUP_PATHS[@]}")" "$(tg_read_user_excludes "$USER_EXCLUDE_FILE")" "$BACKUP_SIZE_HUMAN" "$ARCHIVE_BASE" "obs://${OBS_BUCKET}/${OBS_BACKUP_DIR}/${ARCHIVE_BASE}" "${PACK_SECONDS}s" "${UP_SECONDS}s" "$SPEED_HUMAN" tg_backup_success "$LABEL" "$(printf '%s\n' "${BACKUP_PATHS[@]}")" "$(tg_read_user_excludes "$USER_EXCLUDE_FILE")" "$BACKUP_SIZE_HUMAN" "$ARCHIVE_BASE" "obs://${OBS_BUCKET}/${OBS_BACKUP_DIR}/${ARCHIVE_BASE}" "${PACK_SECONDS}s" "${UP_SECONDS}s" "$SPEED_HUMAN"
exit 0 exit 0
+3 -3
View File
@@ -29,7 +29,7 @@ on_error() {
local tail_log local tail_log
log "ERROR" "清理异常退出(exit=${exit_code})" log "ERROR" "清理异常退出(exit=${exit_code})"
tail_log="$(tail -n 80 "${LOG_FILE:-./backup.log}" 2>/dev/null || true)" tail_log="$(tail -n 80 "${LOG_FILE:-./backup.log}" 2>/dev/null || true)"
tg_clean_error "$LABEL" "$(date +'%Y-%m-%d %H:%M:%S')" "脚本异常退出(exit=${exit_code}" "$tail_log" tg_clean_error "$LABEL" "脚本异常退出(exit=${exit_code}" "$tail_log"
exit "$exit_code" exit "$exit_code"
} }
trap on_error ERR trap on_error ERR
@@ -89,7 +89,7 @@ LIST_OUT="$(mktemp)"
if ! obsutil ls "obs://${OBS_BUCKET}/${TARGET_PREFIX}" >"$LIST_OUT" 2>/dev/null; then if ! obsutil ls "obs://${OBS_BUCKET}/${TARGET_PREFIX}" >"$LIST_OUT" 2>/dev/null; then
log "ERROR" "无法列举 obs://${OBS_BUCKET}/${TARGET_PREFIX}" log "ERROR" "无法列举 obs://${OBS_BUCKET}/${TARGET_PREFIX}"
rm -f "$LIST_OUT" rm -f "$LIST_OUT"
tg_clean_error "$LABEL" "$(date +'%Y-%m-%d %H:%M:%S')" "无法列举 obs://${OBS_BUCKET}/${TARGET_PREFIX}" "" tg_clean_error "$LABEL" "无法列举 obs://${OBS_BUCKET}/${TARGET_PREFIX}" ""
exit 6 exit 6
fi fi
@@ -136,7 +136,7 @@ else
done done
fi fi
tg_clean_success "$LABEL" "$(date +'%Y-%m-%d %H:%M:%S')" "obs://${OBS_BUCKET}/${TARGET_PREFIX}" "${RETAIN_DAYS}" "${#TO_KEEP[@]}" "${DEL_OK}" "${DEL_FAIL}" tg_clean_success "$LABEL" "obs://${OBS_BUCKET}/${TARGET_PREFIX}" "${RETAIN_DAYS}" "${#TO_KEEP[@]}" "${DEL_OK}" "${DEL_FAIL}"
if (( DEL_FAIL > 0 )); then if (( DEL_FAIL > 0 )); then
exit 6 exit 6
fi fi
+5 -10
View File
@@ -79,7 +79,7 @@ tg_read_user_excludes() {
} }
tg_backup_success() { tg_backup_success() {
local label="$1" current_date="$2" backup_dirs="$3" user_excludes="$4" backup_size="$5" archive_name="$6" location="$7" pack_time="$8" upload_time="$9" speed="${10}" local label="$1" backup_dirs="$2" user_excludes="$3" backup_size="$4" archive_name="$5" location="$6" pack_time="$7" upload_time="$8" speed="$9"
local archive_info message local archive_info message
archive_info=$( archive_info=$(
cat <<EOF cat <<EOF
@@ -93,7 +93,6 @@ EOF
) )
message="*备份成功*"$'\n\n' message="*备份成功*"$'\n\n'
message+="*主机*"$'\n'"$(tg_code_block "$label")"$'\n' message+="*主机*"$'\n'"$(tg_code_block "$label")"$'\n'
message+="*时间*"$'\n'"$(tg_code_block "$current_date")"$'\n'
message+="*备份内容*"$'\n'"$(tg_code_block "$backup_dirs")"$'\n' message+="*备份内容*"$'\n'"$(tg_code_block "$backup_dirs")"$'\n'
message+="*用户排除*"$'\n'"$(tg_code_block "$user_excludes")"$'\n' message+="*用户排除*"$'\n'"$(tg_code_block "$user_excludes")"$'\n'
message+="*归档信息*"$'\n'"$(tg_code_block "$archive_info")" message+="*归档信息*"$'\n'"$(tg_code_block "$archive_info")"
@@ -101,7 +100,7 @@ EOF
} }
tg_backup_dryrun() { tg_backup_dryrun() {
local label="$1" current_date="$2" backup_dirs="$3" user_excludes="$4" archive_name="$5" backup_size="$6" pack_time="$7" local label="$1" backup_dirs="$2" user_excludes="$3" archive_name="$4" backup_size="$5" pack_time="$6"
local archive_info message local archive_info message
archive_info=$( archive_info=$(
cat <<EOF cat <<EOF
@@ -112,7 +111,6 @@ EOF
) )
message="*备份自检*"$'\n\n' message="*备份自检*"$'\n\n'
message+="*主机*"$'\n'"$(tg_code_block "$label")"$'\n' message+="*主机*"$'\n'"$(tg_code_block "$label")"$'\n'
message+="*时间*"$'\n'"$(tg_code_block "$current_date")"$'\n'
message+="*备份内容*"$'\n'"$(tg_code_block "$backup_dirs")"$'\n' message+="*备份内容*"$'\n'"$(tg_code_block "$backup_dirs")"$'\n'
message+="*用户排除*"$'\n'"$(tg_code_block "$user_excludes")"$'\n' message+="*用户排除*"$'\n'"$(tg_code_block "$user_excludes")"$'\n'
message+="*归档信息*"$'\n'"$(tg_code_block "$archive_info")" message+="*归档信息*"$'\n'"$(tg_code_block "$archive_info")"
@@ -120,11 +118,10 @@ EOF
} }
tg_backup_error() { tg_backup_error() {
local label="$1" current_date="$2" reason="$3" error_log="$4" local label="$1" reason="$2" error_log="$3"
local message local message
message="*备份失败*"$'\n\n' message="*备份失败*"$'\n\n'
message+="*主机*"$'\n'"$(tg_code_block "$label")"$'\n' message+="*主机*"$'\n'"$(tg_code_block "$label")"$'\n'
message+="*时间*"$'\n'"$(tg_code_block "$current_date")"$'\n'
message+="*原因*"$'\n'"$(tg_code_block "$reason")" message+="*原因*"$'\n'"$(tg_code_block "$reason")"
if [[ -n "$error_log" ]]; then if [[ -n "$error_log" ]]; then
message+=$'\n'"*错误日志*"$'\n'"$(tg_code_block "$error_log")" message+=$'\n'"*错误日志*"$'\n'"$(tg_code_block "$error_log")"
@@ -133,7 +130,7 @@ tg_backup_error() {
} }
tg_clean_success() { tg_clean_success() {
local label="$1" current_date="$2" prefix="$3" retain_days="$4" keep_count="$5" del_ok="$6" del_fail="$7" local label="$1" prefix="$2" retain_days="$3" keep_count="$4" del_ok="$5" del_fail="$6"
local summary message local summary message
summary=$( summary=$(
cat <<EOF cat <<EOF
@@ -146,17 +143,15 @@ EOF
) )
message="*清理完成*"$'\n\n' message="*清理完成*"$'\n\n'
message+="*主机*"$'\n'"$(tg_code_block "$label")"$'\n' message+="*主机*"$'\n'"$(tg_code_block "$label")"$'\n'
message+="*时间*"$'\n'"$(tg_code_block "$current_date")"$'\n'
message+="*结果*"$'\n'"$(tg_code_block "$summary")" message+="*结果*"$'\n'"$(tg_code_block "$summary")"
tg_send_message "$message" tg_send_message "$message"
} }
tg_clean_error() { tg_clean_error() {
local label="$1" current_date="$2" reason="$3" error_log="$4" local label="$1" reason="$2" error_log="$3"
local message local message
message="*清理失败*"$'\n\n' message="*清理失败*"$'\n\n'
message+="*主机*"$'\n'"$(tg_code_block "$label")"$'\n' message+="*主机*"$'\n'"$(tg_code_block "$label")"$'\n'
message+="*时间*"$'\n'"$(tg_code_block "$current_date")"$'\n'
message+="*原因*"$'\n'"$(tg_code_block "$reason")" message+="*原因*"$'\n'"$(tg_code_block "$reason")"
if [[ -n "$error_log" ]]; then if [[ -n "$error_log" ]]; then
message+=$'\n'"*错误日志*"$'\n'"$(tg_code_block "$error_log")" message+=$'\n'"*错误日志*"$'\n'"$(tg_code_block "$error_log")"