Snapshot Management - FreshPerf/PVE4J GitHub Wiki

Snapshot Management

This guide covers snapshot management for both QEMU VMs and LXC containers using PVE4J.

Table of Contents

QEMU VM Snapshots

Listing Snapshots

import fr.freshperf.pve4j.entities.nodes.node.qemu.snapshot.PveQemuSnapshot;
import java.util.List;

try {
    List<PveQemuSnapshot> snapshots = proxmox.getNodes()
            .get("pve-node-01")
            .getQemu()
            .get(100)
            .getSnapshots()
            .list()
            .execute();
    
    for (PveQemuSnapshot snapshot : snapshots) {
        System.out.printf("Name: %s, Description: %s, Time: %d%n",
                snapshot.getName(), 
                snapshot.getDescription(),
                snapshot.getSnaptime());
    }
} catch (ProxmoxAPIError | InterruptedException e) {
    e.printStackTrace();
}

The snapshot list includes a special current entry representing the current VM state. Filter it out if needed:

List<PveQemuSnapshot> realSnapshots = snapshots.stream()
        .filter(s -> !"current".equals(s.getName()))
        .toList();

Creating Snapshots

Simple Snapshot

try {
    PveTask task = proxmox.getNodes()
            .get("pve-node-01")
            .getQemu()
            .get(100)
            .getSnapshots()
            .create("pre-upgrade")
            .waitForCompletion(proxmox)
            .execute();
    
    System.out.println("Snapshot created!");
} catch (ProxmoxAPIError | InterruptedException e) {
    e.printStackTrace();
}

Snapshot with Options

import fr.freshperf.pve4j.entities.nodes.node.qemu.snapshot.PveQemuSnapshotCreateOptions;

try {
    PveQemuSnapshotCreateOptions options = PveQemuSnapshotCreateOptions.builder()
            .description("Snapshot before system upgrade")
            .vmstate(true)  // Include memory state
            .build();
    
    PveTask task = proxmox.getNodes()
            .get("pve-node-01")
            .getQemu()
            .get(100)
            .getSnapshots()
            .create("pre-upgrade", options)
            .waitForCompletion(proxmox)
            .execute();
    
    System.out.println("Snapshot created with VM state!");
} catch (ProxmoxAPIError | InterruptedException e) {
    e.printStackTrace();
}

Note: VM state snapshots are larger and take longer to create, but allow restoring the VM to an exact running state.

Snapshot Information

import fr.freshperf.pve4j.entities.nodes.node.qemu.snapshot.PveQemuSnapshotConfig;

try {
    PveQemuSnapshotConfig config = proxmox.getNodes()
            .get("pve-node-01")
            .getQemu()
            .get(100)
            .getSnapshots()
            .get("pre-upgrade")
            .getConfig()
            .execute();
    
    System.out.println("Description: " + config.getDescription());
    System.out.println("Memory at snapshot: " + config.getMemory() + " MB");
    System.out.println("Cores at snapshot: " + config.getCores());
} catch (ProxmoxAPIError | InterruptedException e) {
    e.printStackTrace();
}

Updating Snapshots

import fr.freshperf.pve4j.entities.nodes.node.qemu.snapshot.PveQemuSnapshotUpdateOptions;

try {
    PveQemuSnapshotUpdateOptions options = PveQemuSnapshotUpdateOptions.builder()
            .description("Updated: Pre-upgrade snapshot - verified working")
            .build();
    
    proxmox.getNodes()
            .get("pve-node-01")
            .getQemu()
            .get(100)
            .getSnapshots()
            .get("pre-upgrade")
            .update(options)
            .execute();
    
    System.out.println("Snapshot description updated!");
} catch (ProxmoxAPIError | InterruptedException e) {
    e.printStackTrace();
}

Deleting Snapshots

try {
    PveTask task = proxmox.getNodes()
            .get("pve-node-01")
            .getQemu()
            .get(100)
            .getSnapshots()
            .get("old-snapshot")
            .delete()
            .waitForCompletion(proxmox)
            .execute();
    
    System.out.println("Snapshot deleted!");
} catch (ProxmoxAPIError | InterruptedException e) {
    e.printStackTrace();
}

Rolling Back

Warning: Rolling back will stop the VM if it's running!

try {
    PveTask task = proxmox.getNodes()
            .get("pve-node-01")
            .getQemu()
            .get(100)
            .getSnapshots()
            .get("pre-upgrade")
            .rollback()
            .waitForCompletion(proxmox)
            .execute();
    
    System.out.println("VM rolled back to snapshot!");
} catch (ProxmoxAPIError | InterruptedException e) {
    e.printStackTrace();
}

LXC Container Snapshots

Listing Container Snapshots

import fr.freshperf.pve4j.entities.nodes.node.lxc.snapshot.PveLxcSnapshot;

try {
    List<PveLxcSnapshot> snapshots = proxmox.getNodes()
            .get("pve-node-01")
            .getLxc()
            .get(200)
            .getSnapshots()
            .list()
            .execute();
    
    for (PveLxcSnapshot snapshot : snapshots) {
        System.out.println("Snapshot: " + snapshot.getName());
    }
} catch (ProxmoxAPIError | InterruptedException e) {
    e.printStackTrace();
}

Creating Container Snapshots

import fr.freshperf.pve4j.entities.nodes.node.lxc.snapshot.PveLxcSnapshotCreateOptions;

try {
    // Simple
    proxmox.getNodes()
            .get("pve-node-01")
            .getLxc()
            .get(200)
            .getSnapshots()
            .create("backup")
            .waitForCompletion(proxmox)
            .execute();
    
    // With options
    PveLxcSnapshotCreateOptions options = PveLxcSnapshotCreateOptions.builder()
            .description("Before package update")
            .build();
    
    proxmox.getNodes()
            .get("pve-node-01")
            .getLxc()
            .get(200)
            .getSnapshots()
            .create("pre-update", options)
            .waitForCompletion(proxmox)
            .execute();
    
    System.out.println("Container snapshots created!");
} catch (ProxmoxAPIError | InterruptedException e) {
    e.printStackTrace();
}

Container Snapshot Operations

var snapItem = proxmox.getNodes()
        .get("pve-node-01")
        .getLxc()
        .get(200)
        .getSnapshots()
        .get("pre-update");

// Get snapshot configuration
PveLxcSnapshotConfig config = snapItem.getConfig().execute();

// Update description
snapItem.update(PveLxcSnapshotUpdateOptions.builder()
        .description("Updated description")
        .build()).execute();

// Rollback
snapItem.rollback().waitForCompletion(proxmox).execute();

// Delete
snapItem.delete().waitForCompletion(proxmox).execute();

Next Steps