@@ -50,6 +50,7 @@ type options struct {
5050 NoConfigFile bool
5151 BinDirectory string
5252 Directory string
53+ Mode int
5354 Host string
5455 Port int
5556 Username string
@@ -130,6 +131,7 @@ func defaultOptions() options {
130131 return options {
131132 NoConfigFile : false ,
132133 Directory : "/var/backups/postgresql" ,
134+ Mode : 0o600 ,
133135 Format : 'c' ,
134136 DirJobs : 1 ,
135137 CompressLevel : - 1 ,
@@ -162,6 +164,17 @@ func (*parseCliResult) Error() string {
162164 return "please exit now"
163165}
164166
167+ func validateMode (s string ) (int , error ) {
168+ if (strings .HasPrefix (s , "0" ) && len (s ) <= 5 ) || (strings .HasPrefix (s , "-" )) {
169+ mode , err := strconv .ParseInt (s , 0 , 32 )
170+ if err != nil {
171+ return 0 , fmt .Errorf ("Invalid permission %q" , s )
172+ }
173+ return int (mode ), nil
174+ }
175+ return 0 , fmt .Errorf ("Invalid permission %q, must be octal (start by 0 and max 5 digits) number or negative" , s )
176+ }
177+
165178func validateDumpFormat (s string ) error {
166179 for _ , format := range []string {"plain" , "custom" , "tar" , "directory" } {
167180 // PostgreSQL tools allow the full name of the format and the
@@ -252,7 +265,7 @@ func validateDirectory(s string) error {
252265}
253266
254267func parseCli (args []string ) (options , []string , error ) {
255- var format , purgeKeep , purgeInterval string
268+ var format , mode , purgeKeep , purgeInterval string
256269
257270 opts := defaultOptions ()
258271 pce := & parseCliResult {}
@@ -269,6 +282,7 @@ func parseCli(args []string) (options, []string, error) {
269282 pflag .BoolVar (& opts .NoConfigFile , "no-config-file" , false , "skip reading config file\n " )
270283 pflag .StringVarP (& opts .BinDirectory , "bin-directory" , "B" , "" , "PostgreSQL binaries directory. Empty to search $PATH" )
271284 pflag .StringVarP (& opts .Directory , "backup-directory" , "b" , "/var/backups/postgresql" , "store dump files there" )
285+ pflag .StringVarP (& mode , "backup-file-mode" , "m" , "0600" , "mode to apply to dump files" )
272286 pflag .StringVarP (& opts .CfgFile , "config" , "c" , defaultCfgFile , "alternate config file" )
273287 pflag .StringSliceVarP (& opts .ExcludeDbs , "exclude-dbs" , "D" , []string {}, "list of databases to exclude" )
274288 pflag .BoolVarP (& opts .WithTemplates , "with-templates" , "t" , false , "include templates" )
@@ -416,6 +430,12 @@ func parseCli(args []string) (options, []string, error) {
416430 changed = append (changed , "include-dbs" )
417431 }
418432
433+ parsed_mode , err := validateMode (mode )
434+ if err != nil {
435+ return opts , changed , fmt .Errorf ("invalid value for --backup-file-mode: %s" , err )
436+ }
437+ opts .Mode = parsed_mode
438+
419439 // Validate purge keep and time limit
420440 keep , err := validatePurgeKeepValue (purgeKeep )
421441 if err != nil {
@@ -527,7 +547,7 @@ func validateConfigurationFile(cfg *ini.File) error {
527547 s , _ := cfg .GetSection (ini .DefaultSection )
528548
529549 known_globals := []string {
530- "bin_directory" , "backup_directory" , "timestamp_format" , "host" , "port" , "user" ,
550+ "bin_directory" , "backup_directory" , "backup_file_mode" , " timestamp_format" , "host" , "port" , "user" ,
531551 "dbname" , "exclude_dbs" , "include_dbs" , "with_templates" , "format" ,
532552 "parallel_backup_jobs" , "compress_level" , "jobs" , "pause_timeout" ,
533553 "purge_older_than" , "purge_min_keep" , "checksum_algorithm" , "pre_backup_hook" ,
@@ -581,7 +601,7 @@ gkLoop:
581601}
582602
583603func loadConfigurationFile (path string ) (options , error ) {
584- var format , purgeKeep , purgeInterval string
604+ var format , mode , purgeKeep , purgeInterval string
585605
586606 opts := defaultOptions ()
587607
@@ -607,6 +627,7 @@ func loadConfigurationFile(path string) (options, error) {
607627 // flags
608628 opts .BinDirectory = s .Key ("bin_directory" ).MustString ("" )
609629 opts .Directory = s .Key ("backup_directory" ).MustString ("/var/backups/postgresql" )
630+ mode = s .Key ("backup_file_mode" ).MustString ("0600" )
610631 timeFormat := s .Key ("timestamp_format" ).MustString ("rfc3339" )
611632 opts .Host = s .Key ("host" ).MustString ("" )
612633 opts .Port = s .Key ("port" ).MustInt (0 )
@@ -670,6 +691,13 @@ func loadConfigurationFile(path string) (options, error) {
670691 opts .AzureKey = s .Key ("azure_key" ).MustString ("" )
671692 opts .AzureEndpoint = s .Key ("azure_endpoint" ).MustString ("blob.core.windows.net" )
672693
694+ // Validate mode and convert to int
695+ m , err := validateMode (mode )
696+ if err != nil {
697+ return opts , err
698+ }
699+ opts .Mode = m
700+
673701 // Validate purge keep and time limit
674702 keep , err := validatePurgeKeepValue (purgeKeep )
675703 if err != nil {
@@ -819,6 +847,8 @@ func mergeCliAndConfigOptions(cliOpts options, configOpts options, onCli []strin
819847 opts .BinDirectory = cliOpts .BinDirectory
820848 case "backup-directory" :
821849 opts .Directory = cliOpts .Directory
850+ case "backup-file-mode" :
851+ opts .Mode = cliOpts .Mode
822852 case "exclude-dbs" :
823853 opts .ExcludeDbs = cliOpts .ExcludeDbs
824854 case "include-dbs" :
0 commit comments