Skip to content

Commit a51dfe9

Browse files
committed
chanbackup: archive old channel backups
In this commit, we first check if a previous backup file exists, if it does we copy it to archive folder before replacing it with a new backup file. We also added a test for archiving chan backups.
1 parent 6cabc74 commit a51dfe9

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed

chanbackup/backupfile.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ package chanbackup
22

33
import (
44
"fmt"
5+
"io"
56
"os"
67
"path/filepath"
8+
"time"
79

810
"github.com/lightningnetwork/lnd/keychain"
11+
"github.com/lightningnetwork/lnd/lnrpc"
912
)
1013

1114
const (
@@ -17,6 +20,10 @@ const (
1720
// file that we'll use to atomically update the primary back up file
1821
// when new channel are detected.
1922
DefaultTempBackupFileName = "temp-dont-use.backup"
23+
24+
// DefaultChanBackupArchiveDirName is the default name of the directory
25+
// that we'll use to store old channel backups.
26+
DefaultChanBackupArchiveDirName = "chan-backup-archives"
2027
)
2128

2229
var (
@@ -44,6 +51,9 @@ type MultiFile struct {
4451

4552
// tempFile is an open handle to the temp back up file.
4653
tempFile *os.File
54+
55+
// archiveDir is the directory where we'll store old channel backups.
56+
archiveDir string
4757
}
4858

4959
// NewMultiFile create a new multi-file instance at the target location on the
@@ -56,10 +66,14 @@ func NewMultiFile(fileName string) *MultiFile {
5666
tempFileName := filepath.Join(
5767
backupFileDir, DefaultTempBackupFileName,
5868
)
69+
archiveDir := filepath.Join(
70+
backupFileDir, DefaultChanBackupArchiveDirName,
71+
)
5972

6073
return &MultiFile{
6174
fileName: fileName,
6275
tempFileName: tempFileName,
76+
archiveDir: archiveDir,
6377
}
6478
}
6579

@@ -117,6 +131,12 @@ func (b *MultiFile) UpdateAndSwap(newBackup PackedMulti) error {
117131
return fmt.Errorf("unable to close file: %w", err)
118132
}
119133

134+
// Archive the old channel backup file before replacing.
135+
if err := b.createArchiveFile(); err != nil {
136+
return fmt.Errorf("unable to archive old channel "+
137+
"backup file: %w", err)
138+
}
139+
120140
// Finally, we'll attempt to atomically rename the temporary file to
121141
// the main back up file. If this succeeds, then we'll only have a
122142
// single file on disk once this method exits.
@@ -147,3 +167,68 @@ func (b *MultiFile) ExtractMulti(keyChain keychain.KeyRing) (*Multi, error) {
147167
packedMulti := PackedMulti(multiBytes)
148168
return packedMulti.Unpack(keyChain)
149169
}
170+
171+
// createArchiveFile creates an archive file with a timestamped name in the
172+
// specified archive directory, and copies the contents of the main backup file
173+
// to the new archive file.
174+
func (b *MultiFile) createArchiveFile() error {
175+
// We check for old channel backup file first.
176+
oldFileExists := lnrpc.FileExists(b.fileName)
177+
if !oldFileExists {
178+
log.Debug("No old channel backup file to archive")
179+
return nil
180+
}
181+
182+
log.Infof("Archiving old channel backup to %v", b.archiveDir)
183+
184+
// Generate archive file path with timestamped name.
185+
baseFileName := filepath.Base(b.fileName)
186+
timestamp := time.Now().Format("2006-01-02-15-04-05")
187+
188+
archiveFileName := fmt.Sprintf("%s-%s", baseFileName, timestamp)
189+
archiveFilePath := filepath.Join(b.archiveDir, archiveFileName)
190+
191+
oldBackupFile, err := os.Open(b.fileName)
192+
if err != nil {
193+
return fmt.Errorf("unable to open old channel backup file: "+
194+
"%w", err)
195+
}
196+
defer func() {
197+
err := oldBackupFile.Close()
198+
if err != nil {
199+
log.Errorf("unable to close old channel backup file: "+
200+
"%v", err)
201+
}
202+
}()
203+
204+
// Ensure the archive directory exists. If it doesn't we create it.
205+
const archiveDirPermissions = 0o700
206+
err = os.MkdirAll(b.archiveDir, archiveDirPermissions)
207+
if err != nil {
208+
return fmt.Errorf("unable to create archive directory: %w", err)
209+
}
210+
211+
// Create new archive file.
212+
archiveFile, err := os.Create(archiveFilePath)
213+
if err != nil {
214+
return fmt.Errorf("unable to create archive file: %w", err)
215+
}
216+
defer func() {
217+
err := archiveFile.Close()
218+
if err != nil {
219+
log.Errorf("unable to close archive file: %v", err)
220+
}
221+
}()
222+
223+
// Copy contents of old backup to the newly created archive files.
224+
_, err = io.Copy(archiveFile, oldBackupFile)
225+
if err != nil {
226+
return fmt.Errorf("unable to copy to archive file: %w", err)
227+
}
228+
err = archiveFile.Sync()
229+
if err != nil {
230+
return fmt.Errorf("unable to sync archive file: %w", err)
231+
}
232+
233+
return nil
234+
}

chanbackup/backupfile_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,70 @@ func TestExtractMulti(t *testing.T) {
274274
assertMultiEqual(t, &unpackedMulti, freshUnpackedMulti)
275275
}
276276
}
277+
278+
// TestCreateArchiveFile tests that we're able to create an archive file
279+
// with a timestamped name in the specified archive directory, and copy the
280+
// contents of the main backup file to the new archive file.
281+
func TestCreateArchiveFile(t *testing.T) {
282+
t.Parallel()
283+
284+
// First, we'll create a temporary directory for our test files.
285+
tempDir := t.TempDir()
286+
archiveDir := filepath.Join(tempDir, DefaultChanBackupArchiveDirName)
287+
288+
// Next, we'll create a test backup file and write some content to it.
289+
backupFile := filepath.Join(tempDir, DefaultBackupFileName)
290+
testContent := []byte("test backup content")
291+
err := os.WriteFile(backupFile, testContent, 0644)
292+
require.NoError(t, err)
293+
294+
tests := []struct {
295+
name string
296+
setup func()
297+
wantError bool
298+
}{
299+
{
300+
name: "successful archive",
301+
},
302+
{
303+
name: "invalid archive directory permissions",
304+
setup: func() {
305+
// Create dir with no write permissions.
306+
err := os.MkdirAll(archiveDir, 0500)
307+
require.NoError(t, err)
308+
},
309+
wantError: true,
310+
},
311+
}
312+
313+
for _, tc := range tests {
314+
tc := tc
315+
t.Run(tc.name, func(t *testing.T) {
316+
defer os.RemoveAll(archiveDir)
317+
if tc.setup != nil {
318+
tc.setup()
319+
}
320+
321+
multiFile := NewMultiFile(backupFile)
322+
323+
err := multiFile.createArchiveFile()
324+
if tc.wantError {
325+
require.Error(t, err)
326+
return
327+
}
328+
329+
require.NoError(t, err)
330+
331+
// Verify archive exists and content matches.
332+
files, err := os.ReadDir(archiveDir)
333+
require.NoError(t, err)
334+
require.Len(t, files, 1)
335+
336+
archivedContent, err := os.ReadFile(
337+
filepath.Join(archiveDir, files[0].Name()),
338+
)
339+
require.NoError(t, err)
340+
assertBackupMatches(t, backupFile, archivedContent)
341+
})
342+
}
343+
}

0 commit comments

Comments
 (0)