Repository navigation
backups: exclude channel databases and let bitcoind copy its wallets - #279
Merged
prusnak merged 2 commits intoNov 18, 2026
Merged
Conversation
ostermayer
force-pushed
the
backups-no-live-lightning-db
branch
from
November 18, 2026 00:19
d9b68f0 to
6ad3427
Compare
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
force-pushed
the
backups-no-live-lightning-db
branch
from
November 18, 2026 01:01
6ad3427 to
8d18c0e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.dband Core Lightning'slightningd.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:
graphdir. In lnd 0.21 it holdschannel.db,sphinxreplay.dbandwtclient.db, orchannel.sqliteandlnd.sqlitewith the SQLite backend.lightningd.sqlite3and the SQLite files thatservices.clightning.walletsets, 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.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 underservices.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, soextraFilescan't include them. The backup keeps the files for recovery:lnd-seed-mnemonicandchannel.backupfor lnd, andhsm_secretandemergency.recoverfor Core Lightning. lnd and Core Lightning replace both files atomically, so a live copy of them is safe.A new
bitcoind-wallet-backupunit 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 tolocalhostand bitcoind's RPC address. Here's what it does:wallet-backups.backupwallet. Each copy goes towallet-<sha256 of the wallet name>.dat.tmpand is renamed towallet-<sha256>.datonly when it's complete. The hash keeps the file name short for any wallet name.wallet-names.jsonmaps each copy to its wallet name, so a restore can find the right file.duplicityunit after the backup.bitcoin-cliruns with-noconfand explicit RPC options. The operator can replacebitcoin.confin the data dir, and a planted config could makebitcoin-clisend the first line of a file it can read to any address. With thebitcoind-remote.nixpreset, 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.mdgets a Backups section. It says what the backup holds and what it leaves out and why. It and thepostgresqlDatabasesoption 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 andchannel.backupfor lnd,hsm_secretandemergency.recoverfor 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
duplicityunit 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.pyruns 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 thatwallet-names.jsonmaps the copy to the wallet. Before the second backup, the test adds a stale copy, awallet.datand another file. The second backup must leave out the stale copy and any.tmpfile, and the other two files must still exist. After the backup with bitcoind stopped, the test checks these things:channel.backupand Core Lightning'semergency.recover, next to the seed andhsm_secret.graph/mainnet/channel.dbandlightningd.sqlite3exist on disk but aren't in the backup.lnd writes
channel.backuponly 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.nixchecks these things:./,../,//and a replica name that ends with:, a DSN with an empty replica, an emptysqlite3://DSN, a database namedchannel[1].sqlite3, an encrypted local replica, an encrypted SSHFS replica, a duplicity root of/var/libwith a replica outside it, and a root of/var/lib/..bitcoind-remote.nixpreset ordisablewallet.nix eval .#ciTestInfo,.#checks.x86_64-linux.module-configand.#checks.x86_64-linux.test-runnerpass.VM scenario
default(the secure node scenario with backups, lnd, Core Lightning and BTCPay Server) passes.VM scenario
netnswith 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,-waland super-journal files, a database namedh, andchannel[1].sqlite3with its journal. It keepschannel1.sqlite3,channel.backup,hsm_secretandemergency.recover.After the rebase onto master c3d955c (Keep unreleased migration notices visible on master #283),
module-config,test-runner,ci-workflowsanddefaultpass on the KVM host. On master, the notice is a warning until 0.2.0 is released, and the tests pass withversion.nixat 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.