diff --git a/chanbackup/backupfile.go b/chanbackup/backupfile.go index 65105c019e..26ee713cbf 100644 --- a/chanbackup/backupfile.go +++ b/chanbackup/backupfile.go @@ -53,12 +53,15 @@ type MultiFile struct { // archiveDir is the directory where we'll store old channel backups. archiveDir string + + // deleteOldBackup indicates whether old backups should be deleted + // rather than archived. + deleteOldBackup bool } // NewMultiFile create a new multi-file instance at the target location on the // file system. -func NewMultiFile(fileName string) *MultiFile { - +func NewMultiFile(fileName string, deleteOldBackup bool) *MultiFile { // We'll our temporary backup file in the very same directory as the // main backup file. backupFileDir := filepath.Dir(fileName) @@ -70,9 +73,10 @@ func NewMultiFile(fileName string) *MultiFile { ) return &MultiFile{ - fileName: fileName, - tempFileName: tempFileName, - archiveDir: archiveDir, + fileName: fileName, + tempFileName: tempFileName, + archiveDir: archiveDir, + deleteOldBackup: deleteOldBackup, } } @@ -130,16 +134,18 @@ func (b *MultiFile) UpdateAndSwap(newBackup PackedMulti) error { return fmt.Errorf("unable to close file: %w", err) } - // We check if the main backup file exists, if it does we archive it - // before replacing it with the new backup. - if _, err := os.Stat(b.fileName); err == nil { - log.Infof("Archiving old backup file at %v", b.fileName) - - if err := createArchiveFile( - b.archiveDir, b.fileName, - ); err != nil { - return fmt.Errorf("unable to archive old backup file:"+ - " %w", err) + if !b.deleteOldBackup { + // We check if the main backup file exists, if it does we + // archive it before replacing it with the new backup. + if _, err := os.Stat(b.fileName); err == nil { + log.Infof("Archiving old backup file at %v", b.fileName) + + if err := createArchiveFile( + b.archiveDir, b.fileName, + ); err != nil { + return fmt.Errorf("unable to archive old "+ + "backup file: %w", err) + } } } diff --git a/chanbackup/backupfile_test.go b/chanbackup/backupfile_test.go index b0d3ba66f4..4471bbf4f8 100644 --- a/chanbackup/backupfile_test.go +++ b/chanbackup/backupfile_test.go @@ -94,7 +94,7 @@ func TestUpdateAndSwap(t *testing.T) { }, } for i, testCase := range testCases { - backupFile := NewMultiFile(testCase.fileName) + backupFile := NewMultiFile(testCase.fileName, false) // To start with, we'll make a random byte slice that'll pose // as our packed multi backup. @@ -238,7 +238,7 @@ func TestExtractMulti(t *testing.T) { } for i, testCase := range testCases { // First, we'll make our backup file with the specified name. - backupFile := NewMultiFile(testCase.fileName) + backupFile := NewMultiFile(testCase.fileName, false) // With our file made, we'll now attempt to read out the // multi-file. diff --git a/config.go b/config.go index 04a4917658..18ead30233 100644 --- a/config.go +++ b/config.go @@ -244,6 +244,12 @@ const ( bitcoindBackendName = "bitcoind" btcdBackendName = "btcd" neutrinoBackendName = "neutrino" + + // defaultDisableBackupArchive indicates whether to disable archiving of + // channel backups. When false (default), old backups are archived to a + // designated location. When true, old backups are simply deleted or + // replaced. + defaultDisableBackupArchive = false ) var ( @@ -360,6 +366,8 @@ type Config struct { MaxPendingChannels int `long:"maxpendingchannels" description:"The maximum number of incoming pending channels permitted per peer."` BackupFilePath string `long:"backupfilepath" description:"The target location of the channel backup file"` + 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."` + 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"` Bitcoin *lncfg.Chain `group:"Bitcoin" namespace:"bitcoin"` @@ -732,9 +740,10 @@ func DefaultConfig() Config { ServerPingTimeout: defaultGrpcServerPingTimeout, ClientPingMinWait: defaultGrpcClientPingMinWait, }, - LogConfig: build.DefaultLogConfig(), - WtClient: lncfg.DefaultWtClientCfg(), - HTTPHeaderTimeout: DefaultHTTPHeaderTimeout, + LogConfig: build.DefaultLogConfig(), + WtClient: lncfg.DefaultWtClientCfg(), + HTTPHeaderTimeout: DefaultHTTPHeaderTimeout, + DisableBackupArchive: defaultDisableBackupArchive, } } diff --git a/sample-lnd.conf b/sample-lnd.conf index 6af5e4b578..5485690dd8 100644 --- a/sample-lnd.conf +++ b/sample-lnd.conf @@ -309,6 +309,10 @@ ; Example: ; backupfilepath=~/.lnd/data/chain/bitcoin/mainnet/channel.backup +; When false (default), old channel backups are archived to a designated location. +; When true, old backups are simply deleted or replaced. +; disable-backup-archive=true + ; The maximum capacity of the block cache in bytes. Increasing this will result ; in more blocks being kept in memory but will increase performance when the ; same block is required multiple times. diff --git a/server.go b/server.go index ec19d0913e..9ef479e398 100644 --- a/server.go +++ b/server.go @@ -1622,7 +1622,9 @@ func newServer(cfg *Config, listenAddrs []net.Addr, chanNotifier: s.channelNotifier, addrs: dbs.ChanStateDB, } - backupFile := chanbackup.NewMultiFile(cfg.BackupFilePath) + backupFile := chanbackup.NewMultiFile( + cfg.BackupFilePath, cfg.DisableBackupArchive, + ) startingChans, err := chanbackup.FetchStaticChanBackups( s.chanStateDB, s.addrSource, )