Site icon Digital Thought Disruption

Nutanix VM Snapshot Management with CLI and Bash

Introduction

Snapshots are a key feature in any backup or testing workflow. Managing them at scale, however, requires automation. With Bash and acli, you can script snapshot creation, enforce retention policies, and delete old snapshots, ideal for daily backup routines or testing environments.


My Personal Repository on GitHub

Nutanix Repository on GitHub


Diagram: Snapshot Management Flow


Goals


Bash Script: nutanix_snapshot_rotate.sh

#!/usr/bin/env bash
set -euo pipefail

vms=("web01" "db01" "api01")
retention=3
timestamp=$(date +%Y%m%d)
log="/var/log/nutanix_snapshots.log"

echo "[$(date)] Starting snapshot rotation" >> "$log"

for vm in "${vms[@]}"; do
snap_name="${vm}_snap_${timestamp}"

echo "Creating snapshot $snap_name for $vm" >> "$log"
acli vm.snapshot_create "$vm" snapshot_name="$snap_name"

# List snapshots and delete older than retention
snaps=$(acli vm.snapshot_list "$vm" | awk 'NR>2 {print $1}' | sort)
count=$(echo "$snaps" | wc -l)

if (( count > retention )); then
remove=$(echo "$snaps" | head -n -"$retention")
for old_snap in $remove; do
echo "Deleting old snapshot $old_snap for $vm" >> "$log"
acli vm.snapshot_delete "$vm" snapshot_name="$old_snap"
done
fi
done

Schedule It with Cron

0 2 * * * /usr/local/bin/nutanix_snapshot_rotate.sh

Runs nightly at 2:00 AM to snapshot and rotate selected VMs.


Best Practices


Summary

With just a few Bash commands, Nutanix snapshots can be automated and controlled. This avoids storage sprawl and aligns with your recovery point goals. Combine with monitoring to alert on failed creations or excess retention.

External Documentation:

Exit mobile version