Skip to content

Commit bd7fb87

Browse files
committed
lnd+chanbackup: add lnd config flag
In this commit, we add the --disable-backup-archive with a default as false. When set to true then previous channel backup file will not be archived but replaced.
1 parent 51ebfae commit bd7fb87

File tree

5 files changed

+42
-21
lines changed

5 files changed

+42
-21
lines changed

chanbackup/backupfile.go

+21-15
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,15 @@ type MultiFile struct {
5353

5454
// archiveDir is the directory where we'll store old channel backups.
5555
archiveDir string
56+
57+
// deleteOldBackup indicates whether old backups should be deleted
58+
// rather than archived.
59+
deleteOldBackup bool
5660
}
5761

5862
// NewMultiFile create a new multi-file instance at the target location on the
5963
// file system.
60-
func NewMultiFile(fileName string) *MultiFile {
61-
64+
func NewMultiFile(fileName string, deleteOldBackup bool) *MultiFile {
6265
// We'll our temporary backup file in the very same directory as the
6366
// main backup file.
6467
backupFileDir := filepath.Dir(fileName)
@@ -70,9 +73,10 @@ func NewMultiFile(fileName string) *MultiFile {
7073
)
7174

7275
return &MultiFile{
73-
fileName: fileName,
74-
tempFileName: tempFileName,
75-
archiveDir: archiveDir,
76+
fileName: fileName,
77+
tempFileName: tempFileName,
78+
archiveDir: archiveDir,
79+
deleteOldBackup: deleteOldBackup,
7680
}
7781
}
7882

@@ -130,16 +134,18 @@ func (b *MultiFile) UpdateAndSwap(newBackup PackedMulti) error {
130134
return fmt.Errorf("unable to close file: %w", err)
131135
}
132136

133-
// We check if the main backup file exists, if it does we archive it
134-
// before replacing it with the new backup.
135-
if _, err := os.Stat(b.fileName); err == nil {
136-
log.Infof("Archiving old backup file at %v", b.fileName)
137-
138-
if err := createArchiveFile(
139-
b.archiveDir, b.fileName,
140-
); err != nil {
141-
return fmt.Errorf("unable to archive old backup file:"+
142-
" %w", err)
137+
if !b.deleteOldBackup {
138+
// We check if the main backup file exists, if it does we
139+
// archive it before replacing it with the new backup.
140+
if _, err := os.Stat(b.fileName); err == nil {
141+
log.Infof("Archiving old backup file at %v", b.fileName)
142+
143+
if err := createArchiveFile(
144+
b.archiveDir, b.fileName,
145+
); err != nil {
146+
return fmt.Errorf("unable to archive old "+
147+
"backup file: %w", err)
148+
}
143149
}
144150
}
145151

chanbackup/backupfile_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func TestUpdateAndSwap(t *testing.T) {
9494
},
9595
}
9696
for i, testCase := range testCases {
97-
backupFile := NewMultiFile(testCase.fileName)
97+
backupFile := NewMultiFile(testCase.fileName, false)
9898

9999
// To start with, we'll make a random byte slice that'll pose
100100
// as our packed multi backup.
@@ -238,7 +238,7 @@ func TestExtractMulti(t *testing.T) {
238238
}
239239
for i, testCase := range testCases {
240240
// First, we'll make our backup file with the specified name.
241-
backupFile := NewMultiFile(testCase.fileName)
241+
backupFile := NewMultiFile(testCase.fileName, false)
242242

243243
// With our file made, we'll now attempt to read out the
244244
// multi-file.

config.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,12 @@ const (
247247
bitcoindBackendName = "bitcoind"
248248
btcdBackendName = "btcd"
249249
neutrinoBackendName = "neutrino"
250+
251+
// defaultDisableBackupArchive indicates whether to disable archiving of
252+
// channel backups. When false (default), old backups are archived to a
253+
// designated location. When true, old backups are simply deleted or
254+
// replaced.
255+
defaultDisableBackupArchive = false
250256
)
251257

252258
var (
@@ -364,6 +370,8 @@ type Config struct {
364370
MaxPendingChannels int `long:"maxpendingchannels" description:"The maximum number of incoming pending channels permitted per peer."`
365371
BackupFilePath string `long:"backupfilepath" description:"The target location of the channel backup file"`
366372

373+
DisableBackupArchive bool `long:"disable-backup-archive" description:"If set to true, channel backups will be deleted or replaced rather than being archived to a separate location."`
374+
367375
FeeURL string `long:"feeurl" description:"DEPRECATED: Use 'fee.url' option. Optional URL for external fee estimation. If no URL is specified, the method for fee estimation will depend on the chosen backend and network. Must be set for neutrino on mainnet." hidden:"true"`
368376

369377
Bitcoin *lncfg.Chain `group:"Bitcoin" namespace:"bitcoin"`
@@ -738,9 +746,10 @@ func DefaultConfig() Config {
738746
ServerPingTimeout: defaultGrpcServerPingTimeout,
739747
ClientPingMinWait: defaultGrpcClientPingMinWait,
740748
},
741-
LogConfig: build.DefaultLogConfig(),
742-
WtClient: lncfg.DefaultWtClientCfg(),
743-
HTTPHeaderTimeout: DefaultHTTPHeaderTimeout,
749+
LogConfig: build.DefaultLogConfig(),
750+
WtClient: lncfg.DefaultWtClientCfg(),
751+
HTTPHeaderTimeout: DefaultHTTPHeaderTimeout,
752+
DisableBackupArchive: defaultDisableBackupArchive,
744753
}
745754
}
746755

sample-lnd.conf

+4
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,10 @@
309309
; Example:
310310
; backupfilepath=~/.lnd/data/chain/bitcoin/mainnet/channel.backup
311311

312+
; When false (default), old channel backups are archived to a designated location.
313+
; When true, old backups are simply deleted or replaced.
314+
; disable-backup-archive=true
315+
312316
; The maximum capacity of the block cache in bytes. Increasing this will result
313317
; in more blocks being kept in memory but will increase performance when the
314318
; same block is required multiple times.

server.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -1595,7 +1595,9 @@ func newServer(cfg *Config, listenAddrs []net.Addr,
15951595
chanNotifier: s.channelNotifier,
15961596
addrs: dbs.ChanStateDB,
15971597
}
1598-
backupFile := chanbackup.NewMultiFile(cfg.BackupFilePath)
1598+
backupFile := chanbackup.NewMultiFile(
1599+
cfg.BackupFilePath, cfg.DisableBackupArchive,
1600+
)
15991601
startingChans, err := chanbackup.FetchStaticChanBackups(
16001602
s.chanStateDB, s.addrSource,
16011603
)

0 commit comments

Comments
 (0)