captaincore/lib/remote-scripts/archive-logs
2026-04-18 09:08:03 -04:00

53 lines
1.1 KiB
Bash
Executable file

#!/usr/bin/env bash
#
# Enumerates rotated access and error log files for archival.
#
# Kinsta rotates logs as:
# ~/logs/access.log-YYYY-MM-DD-EPOCH
# ~/logs/error.log-YYYY-MM-DD-EPOCH
# The bare access.log / error.log is the currently-writing file.
#
# Output: TSV with columns: path, basename, type (access|error), gzipped (0|1)
#
set -u
home_path="$HOME"
# Canonical Kinsta rotation: {type}.log-YYYY-MM-DD-EPOCH[.gz]
canonical_re='^(access|error)\.log-[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]+(\.gz)?$'
is_gzipped() {
local magic
magic=$(head -c 2 "$1" 2>/dev/null | od -An -tx1 | tr -d ' \n')
[ "$magic" = "1f8b" ]
}
emit_file() {
local path="$1"
local type="$2"
[ -f "$path" ] || return
[ -s "$path" ] || return
local base
base="$(basename "$path")"
[[ "$base" =~ $canonical_re ]] || return
local gzipped=0
is_gzipped "$path" && gzipped=1
printf '%s\t%s\t%s\t%d\n' "$path" "$base" "$type" "$gzipped"
}
shopt -s nullglob
for f in "$home_path"/logs/access.log-*; do
emit_file "$f" "access"
done
for f in "$home_path"/logs/error.log-*; do
emit_file "$f" "error"
done