Skip to content
Open
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
45 changes: 40 additions & 5 deletions backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var (
optTar = false
optAll = false
optStopped = false
optExclude = []string{}

paths []string
tw *tar.Writer
Expand Down Expand Up @@ -104,7 +105,7 @@ func collectFileTar(path string, info os.FileInfo, err error) error {
return err
}

func backupTar(filename string, backup Backup) error {
func backupTar(filename string, backup Backup, binds []types.MountPoint) error {
b, err := json.MarshalIndent(backup, "", " ")
if err != nil {
return err
Expand Down Expand Up @@ -142,6 +143,15 @@ func backupTar(filename string, backup Backup) error {
}
}

for _, m := range binds {
// fmt.Printf("Mount (type %s) %s -> %s\n", m.Type, m.Source, m.Destination)

err := filepath.Walk(m.Source, collectFileTar)
if err != nil {
return err
}
}

tw.Close()
fmt.Println("Created backup:", filename+".tar")
return nil
Expand All @@ -154,17 +164,40 @@ func backup(ID string) error {
}
fmt.Printf("Creating backup of %s (%s, %s)\n", conf.Name[1:], conf.Config.Image, conf.ID[:12])

paths = []string{}
var mounts []types.MountPoint
var backupFiles []types.MountPoint

for _, mount := range conf.Mounts {
if mount.Type == "bind" {
skip := false
for _, exclude := range optExclude {
if strings.HasPrefix(mount.Source, exclude) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From a user's perspective, I wonder if we shouldn't support globbing here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did wonder about that, either glob or regex. I'm not sure how common that need would be though, to exclude a more complex pattern of files in a bind (rather than entire folders/trees). The question then is, do you match glob/regex against the path to the bind, or individually against all the files searched in the bind?

skip = true
break
}
}

if !skip {
fmt.Println("Backup bind files:", mount.Source)
backupFiles = append(backupFiles, mount)
}
} else {
fmt.Println("Backup volume files:", mount.Source)
mounts = append(mounts, mount)
backupFiles = append(backupFiles, mount)
}
}

backup := Backup{
PortMap: conf.HostConfig.PortBindings,
Config: conf.Config,
Mounts: conf.Mounts,
Mounts: mounts,
}

filename := sanitize.Path(fmt.Sprintf("%s-%s", conf.Config.Image, ID))
filename = strings.Replace(filename, "/", "_", -1)
if optTar {
return backupTar(filename, backup)
return backupTar(filename, backup, backupFiles)
}

b, err := json.MarshalIndent(backup, "", " ")
Expand All @@ -178,7 +211,8 @@ func backup(ID string) error {
return err
}

for _, m := range conf.Mounts {
paths = []string{}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make sure your sources are always gofmt'd before pushing! It's the best thing since sliced bread, really 😃

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I didn't know about that command - I'll try it out!

for _, m := range backupFiles {
// fmt.Printf("Mount (type %s) %s -> %s\n", m.Type, m.Source, m.Destination)
err := filepath.Walk(m.Source, collectFile)
if err != nil {
Expand Down Expand Up @@ -243,5 +277,6 @@ func init() {
backupCmd.Flags().BoolVarP(&optTar, "tar", "t", false, "create tar backups")
backupCmd.Flags().BoolVarP(&optAll, "all", "a", false, "backup all running containers")
backupCmd.Flags().BoolVarP(&optStopped, "stopped", "s", false, "in combination with --all: also backup stopped containers")
backupCmd.Flags().StringArrayVarP(&optExclude, "exclude", "e", []string{}, "exclude file paths that start with this, can use multiple times")
RootCmd.AddCommand(backupCmd)
}