Better LXD snapshot management

At work we’ve had a couple of minor issues that caused me to hit the docs on LXD again, and I learned that way back in February LXD got a cool new feature - automated snapshot scheduling, with automated expires (The automated snapshots appeared back in 3.8). This means I can replace my entire dodgy bash script that runs in a cron, and just let LXD handle it. This is nice, because it’ll handle automatically expiring manual snapshots as well.

Once you get around the entirely unfamiliar Pongo2 date formatting, it’s dead easy to configure:

# Set the snapshot pattern to snapshot-yyyyMMdd-number.
lxc config set unifi snapshots.pattern 'snapshot-{{creation_date.Format("20060102")}}-%d'

# Set the snapshots to happen daily.
lxc config set unifi snapshots.schedule "08 06 * * *"

# The snapshots expire in 2 weeks
lxc config set unifi snapshots.expiry "14d"

lxc info unifi | grep snapshot-
  snapshot-20190803-0 (taken at 2019/08/03 06:08 UTC) (expires at 2019/08/17 06:08 UTC) (stateless)

Here’s it handling the manual snapshots as well:

lxc snapshot unifi
lxc info unifi | grep snapshot-
  snapshot-20190803-0 (taken at 2019/08/03 06:08 UTC) (expires at 2019/08/17 06:08 UTC) (stateless)
  snapshot-20190803-1 (taken at 2019/08/03 06:24 UTC) (expires at 2019/08/17 06:24 UTC) (stateless)

So if I’m about to shoot my own foot off, I can grab a quick extra snapshot, and LXD will clean up after me in two weeks’ time! Great!

Now, I’d like to spread them out a bit, because from time to time I’ve found deleting bulk ZFS datasets can be a rather expensive process, so some quick bash to set them one minute apart (works fine unless I have >60 containers, but I’ll likely revisit it if that becomes a concern):

#! /bin/bash

count=0
/snap/bin/lxc list volatile.last_state.power=RUNNING -c n --format csv | while read container
do
  echo "Update Snapshot configuration for container: ${container}"

  /snap/bin/lxc config set ${container} snapshots.pattern 'snapshot-{{creation_date.Format("20060102")}}-%d'
  /snap/bin/lxc config set ${container} snapshots.schedule "${count} 19 * * *"
  /snap/bin/lxc config set ${container} snapshots.expiry "14d"

  ((count++))
done

We’ll see how it goes in the coming days!

Horsham, VIC, Australia fwaggle

Published:


Modified:


Filed under:


Location:

Horsham, VIC, Australia

Navigation: Older Entry Newer Entry