Add dump helpers

This commit is contained in:
kolaente 2021-08-18 21:29:17 +02:00
parent 79f6c5cfad
commit 5ec093fbf6
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
4 changed files with 128 additions and 1 deletions

51
config.go Normal file
View File

@ -0,0 +1,51 @@
package main
import (
"log"
"os"
"strings"
"time"
)
// Backup folder
// Backup interval
// Max backups to keep
type conf struct {
Folder string // Backup folder _with_ trailing slash
fullCurrentBackupPath string
Interval time.Duration
MaxBackups int64
}
var (
config *conf
dumpTime time.Time
)
func init() {
config = &conf{
Folder: "/backups/",
Interval: time.Hour * 6,
MaxBackups: 12,
}
folder, has := os.LookupEnv("BACKUP_FOLDER")
if has {
if !strings.HasSuffix(folder, "/") {
folder = folder + "/"
}
config.Folder = folder
}
updateFullBackupPath()
}
func updateFullBackupPath() {
dumpTime = time.Now()
config.fullCurrentBackupPath = config.Folder + dumpTime.Format("02-01-2006_15-04-05") + "/"
err := os.MkdirAll(config.fullCurrentBackupPath, 0644)
if err != nil {
log.Fatalf("Could not create backup folder: %s\n", err)
}
}

27
dump.go
View File

@ -1,6 +1,9 @@
package main
import "github.com/docker/docker/api/types"
import (
"github.com/docker/docker/api/types"
"strings"
)
type Dumper interface {
Dump() error
@ -18,3 +21,25 @@ func NewDumperFromContainer(container *types.ContainerJSON) Dumper {
return nil
}
func dumpAllDatabases() error {
lock.Lock()
defer lock.Unlock()
for _, dumper := range store {
err := dumper.Dump()
if err != nil {
return err
}
}
return nil
}
func getDumpFilename(containerName string) string {
if strings.HasPrefix(containerName, "/") {
containerName = strings.TrimPrefix(containerName, "/")
}
return config.fullCurrentBackupPath + containerName + ".sql"
}

14
helper.go Normal file
View File

@ -0,0 +1,14 @@
package main
import "strings"
func parseEnv(envs []string) map[string]string {
env := make(map[string]string, len(envs))
for _, s := range envs {
parts := strings.SplitN(s, "=", 2)
env[parts[0]] = parts[1]
}
return env
}

37
save.go Normal file
View File

@ -0,0 +1,37 @@
package main
import (
"fmt"
"io"
"os"
"os/exec"
)
func runAndSaveCommand(filename, command string, args ...string) error {
c := exec.Command(command, args...)
fmt.Printf("Running %s\n\n", c.String())
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
stdout, err := c.StdoutPipe()
if err != nil {
return err
}
err = c.Start()
if err != nil {
return err
}
_, err = io.Copy(f, stdout)
if err != nil {
return err
}
return c.Wait()
}