Add dump helpers
parent
79f6c5cfad
commit
5ec093fbf6
@ -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)
|
||||
}
|
||||
}
|
@ -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
|
||||
}
|
@ -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()
|
||||
}
|
Loading…
Reference in New Issue