Skip to content

Commit 5ec2431

Browse files
committed
add dockervolume:// snapshotting
1 parent 4e1dc86 commit 5ec2431

2 files changed

Lines changed: 40 additions & 5 deletions

File tree

README.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,24 @@ Docker host, configure the Docker integration via µbackup config)
5454
`$ docker run -e "BACKUP_COMMAND=..."` for example) that contains the command used to take
5555
a backup of the important state inside the container.
5656

57-
If you need to backup a single file inside a container, use: `BACKUP_COMMAND=cat /yourfile.db`
57+
For different use cases, for `BACKUP_COMMAND` specify:
5858

59-
For PostgreSQL, you could use: `BACKUP_COMMAND=pg_dump -U postgres nameOfYourDatabase`
59+
- If you need to backup a single file inside a container
60+
* Use: `BACKUP_COMMAND=cat /yourfile.db`
6061

61-
For a directory, you could use: `BACKUP_COMMAND=tar -cC /yourdirectory -f - .` (`-` means
62-
`$ tar` will write the archive to `stdout`, `.` just means to process all files in the
63-
selected directory)
62+
- For databases etc. that support atomic dumping, e.g. PostgreSQL
63+
* Use: `BACKUP_COMMAND=pg_dump -U postgres nameOfYourDatabase`
64+
65+
- For a directory
66+
* Use: `BACKUP_COMMAND=tar -cC /yourdirectory -f - .`
67+
* `-` means `$ tar` will write the archive to `stdout`, `.` just means to process all files in the
68+
selected directory).
69+
70+
- If there's no tooling inside the container (think `FROM scratch`), if you have a Docker
71+
volume you want to archive in its entirety
72+
* Use: `BACKUP_COMMAND=dockervolume://` and µbackup will find the volume source data via
73+
Docker and use `tar` to dump the whole directory tree.
74+
* For this option, you have to run µbackup with `$ docker run -v /var/lib/docker/volumes:/var/lib/docker/volumes`
6475

6576
This simple approach is surprisingly flexible and its streaming approach is more efficient
6677
than having to write temporary files.

cmd/ubackup/dockerdiscovery.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/function61/gokit/ezhttp"
88
"github.com/function61/gokit/udocker"
99
"github.com/function61/ubackup/pkg/ubtypes"
10+
"log"
1011
"net/http"
1112
"strings"
1213
)
@@ -77,11 +78,34 @@ func dockerDiscoverBackupTargets(ctx context.Context, dockerEndpoint string) ([]
7778
return targets, nil
7879
}
7980

81+
// "dockervolume://" => docker volume snapshotter
8082
// "cat /data/example.db" => ["docker", "exec", "cat", "/data/example.db"]
8183
func createSnapshotter(
8284
backupCommand string,
8385
container udocker.Container,
8486
) ubtypes.Snapshotter {
87+
if backupCommand == "dockervolume://" {
88+
volumeMounts := []udocker.Mount{}
89+
for _, mount := range container.Mounts {
90+
if mount.Type == "volume" {
91+
volumeMounts = append(volumeMounts, mount)
92+
}
93+
}
94+
95+
if len(volumeMounts) != 1 {
96+
log.Printf(
97+
"disqualifying container %s with dockervolume:// because len(volumeMounts) != 1; got %d",
98+
container.Name,
99+
len(volumeMounts))
100+
101+
return nil
102+
}
103+
104+
return newCommandOutputSnapshotter(
105+
[]string{"tar", "--create", "."},
106+
volumeMounts[0].Source)
107+
}
108+
85109
// FIXME: this doesn't support spaces..
86110
backupCommandParts := strings.Split(backupCommand, " ")
87111

0 commit comments

Comments
 (0)