How to Schedule LXD Snapshots

LXD Snapshots are point in time images that are stored as a part of a container. In my “LXD Custom Images” tutorial a year ago, I showed how to create images from snapshots. In this tutorial, we learn how you can schedule container snapshots to occur on a schedule and also how to expire them according to rules you establish.

We created a simple Ubuntu a simple Ubuntu 22.04 container.

lxc launch ubuntu:22.04 test --profile default -c boot.autostart=true

Create a snapshot of that container:

lxc snapshot test first-shot

We can even create an “image” from the snapshot. Images can be used to create future containers based upon the image.

lxc publish test/first-shot --alias mycoolimage description="Ubuntu 22.04 with goodies"

Examine the image you just created:

lxc image info mycoolimage

Launch a new container based on that image:

lxc launch mycoolimage test2

Snapshots can be created on an automated schedule.
In the tutorial, I create an hourly schedule:

lxc config set test snapshots.schedule "@hourly"

You can set a pattern for the naming of your Snapshots:

lxc config set test snapshots.pattern "Snapshot-{{ creation_date|date:'01-02-2006_15:04' }}"

The command above is a template and does not refer to the year 2006 as I mentioned in the video. The formatting of the pattern is based upon the Pongo2 template engine which is similar to the Django template design. The documentation is very scant, but the example about works to insert the current date as a part of the snapshot name.

You can also specify how long a snapshot will stick around before it is automatically deleted. In the video, I set the expiration to be two hours:

lxc config set test snapshots.expiry "2H"

I also showed that you can create a profile or edit an existing profile to include a snapshot schedule. This policy will be applied to all containers that have the profile as a part of their configuration.

lxc profile create snap-hourly
lxc profile set snap-hourly snapshots.schedule "@hourly"
lxc profile set snap-hourly snapshots.pattern "Hourly-{{ creation_date|date:'01-02-2006_15:04' }}"
lxc profile set snap-hourly snapshots.expiry "2H"

To add the above profile to a container:

lxc profile add test3 snap-hourly

To examine a container settings as well as see if it has any snapshots:

lxc info test

To examine the currently set parameters on a given container:

lxc config show test