Bobcares

Backup and Restore LXD Containers: A Complete Guide

PDF Header PDF Footer

Learn how to backup and restore LXD Containers with this guide. Take a minute and discover what our LXC/LXD Support Team has to say.

Backup and Restore LXD Containers: A Complete Guide

Our Experts recommend keeping proper backups of your LXD containers to ensure data safety, minimize downtime, and enable fast disaster recovery. A well-tested backup and restore strategy can be a lifesaver, whether you’re protecting against accidental deletions, hardware failures, or unexpected server crashes.

Today, we will look at the different approaches to backing up and restoring LXD containers, including full server backups, individual container exports, and remote copies.

What is LXD?

LXD is a modern, secure, and powerful system container and virtual machine manager. It lets us run full Linux systems in containers or virtual machines with:

  • Wide OS support – official Ubuntu images and many community-provided Linux distributions.
  • REST API management – powerful yet simple.
  • Flexible scaling – from a single machine to full data center racks.
  • Optimized resource usage – ideal for development and production environments.

If we want to containerize different environments, run VMs efficiently, or manage infrastructure in a cost-effective way, LXD is a strong choice.

Step 1. Gather LXD Configuration and Metadata

Before backing up, collect details about the LXD setup.

We need LXD 4.x or above for the `export` and `dump` features:

lxd version

To list all instances

lxc list

We can dump networking, storage, and profile info

lxd init –dump

To save it to a backup directory:

mkdir -pv /backups/lxd/
lxd init --dump >/backups/lxd/lxd.config.$(date +'%m-%d-%Y')

Also, to check storage volumes (ZFS example)


zpool list
zfs list

For snap installations, the main LXD data directory is /var/snap/lxd/common/lxd/

This directory must be part of your backups.

Step 2. Full LXD Data Directory Backup

Use `rsync` for efficient backups.

Local backup:

rsync --sparse -avrP \
/var/snap/lxd/common/lxd /backups/lxd/lxd-full-backup/

Remote backup:

rsync --sparse -avrP \
/var/snap/lxd/common/lxd user@server-ip:/backups/lxd/lxd-full-backup/

Key rsync options:

  • `–sparse` – optimize storage for sparse files.
  • `-a` – archive mode (preserve attributes).
  • `-v` – verbose output.
  • `-r` – recursive directory copy.
  • `-P` – show progress.

Step 3. Backup Running Instances with Snapshots

We can export each container as a `.tar.xz` archive.

lxc export {container} /path/to/{container}-backup-$(date +'%m-%d-%Y').tar.xz

For ZFS or Btrfs (space-saving):

lxc export {container} /path/to/{container}-backup-$(date +'%m-%d-%Y').tar.xz –optimized-storage

Example for a container named `nginx-www`:


lxc export nginx-www \
/backups/lxd/nginx-www-backup-$(date +'%m-%d-%Y').tar.xz \
--optimized-storage

To automate instance backups with Bash


#!/bin/bash
NOW=$(date +'%m-%d-%Y')

We can back up the LXD server config


lxd init --dump > "/backups/lxd/lxd.config.${NOW}"
lxc list > "/backups/lxd/lxd.instances.list.${NOW}"
snap list lxd > "/backups/lxd/lxd-version.${NOW}"

To back up all containers


for i in $(lxc list -c n --format csv)
do
echo "Backing up ${i}..."
lxc export "${i}" "/backups/lxd/${i}-backup-${NOW}.tar.xz" --optimized-storage
done

We can run this script daily via `cron` and store backups offsite (S3, NAS, or remote LXD server).

Step 4. Restoring LXD

It’s important to test our backups periodically. Here’s how to fully restore a server.

To simulate a disaster


lxc list
snap remove lxd --purge
zpool destroy lxdzfs

To restore the process

  1. Check the LXD version from the backup
    cat /backups/lxd/lxd-version.05-26-2020
  2. Install a matching LXD version
    snap install lxd --channel 4.0/stable
  3. Restore server configuration
    cat /backups/lxd/lxd.config.05-26-2020 | lxd init –preseed
  4. To restore containers

    lxc import /backups/lxd/nginx-www-backup-05-26-2020.tar.xz
    lxc start nginx-www

Restoring a single instance (when LXD is otherwise fine):

lxc import /backups/lxd/{container}-backup-${NOW}.tar.xz

Step 5. Backups Using `lxc copy` to a Remote Server

We can replicate containers to another LXD server.

On backup server (`server2`):


lxc config set core.https_address 192.168.100.100:8443
lxc config set core.trust_password Same_super_PASSWORD_here

On main server (`server1`):


lxc remote add server2 192.168.100.100
lxc snapshot www-nginx
lxc copy www-nginx/snap0 server2:www-nginx-backup --verbose

To restore from the backup server:

lxc copy server2:www-nginx-backup www-nginx

Step 6. Full LXD Server Backup (Official Method)

For snap users:

sudo tar -czvf lxd-backup.tar.gz /var/snap/lxd/common/lxd

For non-snap users:

sudo tar -czvf lxd-backup.tar.gz /var/lib/lxd

Also back up `/etc/subuid` and `/etc/subgid` if applicable.

To restore:

  1. Stop LXD:
    sudo snap stop lxd
  2. Remove the existing LXD directory and replace it with the backup.
  3. Restore external storage devices.
  4. Restart LXD:
    sudo snap start lxd

Step 7. Snapshots of the LXD Snap

Snap users can also export internal snapshots:

To create a snapshot:

sudo snap save lxd

To export a snapshot:

sudo snap export-snapshot  lxd-snapshot.tar.gz

[Need assistance with a different issue? Our team is available 24/7.]

Conclusion

A strong LXD backup and restore strategy combines full data directory backups, container exports, and off-site replication.

With the commands and processes above, we can safeguard our LXD environment and ensure business continuity even during unexpected outages.

In brief, our Support Experts demonstrated how to backup and restore LXD Containers with this guide.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Get featured on the Bobcares blog and share your expertise with a global tech audience.

WRITE FOR US
server management

Spend time on your business, not on your servers.

TALK TO US

Or click here to learn more.

Speed issues driving customers away?
We’ve got your back!