|
|
|
@@ -96,6 +96,7 @@ 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),
|
|
|
|
@@ -103,6 +104,14 @@ func (c *Client) Dump() error {
|
|
|
|
lipgloss.NewStyle().Foreground(color.Purple).Render(c.hostname),
|
|
|
|
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 {
|
|
|
|
@@ -121,7 +131,6 @@ func (c *Client) Dump() error {
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
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",
|
|
|
|
@@ -145,9 +154,10 @@ 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),
|
|
|
|
@@ -172,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),
|
|
|
|
@@ -185,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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
@@ -221,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"),
|
|
|
|
@@ -233,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
|
|
|
|
|