2021-08-21 19:05:49 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-08-22 14:44:33 +02:00
|
|
|
"bytes"
|
2021-08-21 19:05:49 +02:00
|
|
|
"context"
|
2021-08-21 19:26:42 +02:00
|
|
|
"errors"
|
2021-08-21 19:05:49 +02:00
|
|
|
"fmt"
|
2021-08-21 21:26:27 +02:00
|
|
|
"io"
|
2021-08-22 14:44:33 +02:00
|
|
|
"io/ioutil"
|
2021-08-21 19:05:49 +02:00
|
|
|
"os"
|
2021-08-22 11:02:10 +02:00
|
|
|
"os/exec"
|
2021-08-21 21:26:27 +02:00
|
|
|
"path"
|
2021-08-22 14:00:21 +02:00
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2021-08-22 11:02:10 +02:00
|
|
|
"strings"
|
|
|
|
"time"
|
2021-08-21 19:05:49 +02:00
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
|
|
|
"github.com/docker/docker/client"
|
|
|
|
"github.com/joho/godotenv"
|
2021-08-21 21:26:27 +02:00
|
|
|
minio "github.com/minio/minio-go/v7"
|
|
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
|
|
|
"github.com/walle/targz"
|
2021-08-22 14:44:33 +02:00
|
|
|
"golang.org/x/crypto/openpgp"
|
2021-08-21 19:05:49 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2021-08-21 19:26:42 +02:00
|
|
|
s := &script{}
|
|
|
|
|
2021-08-22 11:02:10 +02:00
|
|
|
must(s.lock)()
|
2021-08-21 19:26:42 +02:00
|
|
|
defer s.unlock()
|
|
|
|
|
2021-08-21 21:26:27 +02:00
|
|
|
must(s.init)()
|
2021-08-22 11:02:10 +02:00
|
|
|
fmt.Println("Successfully initialized resources.")
|
2021-08-21 21:26:27 +02:00
|
|
|
must(s.stopContainers)()
|
2021-08-22 11:02:10 +02:00
|
|
|
fmt.Println("Successfully stopped containers.")
|
2021-08-21 21:26:27 +02:00
|
|
|
must(s.takeBackup)()
|
2021-08-22 11:02:10 +02:00
|
|
|
fmt.Println("Successfully took backup.")
|
2021-08-21 21:26:27 +02:00
|
|
|
must(s.restartContainers)()
|
2021-08-22 11:02:10 +02:00
|
|
|
fmt.Println("Successfully restarted containers.")
|
2021-08-21 21:26:27 +02:00
|
|
|
must(s.encryptBackup)()
|
2021-08-22 11:02:10 +02:00
|
|
|
fmt.Println("Successfully encrypted backup.")
|
2021-08-21 21:26:27 +02:00
|
|
|
must(s.copyBackup)()
|
2021-08-22 11:02:10 +02:00
|
|
|
fmt.Println("Successfully copied backup.")
|
2021-08-21 21:26:27 +02:00
|
|
|
must(s.cleanBackup)()
|
2021-08-22 11:02:10 +02:00
|
|
|
fmt.Println("Successfully cleaned local backup.")
|
|
|
|
must(s.pruneOldBackups)()
|
|
|
|
fmt.Println("Successfully pruned old backup.")
|
2021-08-21 19:26:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type script struct {
|
|
|
|
ctx context.Context
|
|
|
|
cli *client.Client
|
2021-08-21 21:26:27 +02:00
|
|
|
mc *minio.Client
|
2021-08-21 19:26:42 +02:00
|
|
|
stoppedContainers []types.Container
|
2021-08-22 11:02:10 +02:00
|
|
|
releaseLock func() error
|
2021-08-21 19:26:42 +02:00
|
|
|
file string
|
|
|
|
}
|
|
|
|
|
2021-08-22 11:02:10 +02:00
|
|
|
func (s *script) lock() error {
|
|
|
|
lf, err := os.OpenFile("/var/dockervolumebackup.lock", os.O_CREATE, os.ModeAppend)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("lock: error opening lock file: %w", err)
|
|
|
|
}
|
|
|
|
s.releaseLock = lf.Close
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *script) unlock() error {
|
|
|
|
if err := s.releaseLock(); err != nil {
|
|
|
|
return fmt.Errorf("unlock: error releasing file lock: %w", err)
|
|
|
|
}
|
|
|
|
if err := os.Remove("/var/dockervolumebackup.lock"); err != nil {
|
|
|
|
return fmt.Errorf("unlock: error removing lock file: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-08-21 19:26:42 +02:00
|
|
|
|
|
|
|
func (s *script) init() error {
|
|
|
|
s.ctx = context.Background()
|
|
|
|
|
|
|
|
if err := godotenv.Load("/etc/backup.env"); err != nil {
|
|
|
|
return fmt.Errorf("init: failed to load env file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
socketExists, err := fileExists("/var/run/docker.sock")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("init: error checking whether docker.sock is available: %w", err)
|
|
|
|
}
|
2021-08-21 19:05:49 +02:00
|
|
|
if socketExists {
|
2021-08-21 19:26:42 +02:00
|
|
|
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
|
2021-08-21 19:05:49 +02:00
|
|
|
if err != nil {
|
2021-08-21 19:26:42 +02:00
|
|
|
return fmt.Errorf("init: failied to create docker client")
|
2021-08-21 19:05:49 +02:00
|
|
|
}
|
2021-08-21 19:26:42 +02:00
|
|
|
s.cli = cli
|
|
|
|
}
|
2021-08-21 21:26:27 +02:00
|
|
|
|
|
|
|
if bucket := os.Getenv("AWS_S3_BUCKET_NAME"); bucket != "" {
|
|
|
|
mc, err := minio.New(os.Getenv("AWS_ENDPOINT"), &minio.Options{
|
|
|
|
Creds: credentials.NewStaticV4(
|
|
|
|
os.Getenv("AWS_ACCESS_KEY_ID"),
|
|
|
|
os.Getenv("AWS_SECRET_ACCESS_KEY"),
|
|
|
|
"",
|
|
|
|
),
|
|
|
|
Secure: os.Getenv("AWS_ENDPOINT_PROTO") == "https",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("init: error setting up minio client: %w", err)
|
|
|
|
}
|
|
|
|
s.mc = mc
|
|
|
|
}
|
2021-08-21 19:26:42 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *script) stopContainers() error {
|
|
|
|
if s.cli == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2021-08-22 11:02:10 +02:00
|
|
|
allContainers, err := s.cli.ContainerList(s.ctx, types.ContainerListOptions{
|
|
|
|
Quiet: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("stopContainers: error querying for containers: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
containersToStop, err := s.cli.ContainerList(s.ctx, types.ContainerListOptions{
|
2021-08-21 19:26:42 +02:00
|
|
|
Quiet: true,
|
|
|
|
Filters: filters.NewArgs(filters.KeyValuePair{
|
|
|
|
Key: "label",
|
|
|
|
Value: fmt.Sprintf("docker-volume-backup.stop-during-backup=%s", os.Getenv("BACKUP_STOP_CONTAINER_LABEL")),
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("stopContainers: error querying for containers to stop: %w", err)
|
2021-08-21 19:05:49 +02:00
|
|
|
}
|
2021-08-22 11:02:10 +02:00
|
|
|
fmt.Printf("Stopping %d out of %d running containers\n", len(containersToStop), len(allContainers))
|
2021-08-21 19:05:49 +02:00
|
|
|
|
2021-08-22 11:02:10 +02:00
|
|
|
if len(containersToStop) != 0 {
|
2021-08-21 19:26:42 +02:00
|
|
|
for _, container := range s.stoppedContainers {
|
|
|
|
if err := s.cli.ContainerStop(s.ctx, container.ID, nil); err != nil {
|
|
|
|
return fmt.Errorf("stopContainers: error stopping container %s: %w", container.Names[0], err)
|
2021-08-21 19:05:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-21 21:26:27 +02:00
|
|
|
|
2021-08-22 11:02:10 +02:00
|
|
|
s.stoppedContainers = containersToStop
|
2021-08-21 19:26:42 +02:00
|
|
|
return nil
|
|
|
|
}
|
2021-08-21 19:05:49 +02:00
|
|
|
|
2021-08-21 19:26:42 +02:00
|
|
|
func (s *script) takeBackup() error {
|
2021-08-22 11:02:10 +02:00
|
|
|
if os.Getenv("BACKUP_FILENAME") == "" {
|
2021-08-21 21:26:27 +02:00
|
|
|
return errors.New("takeBackup: BACKUP_FILENAME not given")
|
|
|
|
}
|
2021-08-22 11:02:10 +02:00
|
|
|
|
|
|
|
outBytes, err := exec.Command("date", fmt.Sprintf("+%s", os.Getenv("BACKUP_FILENAME"))).Output()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("takeBackup: error formatting filename template: %w", err)
|
|
|
|
}
|
|
|
|
file := fmt.Sprintf("/tmp/%s", strings.TrimSpace(string(outBytes)))
|
|
|
|
|
2021-08-21 21:26:27 +02:00
|
|
|
s.file = file
|
|
|
|
if err := targz.Compress(os.Getenv("BACKUP_SOURCES"), s.file); err != nil {
|
|
|
|
return fmt.Errorf("takeBackup: error compressing backup folder: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
2021-08-21 19:26:42 +02:00
|
|
|
}
|
2021-08-21 19:05:49 +02:00
|
|
|
|
2021-08-21 19:26:42 +02:00
|
|
|
func (s *script) restartContainers() error {
|
|
|
|
servicesRequiringUpdate := map[string]struct{}{}
|
|
|
|
for _, container := range s.stoppedContainers {
|
|
|
|
if swarmServiceName, ok := container.Labels["com.docker.swarm.service.name"]; ok {
|
|
|
|
servicesRequiringUpdate[swarmServiceName] = struct{}{}
|
|
|
|
continue
|
2021-08-21 19:05:49 +02:00
|
|
|
}
|
2021-08-21 19:26:42 +02:00
|
|
|
if err := s.cli.ContainerStart(s.ctx, container.ID, types.ContainerStartOptions{}); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2021-08-21 19:05:49 +02:00
|
|
|
|
2021-08-21 19:26:42 +02:00
|
|
|
if len(servicesRequiringUpdate) != 0 {
|
|
|
|
services, _ := s.cli.ServiceList(s.ctx, types.ServiceListOptions{})
|
|
|
|
for serviceName := range servicesRequiringUpdate {
|
|
|
|
var serviceMatch swarm.Service
|
|
|
|
for _, service := range services {
|
|
|
|
if service.Spec.Name == serviceName {
|
|
|
|
serviceMatch = service
|
|
|
|
break
|
2021-08-21 19:05:49 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-21 19:26:42 +02:00
|
|
|
if serviceMatch.ID == "" {
|
|
|
|
return fmt.Errorf("restartContainers: Couldn't find service with name %s", serviceName)
|
|
|
|
}
|
|
|
|
serviceMatch.Spec.TaskTemplate.ForceUpdate = 1
|
|
|
|
s.cli.ServiceUpdate(
|
|
|
|
s.ctx, serviceMatch.ID,
|
|
|
|
serviceMatch.Version, serviceMatch.Spec, types.ServiceUpdateOptions{},
|
|
|
|
)
|
2021-08-21 19:05:49 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-22 11:02:10 +02:00
|
|
|
|
|
|
|
s.stoppedContainers = []types.Container{}
|
2021-08-21 19:26:42 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *script) encryptBackup() error {
|
2021-08-22 14:44:33 +02:00
|
|
|
passphrase := os.Getenv("GPG_PASSPHRASE")
|
|
|
|
if passphrase == "" {
|
2021-08-21 19:26:42 +02:00
|
|
|
return nil
|
|
|
|
}
|
2021-08-22 14:44:33 +02:00
|
|
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
_, name := path.Split(s.file)
|
|
|
|
pt, err := openpgp.SymmetricallyEncrypt(buf, []byte(passphrase), &openpgp.FileHints{
|
|
|
|
IsBinary: true,
|
|
|
|
FileName: name,
|
|
|
|
}, nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("encryptBackup: error encrypting backup file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
unencrypted, err := ioutil.ReadFile(s.file)
|
|
|
|
if err != nil {
|
|
|
|
pt.Close()
|
|
|
|
return fmt.Errorf("encryptBackup: error reading unencrypted backup file: %w", err)
|
|
|
|
}
|
|
|
|
_, err = pt.Write(unencrypted)
|
|
|
|
if err != nil {
|
|
|
|
pt.Close()
|
|
|
|
return fmt.Errorf("encryptBackup: error writing backup contents: %w", err)
|
|
|
|
}
|
|
|
|
pt.Close()
|
|
|
|
|
|
|
|
gpgFile := fmt.Sprintf("%s.gpg", s.file)
|
|
|
|
if err := ioutil.WriteFile(gpgFile, buf.Bytes(), os.ModeAppend); err != nil {
|
|
|
|
return fmt.Errorf("encryptBackup: error writing encrypted version of backup: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Remove(s.file); err != nil {
|
|
|
|
return fmt.Errorf("encryptBackup: error removing unencrpyted backup: %w", err)
|
|
|
|
}
|
|
|
|
s.file = gpgFile
|
|
|
|
return nil
|
2021-08-21 19:26:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *script) copyBackup() error {
|
2021-08-21 21:26:27 +02:00
|
|
|
_, name := path.Split(s.file)
|
|
|
|
if bucket := os.Getenv("AWS_S3_BUCKET_NAME"); bucket != "" {
|
|
|
|
_, err := s.mc.FPutObject(s.ctx, bucket, name, s.file, minio.PutObjectOptions{
|
|
|
|
ContentType: "application/tar+gzip",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("copyBackup: error uploading backup to remote storage: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if archive := os.Getenv("BACKUP_ARCHIVE"); archive != "" {
|
|
|
|
if _, err := os.Stat(archive); !os.IsNotExist(err) {
|
|
|
|
if err := copy(s.file, path.Join(archive, name)); err != nil {
|
|
|
|
return fmt.Errorf("copyBackup: error copying file to local archive: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2021-08-21 19:26:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *script) cleanBackup() error {
|
2021-08-21 21:26:27 +02:00
|
|
|
if err := os.Remove(s.file); err != nil {
|
|
|
|
return fmt.Errorf("cleanBackup: error removing file: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
2021-08-21 19:26:42 +02:00
|
|
|
}
|
|
|
|
|
2021-08-22 11:02:10 +02:00
|
|
|
func (s *script) pruneOldBackups() error {
|
2021-08-21 21:26:27 +02:00
|
|
|
retention := os.Getenv("BACKUP_RETENTION_DAYS")
|
|
|
|
if retention == "" {
|
2021-08-21 19:26:42 +02:00
|
|
|
return nil
|
|
|
|
}
|
2021-08-22 14:00:21 +02:00
|
|
|
retentionDays, err := strconv.Atoi(retention)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("pruneOldBackups: error parsing BACKUP_RETENTION_DAYS as int: %w", err)
|
|
|
|
}
|
2021-08-22 11:02:10 +02:00
|
|
|
sleepFor, err := time.ParseDuration(os.Getenv("BACKUP_PRUNING_LEEWAY"))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("pruneBackups: error parsing given leeway value: %w", err)
|
|
|
|
}
|
|
|
|
time.Sleep(sleepFor)
|
|
|
|
|
2021-08-22 14:00:21 +02:00
|
|
|
deadline := time.Now().AddDate(0, 0, -retentionDays)
|
|
|
|
|
2021-08-22 11:02:10 +02:00
|
|
|
if bucket := os.Getenv("AWS_S3_BUCKET_NAME"); bucket != "" {
|
|
|
|
}
|
|
|
|
if archive := os.Getenv("BACKUP_ARCHIVE"); archive != "" {
|
2021-08-22 14:00:21 +02:00
|
|
|
matches, err := filepath.Glob(
|
|
|
|
path.Join(archive, fmt.Sprintf("%s%s", os.Getenv("BACKUP_PRUNING_PREFIX"), "*")),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("pruneOldBackups: error looking up matching files: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var candidates []os.FileInfo
|
|
|
|
for _, match := range matches {
|
|
|
|
fi, err := os.Stat(match)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("pruneOldBackups: error calling stat on file %s: %w", match, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi.ModTime().Before(deadline) {
|
|
|
|
candidates = append(candidates, fi)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(candidates) != len(matches) {
|
|
|
|
for _, candidate := range candidates {
|
|
|
|
if err := os.Remove(candidate.Name()); err != nil {
|
|
|
|
return fmt.Errorf("pruneOldBackups: error deleting file %s: %w", candidate.Name(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if len(matches) != 0 {
|
|
|
|
fmt.Println("Refusing to delete all backups. Check your configuration.")
|
|
|
|
}
|
2021-08-22 11:02:10 +02:00
|
|
|
}
|
|
|
|
return nil
|
2021-08-21 19:05:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func fileExists(location string) (bool, error) {
|
|
|
|
_, err := os.Stat(location)
|
2021-08-22 11:02:10 +02:00
|
|
|
if err != nil && !os.IsNotExist(err) {
|
2021-08-21 19:05:49 +02:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return err == nil, nil
|
|
|
|
}
|
2021-08-21 21:26:27 +02:00
|
|
|
|
|
|
|
func must(f func() error) func() {
|
|
|
|
return func() {
|
|
|
|
if err := f(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func copy(src, dst string) error {
|
|
|
|
in, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer in.Close()
|
|
|
|
|
|
|
|
out, err := os.Create(dst)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(out, in)
|
|
|
|
if err != nil {
|
2021-08-22 11:02:10 +02:00
|
|
|
out.Close()
|
2021-08-21 21:26:27 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return out.Close()
|
|
|
|
}
|