Create Scheduled VM Snapshot via Bash Script

Dexter23

Member
Dec 23, 2021
195
15
23
35
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.
 
First of all, thanks to @Dexter23
Second, My contribution, script bellow now is removing snapshots older then 7 days:

Code:
#!/bin/bash
set -e  # Stop the script if something fail

# Set email address for notification
EMAIL="your_email_address"

# ID of VM
VMID="your_machine_id_number"


# 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 | while read SNAPSHOT; do
    SNAPSHOT_DATE=$(echo $SNAPSHOT | awk '{print $3" "$4}' | sed 's/snapshot-//')
    SNAPSHOT_TIMESTAMP=$(date -d "$SNAPSHOT_DATE" +%s)
    SEVEN_DAYS_AGO=$(date -d "7 days ago" +%s)
    SNAPSHOT_DATE=$(echo $SNAPSHOT | awk '{print $2}' | sed 's/snapshot-//')
   
    if [ $SNAPSHOT_TIMESTAMP -lt $SEVEN_DAYS_AGO ]; then
        echo VMID:$VMID snapname:  snapshot-$SNAPSHOT_DATE
        /usr/sbin/qm delsnapshot $VMID snapshot-$SNAPSHOT_DATE
    fi
done
 
Last edited: