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.

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:

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

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

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:

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

Now you can test the script with the following call:

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

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!