Hi all
I want you opinion about this script for create scheduled vm snapshot and delete oldest then 7 days:
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