Exclude data from snapshots

System and user data can be excluded from snapshots by specifying exclusion patterns in an optional metadata file called snapshots.yaml. Such exclusions can be used to control snapshot content and size.


Warning:

The snapshot restore command replaces the system and user data with the snapshot content which means that excluded files and directories are lost with a snapshot restore.


snapshots.yaml syntax

The snapshots.yaml configuration file is written in YAML. It starts with the exclude: keyword, followed by a list of shell-style wildcard (globbing) patterns to indicate which files or directories to exclude.

The following example excludes potentially cached files in .cache, files beginning with excl-, temporary files stored in /tmp and files manually stored in a large-files directory:

exclude:
  - $SNAP_COMMON/.cache
  - $SNAP_DATA/excl-* 
  - $SNAP_USER_COMMON/tmp
  - $SNAP_USER_DATA/large-files/*-not-used.*

The above example snapshots.yaml is taken directly from snapshot data exclusion demo.

Wildcard patterns must start with a system or user data environment variable (see Data locations). Only the asterisk wildcard * is supported.

Integrating snapshots.yaml in a snap

The snapshots.yaml file needs to be located within a snap’s meta directory.

This can be accomplished when building a snap with Snapcraft by creating a part that uses the dump plugin to copy the snapshots.yaml from a local config directory, for example, into the snap:

parts:
  data-creation:
  # …
  snapshot-exclusions:
    source: config/
    plugin: dump
    organize:
      snapshots.yaml: meta/

The organize keyword is used to position the file under meta/ within the snap. Alternatively, the build step override method can be used to move the file at build time:

parts:
  data-creation:
  # …
  snapshot-exclusions:
    plugin: nil
    source: config/
    override-build: |
      snapcraftctl build
      mkdir -p $SNAPCRAFT_PART_INSTALL/meta
      mv $SNAPCRAFT_PART_SRC/snapshots.yaml $SNAPCRAFT_PART_INSTALL/meta/

See the snapshot-exclude-demo for a fully functional test snap using snapshot exclusions.

3 Likes