How to create snapshots of your snaps

This article is more than 5 years old.


Data backups are arguably the most important part of one’s software setup. While many scenarios fall into the realm of if, data loss belongs in the when domain. Over time, every one of us will experience some kind of hardware failure, be it an optical disk, a hard disk or a lost phone. It is vital to have a good backup procedure in place, so you can quickly and smoothly recover, and resume your work.

Last week, we talked about application backups. This week, we will talk about another important aspect of how to preserve your data – snapshots.

Snap structure

When you install a new snap, the application data will be saved in two main locations:

  • Application data will be stored under /snap, or /var/snapd (like in Fedora).
  • User data will be stored under /home/(your user)/snap/.

Each revision of the snap will be stored in a separate directory, with incremental numbers, e.g.: 6 or 42, with the symbolic link current pointing to the version that is in use, typically the latest revision.

Moreover, there will also be a sub-directory called common in each snap folder in your home, containing components that are not specific to any particular revision of the snap, like generic configurations or cached data.

For instance, the contents of the gimp snap under /snap/gimp/:

ls -la
total 8
drwxr-xr-x 5 root root 4096 Feb 22 07:51 .
drwxr-xr-x 37 root root 4096 Mar 1 16:16 ..
drwxr-xr-x 9 root root 167 Feb 1 20:30 110
drwxr-xr-x 9 root root 167 Feb 7 15:00 113
drwxr-xr-x 9 root root 167 Feb 21 19:23 130
lrwxrwxrwx 1 root root 3 Feb 22 07:51 current -> 130

The contents of the same snap under /home/(user)/snap/gimp/:

ls -la
total 24
drwxr-xr-x 6 igor igor 4096 Feb 22 07:51 .
drwxr-xr-x 71 igor igor 4096 Feb 28 13:33 ..
drwxr-xr-x 4 igor igor 4096 Feb 7 10:52 110
drwxr-xr-x 4 igor igor 4096 Feb 7 10:52 113
drwxr-xr-x 4 igor igor 4096 Feb 7 10:52 130
drwxr-xr-x 3 igor igor 4096 Oct 18 17:30 common
lrwxrwxrwx 1 igor igor 3 Feb 7 10:52 current -> 110

At first glance, the system-level data and user data may seem identical, but as you navigate deeper into the folder structure, you will notice the differences. Following our example, the contents of the sub-directory current under /snap/gimp/:

ls -la
total 5
drwxr-xr-x 9 root root 167 Feb 21 19:23 .
drwxr-xr-x 5 root root 4096 Feb 22 07:51 ..
drwxr-xr-x 2 root root 37 Feb 21 19:21 bin
-rwxr-xr-x 1 root root 58 Feb 21 19:23 command-gimp.wrapper
drwxr-xr-x 19 root root 380 Feb 21 19:22 etc
-rw-r--r-- 1 root root 14 Feb 21 19:10 flavor-select
drwxr-xr-x 3 root root 64 Mar 26 2018 lib
drwxr-xr-x 3 root root 43 Feb 21 19:23 meta
drwxr-xr-x 3 root root 79 Feb 21 19:23 snap
drwxr-xr-x 8 root root 128 Feb 21 19:21 usr
drwxr-xr-x 4 root root 51 Oct 14 2017 var

Likewise, here are the contents of the sub-directory current under /home/(user)/snap/gimp/:

ls -la
total 20
drwxr-xr-x 4 igor igor 4096 Feb 7 10:52 .
drwxr-xr-x 6 igor igor 4096 Feb 22 07:51 ..
drwxrwxr-x 8 igor igor 4096 Oct 18 17:30 .config
-rw-rw-r-- 1 igor igor 31 Feb 7 10:52 .last_revision
drwxrwxr-x 3 igor igor 4096 Oct 18 17:30 .local
lrwxrwxrwx 1 igor igor 30 Feb 7 10:52 .themes -> /snap/gimp/110/data-dir/themes

This is user-specific data, and it contains important changes and customisations that people will introduce as they use the snap, including various aesthetic tweaks, plugins, scripts, fonts, and more. And we want to backup this data!

Manual method

If you are somewhat familiar with the Linux command line, you can create your own snap data backups. You can use scripts (or backup) tools to copy the snap data to a backup folder, and you can even automate the task so it’s done in the background (perhaps a nightly run).

You may also want to consider keeping any application-specific data under your home directory in sync with the snap data, so that you always use a common set of tweaks, settings and configurations. We have talked about the tools like rsync and mackup in the last article, and you can use these to manually set up your backup routine for any snap data you need. This way, if you remove and purge your snaps, reinstall your host, or deploy snaps on a different machine, you will still have your user data.

Automated method – snap save

If you’re not comfortable with scripting, luckily, you don’t need to make any big changes yourself. Snaps come with a built-in snapshot functionality, invoked through the use of the save command. If you type snap help save, you will get more details into how snaps create and restore snapshots:

snap help save
Usage:
snap save [save-OPTIONS] […]

The save command creates a snapshot of the current user, system and configuration data for the given snaps.

By default, this command saves the data of all snaps for all users. Alternatively, you can specify the data of which snaps to save, or for which users, or a combination of these.

If a snap is included in a save operation, excluding its system and configuration data from the snapshot is not currently possible. This restriction may be lifted in the future.

[save command options]
--no-wait Do not wait for the operation to finish but just print the change id.
--abs-time Display absolute times (in RFC 3339 format). Otherwise, display short relative times.
--users= Snapshot data of only specific users (comma-separated) (default: all users)

With the snapshot functionality, you can create backups of your snaps with relative ease. You can snapshot all snaps or individual ones (you may only be interested in some of the data), and if there are multiple users on the system, you can backup their data as well, or just your own. For instance:

snap save liveforspeed
Set Snap Age Version Rev Size Notes
1 liveforspeed 1m15s S3-6T 5 753MB -
snap save irfanview
Set Snap Age Version Rev Size Notes
2 irfanview 1.81s 4.52 9 14.2MB -

Once you have snapshots on your system, you can list them with snap saved or verify their integrity with snap check-snapshot (snapshot id):

snap saved
Set Snap Age Version Rev Size Notes
1 liveforspeed 98.0m S3-6T 5 753MB -
2 irfanview 96.2m 4.52 9 14.2MB -
3 liveforspeed 8m01s S3-6T 5 753MB -
snap check-snapshot 2
Snapshot #2 verified successfully.

You also have the option to restore snapshots, and delete (forget) any snapshots you do not require anymore.

snap forget 3
Snapshot #3 forgotten.

What happens under the hood?

In the background, whenever your take a snapshot of a snap, the data stored under /home/(user)/snap/(snap name) will be compressed into a zip archive and stored under /var/lib/snapd/snapshots, e.g.:

drwx------  2 root root      4096 Mar  4 12:01 ./
drwxr-xr-x 20 root root 4096 Mar 4 12:05 ../
-rw------- 1 root root 752702859 Mar 4 10:31 1_liveforspeed_S3-6T_5.zip
-rw------- 1 root root 14218859 Mar 4 10:31 2_irfanview_4.52_9.zip
-rw------- 1 root root 752702862 Mar 4 12:01 3_liveforspeed_S3-6T_5.zip

Each zip archive contains declarative metadata as well as several sub-archives (in .tgz format), including per-user data, stored as your-user-name.tgz. This means you have additional freedom in handling your data. For instance, you copy the top-level zip archive or the per-user tgz archive anywhere you like, add it to any backup procedure you may have, or even deploy it on remote systems. If you reinstall your system, or even set up a new one, you can still retain your snap data, and quickly restore it whenever you need it.

Summary

Snapshots allow you to control your data and create an elegant, modular backup and restore setup for your snaps, so you can port your user settings to other Linux machines, or any new devices you may deploy. You also have the option to use them whenever you have to rebuild your operating system. This allows you to resume working without losing data or having to waste time reapplying the customization you need and like.

You have the ability to save snapshots of individual users or snaps, which gives you even more granularity of the process. Finally, you can also script and automate the functionality, so that everything runs in the background, and you are always ready and up-to-date for any data loss eventuality.

If you have any questions or comments regarding snapshots, please join our discussion forum and share your thoughts with us.

Photo by Mitch Lensink on Unsplash.

Ubuntu desktop

Learn how the Ubuntu desktop operating system powers millions of PCs and laptops around the world.

Newsletter signup

Get the latest Ubuntu news and updates in your inbox.

By submitting this form, I confirm that I have read and agree to Canonical's Privacy Policy.

Related posts

Snapcraft 8.0 and the respectable end of core18

‘E’s not pinin’! ‘E’s passed on! This base is no more! He has ceased to be! ‘E’s expired and gone to meet ‘is maker! ‘E’s a stiff! Bereft of life, ‘e rests in...

Craft team welcomes you to another episode of its adventures

Welcome to the second article in the Craft team saga. Previously, on Craft Team, we gave you a brief introduction into the team’s function, we announced our...

Snapcrafters: 2022 wrap-up

This article was written by Merlijn Sebrechts and Dani Llewellyn from the Snapcrafters community. ===== Last year, we officially re-launched the...