feat: schedule and monitor OCI backups
This commit is contained in:
parent
d095d284d2
commit
3952e34f09
3
jeannie
3
jeannie
|
|
@ -141,7 +141,8 @@ Observability And GitOps
|
|||
promote {plan|validate|rollback APP} Run canary promotion gates and rollback plan.
|
||||
cert-check Check public DNS, TLS, and edge URL health.
|
||||
backup-status Check backup and restore-drill freshness.
|
||||
backup oci {status|plan|upload|restore-verify} Manage encrypted OCI Object Storage backups.
|
||||
backup oci {status|plan|upload|restore-verify|schedule|lifecycle|freshness}
|
||||
Manage encrypted OCI Object Storage backups.
|
||||
synthetic-checks Run end-to-end service probes.
|
||||
resource-budget Report Kubernetes resource request/limit gaps.
|
||||
route-inventory Report routes and Uptime Kuma coverage.
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ BACKUP_DIR="${HOMELAB_PROFILE_BACKUP_DIR:-${XDG_DATA_HOME:-${HOME}/.local/share}
|
|||
AGE_KEY_FILE="${SOPS_AGE_KEY_FILE:-${HOME}/.config/sops/age/keys.txt}"
|
||||
|
||||
usage() {
|
||||
echo "Usage: ./jeannie backup oci {status|plan|upload|restore-verify}" >&2
|
||||
echo "Usage: ./jeannie backup oci {status|plan|upload|restore-verify|schedule|lifecycle|freshness}" >&2
|
||||
}
|
||||
|
||||
oci_bin=""
|
||||
|
|
@ -23,7 +23,7 @@ elif [[ -x "${HOME}/.local/share/homelab/venvs/oci-cli/bin/oci" ]]; then
|
|||
oci_bin="${HOME}/.local/share/homelab/venvs/oci-cli/bin/oci"
|
||||
fi
|
||||
|
||||
if [[ "${ACTION}" != status && "${ACTION}" != plan && "${ACTION}" != upload && "${ACTION}" != restore-verify ]]; then
|
||||
if [[ "${ACTION}" != status && "${ACTION}" != plan && "${ACTION}" != upload && "${ACTION}" != restore-verify && "${ACTION}" != schedule && "${ACTION}" != lifecycle && "${ACTION}" != freshness ]]; then
|
||||
usage
|
||||
exit 2
|
||||
fi
|
||||
|
|
@ -35,6 +35,18 @@ if [[ -z "${oci_bin}" || ! -r "${OCI_CONFIG}" || -z "${OCI_REGION}" || -z "${BUC
|
|||
exit 1
|
||||
fi
|
||||
|
||||
case "${ACTION}" in
|
||||
schedule)
|
||||
exec "${REPO_ROOT}/scripts/oci-backup-schedule"
|
||||
;;
|
||||
lifecycle)
|
||||
exec "${REPO_ROOT}/scripts/oci-backup-lifecycle"
|
||||
;;
|
||||
freshness)
|
||||
exec "${REPO_ROOT}/scripts/oci-backup-freshness"
|
||||
;;
|
||||
esac
|
||||
|
||||
if [[ -z "${NAMESPACE}" ]]; then
|
||||
NAMESPACE="$(${oci_bin} os ns get --config-file "${OCI_CONFIG}" --profile "${OCI_PROFILE}" \
|
||||
--region "${OCI_REGION}" --query data --raw-output)"
|
||||
|
|
|
|||
|
|
@ -0,0 +1,33 @@
|
|||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
oci_bin="$(command -v oci 2>/dev/null || printf '%s' "${HOME}/.local/share/homelab/venvs/oci-cli/bin/oci")"
|
||||
config="${OCI_CONFIG_FILE:-${HOME}/.oci/config}"
|
||||
profile="${OCI_CLI_PROFILE:-${OCI_CONFIG_PROFILE:-DEFAULT}}"
|
||||
region="${LAB_OCI_REGION:-${OCI_REGION:-}}"
|
||||
bucket="${LAB_OCI_BACKUP_BUCKET:-${OCI_OBJECT_STORAGE_BUCKET:-}}"
|
||||
namespace="${LAB_OCI_BACKUP_BUCKET_NAMESPACE:-}"
|
||||
prefix="${LAB_OCI_BACKUP_PREFIX:-homelab/}"
|
||||
max_hours="${LAB_OCI_BACKUP_MAX_AGE_HOURS:-36}"
|
||||
[[ -n "${region}" && -n "${bucket}" ]] || { echo 'FAIL OCI backup bucket/region is not configured.'; exit 1; }
|
||||
if [[ -z "${namespace}" ]]; then
|
||||
namespace="$(${oci_bin} os ns get --config-file "${config}" --profile "${profile}" --region "${region}" --query data --raw-output)"
|
||||
fi
|
||||
latest="$(${oci_bin} os object list --config-file "${config}" --profile "${profile}" --region "${region}" \
|
||||
--namespace-name "${namespace}" --bucket-name "${bucket}" --prefix "${prefix%/}/homelab-pimox-profile-" --all \
|
||||
--query 'data[?ends_with(name, `.tgz.age`)].{name:name,modified:"time-modified"}' --output json)"
|
||||
python3 - "${latest}" "${max_hours}" <<'PY'
|
||||
import datetime as dt, json, sys
|
||||
items = json.loads(sys.argv[1])
|
||||
if not items:
|
||||
print("FAIL OCI backup freshness: no encrypted archive found")
|
||||
raise SystemExit(1)
|
||||
item = max(items, key=lambda x: x.get("modified") or "")
|
||||
stamp = (item.get("modified") or "").replace("Z", "+00:00")
|
||||
age = (dt.datetime.now(dt.timezone.utc) - dt.datetime.fromisoformat(stamp)).total_seconds() / 3600
|
||||
print(f"latest={item['name']} age_hours={age:.1f} threshold_hours={sys.argv[2]}")
|
||||
if age > float(sys.argv[2]):
|
||||
print("FAIL OCI backup freshness: latest archive is stale")
|
||||
raise SystemExit(1)
|
||||
print("PASS OCI backup freshness")
|
||||
PY
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
oci_bin="$(command -v oci 2>/dev/null || printf '%s' "${HOME}/.local/share/homelab/venvs/oci-cli/bin/oci")"
|
||||
config="${OCI_CONFIG_FILE:-${HOME}/.oci/config}"
|
||||
profile="${OCI_CLI_PROFILE:-${OCI_CONFIG_PROFILE:-DEFAULT}}"
|
||||
region="${LAB_OCI_REGION:-${OCI_REGION:-}}"
|
||||
bucket="${LAB_OCI_BACKUP_BUCKET:-${OCI_OBJECT_STORAGE_BUCKET:-}}"
|
||||
namespace="${LAB_OCI_BACKUP_BUCKET_NAMESPACE:-}"
|
||||
prefix="${LAB_OCI_BACKUP_PREFIX:-homelab/}"
|
||||
[[ -x "${oci_bin}" || -x "$(command -v oci 2>/dev/null || true)" ]] || { echo 'OCI CLI is not installed.' >&2; exit 1; }
|
||||
[[ -n "${region}" && -n "${bucket}" ]] || { echo 'OCI backup bucket/region is not configured.' >&2; exit 1; }
|
||||
if [[ -z "${namespace}" ]]; then
|
||||
namespace="$(${oci_bin} os ns get --config-file "${config}" --profile "${profile}" --region "${region}" --query data --raw-output)"
|
||||
fi
|
||||
policy="$(mktemp)"
|
||||
trap 'rm -f "${policy}"' EXIT
|
||||
cat >"${policy}" <<JSON
|
||||
[
|
||||
{
|
||||
"action": "DELETE",
|
||||
"isEnabled": true,
|
||||
"name": "delete-old-homelab-backups",
|
||||
"objectNameFilter": {"inclusionPrefixes": ["${prefix%/}/"]},
|
||||
"timeAmount": 30,
|
||||
"timeUnit": "DAYS"
|
||||
}
|
||||
]
|
||||
JSON
|
||||
"${oci_bin}" os object-lifecycle-policy put --config-file "${config}" --profile "${profile}" \
|
||||
--region "${region}" --namespace-name "${namespace}" --bucket-name "${bucket}" \
|
||||
--items "file://${policy}" >/dev/null
|
||||
echo "OCI lifecycle policy applied: delete backup objects older than 30 days."
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
service=/etc/systemd/system/homelab-oci-backup.service
|
||||
timer=/etc/systemd/system/homelab-oci-backup.timer
|
||||
fresh_service=/etc/systemd/system/homelab-oci-backup-freshness.service
|
||||
fresh_timer=/etc/systemd/system/homelab-oci-backup-freshness.timer
|
||||
|
||||
sudo tee "${service}" >/dev/null <<EOF
|
||||
[Unit]
|
||||
Description=Upload encrypted Jeannie backup to OCI Object Storage
|
||||
After=network-online.target
|
||||
Wants=network-online.target
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
WorkingDirectory=${repo_root}
|
||||
ExecStart=${repo_root}/jeannie backup oci upload
|
||||
EOF
|
||||
|
||||
sudo tee "${timer}" >/dev/null <<'EOF'
|
||||
[Unit]
|
||||
Description=Daily encrypted Jeannie OCI backup upload
|
||||
|
||||
[Timer]
|
||||
OnCalendar=*-*-* 03:15:00
|
||||
Persistent=true
|
||||
RandomizedDelaySec=15m
|
||||
|
||||
[Install]
|
||||
WantedBy=timers.target
|
||||
EOF
|
||||
|
||||
sudo tee "${fresh_service}" >/dev/null <<EOF
|
||||
[Unit]
|
||||
Description=Check freshness of the latest OCI backup
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
WorkingDirectory=${repo_root}
|
||||
ExecStart=${repo_root}/jeannie backup oci freshness
|
||||
EOF
|
||||
|
||||
sudo tee "${fresh_timer}" >/dev/null <<'EOF'
|
||||
[Unit]
|
||||
Description=Daily OCI backup freshness check
|
||||
|
||||
[Timer]
|
||||
OnCalendar=*-*-* 04:00:00
|
||||
Persistent=true
|
||||
|
||||
[Install]
|
||||
WantedBy=timers.target
|
||||
EOF
|
||||
|
||||
sudo systemctl daemon-reload
|
||||
sudo systemctl enable --now homelab-oci-backup.timer
|
||||
sudo systemctl enable --now homelab-oci-backup-freshness.timer
|
||||
echo "OCI backup timer enabled: daily at 03:15 with up to 15 minutes jitter."
|
||||
echo "OCI backup freshness timer enabled: daily at 04:00 (36-hour default threshold)."
|
||||
Loading…
Reference in New Issue