| | |

How to Backup LXD Containers to a Remote Host with Rsync.

In this tutorial, you will learn how to use Rsync to backup your LXD containers running on a ZFS storage pool to a remote host. Backing up your containers is an essential step in protecting your data and ensuring business continuity in case of any disaster. With Rsync, you can easily transfer the snapshot of your LXD container to a remote host, providing an offsite backup solution. We will go through the step-by-step process of creating a snapshot of the container, transferring it to the remote host, and deleting the snapshot. We will be using Ubuntu 22.04 with ZFS storage pool and the LXD snap package. However, this process can also be adapted for other operating systems and storage types.

This tutorial requires you to be logged in as root, so switch to root user if you are not already.

$ sudo -i

Make a new directory to mount snapshot.

# mkdir /mnt/snapshot

Create a snapshot of our LXD container in this example “ct1”.

# lxc snapshot ct1

If this is your first snapshot it will be created as “snap0”. If snapshots have been taken previously, the number of each subsequent snapshot is incremented by one, for example in the case of four snapshots this means snap0, snap1, snap2 and the last snap3. We can also give the snapshot a custom name by adding it to the “lxc snapshot” command, for example “lxc snapshot ct1 <your-custom-snapshot-name>”, but that is beyond the scope of this tutorial.

Check the snapshots that were taken and note the last one taken. This in order to mount the correct snapshot in the next steps.

# lxc info ct1 | grep snap

This should produce similar output:

| snap0 | 2023/03/03 16:26 CET |            | NO       |
# zfs list -t snapshot | grep ct1

This should produce similar output:

NAME                                        USED  AVAIL     REFER  MOUNTPOINT
default/containers/ct1@snapshot-snap0      11.0M      -      719M  -

Under “NAME” find the dataset with the corresponding snapshot, in our case “snap0”, and copy it to use in the next step for mounting the snapshot on the previously created directory “/mnt/snapshot/”.

# mount -t zfs default/containers/ct1@snapshot-snap0 /mnt/snapshot/

Check the content of “/mnt/snapshot/”.

# ls -al /mnt/snapshot/

This should produce similar output:

total 31
d--x------  4 1000000 root       6 Mar  3 12:21 .
drwxr-xr-x  3 root    root    4096 Mar  9 10:08 ..
-r--------  1 root    root    9790 Mar  5 09:40 backup.yaml
-rw-r--r--  1 root    root     297 Mar  2 03:58 metadata.yaml
drwxr-xr-x 18 1000000 1000000   21 Mar  2 03:30 rootfs
drwxr-xr-x  2 root    root       3 Mar  2 03:58 templates

Copy the full content of the “/mnt/snapshot/” directory to the remote host using Rsync. Make sure Rsync is installed on both servers and that the remote host is available with SSH before starting.

# rsync -avz --numeric-ids -e "ssh -p <remote-server-ssh-port-number>" /mnt/snapshot/* <user>@<remote-host>:/<path-to-backup-location/ct1

Unmount your snapshot.

# umount /mnt/snapshot

While it is always good practice to keep at least one snapshot of your container on the server to perform a quick restore if necessary, you may still want to delete the one you created specifically for the Rsync procedure.

# lxc delete ct1/snap0

You can repeat the previous steps periodically to keep the data on the remote host up-to-date. With Rsync, only the bits that actually changed (delta-transfer algorithm) are sent after the last synchronization operation, minimizing data transfer and drastically reducing time. By adding the “–delete” option, files deleted on the host server are also deleted on the remote server.

# rsync -avz --delete --numeric-ids -e "ssh -p <remote-server-ssh-port-number>" /mnt/snapshot/* <user>@<remote-host>:/<path-to-backup-location/ct1

Similar Posts