Skip to content

feat: Add support for DB_PASS_FILE #451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions cmd/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ func TestDumpCmd(t *testing.T) {
DBConn: database.Connection{Host: "abc", Port: defaultPort},
FilenamePattern: "db_backup_{{ .now }}.{{ .compression }}",
}, core.TimerOptions{Frequency: defaultFrequency, Begin: defaultBegin}, nil},
{"file URL with pass-file", []string{"--server", "abc", "--target", "file:///foo/bar", "--pass-file", "testdata/password.txt"}, "", false, core.DumpOptions{
Targets: []storage.Storage{file.New(*fileTargetURL)},
MaxAllowedPacket: defaultMaxAllowedPacket,
Compressor: &compression.GzipCompressor{},
DBConn: database.Connection{Host: "abc", Port: defaultPort, Pass: "testpassword"},
FilenamePattern: "db_backup_{{ .now }}.{{ .compression }}",
}, core.TimerOptions{Frequency: defaultFrequency, Begin: defaultBegin}, nil},
{"file URL with pass and pass-file (pass takes precedence)", []string{"--server", "abc", "--target", "file:///foo/bar", "--pass", "explicitpass", "--pass-file", "testdata/password.txt"}, "", false, core.DumpOptions{
Targets: []storage.Storage{file.New(*fileTargetURL)},
MaxAllowedPacket: defaultMaxAllowedPacket,
Compressor: &compression.GzipCompressor{},
DBConn: database.Connection{Host: "abc", Port: defaultPort, Pass: "explicitpass"},
FilenamePattern: "db_backup_{{ .now }}.{{ .compression }}",
}, core.TimerOptions{Frequency: defaultFrequency, Begin: defaultBegin}, nil},
{"file URL with prune", []string{"--server", "abc", "--target", "file:///foo/bar", "--retention", "1h"}, "", false, core.DumpOptions{
Targets: []storage.Storage{file.New(*fileTargetURL)},
MaxAllowedPacket: defaultMaxAllowedPacket,
Expand Down
15 changes: 15 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,18 @@ func rootCmd(execs execs) (*cobra.Command, error) {
cmdConfig.dbconn.Pass = dbPass
}

// read password from file if pass is not set and pass-file is set
if cmdConfig.dbconn.Pass == "" {
dbPassFile := v.GetString("pass-file")
if dbPassFile != "" {
passBytes, err := os.ReadFile(dbPassFile)
if err != nil {
return fmt.Errorf("failed to read password from file %s: %w", dbPassFile, err)
}
cmdConfig.dbconn.Pass = strings.TrimSpace(string(passBytes))
}
}

// these are not from the config file, as they are generic credentials, used across all targets.
// the config file uses specific ones per target
cmdConfig.creds = credentials.Creds{
Expand Down Expand Up @@ -222,6 +234,9 @@ func rootCmd(execs execs) (*cobra.Command, error) {
// pass via CLI or env var
pflags.String("pass", "", "password for database server")

// pass-file via CLI or env var
pflags.String("pass-file", "", "path to file containing password for database server")

// debug via CLI or env var or default
pflags.IntP("verbose", "v", 0, "set log level, 1 is debug, 2 is trace")
pflags.Bool("debug", false, "set log level to debug, equivalent of --verbose=1; if both set, --version always overrides")
Expand Down
1 change: 1 addition & 0 deletions cmd/testdata/password.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
testpassword
1 change: 1 addition & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ The following are the environment variables, CLI flags and configuration file op
| port to use to connect to database. Optional. | BR | `port` | `DB_PORT` | `database.port` | 3306 |
| username for the database | BR | `user` | `DB_USER` | `database.credentials.username` | |
| password for the database | BR | `pass` | `DB_PASS` | `database.credentials.password` | |
| path to file containing password for the database. `pass` takes precedence if both are set. | BR | `pass-file` | `DB_PASS_FILE` | | |
| names of databases to dump, comma-separated | B | `include` | `DB_DUMP_INCLUDE` | `dump.include` | all databases in the server |
| names of databases to exclude from the dump | B | `exclude` | `DB_DUMP_EXCLUDE` | `dump.exclude` | |
| do not include `USE <database>;` statement in the dump | B | `no-database-name` | `NO_DATABASE_NAME` | `dump.noDatabaseName` | `false` |
Expand Down