LXD Backup Strategy

By chimo on (updated on )
This post describes the way I handle backups with LXD. Nothing too fancy or complicated. Should be good-enough for a hobby. Time will tell.

Preamble

Unsurprisingly, most of my containers are hosting applications. I also have two containers running databases: postgres and mariadb. I am planning on migrating gitea to postgres since it’s the only thing left using mariadb, but I digress.

I opted to spin-up dedicated containers to host databases as opposed to “one db container per app container” to save on resources (I have fairly limited memory available).

The distinction between “application” and “database” containers plays a role in the backup strategy below.

Local Backups

LXD has native snapshot management, which makes it very easy to get started. You can setup snapshot schedules and expiry at the container-level, or the profile-level (among other things).

Up until now, I had been using the same “default” profile for all of my containers but since I wanted different snapshot schedules/retentions for database-like containers compared to application-type containers, I ended up copying the “default” profile to a new one called “data-node”. The only difference between the two so far are snapshot schedules/retentions:

  • "application-nodes" are backed-up weekly, and each snapshot is kept for seven weeks.
  • "data-nodes" are backed-up daily, and each snapshot is kept for seven days.

The idea being that data changes often, hence backed-up more often. Applications are more “stable”, or static, so are backed-up less frequently. Plus, you can usually re-build/reconfigure applications without losing anything (except your time, I suppose).

The lxc commands I used to achieve this are:

# Default nodes: weekly backups, on Fridays at 10pm, kept for 7 weeks
lxc profile set default snapshots.schedule='0 22 * * 5'
lxc profile set default snapshots.expiry='7w'
lxc profile set default snapshots.pattern='snapshot-{{creation_date.Format("20060102")}}-%d'

# Data nodes: daily backups, at 10pm, kept for 7 days
lxc profile copy default data-node
lxc profile set data-node snapshots.schedule='0 22 * * *'
lxc profile set data-node snapshots.expiry='7d'

Off-site Backups

LXD also has the ability to communicate with other remote LXD instances. I’m running LXD at home as well, so copying containers from the VPS to the home instance for off-site backups is easily done with a couple of commands.

  1. Setup remote LXD:
lxc remote add home-instance https://home-instance.example.org:8443 \
    --protocol lxd --auth-type tls
  1. Copy containers over:
lxc copy container1 home-instance:backup-container1 --refresh --mode push

I have a bash script in a cron job that iterates through the running containers and copies the VPS containers back home once a month:

#!/bin/bash

lxc storage volume copy default/certs home-instance:default/certs --refresh \
    --mode push \
    --volume-only

mapfile -t containers < <(lxc list status=running -c n --format csv)

for container in "${containers[@]}"
do
    lxc copy "${container}" home-instance:backup-"${container}" --refresh \
        --mode push \
        --instance-only
done

This is all subject to change, of course. One thing I want to look into and leverage more in general are volumes, for example.

Recent articles from blogs I follow

The Scunthorpe Problem

I was talking with a friend recently about an email of theirs running afoul (🐔) of another aggressive filter system, because they dared to to talk with someone called Dickson. I know right, they’re the absolute worst. For those unfamiliar, this is the The…

via Rubenerd November 21, 2024

In which Neil is surprised by the lack of an HDMI cable

Some modern technology decisions baffle me. Today, I was sitting in a meeting room. In the room was my friend, with her laptop. Her laptop has an HDMI port. Also in the room was a screen, onto which my friend wished to display her laptop’s desktop. The screen …

via Neil's blog November 19, 2024

Helm: JSON schema generation

Helm charts support the inclusion of a values.schema.json file to validate values.yaml. Documentation: https://helm.sh/docs/topics/charts/#schema-files A JSON schema is akin to defining the structure of and type-annotating a JSON file. It helps to “shift lef…

via not just serendipity November 14, 2024