2024-03-15 11:42:22 +01:00
|
|
|
// Copyright 2024 - offen.software <hioffen@posteo.de>
|
2024-02-13 19:21:57 +01:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
openpgp "github.com/ProtonMail/go-crypto/openpgp/v2"
|
2024-02-16 15:35:42 +01:00
|
|
|
"github.com/offen/docker-volume-backup/internal/errwrap"
|
2024-02-13 19:21:57 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// encryptArchive encrypts the backup file using PGP and the configured passphrase.
|
|
|
|
// In case no passphrase is given it returns early, leaving the backup file
|
|
|
|
// untouched.
|
|
|
|
func (s *script) encryptArchive() error {
|
|
|
|
if s.c.GpgPassphrase == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
gpgFile := fmt.Sprintf("%s.gpg", s.file)
|
|
|
|
s.registerHook(hookLevelPlumbing, func(error) error {
|
|
|
|
if err := remove(gpgFile); err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return errwrap.Wrap(err, "error removing gpg file")
|
2024-02-13 19:21:57 +01:00
|
|
|
}
|
|
|
|
s.logger.Info(
|
|
|
|
fmt.Sprintf("Removed GPG file `%s`.", gpgFile),
|
|
|
|
)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
outFile, err := os.Create(gpgFile)
|
|
|
|
if err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return errwrap.Wrap(err, "error opening out file")
|
2024-02-13 19:21:57 +01:00
|
|
|
}
|
|
|
|
defer outFile.Close()
|
|
|
|
|
|
|
|
_, name := path.Split(s.file)
|
|
|
|
dst, err := openpgp.SymmetricallyEncrypt(outFile, []byte(s.c.GpgPassphrase), &openpgp.FileHints{
|
|
|
|
FileName: name,
|
|
|
|
}, nil)
|
|
|
|
if err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return errwrap.Wrap(err, "error encrypting backup file")
|
2024-02-13 19:21:57 +01:00
|
|
|
}
|
|
|
|
defer dst.Close()
|
|
|
|
|
|
|
|
src, err := os.Open(s.file)
|
|
|
|
if err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return errwrap.Wrap(err, fmt.Sprintf("error opening backup file `%s`", s.file))
|
2024-02-13 19:21:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := io.Copy(dst, src); err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return errwrap.Wrap(err, "error writing ciphertext to file")
|
2024-02-13 19:21:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
s.file = gpgFile
|
|
|
|
s.logger.Info(
|
|
|
|
fmt.Sprintf("Encrypted backup using given passphrase, saving as `%s`.", s.file),
|
|
|
|
)
|
|
|
|
return nil
|
|
|
|
}
|