Skip to content

feat(snap): automatic wallet backup on snap removal#13

Merged
dominicletz merged 3 commits into
mainfrom
cursor/diode-node-issue-2-e4b8
May 26, 2026
Merged

feat(snap): automatic wallet backup on snap removal#13
dominicletz merged 3 commits into
mainfrom
cursor/diode-node-issue-2-e4b8

Conversation

@dominicletz
Copy link
Copy Markdown
Member

@dominicletz dominicletz commented May 25, 2026

Summary

Closes #2

When a user runs snap remove diode-node, the snap's data directories are deleted and wallet access can be lost permanently. This PR adds an automatic backup that runs in the snap remove hook before data is wiped.

Approach

This follows the design in issue #2:

  1. remove hook (snap/hooks/remove) runs before snap data deletion and invokes the backup script bundled in the snap.
  2. backup-dir plug requests write access to /var/backups/diode-node via the system-files interface.
  3. Backup script (scripts/snap_backup_on_remove.sh) archives the critical resurrection files:
    • $SNAP_USER_DATA/node (node identity)
    • $SNAP_DATA/nodedata_* (wallet/database)
  4. Fallback UX: if the backup directory cannot be written (plug not connected), the hook logs a warning pointing users to snap saved / snap restore within the 31-day snapd retention window — removal still succeeds.
  5. Restore helper (scripts/restore_snap_backup.sh, installed as bin/restore_snap_backup) and README documentation for manual recovery.

Note: erl_inetrc is intentionally excluded — DNS config is regenerated on service start and is not required to resurrect the node.

Business case

Users who uninstall the snap without knowing about wallet persistence lose access to their node identity and funds. Automatic backup on removal makes recovery possible without requiring users to know about manual snap save procedures upfront.

Demo

Simulated snap-removal flow (mock $SNAP_DATA / $SNAP_USER_DATA directories):

1. Critical files present before removal

Simulated snap data before removal showing node identity, erl_inetrc, and nodedata_prod wallet files

2. Remove hook creates secure backup archive

Remove hook backup success message and backup tarball with 0600 permissions in /var/backups/diode-node

3. Archive preserves resurrection files + manifest

Tarball contents listing snap_user_data, snap_data/nodedata_prod, and manifest.txt

4. Restore after reinstall

Restore script recovering node identity and wallet data into fresh snap directories

5. Fallback when backup-dir plug is not connected

Fallback warning when backup directory is not writable, pointing to snap saved within 31 days

Testing

Added test/snap_backup_on_remove_test.exs covering:

  • Tarball creation with node identity and nodedata, secure permissions (0600)
  • Explicit exclusion of erl_inetrc even when present
  • Skip when no data exists
  • Graceful handling when backup directory is not writable

Validated locally with bash integration tests mirroring the ExUnit cases.

Notes

  • Auto-connection for the backup-dir system-files plug will need a separate Snap Store forum request.
Open in Web Open in Cursor 

Implement the remove hook to archive critical node files (identity,
erl_inetrc, nodedata_*) to /var/backups/diode-node before snap data
is deleted. Adds backup-dir system-files plug, restore helper, unit
tests, and README documentation.

Closes #2

Co-authored-by: Dominic Letz <dominicletz@users.noreply.github.com>
@dominicletz dominicletz added the cursor-waiting cursor-automation.com workflow column label May 25, 2026 — with Cursor
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces an automatic backup and restoration mechanism for the diode-node snap. It adds scripts to archive critical node data (identity and wallet databases) during the snap removal process and provides a corresponding restoration script, supported by new tests and updated documentation. The review feedback focuses on security hardening for handling sensitive wallet data, specifically recommending the use of a restrictive umask (077) in both scripts and ensuring the backup directory is secured immediately upon creation. Additionally, there is a suggestion to align the restoration script's usage instructions with the recommended commands in the README.

Comment thread scripts/restore_snap_backup.sh Outdated

sudo snap install diode-node
sudo snap stop diode-node.service
sudo $0 /var/backups/diode-node/diode_node_backup_YYYY-MM-DD_HHMMSS.tar.gz
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The usage example suggests running the script directly with sudo, but since it relies on SNAP environment variables, it is better to align this message with the recommended command in the README which uses snap run --shell.

Suggested change
sudo $0 /var/backups/diode-node/diode_node_backup_YYYY-MM-DD_HHMMSS.tar.gz
sudo snap run --shell diode-node -c 'bin/restore_snap_backup /var/backups/diode-node/diode_node_backup_YYYY-MM-DD_HHMMSS.tar.gz'

exit 1
fi

staging_dir=$(mktemp -d)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Setting a restrictive umask is recommended when handling sensitive data like wallet backups to ensure that any temporary files or directories created during the restoration process are not world-readable.

Suggested change
staging_dir=$(mktemp -d)
umask 077
staging_dir=$(mktemp -d)

Comment thread scripts/snap_backup_on_remove.sh
Comment on lines +84 to +88
if ! mkdir -p "$BACKUP_DIR" 2>/dev/null; then
log "cannot create backup directory $BACKUP_DIR (is backup-dir plug connected?)"
log "use 'snap saved' within 31 days to recover data via 'snap restore'"
exit 0
fi
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For better security hardening, consider applying chmod 0700 to the backup directory immediately after it is created, rather than at the end of the script. This ensures the directory is secured before the sensitive tarball is written into it.

Suggested change
if ! mkdir -p "$BACKUP_DIR" 2>/dev/null; then
log "cannot create backup directory $BACKUP_DIR (is backup-dir plug connected?)"
log "use 'snap saved' within 31 days to recover data via 'snap restore'"
exit 0
fi
if ! mkdir -p "$BACKUP_DIR" 2>/dev/null; then
log "cannot create backup directory $BACKUP_DIR (is backup-dir plug connected?)"
log "use 'snap saved' within 31 days to recover data via 'snap restore'"
exit 0
fi
chmod 0700 "$BACKUP_DIR" 2>/dev/null || true

@cursor-automation
Copy link
Copy Markdown

@gemini review this PR

@cursor-automation cursor-automation Bot added cursor-waiting-for-ci cursor-automation.com workflow column and removed cursor-waiting cursor-automation.com workflow column labels May 25, 2026
- Set umask 077 before creating staging dirs and backup archives
- chmod 0700 backup directory before writing the tarball
- Align restore usage/errors with snap run --shell workflow
- Grant shell app backup-dir plug so restore can read /var/backups
- Document snap connect backup-dir in README restore steps
- Assert backup directory permissions in ExUnit test

Co-authored-by: Dominic Letz <dominicletz@users.noreply.github.com>
@cursor-automation cursor-automation Bot added cursor-demo cursor-automation.com workflow column cursor-waiting-for-human cursor-automation.com workflow column and removed cursor-waiting-for-ci cursor-automation.com workflow column cursor-demo cursor-automation.com workflow column labels May 25, 2026
@dominicletz
Copy link
Copy Markdown
Member Author

@cursoragent Remove erl_inetrc from the backup strategy and documentation. DNS data is not critical to be backed up.

@cursor
Copy link
Copy Markdown

cursor Bot commented May 26, 2026

Addressed @dominicletz's feedback — erl_inetrc is no longer part of the backup strategy.

Changes made

  1. scripts/snap_backup_on_remove.sh — Removed copying of $SNAP_USER_DATA/erl_inetrc. Backups now only include:

    • $SNAP_USER_DATA/node (identity)
    • $SNAP_DATA/nodedata_* (wallet/database)
  2. test/snap_backup_on_remove_test.exs — Updated the main test and added does not backup erl_inetrc even when present to prevent regression.

  3. README.md — Updated backup description to say "identity and wallet database" (removed "Erlang DNS config").

  4. PR feat(snap): automatic wallet backup on snap removal #13 description — Updated the approach section and added a note that erl_inetrc is intentionally excluded because DNS config is regenerated on service start.

Changes are committed and pushed to the PR branch. snap/run still generates erl_inetrc on start — that's unchanged and correct, since it's runtime config, not resurrection-critical data.

Open in Web Open in Cursor 

DNS config is regenerated on service start and is not required to
resurrect the node. Update tests and README accordingly.

Co-authored-by: Dominic Letz <dominicletz@users.noreply.github.com>
@dominicletz dominicletz merged commit d4c59cd into main May 26, 2026
2 checks passed
@dominicletz dominicletz deleted the cursor/diode-node-issue-2-e4b8 branch May 26, 2026 10:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cursor-waiting-for-human cursor-automation.com workflow column

Projects

None yet

Development

Successfully merging this pull request may close these issues.

If "snap delete" is used, create a snapshot of the critical elements required to resurrect the node

2 participants