mirror of
https://github.com/kolaente/docker-db-backup.git
synced 2026-03-24 21:43:48 +01:00
Also updates all affected new types. --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: kolaente <k@knt.li>
50 lines
1.2 KiB
Go
50 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"strings"
|
|
|
|
"github.com/docker/docker/api/types/container"
|
|
"github.com/docker/docker/client"
|
|
)
|
|
|
|
const containerLabelName = `de.kolaente.db-backup`
|
|
|
|
type Dumper interface {
|
|
Dump(c *client.Client) error
|
|
}
|
|
|
|
func NewDumperFromContainer(ctr *container.InspectResponse) Dumper {
|
|
|
|
// Containers contain the tags, therefore we need to check them one by one
|
|
if strings.HasPrefix(ctr.Config.Image, "mysql") || strings.HasPrefix(ctr.Config.Image, "mariadb") || ctr.Config.Labels[containerLabelName] == "mysql" {
|
|
return NewMysqlDumper(ctr)
|
|
}
|
|
|
|
if strings.HasPrefix(ctr.Config.Image, "postgres") || ctr.Config.Labels[containerLabelName] == "postgres" {
|
|
return NewPostgresDumper(ctr)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func dumpAllDatabases(c *client.Client) {
|
|
lock.Lock()
|
|
defer lock.Unlock()
|
|
|
|
for containerID, dumper := range store {
|
|
err := dumper.Dump(c)
|
|
if err != nil {
|
|
log.Printf("Could not dump database from container %s: %v", containerID, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func getDumpFilename(containerName string) string {
|
|
if strings.HasPrefix(containerName, "/") {
|
|
containerName = strings.TrimPrefix(containerName, "/")
|
|
}
|
|
|
|
return config.fullCurrentBackupPath + containerName + ".sql"
|
|
}
|