Skip to content

Instantly share code, notes, and snippets.

@ddebin
Last active September 27, 2024 14:42
Show Gist options
  • Save ddebin/348e685fad7b03c347f3d3de96800ad9 to your computer and use it in GitHub Desktop.
Save ddebin/348e685fad7b03c347f3d3de96800ad9 to your computer and use it in GitHub Desktop.
Auto-rotate backup for Scaleway instances
#!/usr/bin/env bash
# https://gist.github.com/ddebin/348e685fad7b03c347f3d3de96800ad9
# Auto-rotate backup for Scaleway instances, uses https://github.com/scaleway/scaleway-cli/
set -e
while getopts ":p:i:" FLAG; do
case "${FLAG}" in
p) PROFILE=${OPTARG} ;;
i) INSTANCE_ID=${OPTARG} ;;
:)
echo "Option -${OPTARG} requires an argument."
exit 2
;;
\?)
echo "Invalid option: -${OPTARG}."
exit 2
;;
esac
done
OPTIONS=()
if [ -n "${PROFILE}" ]; then
OPTIONS=(-p "${PROFILE}")
fi
if [ -z "${INSTANCE_ID}" ]; then
echo "Missing -i INSTANCE"
exit 2
fi
echo -n "Backing up instance #${INSTANCE_ID}... "
IMAGE_ID=$(scw instance server backup "${INSTANCE_ID}" -w -o template="{{ .ID }}" "${OPTIONS[@]}")
echo "done"
SHORT_ID=$(echo "${INSTANCE_ID}" | cksum | cut -d" " -f1)
TAG="auto-backup-${SHORT_ID}"
for ID in $(scw instance image list tags="${TAG}" -o template="{{ .ID }}" "${OPTIONS[@]}"); do
echo "Deleting old image #${ID}..."
scw instance image delete "${ID}" with-snapshots=true -o human "${OPTIONS[@]}"
done
echo "Tagging image..."
scw instance image update "${IMAGE_ID}" tags.0="${TAG}" -o template="ID:{{ .Image.ID }} {{ .Image.Tags }} Name:{{ .Image.Name }}" "${OPTIONS[@]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment