Automating taking and deleting of Snapshots

From Notes_Wiki

Home > Debian > Proxmox virtual environment > Automating taking and deleting of Snapshots

We can automate taking and deleting of Snapshots using cv4pve-autosnap tool.

cv4pve-autosnap is a lightweight automation tool designed for Proxmox VE environments to create and manage LVM-Thin and ZFS volume snapshots. It helps administrators implement automated, scheduled snapshot backups of virtual machines (VMs) and containers (CTs), offering a simple but effective mechanism for data protection and rollback.

Installation

Install the tool on Proxmox VE

# wget https://github.com/Corsinvest/cv4pve-autosnap/releases/download/v1.15.2/cv4pve-autosnap-linux-x64.zip

Install unzip package

# apt install unzip

unzip the package

# unzip cv4pve-autosnap-linux-x64.zip

give execute permission

# chmod +x cv4pve-autosnap

move it to sbin folder

# mv cv4pve-autosnap /usr/sbin/

To verify that the commands are working, we can run this command. It will display all available options.

# cv4pve-autosnap --help


VIRTUAL MACHINE

This command takes the backup of VM

# cv4pve-autosnap --host=172.30.7.100 --username=root@pam --password 'file:/root/password' --vmid=101 snap --label=daily --keep=2

Parameters used in the command

  • snap: will take snapshot one time
  • -- label: assigns a label or name to the snapshot (example: daily, monthly)
  • --keep: Specifies the number of snapshots to retain for the given label.
  • --vmid: id of the VM

The password is required the first time, and it can be either pasted or typed. It is then stored in the file.

  • When the above command run successfully, it Confirms that the snapshot command ran without error and a snapshot was created for the VM.
  • The snapshot is visible in the Proxmox web interface under the VM's "Snapshots" tab.
  • We can create A second snapshot using the same command.
  • The GUI now displays both the first and second snapshots for the VM.
  • After taking a third snapshot, the oldest snapshot was automatically deleted as per the --keep=2 rule.
  • Confirms the retention policy is working correctly: keeping only the two most recent snapshots.

Check the status

# cv4pve-autosnap --host=172.30.7.100 --username=root@pam --password 'file:/root/password' --vmid=101 status


CONTAINER

The same process applies to containers; we just need to provide the container ID.

# cv4pve-autosnap --host=172.30.7.100 --username=root@pam --password 'file:/root/password' --vmid=100 snap --label=daily –keep=2
  • Take a snapshot for the container.
  • The container's snapshot is visible in the Proxmox GUI.
  • Taken another snapshot for the container.
  • Both snapshots now show up in the container's "Snapshots" tab.
  • Create a third snapshot triggers automatic deletion of the oldest one, maintaining only two.
  • Validates the retention rule worked for container snapshots as well.

AUTOMATION USING CRONTAB

We can automate this process using crontab. Below is the example:

0 12 * * * /usr/sbin/cv4pve-autosnap --host=172.30.7.100 --username=root@pam --password 'file:/root/password' --vmid=100 snap --label=daily --keep=2

Non Root User

If the root user of Proxmox has TOTP authentication, we can use API token instead of user.

Create an API token

  • Go to Datacenter > Permissions > API Token > Add
  • Give the Token ID
  • uncheck Privilege Separation
  • Note the Token ID and Secret details (this will only show one Time)

Command to take snapshot using API token

# cv4pve-autosnap --host=172.30.7.100 --api-token 'root@pam!snap=056d158b-06dc-4774-9156-56a1efb1e9d7' --vmid=101 snap --label=daily --keep=2
  • Test is manually
  • Take a snapshot
  • It will appear in the GUI

To automate the process, add the following entry to the crontab

0 2 * * * /usr/sbin/cv4pve-autosnap --host=172.30.7.100 --api-token 'root@pam!snap=056d158b-06dc-4774-9156-56a1efb1e9d7' --vmid=101 snap --label=daily --keep=2>

Reference: https://github.com/Corsinvest/cv4pve-autosnap



Home > Debian > Proxmox virtual environment > Automating taking and deleting of Snapshots