Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5b91e64c26 | |||
| 02d93c1904 | |||
| 59acfec670 | |||
| 26eabab106 | |||
| d44cad5ebd | |||
| 0cbfb81096 | |||
| 254183ceff | |||
| 676ed11c3f | |||
| 5d9be49f6c | |||
| 3c5014e90b | |||
| 1cef099a37 | |||
| 292d582a45 |
@@ -8,6 +8,7 @@
|
|||||||
"options": {
|
"options": {
|
||||||
"emojis": "true"
|
"emojis": "true"
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
"maintainedVersion": "0-develop"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
13
cmd/dump.go
13
cmd/dump.go
@@ -48,7 +48,7 @@ to quickly create a Cobra application.`,
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Printf(
|
fmt.Printf(
|
||||||
"%s combining dump files\n\r",
|
"%s Combining files\n\r",
|
||||||
style.Success(icon.Info),
|
style.Success(icon.Info),
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -65,9 +65,18 @@ to quickly create a Cobra application.`,
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Printf(
|
fmt.Printf(
|
||||||
"%s Completer combining dump files\n\r",
|
"%s Completed combining files\n\r",
|
||||||
style.Success(icon.Check),
|
style.Success(icon.Check),
|
||||||
)
|
)
|
||||||
|
fmt.Printf(
|
||||||
|
"%s Zipping file\n\r",
|
||||||
|
style.Success(icon.Info),
|
||||||
|
)
|
||||||
|
|
||||||
|
// Strip definers from the Dumpfile
|
||||||
|
myDump.RemoveDefiners(filename)
|
||||||
|
|
||||||
|
filename, _ = dump.ZipFile(filename)
|
||||||
timeElapsed := time.Since(start)
|
timeElapsed := time.Since(start)
|
||||||
fmt.Printf(
|
fmt.Printf(
|
||||||
"%s Dump file available at %s\r\nTook %s\n\r",
|
"%s Dump file available at %s\r\nTook %s\n\r",
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package dump
|
package dump
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"archive/zip"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/charmbracelet/lipgloss"
|
"github.com/charmbracelet/lipgloss"
|
||||||
"github.com/charmbracelet/log"
|
"github.com/charmbracelet/log"
|
||||||
@@ -95,14 +96,23 @@ func WithPassword(password string) Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
func (c *Client) Dump() error {
|
func (c *Client) Dump() error {
|
||||||
|
|
||||||
// Construct schema output
|
// Construct schema output
|
||||||
fmt.Printf("%s Dumping schema %s from %s\n\r",
|
fmt.Printf("%s Dumping schema %s from %s\n\r",
|
||||||
style.Success(icon.Info),
|
style.Success(icon.Info),
|
||||||
lipgloss.NewStyle().Foreground(color.Purple).Render(c.hostname),
|
|
||||||
lipgloss.NewStyle().Foreground(color.Yellow).Render(fmt.Sprint(c.databaseName)),
|
lipgloss.NewStyle().Foreground(color.Yellow).Render(fmt.Sprint(c.databaseName)),
|
||||||
|
lipgloss.NewStyle().Foreground(color.Purple).Render(c.hostname),
|
||||||
)
|
)
|
||||||
|
|
||||||
_ = exec.Command(c.executable,
|
f, _ := os.OpenFile(filepath.Join(c.storagePath, c.databaseName+"-keys.sql"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
|
||||||
|
|
||||||
|
_, err := f.WriteString("SET FOREIGN_KEY_CHECKS=0;\n")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
f.Close()
|
||||||
|
|
||||||
|
response, err := exec.Command(c.executable,
|
||||||
"--host="+c.hostname,
|
"--host="+c.hostname,
|
||||||
"--port="+strconv.Itoa(c.port),
|
"--port="+strconv.Itoa(c.port),
|
||||||
"--user="+c.username,
|
"--user="+c.username,
|
||||||
@@ -110,26 +120,30 @@ func (c *Client) Dump() error {
|
|||||||
"--no-create-db",
|
"--no-create-db",
|
||||||
"--no-data",
|
"--no-data",
|
||||||
"--skip-triggers",
|
"--skip-triggers",
|
||||||
|
"--compact",
|
||||||
"--result-file", filepath.Join(c.storagePath, c.databaseName+"-schema.sql"),
|
"--result-file", filepath.Join(c.storagePath, c.databaseName+"-schema.sql"),
|
||||||
c.databaseName).Run()
|
c.databaseName).CombinedOutput()
|
||||||
fmt.Printf("%s Removing definers\n\r",
|
if err != nil {
|
||||||
style.Success(icon.Info),
|
fmt.Printf(
|
||||||
|
"%s mysqldump error: %s\n\r",
|
||||||
|
style.Failure(icon.Cross),
|
||||||
|
lipgloss.NewStyle().Foreground(color.Purple).Render(string(response)),
|
||||||
)
|
)
|
||||||
|
return err
|
||||||
// removeDefiners(filepath.Join(c.storagePath, c.databaseName+"-schema.sql"))
|
}
|
||||||
|
|
||||||
fmt.Printf(
|
fmt.Printf(
|
||||||
"%s Done dumping schema %s from %s\n\r",
|
"%s Done dumping schema %s from %s\n\r",
|
||||||
style.Success(icon.Check),
|
style.Success(icon.Check),
|
||||||
lipgloss.NewStyle().Foreground(color.Purple).Render(c.hostname),
|
|
||||||
lipgloss.NewStyle().Foreground(color.Yellow).Render(fmt.Sprint(c.databaseName)),
|
lipgloss.NewStyle().Foreground(color.Yellow).Render(fmt.Sprint(c.databaseName)),
|
||||||
|
lipgloss.NewStyle().Foreground(color.Purple).Render(c.hostname),
|
||||||
)
|
)
|
||||||
// Construct data output
|
// Construct data output
|
||||||
|
|
||||||
fmt.Printf("%s Dumping data %s from %s\n\r",
|
fmt.Printf("%s Dumping data %s from %s\n\r",
|
||||||
style.Success(icon.Info),
|
style.Success(icon.Info),
|
||||||
lipgloss.NewStyle().Foreground(color.Purple).Render(c.hostname),
|
|
||||||
lipgloss.NewStyle().Foreground(color.Yellow).Render(fmt.Sprint(c.databaseName)),
|
lipgloss.NewStyle().Foreground(color.Yellow).Render(fmt.Sprint(c.databaseName)),
|
||||||
|
lipgloss.NewStyle().Foreground(color.Purple).Render(c.hostname),
|
||||||
)
|
)
|
||||||
|
|
||||||
_ = exec.Command(c.executable,
|
_ = exec.Command(c.executable,
|
||||||
@@ -140,21 +154,22 @@ func (c *Client) Dump() error {
|
|||||||
"--no-create-db",
|
"--no-create-db",
|
||||||
"--no-create-info",
|
"--no-create-info",
|
||||||
"--skip-triggers",
|
"--skip-triggers",
|
||||||
|
"--compact",
|
||||||
"--result-file", filepath.Join(c.storagePath, c.databaseName+"-data.sql"),
|
"--result-file", filepath.Join(c.storagePath, c.databaseName+"-data.sql"),
|
||||||
c.databaseName).Run()
|
c.databaseName).Run()
|
||||||
removeDefiners(filepath.Join(c.storagePath, c.databaseName+"-data.sql"))
|
|
||||||
fmt.Printf(
|
fmt.Printf(
|
||||||
"%s Done dumping data %s from %s\n\r",
|
"%s Done dumping data %s from %s\n\r",
|
||||||
style.Success(icon.Check),
|
style.Success(icon.Check),
|
||||||
lipgloss.NewStyle().Foreground(color.Purple).Render(c.hostname),
|
|
||||||
lipgloss.NewStyle().Foreground(color.Yellow).Render(fmt.Sprint(c.databaseName)),
|
lipgloss.NewStyle().Foreground(color.Yellow).Render(fmt.Sprint(c.databaseName)),
|
||||||
|
lipgloss.NewStyle().Foreground(color.Purple).Render(c.hostname),
|
||||||
)
|
)
|
||||||
|
|
||||||
// Construct routines/triggers output
|
// Construct routines/triggers output
|
||||||
fmt.Printf("%s Dumping routines %s from %s\n\r",
|
fmt.Printf("%s Dumping routines %s from %s\n\r",
|
||||||
style.Success(icon.Info),
|
style.Success(icon.Info),
|
||||||
lipgloss.NewStyle().Foreground(color.Purple).Render(c.hostname),
|
|
||||||
lipgloss.NewStyle().Foreground(color.Yellow).Render(fmt.Sprint(c.databaseName)),
|
lipgloss.NewStyle().Foreground(color.Yellow).Render(fmt.Sprint(c.databaseName)),
|
||||||
|
lipgloss.NewStyle().Foreground(color.Purple).Render(c.hostname),
|
||||||
)
|
)
|
||||||
|
|
||||||
_ = exec.Command(c.executable,
|
_ = exec.Command(c.executable,
|
||||||
@@ -167,9 +182,10 @@ func (c *Client) Dump() error {
|
|||||||
"--no-data",
|
"--no-data",
|
||||||
"--triggers",
|
"--triggers",
|
||||||
"--routines",
|
"--routines",
|
||||||
|
"--compact",
|
||||||
"--result-file", filepath.Join(c.storagePath, c.databaseName+"-routines.sql"),
|
"--result-file", filepath.Join(c.storagePath, c.databaseName+"-routines.sql"),
|
||||||
c.databaseName).Run()
|
c.databaseName).Run()
|
||||||
removeDefiners(filepath.Join(c.storagePath, c.databaseName+"-routines.sql"))
|
|
||||||
fmt.Printf(
|
fmt.Printf(
|
||||||
"%s Done dumping routines %s from %s\n\r",
|
"%s Done dumping routines %s from %s\n\r",
|
||||||
style.Success(icon.Check),
|
style.Success(icon.Check),
|
||||||
@@ -180,15 +196,9 @@ func (c *Client) Dump() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeDefiners(filename string) {
|
func (c *Client) RemoveDefiners(filename string) {
|
||||||
// Regex 1: .*(DEFINER=[a-zA-Z0-9\x60%@]+).* (Used by procedures/funcs)
|
expressions := make([]string, 1)
|
||||||
// Regex 2: .*(\/\*\!50003.*!50003+).* (Used by triggers)
|
expressions[0] = `s/DEFINER=[^ ]* / /g`
|
||||||
// Regex 3: (\/\*\!50013.*DEFINER \*\/) (Used in schema)
|
|
||||||
// TODO: Add routine body to strip definers.
|
|
||||||
expressions := make([]string, 3)
|
|
||||||
expressions[0] = `s/(^.*)(.DEFINER=[a-zA-Z0-9\x60%@]+)(.*)/$1$3/g`
|
|
||||||
expressions[1] = `s/(.*)(\/\*\!50003.*!50003+)(.*)/$1$3/g`
|
|
||||||
expressions[2] = `s/(\/\*\!50013.*DEFINER \*\/)//g`
|
|
||||||
|
|
||||||
if !strings.Contains(filename, "data") {
|
if !strings.Contains(filename, "data") {
|
||||||
for _, re := range expressions {
|
for _, re := range expressions {
|
||||||
@@ -200,7 +210,8 @@ func removeDefiners(filename string) {
|
|||||||
|
|
||||||
outputFile, err := os.OpenFile(filename+".new", os.O_WRONLY|os.O_CREATE, 0666)
|
outputFile, err := os.OpenFile(filename+".new", os.O_WRONLY|os.O_CREATE, 0666)
|
||||||
_, _ = io.Copy(outputFile, engine.Wrap(inputFile))
|
_, _ = io.Copy(outputFile, engine.Wrap(inputFile))
|
||||||
|
_ = inputFile.Close()
|
||||||
|
_ = outputFile.Close()
|
||||||
err = util.MoveFile(filename+".new", filename)
|
err = util.MoveFile(filename+".new", filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf(
|
fmt.Printf(
|
||||||
@@ -215,10 +226,11 @@ func removeDefiners(filename string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Combine() (string, error) {
|
func (c *Client) Combine() (string, error) {
|
||||||
var files [3]string
|
var files [4]string
|
||||||
files[0] = "schema"
|
files[0] = "keys"
|
||||||
files[1] = "data"
|
files[1] = "schema"
|
||||||
files[2] = "routines"
|
files[2] = "data"
|
||||||
|
files[3] = "routines"
|
||||||
|
|
||||||
filepath.Join(c.storagePath, c.databaseName+"-backup.sql")
|
filepath.Join(c.storagePath, c.databaseName+"-backup.sql")
|
||||||
err := concatenate.FilesToFile(filepath.Join(c.storagePath, c.databaseName+"-backup.sql"),
|
err := concatenate.FilesToFile(filepath.Join(c.storagePath, c.databaseName+"-backup.sql"),
|
||||||
@@ -227,6 +239,7 @@ func (c *Client) Combine() (string, error) {
|
|||||||
filepath.Join(c.storagePath, c.databaseName+"-"+files[0]+".sql"),
|
filepath.Join(c.storagePath, c.databaseName+"-"+files[0]+".sql"),
|
||||||
filepath.Join(c.storagePath, c.databaseName+"-"+files[1]+".sql"),
|
filepath.Join(c.storagePath, c.databaseName+"-"+files[1]+".sql"),
|
||||||
filepath.Join(c.storagePath, c.databaseName+"-"+files[2]+".sql"),
|
filepath.Join(c.storagePath, c.databaseName+"-"+files[2]+".sql"),
|
||||||
|
filepath.Join(c.storagePath, c.databaseName+"-"+files[3]+".sql"),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
@@ -240,3 +253,48 @@ func (c *Client) Combine() (string, error) {
|
|||||||
}
|
}
|
||||||
return filepath.Join(c.storagePath, c.databaseName+"-backup.sql"), nil
|
return filepath.Join(c.storagePath, c.databaseName+"-backup.sql"), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ZipFile(filename string) (string, error) {
|
||||||
|
//Create a new zip archive and named archive.zip
|
||||||
|
archive, err := os.Create(util.FileNameWithoutExt(filename) + ".zip")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
// this is to catch errors if any
|
||||||
|
}
|
||||||
|
defer archive.Close()
|
||||||
|
_, file := filepath.Split(filename)
|
||||||
|
|
||||||
|
//Create a new zip writer
|
||||||
|
zipWriter := zip.NewWriter(archive)
|
||||||
|
fmt.Printf(
|
||||||
|
"%s Opening .sql file\n\r",
|
||||||
|
style.Success(icon.Info),
|
||||||
|
)
|
||||||
|
|
||||||
|
f1, err := os.Open(filename)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer f1.Close()
|
||||||
|
fmt.Printf(
|
||||||
|
"%s Adding file to archive\n\r",
|
||||||
|
style.Success(icon.Info),
|
||||||
|
)
|
||||||
|
w1, err := zipWriter.Create(file)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if _, err := io.Copy(w1, f1); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
fmt.Printf(
|
||||||
|
"%s Closing archive\n\r",
|
||||||
|
style.Success(icon.Info),
|
||||||
|
)
|
||||||
|
zipWriter.Close()
|
||||||
|
f1.Close()
|
||||||
|
os.Remove(filename)
|
||||||
|
|
||||||
|
return util.FileNameWithoutExt(filename) + ".zip", nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func MoveFile(src, dst string) error {
|
func MoveFile(src, dst string) error {
|
||||||
@@ -39,9 +41,18 @@ func MoveFile(src, dst string) error {
|
|||||||
return fmt.Errorf("chmod error: %s", err)
|
return fmt.Errorf("chmod error: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//err = in.Close()
|
||||||
|
//if err != nil {
|
||||||
|
// return fmt.Errorf("closing file failed: %s", err)
|
||||||
|
//}
|
||||||
|
//time.Sleep(time.Second * 10)
|
||||||
err = os.Remove(src)
|
err = os.Remove(src)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed removing original file: %s", err)
|
return fmt.Errorf("failed removing original file: %s", err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func FileNameWithoutExt(fileName string) string {
|
||||||
|
return strings.TrimSuffix(fileName, filepath.Ext(fileName))
|
||||||
|
}
|
||||||
|
|||||||
6
justfile
6
justfile
@@ -8,7 +8,11 @@ install:
|
|||||||
|
|
||||||
build:
|
build:
|
||||||
@echo -n "Building app ... "
|
@echo -n "Building app ... "
|
||||||
@go build {{flags}} -o bin/ && echo "OK" || echo "FAILED"
|
@go build {{flags}} -o bin/gosqldump ./main.go && echo "OK" || echo "FAILED"
|
||||||
|
|
||||||
|
build-win:
|
||||||
|
@echo -n "Building app for windows ... "
|
||||||
|
@GOOS=windows GOARCH=amd64 go build {{flags}} -o bin/gosqldump.exe ./main.go && echo "OK" || echo "FAILED"
|
||||||
update:
|
update:
|
||||||
go get -u
|
go get -u
|
||||||
go mod tidy -v
|
go mod tidy -v
|
||||||
|
|||||||
Reference in New Issue
Block a user