Skip to content

Commit 2d8b840

Browse files
committed
allow to keep source files when encrypting
This can be set in the config file and overriden on the command line
1 parent f6f9590 commit 2d8b840

5 files changed

Lines changed: 28 additions & 3 deletions

File tree

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,11 @@ encryption of files). To keep things simple, encryption is done using a
131131
passphrase. To encrypt files, use the `--encrypt` option along with the
132132
`--cipher-pass` option or `PGBK_PASSPHRASE` environment variable to specify the
133133
passphrase. When `encrypt` is set to true in the configuration file, the
134-
`--no-encrypt` option allows to disable encryption on the command line.
134+
`--no-encrypt` option allows to disable encryption on the command line. By
135+
default, unencrypted source files are removed when they are successfully
136+
encrypted. Use the `--encrypt-keep-src` option to keep them or
137+
`--no-encrypt-keep-src` to force remove them and override the configuration
138+
file.
135139

136140
Encrypted files can be decrypted with the correct passphrase and the
137141
`--decrypt` option. When `--decrypt` is present on the command line, dumps are

config.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ type options struct {
7272
Verbose bool
7373
Quiet bool
7474
Encrypt bool
75+
EncryptKeepSrc bool
7576
CipherPassphrase string
7677
Decrypt bool
7778
}
@@ -191,6 +192,8 @@ func parseCli(args []string) (options, []string, error) {
191192

192193
pflag.BoolVar(&opts.Encrypt, "encrypt", false, "encrypt the dumps")
193194
NoEncrypt := pflag.Bool("no-encrypt", false, "do not encrypt the dumps")
195+
pflag.BoolVar(&opts.EncryptKeepSrc, "encrypt-keep-src", false, "keep original files when encrypting")
196+
NoEncryptKeepSrc := pflag.Bool("no-encrypt-keep-src", false, "do not keep original files when encrypting")
194197
pflag.BoolVar(&opts.Decrypt, "decrypt", false, "decrypt files in the backup directory")
195198
pflag.StringVar(&opts.CipherPassphrase, "cipher-pass", "", "cipher passphrase for encryption and decryption\n")
196199

@@ -233,6 +236,12 @@ func parseCli(args []string) (options, []string, error) {
233236
changed = append(changed, "encrypt")
234237
}
235238

239+
// Same for encrypt_keep_source = true in the config file
240+
if *NoEncryptKeepSrc {
241+
opts.EncryptKeepSrc = false
242+
changed = append(changed, "encrypt-keep-src")
243+
}
244+
236245
// When --help or --version is given print and tell the caller
237246
// through the error to exit
238247
if pce.ShowHelp {
@@ -350,6 +359,7 @@ func loadConfigurationFile(path string) (options, error) {
350359
opts.PostHook = s.Key("post_backup_hook").MustString("")
351360
opts.Encrypt = s.Key("encrypt").MustBool(false)
352361
opts.CipherPassphrase = s.Key("cipher_passphrase").MustString("")
362+
opts.EncryptKeepSrc = s.Key("encrypt_keep_source").MustBool(false)
353363

354364
// Validate purge keep and time limit
355365
keep, err := validatePurgeKeepValue(purgeKeep)
@@ -539,6 +549,8 @@ func mergeCliAndConfigOptions(cliOpts options, configOpts options, onCli []strin
539549
opts.PostHook = cliOpts.PostHook
540550
case "encrypt":
541551
opts.Encrypt = cliOpts.Encrypt
552+
case "encrypt-keep-src":
553+
opts.EncryptKeepSrc = cliOpts.EncryptKeepSrc
542554
case "cipher-pass":
543555
opts.CipherPassphrase = cliOpts.CipherPassphrase
544556
case "decrypt":

crypto.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ func encryptFile(path string, password string, keep bool) error {
117117
}
118118

119119
if !keep {
120+
l.Verboseln("removeing source file:", path)
120121
src.Close()
121122
if err := os.Remove(path); err != nil {
122123
return fmt.Errorf("could not remove %s: %w", path, err)
@@ -155,6 +156,7 @@ func encryptFile(path string, password string, keep bool) error {
155156
}
156157

157158
if !keep {
159+
l.Verboseln("removeing source file:", path)
158160
src.Close()
159161
if err := os.Remove(path); err != nil {
160162
return fmt.Errorf("could not remove %s: %w", path, err)

main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ type dump struct {
6464
// Cipher passphrase, when not empty cipher the file
6565
CipherPassphrase string
6666

67+
// Keep original files after encryption
68+
EncryptKeepSrc bool
69+
6770
// Result
6871
When time.Time
6972
ExitCode int
@@ -208,7 +211,7 @@ func main() {
208211
}
209212

210213
if opts.Encrypt {
211-
if err = encryptFile(file, opts.CipherPassphrase, false); err != nil {
214+
if err = encryptFile(file, opts.CipherPassphrase, opts.EncryptKeepSrc); err != nil {
212215
l.Warnln("encryption failed", err)
213216
}
214217
}
@@ -310,6 +313,7 @@ func main() {
310313
TimeFormat: opts.TimeFormat,
311314
ConnString: conninfo,
312315
CipherPassphrase: passphrase,
316+
EncryptKeepSrc: opts.EncryptKeepSrc,
313317
ExitCode: -1,
314318
PgDumpVersion: pgDumpVersion,
315319
}
@@ -597,7 +601,7 @@ func (d *dump) dump() error {
597601
// Encrypt the file
598602
if d.CipherPassphrase != "" {
599603
l.Infoln("encrypting", file)
600-
if err = encryptFile(file, d.CipherPassphrase, false); err != nil {
604+
if err = encryptFile(file, d.CipherPassphrase, d.EncryptKeepSrc); err != nil {
601605
return fmt.Errorf("encrypt failed: %s", err)
602606

603607
}

pg_back.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ encrypt = false
6161
# environment variable can be used alternatively.
6262
cipher_passphrase =
6363

64+
# Keep orignal files after encrypting them.
65+
encrypt_keep_source = false
66+
6467
# Purge dumps older than this number of days. If the interval has to
6568
# be shorter than one day, use a duration with units, h for hours, m
6669
# for minutes, s for seconds, us for microseconds or ns for

0 commit comments

Comments
 (0)