You can use the following script to automatically take daily snapshots of your running VMs on Proxmox and remove older snapshots to save disk space. Set it up as a daily cron job on each node you wish to back up. The script will create a new snapshot each day, and then remove any snapshots older than the number of days you specify.
#!/bin/bash
keep_last="1";
local_server="$proxmox_local_server"
keep=$(( $keep_last + 1 ))
get_vm_list (){
pvesh get /nodes/$local_server/qemu --noborder|grep -w "running"|grep -v "vmid"|awk '{ print $2 }'|sort -n
}
get_snapshots_list(){
pvesh get /nodes/$local_server/qemu/$virtual_machine/snapshot --noborder |grep -vw description|grep -vw "You are here!"|awk '{ print $1 }'|awk '{ print $1 }'|sort -nr|tail -n +$keep
}
take_snapshot(){
pvesh create /nodes/$local_server/qemu/$virtual_machine/snapshot --snapname snap-`date "+%F-%H-%M"`
}
delete_snapshot (){
for snap in `get_snapshots_list`
do
pvesh delete /nodes/$local_server/qemu/$virtual_machine/snapshot/$snap
done;
}
###
for virtual_machine in `get_vm_list`;
do
take_snapshot
delete_snapshot
done;
Change $proxmox_local_server to the hostname of the server as is in proxmox and keep_last="1" to the days you want to keep
Remember that snapshots can take up substantial disk space depending on your daily data changes. I prefer to keep a single daily snapshot and rely on remote backups using Proxmox Backup Server for long-term storage and disaster recovery. This approach helps minimize local disk usage while still providing reliable backups.