diff --git a/pkg/backup.go b/pkg/backup.go index 282a9f8..905966a 100644 --- a/pkg/backup.go +++ b/pkg/backup.go @@ -217,10 +217,7 @@ func BackupDatabase(db *dbConfig, backupFileName string, disableCompression bool if disableCompression { // Execute mysqldump cmd := exec.Command("mysqldump", - "-h", db.dbHost, - "-P", db.dbPort, - "-u", db.dbUserName, - db.dbName, + fmt.Sprintf("--defaults-file=%s", mysqlClientConfig), db.dbName, ) output, err := cmd.Output() if err != nil { @@ -247,7 +244,7 @@ func BackupDatabase(db *dbConfig, backupFileName string, disableCompression bool } else { // Execute mysqldump - cmd := exec.Command("mysqldump", "-h", db.dbHost, "-P", db.dbPort, "-u", db.dbUserName, db.dbName) + cmd := exec.Command("mysqldump", fmt.Sprintf("--defaults-file=%s", mysqlClientConfig), db.dbName) stdout, err := cmd.StdoutPipe() if err != nil { return fmt.Errorf("failed to backup database: %v output: %v", err, stdout) diff --git a/pkg/helper.go b/pkg/helper.go index 98efe7c..eb03af7 100644 --- a/pkg/helper.go +++ b/pkg/helper.go @@ -27,6 +27,7 @@ package pkg import ( "bytes" "fmt" + goutils "github.com/jkaninda/go-utils" "github.com/jkaninda/mysql-bkup/utils" "gopkg.in/yaml.v3" "os" @@ -67,16 +68,17 @@ func deleteTemp() { // TestDatabaseConnection tests the database connection func testDatabaseConnection(db *dbConfig) error { - // Set the MYSQL_PWD environment variable - if err := os.Setenv("MYSQL_PWD", db.dbPassword); err != nil { - return fmt.Errorf("failed to set MYSQL_PWD environment variable: %v", err) + // Create the mysql client config file + if err := createMysqlClientConfigFile(*db); err != nil { + return fmt.Errorf(err.Error()) } utils.Info("Connecting to %s database ...", db.dbName) // Set database name for notification error utils.DatabaseName = db.dbName // Prepare the command to test the database connection - cmd := exec.Command("mariadb", "-h", db.dbHost, "-P", db.dbPort, "-u", db.dbUserName, db.dbName, "-e", "quit") + //cmd := exec.Command("mariadb", "-h", db.dbHost, "-P", db.dbPort, "-u", db.dbUserName, db.dbName, "-e", "quit") + cmd := exec.Command("mariadb", fmt.Sprintf("--defaults-file=%s", mysqlClientConfig), db.dbName, "-e", "quit") // Capture the output var out bytes.Buffer cmd.Stdout = &out @@ -189,9 +191,11 @@ func RemoveLastExtension(filename string) string { // Create mysql client config file func createMysqlClientConfigFile(db dbConfig) error { + caCertPath := goutils.GetStringEnvWithDefault("DB_SSL_CA", "/etc/ssl/certs/ca-certificates.crt") + sslMode := goutils.GetStringEnvWithDefault("DB_SSL_MODE", "0") // Create the mysql client config file mysqlClientConfigFile := filepath.Join(tmpPath, "my.cnf") - mysqlClientConfig := fmt.Sprintf("[client]\nhost=%s\nport=%s\nuser=%s\npassword=%s\nssl-ca=%s\nssl=0\n", db.dbHost, db.dbPort, db.dbUserName, db.dbPassword, db.caCertPath) + mysqlClientConfig := fmt.Sprintf("[client]\nhost=%s\nport=%s\nuser=%s\npassword=%s\nssl-ca=%s\nssl=%s\n", db.dbHost, db.dbPort, db.dbUserName, db.dbPassword, caCertPath, sslMode) if err := os.WriteFile(mysqlClientConfigFile, []byte(mysqlClientConfig), 0644); err != nil { return fmt.Errorf("failed to create mysql client config file: %v", err) } diff --git a/pkg/restore.go b/pkg/restore.go index 1dfba20..dd0c799 100644 --- a/pkg/restore.go +++ b/pkg/restore.go @@ -25,6 +25,7 @@ SOFTWARE. package pkg import ( + "fmt" "github.com/jkaninda/encryptor" "github.com/jkaninda/go-storage/pkg/local" "github.com/jkaninda/mysql-bkup/utils" @@ -114,10 +115,6 @@ func RestoreDatabase(db *dbConfig, conf *RestoreConfig) { } if utils.FileExists(filepath.Join(tmpPath, conf.file)) { - err := os.Setenv("MYSQL_PWD", db.dbPassword) - if err != nil { - return - } err = testDatabaseConnection(db) if err != nil { utils.Fatal("Error connecting to the database %v", err) @@ -125,12 +122,12 @@ func RestoreDatabase(db *dbConfig, conf *RestoreConfig) { utils.Info("Restoring database...") extension := filepath.Ext(filepath.Join(tmpPath, conf.file)) - // Restore from compressed file / .sql.gz + // Restore from a compressed file / .sql.gz if extension == ".gz" { - str := "zcat " + filepath.Join(tmpPath, conf.file) + " | mariadb -h " + db.dbHost + " -P " + db.dbPort + " -u " + db.dbUserName + " " + db.dbName - _, err := exec.Command("sh", "-c", str).Output() + str := "zcat " + filepath.Join(tmpPath, conf.file) + " | mariadb " + fmt.Sprintf("--defaults-file=%s", mysqlClientConfig) + " " + db.dbName + output, err := exec.Command("sh", "-c", str).Output() if err != nil { - utils.Fatal("Error, in restoring the database %v", err) + utils.Fatal("Error, in restoring the database %v output: %v", err, output) } utils.Info("Restoring database... done") utils.Info("Database has been restored") @@ -138,11 +135,11 @@ func RestoreDatabase(db *dbConfig, conf *RestoreConfig) { deleteTemp() } else if extension == ".sql" { - // Restore from sql file - str := "cat " + filepath.Join(tmpPath, conf.file) + " | mariadb -h " + db.dbHost + " -P " + db.dbPort + " -u " + db.dbUserName + " " + db.dbName - _, err := exec.Command("sh", "-c", str).Output() + // Restore from SQL file + str := "cat " + filepath.Join(tmpPath, conf.file) + " | mariadb " + fmt.Sprintf("--defaults-file=%s", mysqlClientConfig) + " " + db.dbName + output, err := exec.Command("sh", "-c", str).Output() if err != nil { - utils.Fatal("Error in restoring the database %v", err) + utils.Fatal("Error, in restoring the database %v output: %v", err, output) } utils.Info("Restoring database... done") utils.Info("Database has been restored") diff --git a/pkg/var.go b/pkg/var.go index 24a44df..4a15b1e 100644 --- a/pkg/var.go +++ b/pkg/var.go @@ -24,7 +24,10 @@ SOFTWARE. package pkg -import "time" +import ( + "path/filepath" + "time" +) const tmpPath = "/tmp/backup" const gpgHome = "/config/gnupg" @@ -43,6 +46,7 @@ var ( backupSize int64 = 0 startTime = time.Now() backupRescueMode = false + mysqlClientConfig = filepath.Join(tmpPath, "my.cnf") ) // dbHVars Required environment variables for database