Stateful applications keep preferences, databases, and other working data separate from their executable packages. Snap data snapshots preserve that snap-managed state so it can be backed up and restored after an unwanted change or data loss.

A snapshot includes snap-managed user data, system data, and snap settings. It does not back up the installed package itself or files stored elsewhere, and an application can exclude some of its data. The snap must already be installed when its data is restored.

Restoring replaces current data rather than merging it, so newer changes can be lost. Closed applications and stopped services prevent conflicting writes during recovery; stopping a stateful service before taking its snapshot also lets it flush pending data to disk. Archive integrity and application recovery are separate checks.

Steps to back up and restore snap data snapshots:

  1. Read a retained message from the Mosquitto snap before backing it up.
    $ mosquitto_sub -t sensors/room/temperature -C 1 -W 5 -v
    sensors/room/temperature 22.5

    Mosquitto must already be installed with persistence enabled and an existing retained message on this topic. For another snap, substitute its name and an application-specific check of the data being saved. A retained message held only in memory is not part of a disk snapshot.

  2. Stop the Mosquitto service for a consistent snapshot.

    Stopping the broker disconnects its clients and interrupts messaging until it starts again. For a desktop snap, closing its applications replaces this service-stop action.

    $ sudo snap stop mosquitto
    Stopped.
  3. Save a snapshot of the Mosquitto snap.
    $ sudo snap save mosquitto
    Set  Snap       Age    Version  Rev   Size    Notes
    1    mosquitto  489ms  2.1.2    1138  16.1kB  -

    The Set column identifies the snapshot. The remaining commands use set 1; the ID on another system may differ. Without a snap name, sudo snap save snapshots all installed snaps.

  4. Check the saved snapshot for archive corruption.
    $ sudo snap check-snapshot 1
    Snapshot #1 verified successfully.
  5. Export the snapshot to a backup archive in a private working directory.

    The archive can contain private application data and credentials, and its file permissions may allow other local accounts to read it. The destination directory must be accessible only to the backup owner.

    $ sudo snap export-snapshot 1 mosquitto-snapshot-1.zip
    Exported snapshot #1 into "mosquitto-snapshot-1.zip"

    If protection against disk loss is needed, keep a protected copy on separate storage; an export on the same disk does not provide that protection.

  6. Restore the saved Mosquitto data while the service is stopped.

    Restoring overwrites current snap-managed data and settings for Mosquitto. If newer data may still be needed, a separate snapshot of that state provides a way back. For recovery at a later date, Mosquitto must be installed and its service stopped again before restoring.

    $ sudo snap restore 1 mosquitto
    Restored snapshot #1 of snaps "mosquitto".
  7. Start the Mosquitto service with the restored data.
    $ sudo snap start mosquitto
    Started.
  8. Confirm that the broker returns the retained message saved in the snapshot.
    $ mosquitto_sub -t sensors/room/temperature -C 1 -W 5 -v
    sensors/room/temperature 22.5

    The topic and value must match the data present before the backup. A timeout, connection error, or different value does not confirm recovery. The snapshot and exported archive remain available after restoration.

Share this guide