Skip to content

Commit 34cb671

Browse files
committed
add a dump of hba_file and ident_file
1 parent 5b871c5 commit 34cb671

3 files changed

Lines changed: 86 additions & 1 deletion

File tree

main.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,13 @@ func main() {
191191
}
192192
}
193193

194+
if err := dumpConfigFiles(opts.Directory, opts.TimeFormat, db); err != nil {
195+
db.Close()
196+
l.Fatalln("could not dump configuration files:", err)
197+
postBackupHook(opts.PostHook)
198+
os.Exit(1)
199+
}
200+
194201
databases, err := listDatabases(db, opts.WithTemplates, opts.ExcludeDbs, opts.Dbnames)
195202
if err != nil {
196203
l.Fatalln(err)
@@ -334,7 +341,7 @@ func main() {
334341
}
335342
}
336343

337-
for _, other := range []string{"pg_globals", "pg_settings"} {
344+
for _, other := range []string{"pg_globals", "pg_settings", "hba_file", "ident_file"} {
338345
limit := now.Add(defDbOpts.PurgeInterval)
339346
if err := purgeDumps(opts.Directory, other, defDbOpts.PurgeKeep, limit); err != nil {
340347
exitCode = 1
@@ -607,3 +614,27 @@ func dumpSettings(dir string, timeFormat string, db *pg) error {
607614

608615
return nil
609616
}
617+
618+
func dumpConfigFiles(dir string, timeFormat string, db *pg) error {
619+
for _, param := range []string{"hba_file", "ident_file"} {
620+
file := formatDumpPath(dir, timeFormat, "out", param, time.Now())
621+
622+
if err := os.MkdirAll(filepath.Dir(file), 0755); err != nil {
623+
return err
624+
}
625+
626+
s, err := extractFileFromSettings(db, param)
627+
if err != nil {
628+
return err
629+
}
630+
631+
// Use a Buffer to avoid creating an empty file
632+
if len(s) > 0 {
633+
l.Verbosef("writing contents of '%s' to: %s", param, file)
634+
if err := ioutil.WriteFile(file, []byte(s), 0644); err != nil {
635+
return err
636+
}
637+
}
638+
}
639+
return nil
640+
}

sql.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,41 @@ func showSettings(db *pg) (string, error) {
506506
}
507507
}
508508

509+
func extractFileFromSettings(db *pg, name string) (string, error) {
510+
query := "SELECT setting, pg_read_file(setting, 0, (pg_stat_file(setting)).size) FROM pg_settings WHERE name = $1"
511+
512+
l.Verboseln("executing SQL query:", query)
513+
rows, err := db.conn.Query(query, name)
514+
if err != nil {
515+
return "", fmt.Errorf("could not query file contents from settings: %s", err)
516+
}
517+
defer rows.Close()
518+
519+
var result string
520+
521+
for rows.Next() {
522+
var (
523+
path string
524+
contents string
525+
)
526+
527+
err := rows.Scan(&path, &contents)
528+
if err != nil {
529+
l.Errorln(err)
530+
continue
531+
}
532+
533+
result = fmt.Sprintf("# path: %s\n%s\n", path, contents)
534+
}
535+
536+
err = rows.Err()
537+
if err != nil {
538+
return "", fmt.Errorf("could not retrive rows: %s", err)
539+
}
540+
541+
return result, nil
542+
}
543+
509544
type pgReplicaHasLocks struct{}
510545

511546
func (*pgReplicaHasLocks) Error() string {

sql_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,5 +292,24 @@ func TestDumpCreateDBAndACL(t *testing.T) {
292292
}
293293
}
294294

295+
func TestExtractFileFromSettings(t *testing.T) {
296+
needPgConn(t)
297+
298+
got, err := extractFileFromSettings(testdb, "hba_file")
299+
if err != nil {
300+
t.Errorf("expected non nil error, got %q", err)
301+
}
302+
303+
if got == "" {
304+
t.Errorf("expected some data, got nothing")
305+
} else {
306+
c := strings.Split(got, "\n")
307+
re := regexp.MustCompile(`^# path: \S+`)
308+
if !re.MatchString(c[0]) {
309+
t.Errorf("excepted string matching \"^# path: \\S+\", got %s", c[0])
310+
}
311+
}
312+
}
313+
295314
// Testing replication management fonctions needs a more complex setup
296315
// so we skip it.

0 commit comments

Comments
 (0)