|
|
|
@@ -96,13 +96,22 @@ 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),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
response, err := exec.Command(c.executable,
|
|
|
|
"--host="+c.hostname,
|
|
|
|
"--host="+c.hostname,
|
|
|
|
"--port="+strconv.Itoa(c.port),
|
|
|
|
"--port="+strconv.Itoa(c.port),
|
|
|
|
@@ -111,6 +120,7 @@ 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).CombinedOutput()
|
|
|
|
c.databaseName).CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
@@ -126,15 +136,15 @@ func (c *Client) Dump() error {
|
|
|
|
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,
|
|
|
|
@@ -145,21 +155,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"))
|
|
|
|
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,
|
|
|
|
@@ -172,6 +183,7 @@ 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"))
|
|
|
|
removeDefiners(filepath.Join(c.storagePath, c.databaseName+"-routines.sql"))
|
|
|
|
@@ -189,11 +201,13 @@ func removeDefiners(filename string) {
|
|
|
|
// Regex 1: .*(DEFINER=[a-zA-Z0-9\x60%@]+).* (Used by procedures/funcs)
|
|
|
|
// Regex 1: .*(DEFINER=[a-zA-Z0-9\x60%@]+).* (Used by procedures/funcs)
|
|
|
|
// Regex 2: .*(\/\*\!50003.*!50003+).* (Used by triggers)
|
|
|
|
// Regex 2: .*(\/\*\!50003.*!50003+).* (Used by triggers)
|
|
|
|
// Regex 3: (\/\*\!50013.*DEFINER \*\/) (Used in schema)
|
|
|
|
// Regex 3: (\/\*\!50013.*DEFINER \*\/) (Used in schema)
|
|
|
|
// TODO: Add routine body to strip definers.
|
|
|
|
// Regex 4: ^(.TRIGGER).(\x60.*) (Fix the create triggers)
|
|
|
|
expressions := make([]string, 3)
|
|
|
|
|
|
|
|
expressions[0] = `s/(^.*)(.DEFINER=[a-zA-Z0-9\x60%@]+)(.*)/$1$3/g`
|
|
|
|
expressions := make([]string, 4)
|
|
|
|
|
|
|
|
expressions[0] = `s/(^.*)(.DEFINER=[a-zA-Z0-9_\x60%@]+)(.*)/$1$3/g`
|
|
|
|
expressions[1] = `s/(.*)(\/\*\!50003.*!50003+)(.*)/$1$3/g`
|
|
|
|
expressions[1] = `s/(.*)(\/\*\!50003.*!50003+)(.*)/$1$3/g`
|
|
|
|
expressions[2] = `s/(\/\*\!50013.*DEFINER \*\/)//g`
|
|
|
|
expressions[2] = `s/(\/\*\!50013.*DEFINER \*\/)//g`
|
|
|
|
|
|
|
|
expressions[3] = `s/(^.TRIGGER).(\x60.*)/CREATE$1 $2/g`
|
|
|
|
|
|
|
|
|
|
|
|
if !strings.Contains(filename, "data") {
|
|
|
|
if !strings.Contains(filename, "data") {
|
|
|
|
for _, re := range expressions {
|
|
|
|
for _, re := range expressions {
|
|
|
|
@@ -221,10 +235,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"),
|
|
|
|
@@ -233,6 +248,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
|
|
|
|
@@ -248,37 +264,46 @@ func (c *Client) Combine() (string, error) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func ZipFile(filename string) (string, error) {
|
|
|
|
func ZipFile(filename string) (string, error) {
|
|
|
|
fmt.Println("creating zip archive")
|
|
|
|
|
|
|
|
//Create a new zip archive and named archive.zip
|
|
|
|
//Create a new zip archive and named archive.zip
|
|
|
|
archive, err := os.Create(util.FileNameWithoutExt(filename) + ".zip")
|
|
|
|
archive, err := os.Create(util.FileNameWithoutExt(filename) + ".zip")
|
|
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
panic(err)
|
|
|
|
// this is to catch errors if any
|
|
|
|
// this is to catch errors if any
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer archive.Close()
|
|
|
|
defer archive.Close()
|
|
|
|
fmt.Println("archive file created successfully....")
|
|
|
|
_, file := filepath.Split(filename)
|
|
|
|
|
|
|
|
|
|
|
|
//Create a new zip writer
|
|
|
|
//Create a new zip writer
|
|
|
|
zipWriter := zip.NewWriter(archive)
|
|
|
|
zipWriter := zip.NewWriter(archive)
|
|
|
|
fmt.Println("opening .sql file")
|
|
|
|
fmt.Printf(
|
|
|
|
|
|
|
|
"%s Opening .sql file\n\r",
|
|
|
|
|
|
|
|
style.Success(icon.Info),
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
f1, err := os.Open(filename)
|
|
|
|
f1, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer f1.Close()
|
|
|
|
defer f1.Close()
|
|
|
|
fmt.Println("adding file to archive..")
|
|
|
|
fmt.Printf(
|
|
|
|
w1, err := zipWriter.Create(filename)
|
|
|
|
"%s Adding file to archive\n\r",
|
|
|
|
|
|
|
|
style.Success(icon.Info),
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
w1, err := zipWriter.Create(file)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, err := io.Copy(w1, f1); err != nil {
|
|
|
|
if _, err := io.Copy(w1, f1); err != nil {
|
|
|
|
panic(err)
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Println("closing archive")
|
|
|
|
fmt.Printf(
|
|
|
|
|
|
|
|
"%s Closing archive\n\r",
|
|
|
|
|
|
|
|
style.Success(icon.Info),
|
|
|
|
|
|
|
|
)
|
|
|
|
zipWriter.Close()
|
|
|
|
zipWriter.Close()
|
|
|
|
f1.Close()
|
|
|
|
f1.Close()
|
|
|
|
_ = os.Remove(filename)
|
|
|
|
os.Remove(filename)
|
|
|
|
|
|
|
|
|
|
|
|
return util.FileNameWithoutExt(filename) + ".zip", nil
|
|
|
|
return util.FileNameWithoutExt(filename) + ".zip", nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|