diff --git a/jeannie b/jeannie index a51cb18..92ad12d 100755 --- a/jeannie +++ b/jeannie @@ -140,6 +140,7 @@ 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} Validate OCI Object Storage backup prerequisites (read-only). synthetic-checks Run end-to-end service probes. resource-budget Report Kubernetes resource request/limit gaps. route-inventory Report routes and Uptime Kuma coverage. @@ -503,7 +504,11 @@ case "${1:-}" in backup_gitea ;; backup) - homelab_backup + if [[ "${2:-}" == "oci" ]]; then + backup_oci_status "${3:-status}" + else + homelab_backup + fi ;; drill-restore) drill_restore diff --git a/lib/jeannie/commands.sh b/lib/jeannie/commands.sh index 11bbc8b..0894851 100644 --- a/lib/jeannie/commands.sh +++ b/lib/jeannie/commands.sh @@ -4374,6 +4374,11 @@ backup_status() { "${REPO_ROOT}/scripts/backup-status" } +backup_oci_status() { + require_debian_server "backup oci" + "${REPO_ROOT}/scripts/oci-backup-status" "${1:-status}" +} + install_homelab_backup_timer() { local service_file="/etc/systemd/system/homelab-profile-backup.service" local timer_file="/etc/systemd/system/homelab-profile-backup.timer" diff --git a/scripts/oci-backup-status b/scripts/oci-backup-status new file mode 100755 index 0000000..a58f06d --- /dev/null +++ b/scripts/oci-backup-status @@ -0,0 +1,97 @@ +#!/usr/bin/env bash +set -euo pipefail + +REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +ACTION="${1:-status}" +OCI_PROFILE="${OCI_CLI_PROFILE:-${OCI_CONFIG_PROFILE:-DEFAULT}}" +OCI_CONFIG="${OCI_CONFIG_FILE:-${HOME}/.oci/config}" +OCI_KEY="${OCI_PRIVATE_KEY_PATH:-${HOME}/.oci/oci_api_key.pem}" +OCI_REGION="${LAB_OCI_REGION:-${OCI_REGION:-}}" +BUCKET="${LAB_OCI_BACKUP_BUCKET:-${OCI_OBJECT_STORAGE_BUCKET:-}}" +PREFIX="${LAB_OCI_BACKUP_PREFIX:-homelab/}" +SOPS_CONFIG="${SOPS_CONFIG:-${REPO_ROOT}/.sops.yaml}" +AGE_KEY_FILE="${SOPS_AGE_KEY_FILE:-${HOME}/.config/sops/age/keys.txt}" +failures=0 + +fail() { + printf 'FAIL %-24s %s\n' "$1" "$2" + failures=$((failures + 1)) +} + +ok() { + printf 'OK %-24s %s\n' "$1" "$2" +} + +if [[ "${ACTION}" != status && "${ACTION}" != plan ]]; then + echo "Usage: ./jeannie backup oci {status|plan}" >&2 + exit 2 +fi + +printf 'OCI Object Storage backup %s\n' "${ACTION}" +printf 'region=%s profile=%s bucket=%s prefix=%s\n' \ + "${OCI_REGION:-unset}" "${OCI_PROFILE}" "${BUCKET:-unset}" "${PREFIX}" +printf 'No uploads, deletes, bucket changes, or lifecycle changes are performed.\n' + +oci_bin="" +if command -v oci >/dev/null 2>&1; then + oci_bin="$(command -v oci)" +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 [[ -z "${oci_bin}" ]]; then + fail "OCI CLI" "not installed; run ./jeannie oci-auth install" +elif [[ ! -r "${OCI_CONFIG}" || ! -r "${OCI_KEY}" ]]; then + fail "OCI credentials" "config or private key is missing" +else + if [[ -z "${OCI_REGION}" ]]; then + fail "OCI region" "set LAB_OCI_REGION or OCI_REGION" + else + if "${oci_bin}" iam region list --config-file "${OCI_CONFIG}" --profile "${OCI_PROFILE}" \ + --region "${OCI_REGION}" --query 'data[0].name' --raw-output >/dev/null 2>&1; then + ok "OCI authentication" "API request succeeded" + else + fail "OCI authentication" "API request failed" + fi + fi +fi + +namespace="" +if [[ -z "${BUCKET}" ]]; then + fail "Object Storage bucket" "set LAB_OCI_BACKUP_BUCKET" +elif [[ -z "${oci_bin}" || -z "${OCI_REGION}" ]]; then + fail "Object Storage bucket" "cannot check bucket without OCI CLI and region" +else + namespace="$(${oci_bin} os ns get --config-file "${OCI_CONFIG}" --profile "${OCI_PROFILE}" \ + --region "${OCI_REGION}" --query data --raw-output 2>/dev/null || true)" + if [[ -z "${namespace}" ]]; then + fail "Object Storage namespace" "namespace lookup failed" + elif "${oci_bin}" os bucket get --namespace-name "${namespace}" --bucket-name "${BUCKET}" \ + --config-file "${OCI_CONFIG}" --profile "${OCI_PROFILE}" --region "${OCI_REGION}" \ + --query 'data.name' --raw-output >/dev/null 2>&1; then + ok "Object Storage bucket" "${BUCKET} exists (namespace ${namespace})" + else + fail "Object Storage bucket" "${BUCKET} is missing or inaccessible" + fi +fi + +recipient="${HOMELAB_BACKUP_AGE_RECIPIENT:-}" +if [[ -z "${recipient}" && -r "${SOPS_CONFIG}" ]]; then + recipient="$(awk '/^[[:space:]]*age:[[:space:]]*age1/ { print $2; exit }' "${SOPS_CONFIG}")" +fi +if [[ "${recipient}" =~ ^age1[0-9a-z]+$ ]]; then + ok "Backup encryption" "age recipient configured (${recipient:0:12}...)" +else + fail "Backup encryption" "no valid age recipient in env or ${SOPS_CONFIG}" +fi + +if [[ -r "${AGE_KEY_FILE}" ]] && grep -Eq '^[[:space:]]*AGE-SECRET-KEY-' "${AGE_KEY_FILE}"; then + ok "Restore identity" "age identity is readable at ${AGE_KEY_FILE}" +else + fail "Restore identity" "age identity is missing or unreadable at ${AGE_KEY_FILE}" +fi + +printf 'planned_upload_prefix=%s\n' "${PREFIX}" +printf 'planned_restore_verification=download latest archive, decrypt with age, verify checksum, list tar contents\n' +printf 'failures=%s\n' "${failures}" +(( failures == 0 ))