Skip to content

Commit

Permalink
ssh dumper
Browse files Browse the repository at this point in the history
  • Loading branch information
liweiyi88 committed Oct 7, 2022
1 parent 7957e54 commit 48d9c5c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 34 deletions.
24 changes: 12 additions & 12 deletions cmd/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

var (
dbName, username, password, host string
port int
mysqldbName, mysqlUsername, mysqlPassword, mysqlHost string
mysqlPort int
createTables bool
)

Expand All @@ -25,11 +25,11 @@ var mysqlDumpCmd = &cobra.Command{
}

dumper := dbdump.NewMysqlDumper()
dumper.DBName = dbName
dumper.Username = username
dumper.Password = password
dumper.Host = host
dumper.Port = port
dumper.DBName = mysqldbName
dumper.Username = mysqlUsername
dumper.Password = mysqlPassword
dumper.Host = mysqlHost
dumper.Port = mysqlPort
dumper.CreateTables = createTables

err := dumper.Dump(dumpFile)
Expand All @@ -41,11 +41,11 @@ var mysqlDumpCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(mysqlDumpCmd)
mysqlDumpCmd.Flags().StringVarP(&dbName, "dbname", "d", "", "database name (required) ")
mysqlDumpCmd.Flags().StringVarP(&mysqldbName, "dbname", "d", "", "database name (required) ")
mysqlDumpCmd.MarkFlagRequired("dbname")
mysqlDumpCmd.Flags().StringVarP(&username, "user", "u", "root", "database username")
mysqlDumpCmd.Flags().StringVarP(&password, "password", "p", "", "database password")
mysqlDumpCmd.Flags().StringVar(&host, "host", "127.0.0.1", "database host")
mysqlDumpCmd.Flags().IntVar(&port, "port", 3306, "database port")
mysqlDumpCmd.Flags().StringVarP(&mysqlUsername, "user", "u", "root", "database username")
mysqlDumpCmd.Flags().StringVarP(&mysqlPassword, "password", "p", "", "database password")
mysqlDumpCmd.Flags().StringVar(&mysqlHost, "host", "127.0.0.1", "database host")
mysqlDumpCmd.Flags().IntVar(&mysqlPort, "port", 3306, "database port")
mysqlDumpCmd.Flags().BoolVar(&createTables, "create-tables", true, "if set false, do not write CREATE TABLE statements that re-create each dumped table")
}
18 changes: 9 additions & 9 deletions dbdump/dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ type DBDump interface {
Dump(dumpFile string) error
}

type ExecutableDumper interface{
GetExecutableCommand(dumpFile string) (string, error)
type SshDumpCommandProvider interface {
GetSshDumpCommand(dumpFile string) (string, error)
}

type DBDumper struct {
DBName string
DBName string
Username string
Password string
Host string
Port int
Host string
Port int
}

func NewDBDumper() *DBDumper {
return &DBDumper{
DBName: "",
DBName: "",
Username: "root",
Password: "",
Host: "127.0.0.1",
Port: 3306,
Host: "127.0.0.1",
Port: 3306,
}
}
}
3 changes: 2 additions & 1 deletion dbdump/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ func NewMysqlDumper() *Mysql {
}
}

func (mysql *Mysql) GetExecutableCommand(dumpFile string) (string, error) {
// Get dump command used by ssh dumper.
func (mysql *Mysql) GetSshDumpCommand(dumpFile string) (string, error) {
args, err := mysql.getDumpCommandArgs()
if err != nil {
return "", err
Expand Down
30 changes: 18 additions & 12 deletions dbdump/sshdumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,28 @@ import (
"fmt"
"log"
"os"
"strconv"

"golang.org/x/crypto/ssh"
)

type SSHDumper struct {
User string
Host string
Port string
PrivateKeyFile string
DbType string
ExecutableDumper
type SshDumper struct {
User string
Host string
Port int
PrivateKeyFile string
DbType string
DumpCommandProvider SshDumpCommandProvider
}

func (sshDumper *SSHDumper) Dump(dumpFile string) {
host := sshDumper.Host + ":" + sshDumper.Port
func NewSshDumper() *SshDumper {
return &SshDumper{
Port: 22,
}
}

func (sshDumper *SshDumper) Dump(dumpFile string) error {
host := sshDumper.Host + ":" + strconv.Itoa(sshDumper.Port)

pKey, err := os.ReadFile(sshDumper.PrivateKeyFile)
if err != nil {
Expand Down Expand Up @@ -59,8 +66,8 @@ func (sshDumper *SSHDumper) Dump(dumpFile string) {
session.Stdout = &remoteOut
session.Stderr = &remoteErr

command, err := sshDumper.GetExecutableCommand(dumpFile)
if err !=nil {
command, err := sshDumper.DumpCommandProvider.GetSshDumpCommand(dumpFile)
if err != nil {
log.Fatal(err)
}

Expand All @@ -69,6 +76,5 @@ func (sshDumper *SSHDumper) Dump(dumpFile string) {
}

fmt.Println(remoteOut.String())

// if provide download option, we download the db to local.
}

0 comments on commit 48d9c5c

Please sign in to comment.