Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
Export the database in backup service backend.
spinningbota57a356eecfa
diff --git a/bin/bb/cmd/dump.go b/bin/bb/cmd/dump.goindex 0a8b303099..a5bcf46c2b 100644--- a/bin/bb/cmd/dump.go+++ b/bin/bb/cmd/dump.go@@ -65,9 +65,6 @@ func dumpDatabase(databaseType, username, password, hostname, port, database, diif username == "" {username = "root"}- if port == "" {- port = "3306"- }tlsConfig, err := tlsCfg.GetSslConfig()if err != nil {return fmt.Errorf("TlsConfig.GetSslConfig() got error: %v", err)diff --git a/bin/bb/cmd/restore.go b/bin/bb/cmd/restore.goindex 7a351d3de6..fdb84b2308 100644--- a/bin/bb/cmd/restore.go+++ b/bin/bb/cmd/restore.go@@ -60,9 +60,6 @@ func restoreDatabase(databaseType, username, password, hostname, port, database,if username == "" {username = "root"}- if port == "" {- port = "3306"- }tlsConfig, err := tlsCfg.GetSslConfig()if err != nil {return fmt.Errorf("TlsConfig.GetSslConfig() got error: %v", err)diff --git a/bin/bb/connect/mysql.go b/bin/bb/connect/mysql.goindex 65308e24eb..ccf5d21c02 100644--- a/bin/bb/connect/mysql.go+++ b/bin/bb/connect/mysql.go@@ -14,9 +14,13 @@ type MysqlConnect struct {DB *sql.DB}-// New creates a new MySQL connection.+// NewMysql creates a new MySQL connection.func NewMysql(username, password, hostname, port, database string, tlsCfg *tls.Config) (*MysqlConnect, error) {- dns := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", username, password, hostname, port, database)+ portPart := ""+ if port != "" {+ portPart = fmt.Sprintf(":%s", port)+ }+ dns := fmt.Sprintf("%s:%s@tcp(%s%s)/%s", username, password, hostname, portPart, database)if tlsCfg != nil {mysql.RegisterTLSConfig("custom", tlsCfg)dns += "?tls=custom"diff --git a/server/database.go b/server/database.goindex 0cfe03acdc..6754f2be62 100644--- a/server/database.go+++ b/server/database.go@@ -4,10 +4,13 @@ import ("context""fmt""net/http"+ "os""strconv""github.com/bytebase/bytebase""github.com/bytebase/bytebase/api"+ "github.com/bytebase/bytebase/bin/bb/connect"+ "github.com/bytebase/bytebase/bin/bb/dump/mysqldump""github.com/google/jsonapi""github.com/labstack/echo/v4")@@ -287,6 +290,10 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {}backup.Database = database+ if err := s.BackupDatabase(backup); err != nil {+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to create backup %q.", backup.Name)).SetInternal(err)+ }+c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)if err := jsonapi.MarshalPayload(c.Response().Writer, backup); err != nil {return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal create backup response").SetInternal(err)@@ -386,3 +393,26 @@ func (s *Server) ComposeDatabaseRelationship(ctx context.Context, database *api.return nil}++// BackupDatabase will take a backup of a database.+func (s *Server) BackupDatabase(backup *api.Backup) error {+ instance := backup.Database.Instance+ conn, err := connect.NewMysql(instance.Username, instance.Password, instance.Host, instance.Port, backup.Database.Name, nil /* tlsConfig */)+ if err != nil {+ return fmt.Errorf("connect.NewMysql(%q, %q, %q, %q) got error: %v", instance.Username, instance.Password, instance.Host, instance.Port, err)+ }+ defer conn.Close()+ dp := mysqldump.New(conn)++ f, err := os.Create(backup.Path)+ if err != nil {+ return fmt.Errorf("Failed to open backup path: %s", backup.Path)+ }+ defer f.Close()++ if err := dp.Dump(backup.Database.Name, f, false /* schemaOnly */); err != nil {+ return err+ }++ return nil+}