Skip to content

Commit df166fa

Browse files
committed
Add the --dump-only option to only dump databases
The new option --dump-only allows skipping dumps of configuration and globals, including database ACL and configuration when pg_dump is older than 11. More than one file can still be created, for example if checksum are enabled. The corresponding configuration parameter is dump_only, it is false by default.
1 parent 358c8ba commit df166fa

3 files changed

Lines changed: 49 additions & 29 deletions

File tree

config.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ type options struct {
8080
CipherPrivateKey string
8181
Decrypt bool
8282
WithRolePasswords bool
83+
DumpOnly bool
8384

8485
Upload string // values are none, s3, sftp, gcs
8586
PurgeRemote bool
@@ -261,6 +262,7 @@ func parseCli(args []string) (options, []string, error) {
261262
WithoutTemplates := pflag.Bool("without-templates", false, "force exclude templates")
262263
pflag.BoolVar(&opts.WithRolePasswords, "with-role-passwords", true, "dump globals with role passwords")
263264
WithoutRolePasswords := pflag.Bool("without-role-passwords", false, "do not dump passwords of roles")
265+
pflag.BoolVar(&opts.DumpOnly, "dump-only", false, "only dump databases, excluding configuration and globals")
264266
pflag.IntVarP(&opts.PauseTimeout, "pause-timeout", "T", 3600, "abort if replication cannot be paused after this number\nof seconds")
265267
pflag.IntVarP(&opts.Jobs, "jobs", "j", 1, "dump this many databases concurrently")
266268
pflag.StringVarP(&format, "format", "F", "custom", "database dump format: plain, custom, tar or directory")
@@ -484,7 +486,7 @@ func validateConfigurationFile(cfg *ini.File) error {
484486
"sftp_port", "sftp_user", "sftp_password", "sftp_directory", "sftp_identity",
485487
"sftp_ignore_hostkey", "gcs_bucket", "gcs_endpoint", "gcs_keyfile",
486488
"azure_container", "azure_account", "azure_key", "azure_endpoint", "pg_dump_options",
487-
"dump_role_passwords",
489+
"dump_role_passwords", "dump_only",
488490
}
489491

490492
gkLoop:
@@ -561,6 +563,7 @@ func loadConfigurationFile(path string) (options, error) {
561563
opts.Dbnames = s.Key("include_dbs").Strings(",")
562564
opts.WithTemplates = s.Key("with_templates").MustBool(false)
563565
opts.WithRolePasswords = s.Key("dump_role_passwords").MustBool(true)
566+
opts.DumpOnly = s.Key("dump_only").MustBool(false)
564567
format = s.Key("format").MustString("custom")
565568
opts.DirJobs = s.Key("parallel_backup_jobs").MustInt(1)
566569
opts.CompressLevel = s.Key("compress_level").MustInt(-1)
@@ -758,6 +761,8 @@ func mergeCliAndConfigOptions(cliOpts options, configOpts options, onCli []strin
758761
opts.WithTemplates = cliOpts.WithTemplates
759762
case "with-role-passwords":
760763
opts.WithRolePasswords = cliOpts.WithRolePasswords
764+
case "dump-only":
765+
opts.DumpOnly = cliOpts.DumpOnly
761766
case "pause-timeout":
762767
opts.PauseTimeout = cliOpts.PauseTimeout
763768
case "jobs":

main.go

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func run() (retVal error) {
153153
var cliOptions options
154154

155155
if cliOpts.NoConfigFile {
156-
l.Infoln("*** Skipping reading config file")
156+
l.Infoln("Skipping reading config file")
157157
cliOptions = defaultOptions()
158158
} else {
159159
// Load configuration file and allow the default configuration
@@ -277,38 +277,40 @@ func run() (retVal error) {
277277
}
278278
defer db.Close()
279279

280-
if !db.superuser {
281-
l.Infoln("connection user is not superuser, some information will not be dumped")
282-
}
280+
if !opts.DumpOnly {
281+
if !db.superuser {
282+
l.Infoln("connection user is not superuser, some information will not be dumped")
283+
}
283284

284-
// Then we can implicitely avoid dumping role password when using a
285-
// regular user
286-
dumpRolePasswords := opts.WithRolePasswords && db.superuser
287-
if dumpRolePasswords {
288-
l.Infoln("dumping globals")
289-
} else {
290-
l.Infoln("dumping globals without role passwords")
291-
}
292-
if err := dumpGlobals(opts.Directory, opts.TimeFormat, dumpRolePasswords, conninfo, producedFiles); err != nil {
293-
return fmt.Errorf("pg_dumpall of globals failed: %w", err)
294-
}
285+
// Then we can implicitely avoid dumping role password when using a
286+
// regular user
287+
dumpRolePasswords := opts.WithRolePasswords && db.superuser
288+
if dumpRolePasswords {
289+
l.Infoln("dumping globals")
290+
} else {
291+
l.Infoln("dumping globals without role passwords")
292+
}
293+
if err := dumpGlobals(opts.Directory, opts.TimeFormat, dumpRolePasswords, conninfo, producedFiles); err != nil {
294+
return fmt.Errorf("pg_dumpall of globals failed: %w", err)
295+
}
295296

296-
l.Infoln("dumping instance configuration")
297-
var (
298-
verr *pgVersionError
299-
perr *pgPrivError
300-
)
297+
l.Infoln("dumping instance configuration")
298+
var (
299+
verr *pgVersionError
300+
perr *pgPrivError
301+
)
301302

302-
if err := dumpSettings(opts.Directory, opts.TimeFormat, db, producedFiles); err != nil {
303-
if errors.As(err, &verr) || errors.As(err, &perr) {
304-
l.Warnln(err)
305-
} else {
306-
return fmt.Errorf("could not dump configuration parameters: %w", err)
303+
if err := dumpSettings(opts.Directory, opts.TimeFormat, db, producedFiles); err != nil {
304+
if errors.As(err, &verr) || errors.As(err, &perr) {
305+
l.Warnln(err)
306+
} else {
307+
return fmt.Errorf("could not dump configuration parameters: %w", err)
308+
}
307309
}
308-
}
309310

310-
if err := dumpConfigFiles(opts.Directory, opts.TimeFormat, db, producedFiles); err != nil {
311-
return fmt.Errorf("could not dump configuration files: %w", err)
311+
if err := dumpConfigFiles(opts.Directory, opts.TimeFormat, db, producedFiles); err != nil {
312+
return fmt.Errorf("could not dump configuration files: %w", err)
313+
}
312314
}
313315

314316
databases, err := listDatabases(db, opts.WithTemplates, opts.ExcludeDbs, opts.Dbnames)
@@ -367,6 +369,15 @@ func run() (retVal error) {
367369

368370
canDumpACL := true
369371
canDumpConfig := true
372+
373+
// When asked to only dump database, exclude ACL and config even if
374+
// this can lead of missing info on restore when pg_dump is older than
375+
// 11
376+
if opts.DumpOnly {
377+
canDumpACL = false
378+
canDumpConfig = false
379+
}
380+
370381
// collect the result of the jobs
371382
for j := 0; j < numJobs; j++ {
372383
var b, c string
@@ -407,6 +418,7 @@ func run() (retVal error) {
407418
l.Verboseln("dumping configuration of", dbname)
408419
c, err = dumpDBConfig(db, dbname)
409420
if err != nil {
421+
var verr *pgVersionError
410422
if !errors.As(err, &verr) {
411423
l.Errorln(err)
412424
exitCode = 1

pg_back.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ exclude_dbs =
4040
# include_dbs is empty.
4141
with_templates = false
4242

43+
# Dump only databases, excluding configuration and globals
44+
dump_only = false
45+
4346
# Format of the dump, understood by pg_dump. Possible values are
4447
# plain, custom, tar or directory.
4548
format = custom

0 commit comments

Comments
 (0)