Automating Snapshots
This guide provides the steps to automate snapshots in a Triton Datacenter using Unix-like system's cron jobs, streamlining the task and ensuring regular creation of your instance snapshots.
Last updated
Was this helpful?
Was this helpful?
#!/bin/bash
instance_uuid=$(mdata-get sdc:uuid)
export TRITON_URL="https://us-central-1.api.mnx.io"
export TRITON_ACCOUNT=<portal login>
# How many snapshots to keep.
# This will remove snapshots more than this many
keep_count=2
# log file location
log_file="/var/log/snapshot.log"
export TRITON_PROFILE="env"
unset TRITON_USER
export TRITON_KEY_ID="$(ssh-keygen -l -f $HOME/.ssh/id_rsa.pub | awk '{print $2}')"
unset TRITON_TESTING
unset TRITON_PROFILE
keep=$(expr $keep_count + 1)
exec >> "${log_file}"
exec 2>&1
echo "==== STARTED $(date +'%Y%m%d_%H%M') ===="
triton instance snapshot create -w --name="$(date +'%s')" "${instance_uuid}"
# Get snapshots more than keep_count
for snap_name in $(triton instance snapshot list "${instance_uuid}" | sed -n '1!p' |\
sort -nr -k1 | awk '{ print $1 }' | tail -n +${keep}); do
triton instance snapshot delete -f -w "${instance_uuid}" "${snap_name}"
done
echo "==== FINISHED $(date +'%Y%m%d_%H%M') ===="
echo ""