mirror of
https://github.com/offen/docker-volume-backup.git
synced 2025-12-05 17:18:02 +01:00
Compare commits
18 Commits
v2.0.0-alp
...
v2.0.0-alp
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7a75b725dc | ||
|
|
8c46bd54aa | ||
|
|
edefe69e6f | ||
|
|
01d1a60bb7 | ||
|
|
f15379795f | ||
|
|
053422ba0e | ||
|
|
39b933b8e8 | ||
|
|
8161ad7f8f | ||
|
|
34e01fa303 | ||
|
|
2554c538ea | ||
|
|
77f948d4da | ||
|
|
d388785222 | ||
|
|
b46c402b19 | ||
|
|
29f8a078bc | ||
|
|
3f7c08d616 | ||
|
|
8b7d27740a | ||
|
|
9ddddc139a | ||
|
|
d34b525804 |
@@ -11,10 +11,6 @@ jobs:
|
||||
name: Build
|
||||
command: |
|
||||
docker build . -t offen/docker-volume-backup:canary
|
||||
- run:
|
||||
name: Install gnupg
|
||||
command: |
|
||||
sudo apt-get install -y gnupg
|
||||
- run:
|
||||
name: Run tests
|
||||
working_directory: ~/docker-volume-backup/test
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
Backup Docker volumes locally or to any S3 compatible storage.
|
||||
|
||||
The [offen/docker-volume-backup](https://hub.docker.com/r/offen/docker-volume-backup) Docker image can be used as a lightweight (below 15MB) sidecar container to an existing Docker setup. It handles recurring backups of Docker volumes to a local directory or any S3 compatible storage (or both), and rotates away old backups if configured.
|
||||
The [offen/docker-volume-backup](https://hub.docker.com/r/offen/docker-volume-backup) Docker image can be used as a sidecar container to an existing Docker setup. It handles recurring backups of Docker volumes to a local directory or any S3 compatible storage (or both) and rotates away old backups if configured.
|
||||
|
||||
## Configuration
|
||||
|
||||
@@ -177,8 +177,8 @@ docker exec <container_ref> backup
|
||||
This image is heavily inspired by the `futurice/docker-volume-backup`. We decided to publish this image as a simpler and more lightweight alternative because of the following requirements:
|
||||
|
||||
- The original image is based on `ubuntu` and additional tools, making it very heavy. This version is roughly 1/25 in compressed size (it's ~12MB).
|
||||
- The original image uses a shell script, when this is written in Go, which makes it easier to extend and maintain (more verbose also).
|
||||
- The original image uses a shell script, when this is written in Go.
|
||||
- The original image proposed to handle backup rotation through AWS S3 lifecycle policies. This image adds the option to rotate away old backups through the same command so this functionality can also be offered for non-AWS storage backends like MinIO. Local copies of backups can also be pruned once they reach a certain age.
|
||||
- InfluxDB specific functionality from the original image was removed.
|
||||
- InfluxDB specific functionality was removed.
|
||||
- `arm64` and `arm/v7` architectures are supported.
|
||||
- Docker in Swarm mode is supported.
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
@@ -44,8 +43,6 @@ func main() {
|
||||
s.logger.Info("Finished running backup tasks.")
|
||||
}
|
||||
|
||||
// script holds all the stateful information required to orchestrate a
|
||||
// single backup run.
|
||||
type script struct {
|
||||
ctx context.Context
|
||||
cli *client.Client
|
||||
@@ -65,10 +62,27 @@ type script struct {
|
||||
pruningPrefix string
|
||||
}
|
||||
|
||||
// lock opens a lockfile at the given location, keeping it locked until the
|
||||
// caller invokes the returned release func. When invoked while the file is
|
||||
// still locked the function panics.
|
||||
func lock(lockfile string) func() error {
|
||||
lf, err := os.OpenFile(lockfile, os.O_CREATE, os.ModeAppend)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return func() error {
|
||||
if err := lf.Close(); err != nil {
|
||||
return fmt.Errorf("lock: error releasing file lock: %w", err)
|
||||
}
|
||||
if err := os.Remove(lockfile); err != nil {
|
||||
return fmt.Errorf("lock: error removing lock file: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// init creates all resources needed for the script to perform actions against
|
||||
// remote resources like the Docker engine or remote storage locations. All
|
||||
// reading from env vars or other configuration sources is expected to happen
|
||||
// in this method.
|
||||
// remote resources like the Docker engine or remote storage locations.
|
||||
func (s *script) init() error {
|
||||
s.ctx = context.Background()
|
||||
s.logger = logrus.New()
|
||||
@@ -164,25 +178,22 @@ func (s *script) stopContainersAndRun(thunk func() error) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("stopContainersAndRun: error querying for containers to stop: %w", err)
|
||||
}
|
||||
|
||||
if len(containersToStop) == 0 {
|
||||
return thunk()
|
||||
}
|
||||
|
||||
s.logger.Infof(
|
||||
"Stopping %d container(s) labeled `%s` out of %d running container(s).",
|
||||
"Stopping %d containers labeled `%s` out of %d running container(s).",
|
||||
len(containersToStop),
|
||||
containerLabel,
|
||||
len(allContainers),
|
||||
)
|
||||
|
||||
var stoppedContainers []types.Container
|
||||
var stopErrors []error
|
||||
for _, container := range containersToStop {
|
||||
if err := s.cli.ContainerStop(s.ctx, container.ID, nil); err != nil {
|
||||
stopErrors = append(stopErrors, err)
|
||||
} else {
|
||||
stoppedContainers = append(stoppedContainers, container)
|
||||
var errors []error
|
||||
if len(containersToStop) != 0 {
|
||||
for _, container := range containersToStop {
|
||||
if err := s.cli.ContainerStop(s.ctx, container.ID, nil); err != nil {
|
||||
errors = append(errors, err)
|
||||
} else {
|
||||
stoppedContainers = append(stoppedContainers, container)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -235,13 +246,17 @@ func (s *script) stopContainersAndRun(thunk func() error) error {
|
||||
return nil
|
||||
}()
|
||||
|
||||
if len(stopErrors) != 0 {
|
||||
return fmt.Errorf(
|
||||
"stopContainersAndRun: %d error(s) stopping containers: %w",
|
||||
len(stopErrors),
|
||||
var stopErr error
|
||||
if len(errors) != 0 {
|
||||
stopErr = fmt.Errorf(
|
||||
"stopContainersAndRun: %d errors stopping containers: %w",
|
||||
len(errors),
|
||||
err,
|
||||
)
|
||||
}
|
||||
if stopErr != nil {
|
||||
return stopErr
|
||||
}
|
||||
|
||||
return thunk()
|
||||
}
|
||||
@@ -265,10 +280,9 @@ func (s *script) encryptBackup() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
output := bytes.NewBuffer(nil)
|
||||
buf := bytes.NewBuffer(nil)
|
||||
_, name := path.Split(s.file)
|
||||
|
||||
pt, err := openpgp.SymmetricallyEncrypt(output, []byte(s.passphrase), &openpgp.FileHints{
|
||||
pt, err := openpgp.SymmetricallyEncrypt(buf, []byte(s.passphrase), &openpgp.FileHints{
|
||||
IsBinary: true,
|
||||
FileName: name,
|
||||
}, nil)
|
||||
@@ -276,16 +290,20 @@ func (s *script) encryptBackup() error {
|
||||
return fmt.Errorf("encryptBackup: error encrypting backup file: %w", err)
|
||||
}
|
||||
|
||||
file, err := os.Open(s.file)
|
||||
unencrypted, err := ioutil.ReadFile(s.file)
|
||||
if err != nil {
|
||||
return fmt.Errorf("encryptBackup: error opening backup file %s: %w", s.file, err)
|
||||
pt.Close()
|
||||
return fmt.Errorf("encryptBackup: error reading unencrypted backup file: %w", err)
|
||||
}
|
||||
_, err = pt.Write(unencrypted)
|
||||
if err != nil {
|
||||
pt.Close()
|
||||
return fmt.Errorf("encryptBackup: error writing backup contents: %w", err)
|
||||
}
|
||||
fileReader := bufio.NewReader(file)
|
||||
fileReader.WriteTo(pt)
|
||||
pt.Close()
|
||||
|
||||
gpgFile := fmt.Sprintf("%s.gpg", s.file)
|
||||
if err := ioutil.WriteFile(gpgFile, output.Bytes(), os.ModeAppend); err != nil {
|
||||
if err := ioutil.WriteFile(gpgFile, buf.Bytes(), os.ModeAppend); err != nil {
|
||||
return fmt.Errorf("encryptBackup: error writing encrypted version of backup: %w", err)
|
||||
}
|
||||
|
||||
@@ -412,7 +430,7 @@ func (s *script) pruneOldBackups() error {
|
||||
)
|
||||
}
|
||||
|
||||
var matches []string
|
||||
var matches []os.FileInfo
|
||||
for _, candidate := range candidates {
|
||||
fi, err := os.Stat(candidate)
|
||||
if err != nil {
|
||||
@@ -424,14 +442,14 @@ func (s *script) pruneOldBackups() error {
|
||||
}
|
||||
|
||||
if fi.ModTime().Before(deadline) {
|
||||
matches = append(matches, candidate)
|
||||
matches = append(matches, fi)
|
||||
}
|
||||
}
|
||||
|
||||
if len(matches) != 0 && len(matches) != len(candidates) {
|
||||
var errors []error
|
||||
for _, candidate := range matches {
|
||||
if err := os.Remove(candidate); err != nil {
|
||||
if err := os.Remove(candidate.Name()); err != nil {
|
||||
errors = append(errors, err)
|
||||
}
|
||||
}
|
||||
@@ -469,26 +487,6 @@ func (s *script) must(err error) {
|
||||
}
|
||||
}
|
||||
|
||||
// lock opens a lockfile at the given location, keeping it locked until the
|
||||
// caller invokes the returned release func. When invoked while the file is
|
||||
// still locked the function panics.
|
||||
func lock(lockfile string) func() error {
|
||||
lf, err := os.OpenFile(lockfile, os.O_CREATE|os.O_RDWR, os.ModeAppend)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return func() error {
|
||||
if err := lf.Close(); err != nil {
|
||||
return fmt.Errorf("lock: error releasing file lock: %w", err)
|
||||
}
|
||||
if err := os.Remove(lockfile); err != nil {
|
||||
return fmt.Errorf("lock: error removing lock file: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// copy creates a copy of the file located at `dst` at `src`.
|
||||
func copy(src, dst string) error {
|
||||
in, err := os.Open(src)
|
||||
if err != nil {
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
set -e
|
||||
|
||||
# Write cronjob env to file, fill in sensible defaults, and read them back in
|
||||
mkdir -p /etc/backup
|
||||
cat <<EOF > /etc/backup.env
|
||||
BACKUP_SOURCES="${BACKUP_SOURCES:-/backup}"
|
||||
BACKUP_CRON_EXPRESSION="${BACKUP_CRON_EXPRESSION:-@daily}"
|
||||
|
||||
@@ -28,7 +28,6 @@ services:
|
||||
BACKUP_RETENTION_DAYS: ${BACKUP_RETENTION_DAYS:-7}
|
||||
BACKUP_PRUNING_LEEWAY: 5s
|
||||
BACKUP_PRUNING_PREFIX: test
|
||||
GPG_PASSPHRASE: 1234secret
|
||||
volumes:
|
||||
- ./local:/archive
|
||||
- app_data:/backup/app_data:ro
|
||||
|
||||
@@ -13,13 +13,11 @@ docker-compose exec backup backup
|
||||
|
||||
docker run --rm -it \
|
||||
-v compose_backup_data:/data alpine \
|
||||
ash -c 'apk add gnupg && echo 1234secret | gpg -d --pinentry-mode loopback --passphrase-fd 0 --yes /data/backup/test.tar.gz.gpg > /tmp/test.tar.gz && tar -xf /tmp/test.tar.gz -C /tmp && test -f /tmp/backup/app_data/offen.db'
|
||||
ash -c 'tar -xf /data/backup/test.tar.gz && test -f /backup/app_data/offen.db'
|
||||
|
||||
echo "[TEST:PASS] Found relevant files in untared remote backup."
|
||||
|
||||
echo 1234secret | gpg -d --yes --passphrase-fd 0 ./local/test.tar.gz.gpg > ./local/decrypted.tar.gz
|
||||
tar -xf ./local/decrypted.tar.gz -C /tmp && test -f /tmp/backup/app_data/offen.db
|
||||
rm ./local/decrypted.tar.gz
|
||||
tar -xf ./local/test.tar.gz -C /tmp && test -f /tmp/backup/app_data/offen.db
|
||||
|
||||
echo "[TEST:PASS] Found relevant files in untared local backup."
|
||||
|
||||
@@ -31,6 +29,8 @@ fi
|
||||
|
||||
echo "[TEST:PASS] All containers running post backup."
|
||||
|
||||
docker-compose down
|
||||
|
||||
# The second part of this test checks if backups get deleted when the retention
|
||||
# is set to 0 days (which it should not as it would mean all backups get deleted)
|
||||
# TODO: find out if we can test actual deletion without having to wait for a day
|
||||
|
||||
Reference in New Issue
Block a user