-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: break lib into multiple packages
- Loading branch information
1 parent
d7cfa65
commit ee1c0c1
Showing
8 changed files
with
252 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.