How to Back Up and Restore LXD Instances?

Master the use of snapshots and exports to back up, migrate, and restore your LXD containers and virtual machines.

LXD's Two Main Backup Strategies

LXD provides two powerful and flexible backup and recovery mechanisms suitable for different scenarios:

  • Snapshots: Used for quick, instant, in-place backups and restores. Ideal for creating a "safety net" before performing risky operations.
  • Export/Import: Used to create complete, portable backup files. Suitable for offline storage, archiving, or migrating instances between different hosts.

Method 1: Using Snapshots for Quick Rollbacks

A snapshot is a complete record of an instance's state at a specific point in time (including the filesystem and configuration). It is extremely fast to create and (when using ZFS or Btrfs) takes up almost no additional space.

1. Create a Snapshot

Create a snapshot named `before-update` for an instance called `my-web-server`:

lxc snapshot my-web-server before-update

2. View Snapshots

You can use the `lxc info` command to see all snapshots for an instance:

lxc info my-web-server

3. Restore from a Snapshot

If an update operation fails, you can instantly restore the instance to the state of the snapshot:

lxc restore my-web-server before-update

Note: The restore operation will overwrite all current changes in the instance.

4. Delete a Snapshot

Once you are sure a snapshot is no longer needed, you can delete it to free up resources:

lxc delete my-web-server/before-update

Method 2: Export/Import for Full Backups and Migration

This method packages your instance (or one of its snapshots) into a single compressed `.tar.gz` file, which you can download, store anywhere, or import on another LXD host.

1. Create a Snapshot (Recommended)

To ensure data consistency, the best practice is to first create a snapshot of a running instance and then export that snapshot.

lxc snapshot my-web-server backup-snap

2. Export the Instance

Export the snapshot you just created into a file named `web-server-backup.tar.gz`:

lxc export my-web-server/backup-snap web-server-backup.tar.gz

Tip: You can also export a running instance directly (`lxc export my-web-server ...`), but this may lead to data inconsistency. Exporting a snapshot is the safer choice.

3. Import the Instance

You can import the backup file as a brand new instance. This can be done on the same host (for cloning) or on another host (for migration).

# Run on the target host
lxc import web-server-backup.tar.gz my-restored-server

Once the command completes, a new instance named `my-restored-server` will be created with the exact same state as when it was backed up.

Summary: Which Method to Choose?

Feature Snapshot Export/Import
Primary Use Case Quick, temporary state saving and rollbacks Complete, portable backups and migration
Speed Extremely fast, almost instantaneous Slower, requires file compression and I/O
Storage Location Stored in the same storage pool as the original instance A separate compressed file that can be stored anywhere
Best For Before software upgrades or major configuration changes Daily/weekly backups, server migration, instance archiving