34 lines
1.4 KiB
Bash
Executable File
34 lines
1.4 KiB
Bash
Executable File
#!/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}" --force >/dev/null
|
|
echo "OCI lifecycle policy applied: delete backup objects older than 30 days."
|