@@ -302,6 +302,12 @@ func run() (retVal error) {
302302 }
303303 defer db .Close ()
304304
305+ // Generate a single datetime that will be used in all generated snapshot names
306+ var snapshotTime time.Time
307+ if opts .UniformSnapshotTimestamp {
308+ snapshotTime = time .Now ()
309+ }
310+
305311 if ! opts .DumpOnly {
306312 if ! db .superuser {
307313 l .Infoln ("connection user is not superuser, some information will not be dumped" )
@@ -315,7 +321,7 @@ func run() (retVal error) {
315321 } else {
316322 l .Infoln ("dumping globals without role passwords" )
317323 }
318- if err := dumpGlobals (opts .Directory , opts .TimeFormat , dumpRolePasswords , conninfo , producedFiles ); err != nil {
324+ if err := dumpGlobals (opts .Directory , opts .TimeFormat , dumpRolePasswords , conninfo , producedFiles , snapshotTime ); err != nil {
319325 return fmt .Errorf ("pg_dumpall of globals failed: %w" , err )
320326 }
321327
@@ -325,15 +331,15 @@ func run() (retVal error) {
325331 perr * pgPrivError
326332 )
327333
328- if err := dumpSettings (opts .Directory , opts .TimeFormat , db , producedFiles ); err != nil {
334+ if err := dumpSettings (opts .Directory , opts .TimeFormat , db , producedFiles , snapshotTime ); err != nil {
329335 if errors .As (err , & verr ) || errors .As (err , & perr ) {
330336 l .Warnln (err )
331337 } else {
332338 return fmt .Errorf ("could not dump configuration parameters: %w" , err )
333339 }
334340 }
335341
336- if err := dumpConfigFiles (opts .Directory , opts .TimeFormat , db , producedFiles ); err != nil {
342+ if err := dumpConfigFiles (opts .Directory , opts .TimeFormat , db , producedFiles , snapshotTime ); err != nil {
337343 return fmt .Errorf ("could not dump configuration files: %w" , err )
338344 }
339345 }
@@ -384,6 +390,7 @@ func run() (retVal error) {
384390 CipherPassphrase : passphrase ,
385391 CipherPublicKey : publicKey ,
386392 EncryptKeepSrc : opts .EncryptKeepSrc ,
393+ When : snapshotTime ,
387394 ExitCode : - 1 ,
388395 PgDumpVersion : pgDumpVersion ,
389396 }
@@ -612,8 +619,6 @@ func (d *dump) dump(fc chan<- sumFileJob) error {
612619 return fmt .Errorf ("could not acquire lock for %s" , dbname )
613620 }
614621
615- d .When = time .Now ()
616-
617622 var fileEnd string
618623 switch d .Options .Format {
619624 case 'p' :
@@ -630,6 +635,10 @@ func (d *dump) dump(fc chan<- sumFileJob) error {
630635 fileEnd = "d"
631636 }
632637
638+ if d .When .IsZero () {
639+ d .When = time .Now ()
640+ }
641+
633642 file := formatDumpPath (d .Directory , d .TimeFormat , fileEnd , dbname , d .When , d .Options .CompressLevel )
634643 formatOpt := fmt .Sprintf ("-F%c" , d .Options .Format )
635644
@@ -898,7 +907,7 @@ func pgToolVersion(tool string) int {
898907 return numver
899908}
900909
901- func dumpGlobals (dir string , timeFormat string , withRolePasswords bool , conninfo * ConnInfo , fc chan <- sumFileJob ) error {
910+ func dumpGlobals (dir string , timeFormat string , withRolePasswords bool , conninfo * ConnInfo , fc chan <- sumFileJob , snapshotTime time. Time ) error {
902911 command := execPath ("pg_dumpall" )
903912 args := []string {"-g" , "-w" }
904913
@@ -930,7 +939,11 @@ func dumpGlobals(dir string, timeFormat string, withRolePasswords bool, conninfo
930939 args = append (args , "--no-role-passwords" )
931940 }
932941
933- file := formatDumpPath (dir , timeFormat , "sql" , "pg_globals" , time .Now (), 0 )
942+ if snapshotTime .IsZero () {
943+ snapshotTime = time .Now ()
944+ }
945+
946+ file := formatDumpPath (dir , timeFormat , "sql" , "pg_globals" , snapshotTime , 0 )
934947 args = append (args , "-f" , file )
935948
936949 if err := os .MkdirAll (filepath .Dir (file ), 0700 ); err != nil {
@@ -970,9 +983,12 @@ func dumpGlobals(dir string, timeFormat string, withRolePasswords bool, conninfo
970983 return nil
971984}
972985
973- func dumpSettings (dir string , timeFormat string , db * pg , fc chan <- sumFileJob ) error {
986+ func dumpSettings (dir string , timeFormat string , db * pg , fc chan <- sumFileJob , snapshotTime time.Time ) error {
987+ if snapshotTime .IsZero () {
988+ snapshotTime = time .Now ()
989+ }
974990
975- file := formatDumpPath (dir , timeFormat , "out" , "pg_settings" , time . Now () , 0 )
991+ file := formatDumpPath (dir , timeFormat , "out" , "pg_settings" , snapshotTime , 0 )
976992
977993 if err := os .MkdirAll (filepath .Dir (file ), 0700 ); err != nil {
978994 return err
@@ -1000,9 +1016,12 @@ func dumpSettings(dir string, timeFormat string, db *pg, fc chan<- sumFileJob) e
10001016 return nil
10011017}
10021018
1003- func dumpConfigFiles (dir string , timeFormat string , db * pg , fc chan <- sumFileJob ) error {
1019+ func dumpConfigFiles (dir string , timeFormat string , db * pg , fc chan <- sumFileJob , snapshotTime time. Time ) error {
10041020 for _ , param := range []string {"hba_file" , "ident_file" } {
1005- file := formatDumpPath (dir , timeFormat , "out" , param , time .Now (), 0 )
1021+ if snapshotTime .IsZero () {
1022+ snapshotTime = time .Now ()
1023+ }
1024+ file := formatDumpPath (dir , timeFormat , "out" , param , snapshotTime , 0 )
10061025
10071026 if err := os .MkdirAll (filepath .Dir (file ), 0700 ); err != nil {
10081027 return err
0 commit comments