Files
cuotibiye/deploy/backup.sh
T

35 lines
849 B
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# SQLite 在线备份:不 copy 文件(WAL 模式下可能拿到半截),用 .backup。
# 保留 14 天。放进 crontab: 15 3 * * * /opt/errortopic/deploy/backup.sh
set -euo pipefail
DB="${1:-/app/data/app.db}"
DIR="${2:-/backup}"
KEEP="${KEEP:-14}"
STAMP="$(date +%Y%m%d-%H%M%S)"
OUT="$DIR/app-$STAMP.db"
mkdir -p "$DIR"
if command -v sqlite3 >/dev/null 2>&1; then
sqlite3 "$DB" ".backup '$OUT'"
else
# 没装 sqlite3 就用 Python 自带模块:同样走 backup API,安全
python3 - "$DB" "$OUT" <<'PY'
import sqlite3, sys
src, dst = sys.argv[1], sys.argv[2]
s = sqlite3.connect(src)
d = sqlite3.connect(dst)
with d:
s.backup(d)
d.close(); s.close()
PY
fi
gzip -f "$OUT"
# 只留最近 N 个
ls -1t "$DIR"/app-*.db.gz 2>/dev/null | tail -n +$((KEEP + 1)) | xargs -r rm -f
echo "backup ok: $OUT.gz"