Skip to content

Commit 8ea7a6a

Browse files
authored
Merge pull request #39 from mittwald/fix/postgres-directory-backup-restore
Fix Postgres Directory Restore
2 parents 2265486 + 16e8571 commit 8ea7a6a

File tree

2 files changed

+84
-3
lines changed

2 files changed

+84
-3
lines changed

pkg/source/pgrestore/backend_config_based.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,32 @@ func NewConfigBasedBackend() (*ConfigBasedBackend, error) {
3333
}
3434

3535
func (b *ConfigBasedBackend) RestoreBackup(ctx context.Context) error {
36-
fileName, err := cli.CheckAndGunzipFile(b.cfg.Options.SourceFile)
37-
if err != nil {
38-
return err
36+
// Check if SourceFile is a directory (i.e., a pg_dump directory format)
37+
src := b.cfg.Options.SourceFile
38+
info, statErr := os.Stat(src)
39+
if statErr != nil {
40+
return statErr
41+
}
42+
43+
fileName := src
44+
45+
if !info.IsDir() {
46+
unzippedFileName, err := cli.CheckAndGunzipFile(src)
47+
if err != nil {
48+
return err
49+
}
50+
51+
fileName = unzippedFileName
3952
}
53+
4054
args := append(cli.StructToCLI(b.cfg.Options.Flags), b.cfg.Options.AdditionalArgs...)
4155
args = append(args, fileName)
4256
cmd := cli.CommandType{
4357
Binary: binary,
4458
Args: args,
4559
}
4660
var out []byte
61+
var err error
4762
out, err = cli.Run(ctx, cmd)
4863
if err != nil {
4964
return errors.WithStack(fmt.Errorf("%+v - %s", err, out))

test/pkg/source/postgrestest/postgres_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
const pgPort = "5432/tcp"
2727
const backupPath = "/tmp/postgres.dump.tar"
2828
const backupPathPlain = "/tmp/postgres.dump"
29+
const backupPathDir = "/tmp/postgres-dir"
2930
const backupPathZip = "/tmp/postgres.dump.tar.gz"
3031
const backupPathPlainZip = "/tmp/postgres.dump.gz"
3132
const postgresPW = "postgresroot"
@@ -122,6 +123,71 @@ func (pgDumpAndRestoreTestSuite *PGDumpAndRestoreTestSuite) TestBasicPGDumpAndRe
122123
assert.DeepEqual(pgDumpAndRestoreTestSuite.T(), testDataPlain, restoreResultPlain)
123124
}
124125

126+
// TestBasicPGDumpAndRestoreDirectory führt einen Integrationstest mit Directory-Format (-F d) durch, ohne Restic
127+
func (pgDumpAndRestoreTestSuite *PGDumpAndRestoreTestSuite) TestBasicPGDumpAndRestoreDirectory() {
128+
ctx := context.Background()
129+
130+
// Verzeichnis nach dem Test aufräumen
131+
defer func() {
132+
removeErr := os.RemoveAll(backupPathDir)
133+
if removeErr != nil {
134+
log.WithError(removeErr).Error("failed to remove pgdump directory backup")
135+
}
136+
}()
137+
138+
log.Info("Testing postgres restoration with directory dump via pg_restore")
139+
// Backup mit Directory-Format erstellen
140+
testData, err := pgDoBackup(
141+
ctx, false, commons.TestContainerSetup{
142+
Port: "",
143+
Address: "",
144+
},
145+
"d", backupPathDir,
146+
)
147+
pgDumpAndRestoreTestSuite.Require().NoError(err)
148+
149+
// In neuen Container restoren und Daten verifizieren
150+
restoreResult, err := pgDoRestore(
151+
ctx, false, commons.TestContainerSetup{
152+
Port: "",
153+
Address: "",
154+
},
155+
"d", backupPathDir,
156+
)
157+
pgDumpAndRestoreTestSuite.Require().NoError(err)
158+
159+
assert.DeepEqual(pgDumpAndRestoreTestSuite.T(), testData, restoreResult)
160+
}
161+
162+
// TestPGDumpAndRestoreDirectoryRestic testet Directory-Format (-F d) mit Restic
163+
func (pgDumpAndRestoreTestSuite *PGDumpAndRestoreTestSuite) TestPGDumpAndRestoreDirectoryRestic() {
164+
ctx := context.Background()
165+
166+
defer func() {
167+
removeErr := os.RemoveAll(backupPathDir)
168+
if removeErr != nil {
169+
log.WithError(removeErr).Error("failed to remove pgdump directory backup (restic)")
170+
}
171+
}()
172+
173+
resticContainer, err := commons.NewTestContainerSetup(ctx, &commons.ResticReq, commons.ResticPort)
174+
pgDumpAndRestoreTestSuite.Require().NoError(err)
175+
defer func() {
176+
resticErr := resticContainer.Container.Terminate(ctx)
177+
if resticErr != nil {
178+
log.WithError(resticErr).Error("failed to terminate directory restic container")
179+
}
180+
}()
181+
182+
testData, err := pgDoBackup(ctx, true, resticContainer, "d", backupPathDir)
183+
pgDumpAndRestoreTestSuite.Require().NoError(err)
184+
185+
restoreResult, err := pgDoRestore(ctx, true, resticContainer, "d", backupPathDir)
186+
pgDumpAndRestoreTestSuite.Require().NoError(err)
187+
188+
assert.DeepEqual(pgDumpAndRestoreTestSuite.T(), testData, restoreResult)
189+
}
190+
125191
// TestBasicPGDumpZip performs an integration test for brudi pgdump, with gzip and without use of restic
126192
func (pgDumpAndRestoreTestSuite *PGDumpAndRestoreTestSuite) TestBasicPGDumpAndRestoreGzip() {
127193
ctx := context.Background()

0 commit comments

Comments
 (0)