From 3952e34f09c893477e9012e0d9bd4b6ce3b3ca70 Mon Sep 17 00:00:00 2001 From: "juvenal.diaz" Date: Wed, 28 Oct 2026 08:16:59 -0600 Subject: [PATCH] feat: schedule and monitor OCI backups --- jeannie | 3 +- scripts/oci-backup | 16 ++++++++-- scripts/oci-backup-freshness | 33 +++++++++++++++++++ scripts/oci-backup-lifecycle | 33 +++++++++++++++++++ scripts/oci-backup-schedule | 61 ++++++++++++++++++++++++++++++++++++ 5 files changed, 143 insertions(+), 3 deletions(-) create mode 100755 scripts/oci-backup-freshness create mode 100755 scripts/oci-backup-lifecycle create mode 100755 scripts/oci-backup-schedule diff --git a/jeannie b/jeannie index afab99f..6fed6fe 100755 --- a/jeannie +++ b/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. diff --git a/scripts/oci-backup b/scripts/oci-backup index d25dbe5..92d979c 100755 --- a/scripts/oci-backup +++ b/scripts/oci-backup @@ -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)" diff --git a/scripts/oci-backup-freshness b/scripts/oci-backup-freshness new file mode 100755 index 0000000..1308bbd --- /dev/null +++ b/scripts/oci-backup-freshness @@ -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 diff --git a/scripts/oci-backup-lifecycle b/scripts/oci-backup-lifecycle new file mode 100755 index 0000000..af3c048 --- /dev/null +++ b/scripts/oci-backup-lifecycle @@ -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}" </dev/null +echo "OCI lifecycle policy applied: delete backup objects older than 30 days." diff --git a/scripts/oci-backup-schedule b/scripts/oci-backup-schedule new file mode 100755 index 0000000..d66498a --- /dev/null +++ b/scripts/oci-backup-schedule @@ -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 </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 </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)."