Post

Restore XenServer VM Snapshots from the CLI / Command Line

Restore XenServer VM Snapshots from the CLI / Command Line

Managing XenServer VM Snapshots from the Command Line

While taking snapshots from the command line in XenServer is straightforward, restoring them can be more challenging. This guide explains how to list and restore snapshots using the CLI, which is particularly useful for automation in testing environments where VMs need to be reverted to the same state after each test.

The Challenge

The main difficulty when working with snapshots via CLI is matching snapshot UUIDs with their corresponding VM UUIDs. This can be accomplished by extracting the “children” parameter from each snapshot.

Listing Snapshots with Their VM UUIDs

To list all snapshots along with the UUIDs of their associated VMs, use the following command:

1
2
3
4
for i in `xe snapshot-list | grep uuid | awk '{print$5}'`; do 
    export j=$(xe snapshot-param-get uuid=$i param-name=children)
    echo "VM UUID: $j – Snapshot UUID: $i"
done

Example Output:

1
2
3
4
5
6
VM UUID: 9211f2a2-4624-4254-543f-b6a99cce7760 – Snapshot UUID: 89ed788c-987e-75ad-9d72-84b2d06486de
VM UUID: 92bbf92c-57df-a6c3-fab2-366573ea3f29 – Snapshot UUID: 23da7e91-19d6-07d3-5fb3-818b834d6883
VM UUID: 3dd5209b-77cc-923a-d0da-4fb7aa013498 – Snapshot UUID: c91e63c6-6fd7-e49e-1a80-6215fecdda10
VM UUID: 29a28e86-dd60-0727-487c-12743b833a6b – Snapshot UUID: 4d21112b-b92d-4b1e-16c2-d1bcfb2d1a0b
VM UUID: 6bc1fd07-cfa7-6b00-dc90-dfe2f228db9c – Snapshot UUID: 44abca3c-eadc-c74f-2095-9c6198681305
VM UUID: c4348bab-cf34-217c-85be-3661a0e5cb60 – Snapshot UUID: af1859ae-675b-6a3d-f688-a0af65baba13

Restoring All Snapshots

If you have only one snapshot per VM and want to restore all of them, you can use this command:

1
2
3
4
for i in `xe snapshot-list | grep uuid | awk '{print$5}'`; do 
    export j=$(xe snapshot-param-get uuid=$i param-name=children)
    xe snapshot-revert uuid=$j snapshot-uuid=$i
done

Important Notes

  • This approach works only when there is exactly one snapshot per VM and you want to restore all of them.
  • For more complex scenarios (multiple snapshots per VM or selective restoration), you’ll need more sophisticated scripting to filter the desired snapshots.
  • Always ensure your VMs are in an appropriate state before restoring snapshots (e.g., powered off if required).

Additional Snapshot Management Commands

Taking a Snapshot

1
2
# Take a snapshot of a VM
xe vm-snapshot vm=<vm-name-or-uuid> new-name-label="<snapshot-name>"

Listing Snapshots for a Specific VM

1
2
# List snapshots for a specific VM
xe snapshot-list name-label=<vm-name>

Deleting a Snapshot

1
2
# Delete a snapshot
xe snapshot-uninstall uuid=<snapshot-uuid> force=true

Getting Detailed Snapshot Information

1
2
# Get detailed information about a snapshot
xe snapshot-param-list uuid=<snapshot-uuid>
This post is licensed under CC BY 4.0 by the author.