2024-07-23 14:48:37 +08:00
package client
import (
"compress/gzip"
"context"
"database/sql"
"errors"
"fmt"
"os"
"os/exec"
"path"
"strings"
"time"
2025-11-24 16:07:14 +08:00
"github.com/1Panel-dev/1Panel/agent/app/dto"
2024-07-23 14:48:37 +08:00
"github.com/1Panel-dev/1Panel/agent/buserr"
"github.com/1Panel-dev/1Panel/agent/constant"
"github.com/1Panel-dev/1Panel/agent/global"
2026-02-10 18:32:42 +08:00
"github.com/1Panel-dev/1Panel/agent/utils/cmd"
2026-01-08 15:27:00 +08:00
"github.com/1Panel-dev/1Panel/agent/utils/common"
2024-07-23 14:48:37 +08:00
"github.com/1Panel-dev/1Panel/agent/utils/files"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/client"
)
type Remote struct {
Type string
Client * sql . DB
Database string
User string
Password string
Address string
Port uint
SSL bool
RootCert string
ClientKey string
ClientCert string
SkipVerify bool
}
func NewRemote ( db Remote ) * Remote {
return & db
}
func ( r * Remote ) Create ( info CreateInfo ) error {
2026-07-16 14:24:38 +08:00
if err := r . CreateDatabase ( info ); err != nil {
return err
}
if len ( info . Username ) == 0 {
return nil
}
if err := r . CreateUser ( info , true ); err != nil {
_ = r . ExecSQL ( fmt . Sprintf ( "drop database if exists `%s`" , info . Name ), info . Timeout )
return err
}
return nil
}
func ( r * Remote ) CreateDatabase ( info CreateInfo ) error {
2025-11-24 16:07:14 +08:00
createSql := fmt . Sprintf ( "create database `%s` default character set %s collate %s" , info . Name , info . Format , info . Collation )
2025-11-25 15:02:37 +08:00
if len ( info . Collation ) == 0 {
createSql = fmt . Sprintf ( "create database `%s` default character set %s" , info . Name , info . Format )
}
2024-07-23 14:48:37 +08:00
if err := r . ExecSQL ( createSql , info . Timeout ); err != nil {
if strings . Contains ( strings . ToLower ( err . Error ()), "error 1007" ) {
2025-01-24 11:00:07 +08:00
return buserr . New ( "ErrDatabaseIsExist" )
2024-07-23 14:48:37 +08:00
}
return err
}
return nil
}
func ( r * Remote ) CreateUser ( info CreateInfo , withDeleteDB bool ) error {
2026-07-16 14:24:38 +08:00
for _ , user := range userIdentities ( info . Username , info . Permission ) {
if err := r . ExecSQL ( createUserSQL ( user , info . Password ), info . Timeout ); err != nil {
if isUserExistsErr ( err ) {
2025-01-24 11:00:07 +08:00
return buserr . New ( "ErrUserIsExist" )
2024-07-23 14:48:37 +08:00
}
if withDeleteDB {
_ = r . Delete ( DeleteInfo {
Name : info . Name ,
Version : info . Version ,
Username : info . Username ,
Permission : info . Permission ,
ForceDelete : true ,
Timeout : 300 })
}
return err
}
2026-07-16 14:24:38 +08:00
if err := r . ExecSQL ( createUserGrantSQL ( info , user ), info . Timeout ); err != nil {
2024-07-23 14:48:37 +08:00
if withDeleteDB {
_ = r . Delete ( DeleteInfo {
Name : info . Name ,
Version : info . Version ,
Username : info . Username ,
Permission : info . Permission ,
ForceDelete : true ,
Timeout : 300 })
}
return err
}
}
return nil
}
2026-07-16 14:24:38 +08:00
func ( r * Remote ) CreateUserOnly ( info UserInfo , password string , timeout uint ) error {
if err := r . ExecSQL ( createUserSQL ( userIdentity ( info . Username , info . Host ), password ), timeout ); err != nil {
if isUserExistsErr ( err ) {
return buserr . New ( "ErrUserIsExist" )
2024-07-23 14:48:37 +08:00
}
2026-07-16 14:24:38 +08:00
return err
2024-07-23 14:48:37 +08:00
}
2026-07-16 14:24:38 +08:00
return nil
}
2024-07-23 14:48:37 +08:00
2026-07-16 14:24:38 +08:00
func ( r * Remote ) GrantUser ( info GrantInfo , timeout uint ) error {
return r . ExecSQL ( grantUserSQL ( info ), timeout )
}
func ( r * Remote ) RevokeGrant ( info GrantInfo , timeout uint ) error {
if err := r . ExecSQL ( revokeGrantSQL ( info ), timeout ); err != nil {
return err
}
_ = r . ExecSQL ( revokeGrantOptionSQL ( info ), timeout )
return nil
}
func ( r * Remote ) DeleteUser ( info UserInfo , version string , timeout uint ) error {
return r . ExecSQL ( dropUserSQL ( info , version ), timeout )
}
func ( r * Remote ) UpdateUser ( info UserUpdateInfo , timeout uint ) error {
if info . Host == info . NewHost {
return nil
}
return r . ExecSQL ( renameUserSQL ( info ), timeout )
}
func ( r * Remote ) DeleteDatabase ( info DeleteInfo ) error {
if len ( info . Name ) == 0 {
return nil
}
if err := r . ExecSQL ( dropDatabaseSQL ( info . Name ), info . Timeout ); err != nil && ! info . ForceDelete {
return fmt . Errorf ( "drop database failed, err: %v" , err )
}
return nil
}
func ( r * Remote ) ListUsers ( timeout uint ) ([] UserInfo , error ) {
ctx , cancel := context . WithTimeout ( context . Background (), time . Duration ( timeout ) * time . Second )
defer cancel ()
rows , err := r . Client . QueryContext ( ctx , "select user,host from mysql.user order by user,host" )
if err != nil {
return nil , err
}
defer rows . Close ()
users := make ([] UserInfo , 0 )
for rows . Next () {
var user , host string
if err := rows . Scan ( & user , & host ); err != nil {
return nil , err
}
users = append ( users , UserInfo { Username : user , Host : host })
}
return users , rows . Err ()
}
func ( r * Remote ) ListGrants ( timeout uint ) ([] GrantInfo , error ) {
ctx , cancel := context . WithTimeout ( context . Background (), time . Duration ( timeout ) * time . Second )
defer cancel ()
rows , err := r . Client . QueryContext ( ctx , "select db,user,host from mysql.db where db not in ('information_schema','mysql','performance_schema','sys','__recycle_bin__','recycle_bin') order by db,user,host" )
if err != nil {
return nil , err
}
defer rows . Close ()
grants := make ([] GrantInfo , 0 )
for rows . Next () {
var db , user , host string
if err := rows . Scan ( & db , & user , & host ); err != nil {
return nil , err
}
if user == "root" {
continue
}
grants = append ( grants , GrantInfo { Database : db , Username : user , Host : host })
}
return grants , rows . Err ()
}
func ( r * Remote ) Delete ( info DeleteInfo ) error {
for _ , user := range userIdentities ( info . Username , info . Permission ) {
2024-07-23 14:48:37 +08:00
if strings . HasPrefix ( info . Version , "5.6" ) {
if err := r . ExecSQL ( fmt . Sprintf ( "drop user %s" , user ), info . Timeout ); err != nil && ! info . ForceDelete {
2025-09-02 13:43:44 +08:00
return fmt . Errorf ( "drop user failed, err: %v" , err )
2024-07-23 14:48:37 +08:00
}
} else {
if err := r . ExecSQL ( fmt . Sprintf ( "drop user if exists %s" , user ), info . Timeout ); err != nil && ! info . ForceDelete {
2025-09-02 13:43:44 +08:00
return fmt . Errorf ( "drop user failed, err: %v" , err )
2024-07-23 14:48:37 +08:00
}
}
}
if len ( info . Name ) != 0 {
2026-07-16 14:24:38 +08:00
if err := r . ExecSQL ( dropDatabaseSQL ( info . Name ), info . Timeout ); err != nil && ! info . ForceDelete {
2025-09-02 13:43:44 +08:00
return fmt . Errorf ( "drop database failed, err: %v" , err )
2024-07-23 14:48:37 +08:00
}
}
if ! info . ForceDelete {
global . LOG . Info ( "execute delete database sql successful, now start to drop uploads and records" )
}
return nil
}
func ( r * Remote ) ChangePassword ( info PasswordChangeInfo ) error {
if info . Username != "root" {
2026-07-16 14:24:38 +08:00
for _ , user := range userIdentities ( info . Username , info . Permission ) {
if err := r . ExecSQL ( changeUserPasswordSQL ( user , info . Password , info . Version ), info . Timeout ); err != nil {
2024-07-23 14:48:37 +08:00
return err
}
}
return nil
}
hosts , err := r . ExecSQLForHosts ( info . Timeout )
if err != nil {
return err
}
for _ , host := range hosts {
if host == "%" || host == "localhost" {
passwordRootChangeCMD := fmt . Sprintf ( "set password for 'root'@'%s' = password('%s')" , host , info . Password )
if ! strings . HasPrefix ( info . Version , "5.7" ) && ! strings . HasPrefix ( info . Version , "5.6" ) {
passwordRootChangeCMD = fmt . Sprintf ( "alter user 'root'@'%s' identified by '%s';" , host , info . Password )
}
if err := r . ExecSQL ( passwordRootChangeCMD , info . Timeout ); err != nil {
return err
}
}
}
return nil
}
func ( r * Remote ) ChangeAccess ( info AccessChangeInfo ) error {
if info . Username == "root" {
info . OldPermission = "%"
info . Name = "*"
info . Password = r . Password
}
if info . Permission != info . OldPermission {
if err := r . Delete ( DeleteInfo {
Version : info . Version ,
Username : info . Username ,
Permission : info . OldPermission ,
ForceDelete : true ,
Timeout : 300 }); err != nil {
return err
}
if info . Username == "root" {
return nil
}
}
if err := r . CreateUser ( CreateInfo {
Name : info . Name ,
Version : info . Version ,
Username : info . Username ,
Password : info . Password ,
Permission : info . Permission ,
Timeout : info . Timeout ,
}, false ); err != nil {
return err
}
if err := r . ExecSQL ( "flush privileges" , 300 ); err != nil {
return err
}
return nil
}
func ( r * Remote ) Backup ( info BackupInfo ) error {
2026-02-10 18:32:42 +08:00
if cmd . CheckIllegal ( r . Password , r . Address , r . User , info . Name , info . Format ) {
return buserr . New ( "ErrCmdIllegal" )
}
2024-07-23 14:48:37 +08:00
fileOp := files . NewFileOp ()
if ! fileOp . Stat ( info . TargetDir ) {
if err := os . MkdirAll ( info . TargetDir , os . ModePerm ); err != nil {
return fmt . Errorf ( "mkdir %s failed, err: %v" , info . TargetDir , err )
}
}
dumpCmd := "mysqldump"
if r . Type == constant . AppMariaDB {
dumpCmd = "mariadb-dump"
}
global . LOG . Infof ( "start to %s | gzip > %s.gzip" , dumpCmd , info . TargetDir + "/" + info . FileName )
image , err := loadImage ( info . Type , info . Version )
if err != nil {
return err
}
2026-01-08 15:27:00 +08:00
info . Args = append ( info . Args , "--routines" )
itemArgs := common . RemoveRepeatStr ( info . Args )
var args [] string
for _ , arg := range itemArgs {
if len ( arg ) == 0 {
continue
}
args = append ( args , arg )
}
2026-04-28 11:17:30 +08:00
backupArgs := [] string { "run" , "--rm" , "--net=host" , "-i" , image , dumpCmd }
backupArgs = append ( backupArgs , args ... )
backupArgs = append (
backupArgs ,
"-h" , r . Address ,
"-P" , fmt . Sprintf ( "%d" , r . Port ),
"-u" + r . User ,
"-p" + r . Password ,
sslSkip ( info . Version , r . Type ),
"--default-character-set=" + info . Format ,
info . Name ,
)
debugArgs := append ([] string {}, backupArgs ... )
for i , arg := range debugArgs {
if strings . Contains ( arg , r . Password ) {
debugArgs [ i ] = strings . ReplaceAll ( arg , r . Password , "******" )
}
}
global . LOG . Debug ( "docker " + strings . Join ( debugArgs , " " ))
2026-05-26 09:33:41 +08:00
cmdMgr := cmd . NewCommandMgr ()
if _ , err := cmdMgr . RunPipeToFile ( path . Join ( info . TargetDir , info . FileName ),
2026-04-28 11:17:30 +08:00
cmd . PipeCommand { Name : "docker" , Args : backupArgs },
cmd . PipeCommand { Name : "gzip" , Args : [] string { "-cf" }},
); err != nil {
return fmt . Errorf ( "handle backup database failed, err: %v" , err )
2024-07-23 14:48:37 +08:00
}
return nil
}
func ( r * Remote ) Recover ( info RecoverInfo ) error {
2026-02-10 18:32:42 +08:00
if cmd . CheckIllegal ( r . Password , r . Address , r . User , info . Name , info . Format ) {
return buserr . New ( "ErrCmdIllegal" )
}
2024-07-23 14:48:37 +08:00
fi , _ := os . Open ( info . SourceFile )
2026-02-27 22:43:50 +08:00
defer func () { _ = fi . Close () }()
2024-07-23 14:48:37 +08:00
image , err := loadImage ( info . Type , info . Version )
if err != nil {
return err
}
2026-04-28 11:17:30 +08:00
recoverArgs := [] string {
"run" , "--rm" , "--net=host" , "-i" , image , r . Type ,
"-h" , r . Address ,
"-P" , fmt . Sprintf ( "%d" , r . Port ),
"-u" + r . User ,
"-p" + r . Password ,
sslSkip ( info . Version , r . Type ),
"--default-character-set=" + info . Format ,
info . Name ,
}
debugArgs := append ([] string {}, recoverArgs ... )
for i , arg := range debugArgs {
if strings . Contains ( arg , r . Password ) {
debugArgs [ i ] = strings . ReplaceAll ( arg , r . Password , "******" )
}
}
global . LOG . Debug ( "docker " + strings . Join ( debugArgs , " " ))
cmd := exec . Command ( "docker" , recoverArgs ... )
2024-07-23 14:48:37 +08:00
if strings . HasSuffix ( info . SourceFile , ".gz" ) {
gzipFile , err := os . Open ( info . SourceFile )
if err != nil {
return err
}
2026-02-27 22:43:50 +08:00
defer func () { _ = gzipFile . Close () }()
2024-07-23 14:48:37 +08:00
gzipReader , err := gzip . NewReader ( gzipFile )
if err != nil {
return err
}
2026-02-27 22:43:50 +08:00
defer func () { _ = gzipReader . Close () }()
2024-07-23 14:48:37 +08:00
cmd . Stdin = gzipReader
} else {
cmd . Stdin = fi
}
stdout , err := cmd . CombinedOutput ()
stdStr := strings . ReplaceAll ( string ( stdout ), "mysql: [Warning] Using a password on the command line interface can be insecure.\n" , "" )
if err != nil || strings . HasPrefix ( string ( stdStr ), "ERROR " ) {
return errors . New ( stdStr )
}
return nil
}
func ( r * Remote ) SyncDB ( version string ) ([] SyncDBInfo , error ) {
var datas [] SyncDBInfo
2025-11-25 15:02:37 +08:00
rows , err := r . Client . Query ( "select schema_name, default_character_set_name, default_collation_name from information_schema.SCHEMATA" )
2024-07-23 14:48:37 +08:00
if err != nil {
return datas , err
}
2026-02-27 22:43:50 +08:00
defer func () { _ = rows . Close () }()
2024-07-23 14:48:37 +08:00
for rows . Next () {
2025-11-25 15:02:37 +08:00
var dbName , charsetName , collation string
if err = rows . Scan ( & dbName , & charsetName , & collation ); err != nil {
2024-07-23 14:48:37 +08:00
return datas , err
}
if dbName == "information_schema" || dbName == "mysql" || dbName == "performance_schema" || dbName == "sys" || dbName == "__recycle_bin__" || dbName == "recycle_bin" {
continue
}
dataItem := SyncDBInfo {
Name : dbName ,
From : "remote" ,
MysqlName : r . Database ,
Format : charsetName ,
2025-11-25 15:02:37 +08:00
Collation : collation ,
2024-07-23 14:48:37 +08:00
}
userRows , err := r . Client . Query ( "select user,host from mysql.db where db = ?" , dbName )
if err != nil {
global . LOG . Debugf ( "sync user of db %s failed, err: %v" , dbName , err )
dataItem . Permission = "%"
datas = append ( datas , dataItem )
continue
}
var permissionItem [] string
isLocal := true
i := 0
for userRows . Next () {
var user , host string
if err = userRows . Scan ( & user , & host ); err != nil {
return datas , err
}
if user == "root" {
continue
}
if i == 0 {
dataItem . Username = user
}
if dataItem . Username == user && host == "%" {
isLocal = false
dataItem . Permission = "%"
} else if dataItem . Username == user && host != "localhost" {
isLocal = false
permissionItem = append ( permissionItem , host )
}
i ++
}
if len ( dataItem . Username ) == 0 {
dataItem . Permission = "%"
} else {
if isLocal {
dataItem . Permission = "localhost"
}
if len ( dataItem . Permission ) == 0 {
dataItem . Permission = strings . Join ( permissionItem , "," )
}
}
datas = append ( datas , dataItem )
}
if err = rows . Err (); err != nil {
return datas , err
}
return datas , nil
}
func ( r * Remote ) Close () {
_ = r . Client . Close ()
}
func ( r * Remote ) ExecSQL ( command string , timeout uint ) error {
ctx , cancel := context . WithTimeout ( context . Background (), time . Duration ( timeout ) * time . Second )
defer cancel ()
if _ , err := r . Client . ExecContext ( ctx , command ); err != nil {
return err
}
if errors . Is ( ctx . Err (), context . DeadlineExceeded ) {
2025-01-24 11:00:07 +08:00
return buserr . New ( "ErrExecTimeOut" )
2024-07-23 14:48:37 +08:00
}
return nil
}
2025-11-24 16:07:14 +08:00
func ( r * Remote ) LoadFormatCollation ( timeout uint ) ([] dto . MysqlFormatCollationOption , error ) {
ctx , cancel := context . WithTimeout ( context . Background (), time . Duration ( timeout ) * time . Second )
defer cancel ()
rows , err := r . Client . QueryContext ( ctx , "SELECT CHARACTER_SET_NAME, COLLATION_NAME FROM INFORMATION_SCHEMA.COLLATIONS ORDER BY CHARACTER_SET_NAME, COLLATION_NAME;" )
if errors . Is ( ctx . Err (), context . DeadlineExceeded ) {
return nil , buserr . New ( "ErrExecTimeOut" )
}
if err != nil {
return nil , err
}
2026-02-27 22:43:50 +08:00
defer func () { _ = rows . Close () }()
2025-11-24 16:07:14 +08:00
formatMap := make ( map [ string ][] string )
for rows . Next () {
var item FormatCollation
if err := rows . Scan ( & item . Format , & item . Collation ); err != nil {
return nil , err
}
2025-11-26 17:21:21 +08:00
if ! item . Format . Valid {
continue
}
if _ , ok := formatMap [ item . Format . String ]; ! ok {
formatMap [ item . Format . String ] = [] string { item . Collation . String }
2025-11-24 16:07:14 +08:00
} else {
2025-11-26 17:21:21 +08:00
formatMap [ item . Format . String ] = append ( formatMap [ item . Format . String ], item . Collation . String )
2025-11-24 16:07:14 +08:00
}
}
options := [] dto . MysqlFormatCollationOption {}
for key , val := range formatMap {
options = append ( options , dto . MysqlFormatCollationOption {
Format : key ,
Collations : val ,
})
}
return options , nil
}
2024-07-23 14:48:37 +08:00
func ( r * Remote ) ExecSQLForHosts ( timeout uint ) ([] string , error ) {
ctx , cancel := context . WithTimeout ( context . Background (), time . Duration ( timeout ) * time . Second )
defer cancel ()
results , err := r . Client . QueryContext ( ctx , "select host from mysql.user where user='root';" )
if err != nil {
return nil , err
}
if errors . Is ( ctx . Err (), context . DeadlineExceeded ) {
2025-01-24 11:00:07 +08:00
return nil , buserr . New ( "ErrExecTimeOut" )
2024-07-23 14:48:37 +08:00
}
var rows [] string
2026-02-27 22:43:50 +08:00
defer func () { _ = results . Close () }()
2024-07-23 14:48:37 +08:00
for results . Next () {
var host string
if err := results . Scan ( & host ); err != nil {
continue
}
rows = append ( rows , host )
}
return rows , nil
}
func loadImage ( dbType , version string ) ( string , error ) {
cli , err := client . NewClientWithOpts ( client . FromEnv , client . WithAPIVersionNegotiation ())
if err != nil {
return "" , err
}
2026-05-28 11:09:00 +08:00
defer cli . Close ()
2024-07-23 14:48:37 +08:00
images , err := cli . ImageList ( context . Background (), image . ListOptions {})
if err != nil {
return "" , err
}
for _ , image := range images {
for _ , tag := range image . RepoTags {
if ! strings . HasPrefix ( tag , dbType + ":" ) {
continue
}
if dbType == "mariadb" && strings . HasPrefix ( tag , "mariadb:" ) {
return tag , nil
}
if strings . HasPrefix ( version , "5.6" ) && strings . HasPrefix ( tag , "mysql:5.6" ) {
return tag , nil
}
if strings . HasPrefix ( version , "5.7" ) && strings . HasPrefix ( tag , "mysql:5.7" ) {
return tag , nil
}
if strings . HasPrefix ( version , "8." ) && strings . HasPrefix ( tag , "mysql:8." ) {
return tag , nil
}
}
}
return loadVersion ( dbType , version ), nil
}
func loadVersion ( dbType string , version string ) string {
if dbType == "mariadb" {
2026-07-08 17:04:18 +08:00
return "mariadb:11.3.2"
2024-07-23 14:48:37 +08:00
}
if strings . HasPrefix ( version , "5.6" ) {
return "mysql:5.6.51"
}
if strings . HasPrefix ( version , "5.7" ) {
return "mysql:5.7.44"
}
return "mysql:8.2.0"
}
func sslSkip ( version , dbType string ) string {
if dbType == constant . AppMariaDB || strings . HasPrefix ( version , "5.6" ) || strings . HasPrefix ( version , "5.7" ) {
return "--skip-ssl"
}
return "--ssl-mode=DISABLED"
}