Skip to content

backups: exclude channel databases and let bitcoind copy its wallets - #279

Merged
prusnak merged 2 commits into
bitcoinix:masterfrom
ostermayer:backups-no-live-lightning-db
Nov 18, 2026
Merged

prusnak merged 2 commits into
bitcoinix:masterfrom
ostermayer:backups-no-live-lightning-db

Conversation

@ostermayer

@ostermayer ostermayer commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #95.

The backups service copied the whole lnd and Core Lightning data dirs while the daemons ran. So the backup had lnd's channel.db and Core Lightning's lightningd.sqlite3, copied while the daemons wrote them. A copy made during a write can be torn. And a node that runs from an outdated copy can broadcast a revoked channel state. The peer can then take all funds in that channel. The docs didn't warn against restoring these files. The bitcoind wallet files were copied live too.

This PR changes three things:

  • The filelist leaves out the Lightning channel databases:

    • lnd's graph dir. In lnd 0.21 it holds channel.db, sphinxreplay.db and wtclient.db, or channel.sqlite and lnd.sqlite with the SQLite backend.
    • Core Lightning's lightningd.sqlite3 and the SQLite files that services.clightning.wallet sets, such as a replica. Each database also gets a <file>-* line for its journal files. Like Core Lightning, the DSN is split at the first : only. A relative path in it starts in the network dir, and an empty one names no file.
    • The gocryptfs dir of an encrypted replica in local.directory, which holds the encrypted database. With SSHFS or encrypted replication, also the mounts dir /var/cache/clightning-replication, which holds the SSHFS cipher dir and the plaintext mount.

    The paths are normalised, because duplicity matches them as strings, so ./ or ../ would slip through. duplicity also reads each line as a glob, so [, * and ? in a path are escaped as one-char classes. Only paths under services.duplicity.root, normalised too, are listed, because duplicity aborts the whole backup on a selection outside its root. These entries come first in the filelist, so extraFiles can't include them. The backup keeps the files for recovery: lnd-seed-mnemonic and channel.backup for lnd, and hsm_secret and emergency.recover for Core Lightning. lnd and Core Lightning replace both files atomically, so a live copy of them is safe.

  • A new bitcoind-wallet-backup unit runs before each backup, like the PostgreSQL dumps. It runs as the bitcoind user with the usual hardening, write access only to /var/lib/bitcoind/wallet-backups, and network access only to localhost and bitcoind's RPC address. Here's what it does:

    • It removes the files of the last backup. It deletes only the files it writes, because the dir can also hold other files, like a wallet named wallet-backups.
    • If bitcoind runs, it asks it for a consistent copy of each loaded wallet with backupwallet. Each copy goes to wallet-<sha256 of the wallet name>.dat.tmp and is renamed to wallet-<sha256>.dat only when it's complete. The hash keeps the file name short for any wallet name. wallet-names.json maps each copy to its wallet name, so a restore can find the right file.
    • If bitcoind is stopped, it makes no copies, because the wallet files are consistent then.
    • If bitcoind is starting or stopping, or a copy fails, the unit fails, and so does the duplicity unit after the backup.

    bitcoin-cli runs with -noconf and explicit RPC options. The operator can replace bitcoin.conf in the data dir, and a planted config could make bitcoin-cli send the first line of a file it can read to any address. With the bitcoind-remote.nix preset, the wallets are on the other node, so the unit is masked and the backup doesn't start it. The live wallet files stay in the backup, because they're the only copy of wallets that bitcoind doesn't load.

  • docs/services.md gets a Backups section. It says what the backup holds and what it leaves out and why. It and the postgresqlDatabases option say not to list Core Lightning's PostgreSQL database, because its dump is an outdated copy of the channel state. It also says how to recover Lightning funds: the seed and channel.backup for lnd, hsm_secret and emergency.recover for Core Lightning, and database replication for a Core Lightning database that's always current.

The backup no longer covers data it covered before, and the duplicity unit can now fail in new cases. So a 0.2.0 notice tells every config with backups about both changes. Backups made before this change still contain the channel databases. The notice and the upgrade notes say to never restore them.

Testing

  • test/suite/backups.py runs two backups while bitcoind and the Lightning daemons run. Both must have the copy of the loaded test wallet and no channel database. The test checks that wallet-names.json maps the copy to the wallet. Before the second backup, the test adds a stale copy, a wallet.dat and another file. The second backup must leave out the stale copy and any .tmp file, and the other two files must still exist. After the backup with bitcoind stopped, the test checks these things:

    • The backup has no wallet copy.
    • It has lnd's channel.backup and Core Lightning's emergency.recover, next to the seed and hsm_secret.
    • graph/mainnet/channel.db and lightningd.sqlite3 exist on disk but aren't in the backup.

    lnd writes channel.backup only once it's synced to the chain, which the test node never is, so the test adds a stand-in file for that check.

  • test/configuration.nix checks these things:

    • The exclusions come first in the filelist: for the default config, a DSN with ./, ../, // and a replica name that ends with :, a DSN with an empty replica, an empty sqlite3:// DSN, a database named channel[1].sqlite3, an encrypted local replica, an encrypted SSHFS replica, a duplicity root of /var/lib with a replica outside it, and a root of /var/lib/..
    • The wallet unit is defined and started for a local bitcoind, and not for the bitcoind-remote.nix preset or disablewallet.
    • The 0.2.0 notice shows for any config with backups.
  • nix eval .#ciTestInfo, .#checks.x86_64-linux.module-config and .#checks.x86_64-linux.test-runner pass.

  • VM scenario default (the secure node scenario with backups, lnd, Core Lightning and BTCPay Server) passes.

  • VM scenario netns with the backups test turned on passes. It checks that the wallet unit reaches bitcoind across the namespace with its IP allow list.

  • I ran duplicity 3.0.7 on its own with a filelist like the new one. It leaves out the databases, their -journal, -wal and super-journal files, a database named h, and channel[1].sqlite3 with its journal. It keeps channel1.sqlite3, channel.backup, hsm_secret and emergency.recover.

  • After the rebase onto master c3d955c (Keep unreleased migration notices visible on master #283), module-config, test-runner, ci-workflows and default pass on the KVM host. On master, the notice is a warning until 0.2.0 is released, and the tests pass with version.nix at 0.1.6 and at 0.2.0.

Models

Claude Opus 5.5 wrote the change. Codex GPT-5.6 Sol, Codex GPT-6 Astra (two passes plus rechecks), Kimi K3.1 Pro (two passes), DeepSeek V4.1 Flash 1017 and GLM 5.3 reviewed it. Claude Opus 5.5 rebased it onto #224 and #283.

@github-actions github-actions Bot added area: ci .github/, test/, dev/ area: docs docs/, README, examples/, SECURITY.md, book.toml, assets area: modules modules/ and pkgs/lib.nix type: feature New capability or service status: rebase needed Merge conflicts need to be resolved labels Nov 16, 2026
@ostermayer
ostermayer force-pushed the backups-no-live-lightning-db branch from d9b68f0 to 6ad3427 Compare November 18, 2026 00:19
The backup copied the whole lnd and Core Lightning data dirs while the
daemons ran, including lnd's channel.db and Core Lightning's
lightningd.sqlite3. A copy made during a write can be torn. And a node
that runs from an outdated copy can broadcast a revoked channel state.
The peer can then take all funds in that channel. Nothing warned against
restoring these files.

The filelist now leaves out lnd's graph dir, which holds channel.db and
the other channel state databases. It also leaves out Core Lightning's
lightningd.sqlite3, the SQLite files that services.clightning.wallet
sets, their journal files, the dir of an encrypted local replica and
the mounts dir of SSHFS or encrypted replication.
The paths are normalised, because duplicity matches them as strings,
and only paths under duplicity's root are listed. These entries come
first, so extraFiles can't include them. The backup keeps what recovery
needs: the lnd seed and channel.backup, and Core Lightning's hsm_secret
and emergency.recover.

bitcoind's wallet files were copied live too. Before each backup, the
new bitcoind-wallet-backup unit now asks a local bitcoind for a
consistent copy of each loaded wallet in
/var/lib/bitcoind/wallet-backups. It runs as the bitcoind user, removes
the copies of the last backup, and renames each copy into place only
when it's complete. bitcoin-cli ignores bitcoin.conf, because the
operator can replace that file in the data dir. Like a failed database
dump, a failed copy fails the duplicity unit after the backup. With the
bitcoind-remote preset, the wallets are on the other node and the
backup makes no copies.

The docs now say what the backup holds, what it leaves out and how to
recover Lightning funds. A 0.2.0 notice tells all users of the backups
service about both changes.

Fixes bitcoinix#95.

Assisted-by: Claude Code (Claude Opus 5.5)
@ostermayer
ostermayer force-pushed the backups-no-live-lightning-db branch from 6ad3427 to 8d18c0e Compare November 18, 2026 01:01
@github-actions github-actions Bot added status: rebase needed Merge conflicts need to be resolved and removed status: rebase needed Merge conflicts need to be resolved labels Nov 18, 2026
@prusnak prusnak added this to the v0.2.0 milestone Nov 18, 2026
@github-actions github-actions Bot removed the status: rebase needed Merge conflicts need to be resolved label Nov 18, 2026
@prusnak
prusnak merged commit 3b50f0a into bitcoinix:master Nov 18, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: ci .github/, test/, dev/ area: docs docs/, README, examples/, SECURITY.md, book.toml, assets area: modules modules/ and pkgs/lib.nix type: feature New capability or service

Projects

None yet

Development

Successfully merging this pull request may close these issues.

backups: lnd and clightning databases are copied live; a restored channel.db can lose funds

2 participants