Create Scheduled VM Snapshot via Bash Script

Dexter23

Member
Dec 23, 2021
176
12
23
34
Hi all
I want you opinion about this script for create scheduled vm snapshot and delete oldest then 7 days:

Code:
#!/bin/bash


# ID della VM
VMID="100"


# Snapshot Name
SNAPSHOT_NAME="snapshot-$(date +%Y-%m-%d_%H-%M-%S)"


# Create snapshot
/usr/sbin/qm snapshot $VMID $SNAPSHOT_NAME


# Remove snapshot oldest then 7 days
/usr/sbin/qm listsnapshot $VMID | grep -E '^ [0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}-[0-9]{2}-[0-9]{2}' | while read SNAPSHOT; do
    SNAPSHOT_DATE=$(echo $SNAPSHOT | awk '{print $1}' | sed 's/snapshot-//')
    SNAPSHOT_TIMESTAMP=$(date -d $SNAPSHOT_DATE +%s)
    SEVEN_DAYS_AGO=$(date -d "7 days ago" +%s)


    if [ $SNAPSHOT_TIMESTAMP -lt $SEVEN_DAYS_AGO ]; then
        /usr/sbin/qm delsnapshot $VMID $SNAPSHOT_DATE
    fi
done
 
  • Like
Reactions: Kingneutron
Looks fine so far - but I have not tested it.

----
I have a general critique when I see most of the scripts shared and offered, please do not take it as a personal offense: What I am nearly always missing is error handling. This is bash, but I do not see "set -e" nor "trap" nor checking execution results of the core commands like "qm snapshot"...

(In my days as a programmer 33% was code, 33% was error handling and 33% was documentation. Yes, I am old.)
 
  • Like
Reactions: Kingneutron
I'm not a programmer.
Ok what does "set-e" and "nor" do?
Feel free to edit this script to improve it.
 
  • Like
Reactions: waltar
UPDATE: this is the actual version if something goes wrong we receive notification via email
Code:
#!/bin/bash


set -e  # Stop the script if something fail


# Set email address for notification
EMAIL="emailaddress@email.com"


# ID of VM
VMID="100"


# Name of snapshot
SNAPSHOT_NAME="snapshot-$(date +%Y-%m-%d_%H-%M-%S)"


# Snapshot creation with email notifications
if ! /usr/sbin/qm snapshot $VMID $SNAPSHOT_NAME; then
    echo "Errore durante la creazione dello snapshot per la VM $VMID" | mail -s "Errore Snapshot Proxmox" $EMAIL
    exit 1
fi


# Remove snapshot oldest than 7 days
/usr/sbin/qm listsnapshot $VMID | grep -E '^ [0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}-[0-9]{2}-[0-9]{2}' | while read SNAPSHOT; do
    SNAPSHOT_DATE=$(echo $SNAPSHOT | awk '{print $1}' | sed 's/snapshot-//')
    SNAPSHOT_TIMESTAMP=$(date -d $SNAPSHOT_DATE +%s)
    SEVEN_DAYS_AGO=$(date -d "7 days ago" +%s)


    if [ $SNAPSHOT_TIMESTAMP -lt $SEVEN_DAYS_AGO ]; then
        /usr/sbin/qm delsnapshot $VMID $SNAPSHOT_DATE
    fi
done
 
  • Like
Reactions: UdoB
Hi all

I've seen that the script not work completly, the snapshot create successfully, but the part should remove oldest snapshot not work, can someone who have experience with bash scripting can take a look, i appreciate it, thanks.
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!