Skip to content

Commit

Permalink
refactor: break lib into multiple packages
Browse files Browse the repository at this point in the history
  • Loading branch information
owojcikiewicz committed Nov 28, 2023
1 parent d7cfa65 commit ee1c0c1
Show file tree
Hide file tree
Showing 8 changed files with 252 additions and 208 deletions.
11 changes: 6 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"os"

"github.com/robfig/cron/v3"
lib "github.com/saturdayshdev/shbackup/internal"
backup "github.com/saturdayshdev/shbackup/internal/backup"
docker "github.com/saturdayshdev/shbackup/internal/docker"
)

func main() {
storage, err := lib.CreateStorageClient(lib.StorageClientConfig{
storage, err := backup.CreateStorageClient(backup.StorageClientConfig{
BucketName: os.Getenv("BUCKET_NAME"),
BucketLocation: os.Getenv("BUCKET_REGION"),
BucketClass: os.Getenv("BUCKET_CLASS"),
Expand All @@ -23,7 +24,7 @@ func main() {
panic(err)
}

docker, err := lib.CreateDockerClient()
docker, err := docker.CreateClient()
if err != nil {
panic(err)
}
Expand All @@ -41,12 +42,12 @@ func main() {
continue
}

config, err := lib.GetBackupConfig(labels, &container)
config, err := backup.GetBackupConfig(labels, &container)
if err != nil {
log.Println(err)
}

err = lib.BackupDatabase(docker, storage, config)
err = backup.BackupDatabase(docker, storage, config)
if err != nil {
log.Println(err)
}
Expand Down
197 changes: 0 additions & 197 deletions internal/backup.go

This file was deleted.

84 changes: 84 additions & 0 deletions internal/backup/backup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package lib

import (
"errors"
"log"
"os"

strategies "github.com/saturdayshdev/shbackup/internal/backup/strategies"
docker "github.com/saturdayshdev/shbackup/internal/docker"
)

type BackupConfig struct {
Name string
Strategy strategies.Strategy
User string
Password string
Container *docker.Container
}

func GetBackupConfig(labels map[string]string, container *docker.Container) (*BackupConfig, error) {
name, ok := labels["shbackup.name"]
if !ok {
return nil, errors.New("shbackup.name label not found")
}

strategy, ok := labels["shbackup.strategy"]
if !ok {
return nil, errors.New("shbackup.strategy label not found")
}

user, ok := labels["shbackup.user"]
if !ok {
return nil, errors.New("shbackup.user label not found")
}

password, ok := labels["shbackup.password"]
if !ok {
return nil, errors.New("shbackup.password label not found")
}

backupStrategy, err := strategies.GetStrategy(strategy)
if err != nil {
return nil, err
}

return &BackupConfig{
Name: name,
Strategy: backupStrategy,
User: user,
Password: password,
Container: container,
}, nil
}

func BackupDatabase(docker *docker.Client, storage *StorageClient, config *BackupConfig) error {
log.Printf("Backing up %s database\n", config.Name)

file, err := config.Strategy.GetDump(docker, strategies.DumpConfig{
Name: config.Name,
User: config.User,
Password: config.Password,
Container: config.Container.ID,
})
if err != nil {
return err
}

errCh := make(chan error)
go storage.UploadFile(*file, *file, errCh)

err = <-errCh
if err != nil {
return err
}

err = os.Remove(*file)
if err != nil {
return err
}

log.Printf("Backup of %s database completed\n", config.Name)

return nil
}
File renamed without changes.
62 changes: 62 additions & 0 deletions internal/backup/strategies/mysql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package strategies

import (
"archive/tar"
"context"
"fmt"
"io"
"os"
"time"

docker "github.com/saturdayshdev/shbackup/internal/docker"
)

type MySQLStrategy struct{}

func (s *MySQLStrategy) GetDump(docker *docker.Client, config DumpConfig) (*string, error) {
file := fmt.Sprint(time.Now().Unix()) + "_" + config.Name + ".sql"

cmd := []string{"mysqldump", "-u", config.User, "-p" + config.Password, "-f", file}
err := docker.ExecInContainer(config.Container, cmd)
if err != nil {
return nil, err
}

var stream io.ReadCloser
for i := 0; i < 10; i++ {
stream, _, err = docker.Client.CopyFromContainer(context.Background(), config.Container, "/"+file)
if err == nil {
break
}

time.Sleep(1000 * time.Millisecond)
}
if err != nil {
return nil, err
}
defer stream.Close()

tr := tar.NewReader(stream)
if _, err := tr.Next(); err != nil {
return nil, err
}

dest, err := os.Create(file)
if err != nil {
return nil, err
}
defer dest.Close()

_, err = io.Copy(dest, tr)
if err != nil {
return nil, err
}

cmd = []string{"rm", file}
err = docker.ExecInContainer(config.Container, cmd)
if err != nil {
return nil, err
}

return &file, nil
}
Loading

0 comments on commit ee1c0c1

Please sign in to comment.