How to backup LXD containers

Published by Stephan on

LXD is a hypervisor for Linuxcontainer, which is available in Ubuntu since several versions. A Linux container is basically like a virtual machine, only more lightweight. We use LXD / LXC for many of our applications at school. LXD is easy to use and there are several reasons why we use it. Today I would like to show how to backup LXD containers.

1. lxc copy

The easiest way is to copy an LXD container to another computer. To do this you need a second LXD host, which you add on the first one as remote.

$ lxc remote add lxd2 192.168.1.50

lxd2 is the name you want to give to the second LXD host, followed by the IP. When setting up the second LXD host you have to make sure that it is accessible over the network and you also have to assign an admin password. You have to enter this password if you want to add the remote.

Once everything is set up, you can, for example, display all containers on the remote:

$ lxc list lxd2:

To make a backup of a container, copy it or a snapshot to the newly added remote:

$ lxc snapshot my-container my-snapshot
$ lxc copy my-container/my-snapshot lxd2:my-backup

Conversely, you can restore the container to the original host.

$ lxc copy lxd2:my-backup my-container-restored

2. lxc export and lxc publish

With LXD you can also export a container as a compressed image and then back it up to a NAS, an offsite backup or a cloud storage. Later, these images can be imported or restored with lxc import.

On Github I found a script that exactly meets our requirements. It exports the desired container and then saves it with rclone (great project!!!!) to a cloud storage or NAS of your choice.

You have to install and set up rclone first. Installation instructions and packages for all platforms can be found on the project’s website. rclone supports almost any cloud storage and protocol (ssh, webdav,…). We decided to backup to Google Drive because GSuite for Education gives us unlimited storage 🙂

If rclone is configured, you download the backup script and make it executable:

$ wget https://raw.githubusercontent.com/cloudrkt/lxdbackup/master/lxdbackup
$ chmod +x lxdbackup

For the upload of the backup to work you have to set some parameters (especially RCLONETARGET).

$ nano lxdbackup
# Settings

# The target bucket or container in your Rclone cloudstorage
RCLONETARGETDIR="lxdbackup"
# Optional Rclone settings.
RCLONEOPTIONS="""
# Rclone target cloud used in your rlcone.conf
RCLONETARGET="my-remote"
# Directory were local images are stored before upload
WORKDIR="/tmp/lxdbackup"

Now you can test the script with the following call:

$ lxdbackup my-container

If all goes well, you should find your backup after some time (exporting a container can take some time, depending on its size) on the storage that you have configured in rclone.

Conclusion

So far we have simply backuped the LXD host as a whole. This is usually sufficient, but you lose the ability to restore individual containers separately. With the lxdbackup script and the combination with rclone we now have many more possibilities and can backup our containers very flexibly. With the help of cronjobs we have automated the backup. For example, some containers are backuped twice a day, others only once a week (and only if there are no holidays) – depending on the use case.

Categories: HowTo

Stephan

I'm a teacher and IT system administrator in an international school. I love open source software and I used it over a decade in my private and work life. My passion is to solve problems with open source software!

2 Comments

brian mullan · September 9, 2020 at 10:33 am

FYI…

You have a syntax error under Step 1

$ sudo lxc remote add lxd2 192.168.1.50

should be… without sudo

$ lxc remote add lxd2 192.168.1.50

Brian

Leave a Reply

Avatar placeholder

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