Files
ncue_backup/scripts/lib/common.sh
dsyoon 3c9d9283bd Add daily backup scripts for PostgreSQL, MariaDB, and Gitea.
Enable scheduled backups of ncue.net databases and git.ncue.net repository mirrors via .env-driven shell scripts.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-26 17:02:53 +09:00

67 lines
1.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# 백업 스크립트 공통 유틸
log_ts() { date '+%Y-%m-%dT%H:%M:%S%z'; }
# 오래된 YYYYMMDD 백업 디렉터리 삭제
# 사용: prune_old_backups "$BACKUP_ROOT" "$RETENTION_DAYS"
prune_old_backups() {
local backup_root="$1"
local retention_days="$2"
local cutoff removed=0 base day entry
[[ "$retention_days" =~ ^[0-9]+$ ]] || return 0
[[ "$retention_days" -le 0 ]] && return 0
if date -d "1 day ago" +%Y%m%d >/dev/null 2>&1; then
cutoff=$(date -d "${retention_days} days ago" +%Y%m%d)
else
cutoff=$(date -v-"${retention_days}"d +%Y%m%d)
fi
shopt -s nullglob
for entry in "$backup_root"/*; do
[[ -d "$entry" ]] || continue
base=$(basename "$entry")
[[ "$base" == "latest" ]] && continue
if [[ "$base" =~ ^([0-9]{8}) ]]; then
day="${BASH_REMATCH[1]}"
if [[ "$day" < "$cutoff" ]]; then
rm -rf "$entry"
removed=$((removed + 1))
echo "[$(log_ts)] retention: removed $entry (backup date $day < cutoff $cutoff)"
fi
fi
done
shopt -u nullglob
echo "[$(log_ts)] retention: pruned ${removed} dir(s); keeping backups from ${cutoff} onward (${retention_days} days)"
}
resolve_run_dir() {
local component="$1"
local stamp="${BACKUP_STAMP:-$(date +%Y%m%d)}"
local root="${BACKUP_DIR:-$REPO_ROOT/backup}"
if [[ -n "${BACKUP_RUN_DIR:-}" ]]; then
RUN_DIR="$BACKUP_RUN_DIR/$component"
else
RUN_DIR="$root/$stamp/$component"
fi
mkdir -p "$RUN_DIR"
}
update_latest_link() {
local component="$1"
local root="${BACKUP_DIR:-$REPO_ROOT/backup}"
local stamp="${BACKUP_STAMP:-$(date +%Y%m%d)}"
local target
if [[ -n "${BACKUP_RUN_DIR:-}" ]]; then
target="$BACKUP_RUN_DIR/$component"
else
target="$root/$stamp/$component"
fi
ln -sfn "$target" "$root/latest/$component"
}