mirror of
https://github.com/offen/docker-volume-backup.git
synced 2024-11-10 00:30:29 +01:00
scaffold script flow
This commit is contained in:
parent
efb52aa806
commit
8b110fd789
@ -12,9 +12,7 @@ FROM alpine:3.14
|
|||||||
|
|
||||||
WORKDIR /root
|
WORKDIR /root
|
||||||
|
|
||||||
RUN apk add --update ca-certificates docker openrc gnupg
|
RUN apk add --update ca-certificates
|
||||||
RUN update-ca-certificates
|
|
||||||
RUN rc-update add docker boot
|
|
||||||
|
|
||||||
COPY --from=builder /app/backup /usr/bin/backup
|
COPY --from=builder /app/backup /usr/bin/backup
|
||||||
|
|
||||||
|
149
src/main.go
149
src/main.go
@ -2,8 +2,10 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
"github.com/docker/docker/api/types/filters"
|
"github.com/docker/docker/api/types/filters"
|
||||||
@ -13,63 +15,138 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if err := godotenv.Load("/etc/backup.env"); err != nil {
|
s := &script{}
|
||||||
|
defer func() {
|
||||||
|
for _, thunk := range s.cleanupTasks {
|
||||||
|
thunk()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
s.lock()
|
||||||
|
defer s.unlock()
|
||||||
|
|
||||||
|
if err := s.init(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := context.Background()
|
if err := s.stopContainers(); err != nil {
|
||||||
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := s.takeBackup(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.restartContainers(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.encryptBackup(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.copyBackup(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.cleanBackup(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.pruneBackups(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type script struct {
|
||||||
|
ctx context.Context
|
||||||
|
cli *client.Client
|
||||||
|
stoppedContainers []types.Container
|
||||||
|
file string
|
||||||
|
cleanupTasks []func()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *script) lock() {}
|
||||||
|
func (s *script) unlock() {}
|
||||||
|
|
||||||
|
func (s *script) init() error {
|
||||||
|
s.ctx = context.Background()
|
||||||
|
if timeout, ok := os.LookupEnv("BACKUP_TIMEOUT_DURATION"); ok {
|
||||||
|
d, err := time.ParseDuration(timeout)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("init: error parsing given timeout duration: %w", err)
|
||||||
|
}
|
||||||
|
withTimeout, cancelFunc := context.WithTimeout(context.Background(), d)
|
||||||
|
s.ctx = withTimeout
|
||||||
|
s.cleanupTasks = append(s.cleanupTasks, cancelFunc)
|
||||||
|
}
|
||||||
|
|
||||||
|
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")
|
socketExists, err := fileExists("/var/run/docker.sock")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return fmt.Errorf("init: error checking whether docker.sock is available: %w", err)
|
||||||
|
}
|
||||||
|
if socketExists {
|
||||||
|
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("init: failied to create docker client")
|
||||||
|
}
|
||||||
|
s.cli = cli
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var containersToStop []types.Container
|
func (s *script) stopContainers() error {
|
||||||
if socketExists {
|
if s.cli == nil {
|
||||||
containersToStop, err = cli.ContainerList(ctx, types.ContainerListOptions{
|
return nil
|
||||||
|
}
|
||||||
|
stoppedContainers, err := s.cli.ContainerList(s.ctx, types.ContainerListOptions{
|
||||||
Quiet: true,
|
Quiet: true,
|
||||||
Filters: filters.NewArgs(filters.KeyValuePair{
|
Filters: filters.NewArgs(filters.KeyValuePair{
|
||||||
Key: "label",
|
Key: "label",
|
||||||
Value: fmt.Sprintf("docker-volume-backup.stop-during-backup=%s", os.Getenv("BACKUP_STOP_CONTAINER_LABEL")),
|
Value: fmt.Sprintf("docker-volume-backup.stop-during-backup=%s", os.Getenv("BACKUP_STOP_CONTAINER_LABEL")),
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
s.stoppedContainers = stoppedContainers
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return fmt.Errorf("stopContainers: error querying for containers to stop: %w", err)
|
||||||
}
|
|
||||||
fmt.Printf("Stopping %d containers\n", len(containersToStop))
|
|
||||||
}
|
}
|
||||||
|
fmt.Printf("Stopping %d containers\n", len(s.stoppedContainers))
|
||||||
|
|
||||||
if len(containersToStop) != 0 {
|
if len(s.stoppedContainers) != 0 {
|
||||||
fmt.Println("Stopping containers")
|
fmt.Println("Stopping containers")
|
||||||
for _, container := range containersToStop {
|
for _, container := range s.stoppedContainers {
|
||||||
if err := cli.ContainerStop(ctx, container.ID, nil); err != nil {
|
if err := s.cli.ContainerStop(s.ctx, container.ID, nil); err != nil {
|
||||||
panic(err)
|
return fmt.Errorf("stopContainers: error stopping container %s: %w", container.Names[0], err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Println("Creating backup")
|
func (s *script) takeBackup() error {
|
||||||
// TODO: Implement backup
|
return errors.New("takeBackup: not implemented yet")
|
||||||
|
}
|
||||||
|
|
||||||
if len(containersToStop) != 0 {
|
func (s *script) restartContainers() error {
|
||||||
fmt.Println("Starting containers/services back up")
|
fmt.Println("Starting containers/services back up")
|
||||||
servicesRequiringUpdate := map[string]struct{}{}
|
servicesRequiringUpdate := map[string]struct{}{}
|
||||||
for _, container := range containersToStop {
|
for _, container := range s.stoppedContainers {
|
||||||
if swarmServiceName, ok := container.Labels["com.docker.swarm.service.name"]; ok {
|
if swarmServiceName, ok := container.Labels["com.docker.swarm.service.name"]; ok {
|
||||||
servicesRequiringUpdate[swarmServiceName] = struct{}{}
|
servicesRequiringUpdate[swarmServiceName] = struct{}{}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if err := cli.ContainerStart(ctx, container.ID, types.ContainerStartOptions{}); err != nil {
|
if err := s.cli.ContainerStart(s.ctx, container.ID, types.ContainerStartOptions{}); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(servicesRequiringUpdate) != 0 {
|
if len(servicesRequiringUpdate) != 0 {
|
||||||
services, _ := cli.ServiceList(ctx, types.ServiceListOptions{})
|
services, _ := s.cli.ServiceList(s.ctx, types.ServiceListOptions{})
|
||||||
for serviceName := range servicesRequiringUpdate {
|
for serviceName := range servicesRequiringUpdate {
|
||||||
var serviceMatch swarm.Service
|
var serviceMatch swarm.Service
|
||||||
for _, service := range services {
|
for _, service := range services {
|
||||||
@ -79,16 +156,40 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if serviceMatch.ID == "" {
|
if serviceMatch.ID == "" {
|
||||||
panic(fmt.Sprintf("Couldn't find service with name %s", serviceName))
|
return fmt.Errorf("restartContainers: Couldn't find service with name %s", serviceName)
|
||||||
}
|
}
|
||||||
serviceMatch.Spec.TaskTemplate.ForceUpdate = 1
|
serviceMatch.Spec.TaskTemplate.ForceUpdate = 1
|
||||||
cli.ServiceUpdate(
|
s.cli.ServiceUpdate(
|
||||||
ctx, serviceMatch.ID,
|
s.ctx, serviceMatch.ID,
|
||||||
serviceMatch.Version, serviceMatch.Spec, types.ServiceUpdateOptions{},
|
serviceMatch.Version, serviceMatch.Spec, types.ServiceUpdateOptions{},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *script) encryptBackup() error {
|
||||||
|
_, ok := os.LookupEnv("GPG_PASSPHRASE")
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return errors.New("encryptBackup: not implemented yet")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *script) copyBackup() error {
|
||||||
|
return errors.New("copyBackup: not implemented yet")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *script) cleanBackup() error {
|
||||||
|
return errors.New("cleanBackup: not implemented yet")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *script) pruneBackups() error {
|
||||||
|
_, ok := os.LookupEnv("BACKUP_RETENTION_DAYS")
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return errors.New("pruneBackups: not implemented yet")
|
||||||
}
|
}
|
||||||
|
|
||||||
func fileExists(location string) (bool, error) {
|
func fileExists(location string) (bool, error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user