Files
xamxam/scripts/migrate-log-names.sh
T

152 lines
6.5 KiB
Bash
Executable File
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.
#!/bin/bash
# migrate-log-names.sh — rename (and relocate) pre-existing log files to the
# xamxam-{service}-{date}.log convention under the standard /var/log root.
#
# Why: the app adopted a uniform log naming scheme where production app logs
# live under /var/log/xamxam/ (system standard), nginx under /var/log/nginx/,
# and cron jobs under /var/log/:
# /var/log/xamxam/xamxam-{channel}-YYYY-MM-DD.log (app/admin/error/audit)
# /var/log/nginx/xamxam-nginx-{access,error}.log (nginx, fixed live name)
# /var/log/xamxam-{backup,cleanup}-YYYY-MM-DD.log (cron jobs)
# but logs written before that change still carry the old names/locations
# (storage/logs/admin-*.log, xamxam_error.log, sqlite-backup.log, …). This
# script renames and moves those in place so they follow the same pattern and
# stay visible to the admin log viewer (which globs for xamxam-{channel}-*.log).
#
# Behaviour:
# - Idempotent: files already matching the target pattern are skipped.
# - Non-destructive: never overwrites an existing destination. If the
# destination already exists the source is left untouched and a warning
# is printed so you can merge/decide manually.
# - Dry-run by default; pass --apply to actually rename.
# - Uses each file's own mtime as its date (app/cron logs embed the date in
# their name already; nginx/cron flat files derive one from mtime).
#
# Usage (run on the server):
# sudo bash /tmp/migrate-log-names.sh # preview only
# sudo bash /tmp/migrate-log-names.sh --apply # perform the renames
#
# Install: just deploy-script migrate-log-names (uploads this file to /tmp)
set -euo pipefail
# ── Helpers ────────────────────────────────────────────────────────────────────
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; NC='\033[0m'
info() { printf "ℹ️ %s\n" "$*"; }
ok() { printf "${GREEN}✓${NC} %s\n" "$*"; }
warn() { printf "${YELLOW}!${NC} %s\n" "$*"; }
err() { printf "${RED}✗${NC} %s\n" "$*" >&2; }
APPLY=0
[ "${1:-}" = "--apply" ] && APPLY=1
APP_LOGS_OLD_DIR="/var/www/xamxam/storage/logs"
APP_LOGS_DIR="/var/log/xamxam"
NGINX_LOGS_DIR="/var/log/nginx"
# ── App channels (Monolog rotating files) ─────────────────────────────────────
# App logs now live under /var/log/xamxam (production). Handle both the old
# location (storage/logs, pre-move) and files already in the new location that
# still carry the old naming (missing xamxam- prefix).
# Old: {channel}-YYYY-MM-DD.log New: xamxam-{channel}-YYYY-MM-DD.log
# Old bare base also possible: {channel}.log (no date)
CHANNELS=(app admin error audit)
changed=0
for ch in "${CHANNELS[@]}"; do
# Source directory: prefer the old storage/logs location for files that
# predate the move; also scan the new /var/log/xamxam in case a previous
# partial migration already moved (but did not rename) some files.
for srcdir in "$APP_LOGS_OLD_DIR" "$APP_LOGS_DIR"; do
[ -d "$srcdir" ] || continue
# Dated files: {channel}-YYYY-MM-DD.log → xamxam-{channel}-YYYY-MM-DD.log
for src in "$srcdir"/"$ch"-20[0-9][0-9]-[0-9][0-9]-[0-9][0-9].log; do
[ -e "$src" ] || continue
base=$(basename "$src")
dst="$APP_LOGS_DIR/xamxam-$base"
if [ -e "$dst" ]; then
warn "skip (dest exists): $base"
continue
fi
if [ "$APPLY" -eq 1 ]; then
mv "$src" "$dst" && ok "moved $srcdir/$base → $dst"
else
info "[dry-run] $srcdir/$base → $dst"
fi
changed=$((changed + 1))
done
# Bare base file (rare, pre-rotation): {channel}.log → xamxam-{channel}.log
src="$srcdir/$ch.log"
if [ -f "$src" ]; then
dst="$APP_LOGS_DIR/xamxam-$ch.log"
if [ -e "$dst" ]; then
warn "skip (dest exists): $ch.log"
elif [ "$APPLY" -eq 1 ]; then
mv "$src" "$dst" && ok "moved $srcdir/$ch.log → $dst"
else
info "[dry-run] $srcdir/$ch.log → $dst"
fi
changed=$((changed + 1))
fi
done
done
[ "$changed" -eq 0 ] && info " none found"
# ── nginx logs (fixed live name + logrotate .N / .N.gz rotations) ─────────────
# Old: xamxam_error.log[.N[.gz]] New: xamxam-nginx-error.log[.N[.gz]]
# Old: xamxam_access.log[.N[.gz]] New: xamxam-nginx-access.log[.N[.gz]]
info "nginx logs — $NGINX_LOGS_DIR"
for kind in access error; do
# Match the live file and any rotations (.1, .2.gz, …)
for src in "$NGINX_LOGS_DIR"/xamxam_"$kind".log*; do
[ -e "$src" ] || continue
base=$(basename "$src")
# strip the leading xamxam_{kind}.log, keep the rotation suffix
suffix=${base#xamxam_"$kind".log}
dst="$NGINX_LOGS_DIR/xamxam-nginx-$kind.log$suffix"
if [ -e "$dst" ]; then
warn "skip (dest exists): $base"
continue
fi
if [ "$APPLY" -eq 1 ]; then
mv "$src" "$dst" && ok "renamed $base → $(basename "$dst")"
else
info "[dry-run] $base → $(basename "$dst")"
fi
changed=$((changed + 1))
done
done
# ── Flat cron logs (derive a date from mtime) ────────────────────────────────
# Old: /var/log/sqlite-backup.log → /var/log/xamxam-backup-YYYY-MM-DD.log
# Old: /var/log/xamxam-cleanup.log → /var/log/xamxam-cleanup-YYYY-MM-DD.log
info "cron logs — /var/log"
rename_cron_log() {
local src="$1" svc="$2"
[ -f "$src" ] || return 0
local d
d=$(date -r "$src" +%Y-%m-%d 2>/dev/null || stat -c %y "$src" | cut -d' ' -f1)
local dst="/var/log/xamxam-$svc-$d.log"
if [ -e "$dst" ]; then
warn "skip (dest exists): $(basename "$src")"
return 0
fi
if [ "$APPLY" -eq 1 ]; then
mv "$src" "$dst" && ok "renamed $(basename "$src") → $(basename "$dst")"
else
info "[dry-run] $(basename "$src") → $(basename "$dst")"
fi
changed=$((changed + 1))
}
rename_cron_log /var/log/sqlite-backup.log backup
rename_cron_log /var/log/xamxam-cleanup.log cleanup
echo ""
if [ "$APPLY" -eq 0 ]; then
info "Dry-run complete. Re-run with --apply to perform the renames."
else
ok "Renames complete ($changed changed)."
fi