Leave 3 last snapshots and delete the rest via CLI

Jun 7, 2022
11
1
8
Brazil
www.update.inf.br
Hello everyone, I would like a script to scan the snapshots created and leave only the last 3 of each VM. I'm seeing the qm delsnapshot command, but I believe I have to use it together with another function for all the lists I want to delete.
Thank you all!
 
Reactivating the topic, I managed to make the backup script this way, I know it's very basic, and I'd like people's help to make it more automatic.


Basically I manually take a snapshot of each VM by assigning the current date.

Code:
# declare snapshot variable and set today's date
export data=$(date +"%d_%m_%y")

# snapshot VM 100
export vmid=100
pvesh create /nodes/qv3wse9z72/qemu/$vmid/snapshot --snapname auto_$data

# snapshot VM 102
export vmid=102
pvesh create /nodes/qv3wse9z72/qemu/$vmid/snapshot --snapname auto_$data

# snapshot VM 103
export vmid=103
pvesh create /nodes/qv3wse9z72/qemu/$vmid/snapshot --snapname auto_$data

# snapshot VM 104
export vmid=104
pvesh create /nodes/qv3wse9z72/qemu/$vmid/snapshot --snapname auto_$data


In this more grotesque script, I do a manual check of the last 7 days and leave the 3 most recent and delete the rest.

Code:
# declare snapshot variable and set today's date
export data=$(date +"%d_%m_%y")

# loop that takes the last 7 to 3 days and deletes the snapshotsfor i in {3..7}
        do
        ontem=$(date +%d_%m_%y -d "$data - $i day")

        # snapshot VM 100
        export vmid=100
        pvesh delete /nodes/qv3wse9z72/qemu/$vmid/snapshot/auto_$ontem

        # snapshot VM 102
        export vmid=102
        pvesh delete /nodes/qv3wse9z72/qemu/$vmid/snapshot/auto_$ontem

        # snapshot VM 103
        export vmid=103
        pvesh delete /nodes/qv3wse9z72/qemu/$vmid/snapshot/auto_$ontem

        # snapshot VM 104
        export vmid=104
        pvesh delete /nodes/qv3wse9z72/qemu/$vmid/snapshot/auto_$ontem

        #end of the snapshot delete loop
        done

The purpose of this post is to help those looking for a script and those who know how to improve this one I started using the tools that are already included in proxmox.
 
The difference is that with my code it is not necessary to install anything on Proxomox, which, in my opinion, I think is better. The point that I posted my script is to help those who want to do it the same way I did, without installing anything and, if possible, improve this script.
 
I don't see the point. You also don't have to install cv4pve-autosnap. You just download and unzip it. Makes no difference to me if I would run a script I found in some forums or a script from some github repo. In both cases I should have to look at the code and see what it does, as someone else wrote that script.

Difference is just that cv4pve already got alot of features implemented with years of work put into it, its running for years on alot of PVE nodes so its well tested to not screw something up.
 

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!