docker-db-backup/dump.go

49 lines
1.2 KiB
Go
Raw Permalink Normal View History

2021-08-18 18:25:59 +00:00
package main
2021-08-18 19:29:17 +00:00
import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
2023-06-15 12:35:28 +00:00
"log"
2021-08-18 19:29:17 +00:00
"strings"
)
2021-08-18 18:25:59 +00:00
const containerLabelName = `de.kolaente.db-backup`
2021-08-18 18:25:59 +00:00
type Dumper interface {
Dump(c *client.Client) error
2021-08-18 18:25:59 +00:00
}
func NewDumperFromContainer(container *types.ContainerJSON) Dumper {
// Containers contain the tags, therefore we need to check them one by one
if strings.HasPrefix(container.Config.Image, "mysql") || strings.HasPrefix(container.Config.Image, "mariadb") || container.Config.Labels[containerLabelName] == "mysql" {
2021-08-18 18:25:59 +00:00
return NewMysqlDumper(container)
}
if strings.HasPrefix(container.Config.Image, "postgres") || container.Config.Labels[containerLabelName] == "postgres" {
2021-08-18 18:25:59 +00:00
return NewPostgresDumper(container)
}
return nil
}
2021-08-18 19:29:17 +00:00
func dumpAllDatabases(c *client.Client) {
2021-08-18 19:29:17 +00:00
lock.Lock()
defer lock.Unlock()
for containerID, dumper := range store {
err := dumper.Dump(c)
2021-08-18 19:29:17 +00:00
if err != nil {
2023-06-15 12:35:28 +00:00
log.Printf("Could not dump database from container %s: %v", containerID, err)
2021-08-18 19:29:17 +00:00
}
}
}
func getDumpFilename(containerName string) string {
if strings.HasPrefix(containerName, "/") {
containerName = strings.TrimPrefix(containerName, "/")
}
return config.fullCurrentBackupPath + containerName + ".sql"
}