How do I list lxc snapshots for LXD on Linux?

I explained a long time ago how to take LXD instance snapshots using the lxc command. Today I will talk about how to list lxc snapshots and automatically purge those snapshots from LXD to free up disk space on Linux operating systems.

We can create instance snapshots using the lxc snapshot command. It is also possible to make stateful snapshots where LXD attempts to checkpoint the instance’s running state, including process memory state, TCP connections, process, and more. Let us see how to list lxc snapshots for LXD and automatically expires them too.

Tutorial details
Difficulty level Easy
Root privileges No
Requirements LXD on Linux
Est. reading time 3 minutes
ADVERTISEMENT

Listing lxc snapshots for LXD

The procedure is as follows for making and listing snapshots:

  1. Create a snapshot of “linode-mariadb” called “snap0”:
    lxc snapshot linode-mariadb snap0
  2. Verify a snapshot and look into the “SNAPSHOTS” column, run:
    lxc list linode-mariadb
  3. List a snapshot of “linode-mariadb” container:
    lxc info linode-mariadb snap0
    How to list lxc snapshots for LXD on Linux

    Click to enlarge

Understanding LXD snapshots outputs

Take a look at the following snapshots:

Snapshots:
+----------------------------------+----------------------+----------------------+----------+
| NAME | TAKEN AT | EXPIRES AT | STATEFUL |
+----------------------------------+----------------------+----------------------+----------+
| autoupdate-2021-07-22-1626936762 | 2021/07/22 06:52 UTC | | NO |
+----------------------------------+----------------------+----------------------+----------+
| autoupdate-2021-07-22-1626986837 | 2021/07/22 20:47 UTC | 2021/10/22 20:47 UTC | NO |
+----------------------------------+----------------------+----------------------+----------+
| autoupdate-2021-07-24-1627105777 | 2021/07/24 05:49 UTC | 2021/10/24 05:49 UTC | NO |
+----------------------------------+----------------------+----------------------+----------+
| autoupdate-2021-07-24-1627106368 | 2021/07/24 05:59 UTC | 2021/10/24 05:59 UTC | NO |
+----------------------------------+----------------------+----------------------+----------+
| autoupdate-2021-07-27-1627418679 | 2021/07/27 20:44 UTC | 2021/10/27 20:44 UTC | NO |
+----------------------------------+----------------------+----------------------+----------+
| autoupdate-2021-07-31-1627737799 | 2021/07/31 13:23 UTC | 2021/10/31 13:23 UTC | NO |
+----------------------------------+----------------------+----------------------+----------+
| autoupdate-2021-08-02-1627931311 | 2021/08/02 19:08 UTC | 2021/11/02 19:08 UTC | NO |
+----------------------------------+----------------------+----------------------+----------+
| autoupdate-2021-08-05-1628122998 | 2021/08/05 00:23 UTC | 2021/11/05 00:23 UTC | NO |
+----------------------------------+----------------------+----------------------+----------+
| autoupdate-2021-08-06-1628238945 | 2021/08/06 08:35 UTC | 2021/11/06 08:35 UTC | NO |
+----------------------------------+----------------------+----------------------+----------+
....
...
+----------------------------------+----------------------+----------------------+----------+
| autoupdate-2021-09-10-1631242574 | 2021/09/10 02:56 UTC | 2021/12/10 02:56 UTC | NO |
+----------------------------------+----------------------+----------------------+----------+
| autoupdate-2021-09-11-1631332965 | 2021/09/11 04:02 UTC | 2021/12/11 04:02 UTC | NO |
+----------------------------------+----------------------+----------------------+----------+

Where,

Patreon supporters only guides 🤓

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:

Join Patreon

  • NAME : It is the name of the snapshot. I use an automated tool such as Ansible to create those snapshots before updating the instance with the latest code or patches. Hence they have patten like autoupdate-2021-09-08-1631079083.
  • TAKEN AT : Date and time stamp when the snapshot was taken.
  • EXPIRES AT : LXD offers automatically remove the snapshot to avoid disk space and other issues.
  • STATEFUL : I only care about files and not about processor, memory, or TCP state. Hence, they are not stateful.

The two best options for use with LXD are ZFS and btrfs. They make taking snapshot easy and faster too.

How to make instance’s snapshot stateful

Pass the --stateful option to the lxc command to tell whether or not to snapshot the instance’s running state. For instance:
lxc snapshot linode-mariadb snap1 --stateful
lxc list linode-mariadb
lxc info linode-mariadb

How to set up snapshot expiry date

Developers and Linux sysadmin can controls when snapshots are to be deleted. The syntax is:
lxc config set {container_name_here} snapshots.expiry "pattern"
Pattern can be like 1M (1 month), 2H (2 hours), 3d (3 days), 4w, 5m, and 6y. For example, set expiry to 3 months for the instance named linode-mariadb:
lxc config set linode-mariadb snapshots.expiry "3m"
# verify it #
lxc config get linode-mariadb snapshots.expiry

We can set the snapshot pattern to autosnapshot-YYYMMDD-number format as follows:
lxc config set linode-mariadb snapshots.pattern 'autosnapshot-{{creation_date.Format("20210101")}}-%d'
Finally, set the snapshots to happen daily using cron as follows:
lxc config set linode-mariadb snapshots.schedule "expression"
lxc config set linode-mariadb snapshots.schedule "@daily"

The cron expression are like:
minute hour dom month dow
You can also set a comma separated list of schedule aliases like:
@hourly @daily @midnight @weekly @monthly @annually @yearly

Summing up

You learned how to make lxc snapshots for LXD. See lxd documentation for more information. Or read man pages using the man command or help command:
lxc --help
lxc snapshot --help


ADVERTISEMENT