fix: warning message when using MYSQL_PASSWORD env

This commit is contained in:
2025-03-12 14:13:21 +01:00
parent 489dfdf842
commit 0bc7497512
4 changed files with 25 additions and 23 deletions

View File

@@ -217,10 +217,7 @@ func BackupDatabase(db *dbConfig, backupFileName string, disableCompression bool
if disableCompression { if disableCompression {
// Execute mysqldump // Execute mysqldump
cmd := exec.Command("mysqldump", cmd := exec.Command("mysqldump",
"-h", db.dbHost, fmt.Sprintf("--defaults-file=%s", mysqlClientConfig), db.dbName,
"-P", db.dbPort,
"-u", db.dbUserName,
db.dbName,
) )
output, err := cmd.Output() output, err := cmd.Output()
if err != nil { if err != nil {
@@ -247,7 +244,7 @@ func BackupDatabase(db *dbConfig, backupFileName string, disableCompression bool
} else { } else {
// Execute mysqldump // 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() stdout, err := cmd.StdoutPipe()
if err != nil { if err != nil {
return fmt.Errorf("failed to backup database: %v output: %v", err, stdout) return fmt.Errorf("failed to backup database: %v output: %v", err, stdout)

View File

@@ -27,6 +27,7 @@ package pkg
import ( import (
"bytes" "bytes"
"fmt" "fmt"
goutils "github.com/jkaninda/go-utils"
"github.com/jkaninda/mysql-bkup/utils" "github.com/jkaninda/mysql-bkup/utils"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"os" "os"
@@ -67,16 +68,17 @@ func deleteTemp() {
// TestDatabaseConnection tests the database connection // TestDatabaseConnection tests the database connection
func testDatabaseConnection(db *dbConfig) error { func testDatabaseConnection(db *dbConfig) error {
// Set the MYSQL_PWD environment variable // Create the mysql client config file
if err := os.Setenv("MYSQL_PWD", db.dbPassword); err != nil { if err := createMysqlClientConfigFile(*db); err != nil {
return fmt.Errorf("failed to set MYSQL_PWD environment variable: %v", err) return fmt.Errorf(err.Error())
} }
utils.Info("Connecting to %s database ...", db.dbName) utils.Info("Connecting to %s database ...", db.dbName)
// Set database name for notification error // Set database name for notification error
utils.DatabaseName = db.dbName utils.DatabaseName = db.dbName
// Prepare the command to test the database connection // 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 // Capture the output
var out bytes.Buffer var out bytes.Buffer
cmd.Stdout = &out cmd.Stdout = &out
@@ -189,9 +191,11 @@ func RemoveLastExtension(filename string) string {
// Create mysql client config file // Create mysql client config file
func createMysqlClientConfigFile(db dbConfig) error { 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 // Create the mysql client config file
mysqlClientConfigFile := filepath.Join(tmpPath, "my.cnf") 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 { if err := os.WriteFile(mysqlClientConfigFile, []byte(mysqlClientConfig), 0644); err != nil {
return fmt.Errorf("failed to create mysql client config file: %v", err) return fmt.Errorf("failed to create mysql client config file: %v", err)
} }

View File

@@ -25,6 +25,7 @@ SOFTWARE.
package pkg package pkg
import ( import (
"fmt"
"github.com/jkaninda/encryptor" "github.com/jkaninda/encryptor"
"github.com/jkaninda/go-storage/pkg/local" "github.com/jkaninda/go-storage/pkg/local"
"github.com/jkaninda/mysql-bkup/utils" "github.com/jkaninda/mysql-bkup/utils"
@@ -114,10 +115,6 @@ func RestoreDatabase(db *dbConfig, conf *RestoreConfig) {
} }
if utils.FileExists(filepath.Join(tmpPath, conf.file)) { if utils.FileExists(filepath.Join(tmpPath, conf.file)) {
err := os.Setenv("MYSQL_PWD", db.dbPassword)
if err != nil {
return
}
err = testDatabaseConnection(db) err = testDatabaseConnection(db)
if err != nil { if err != nil {
utils.Fatal("Error connecting to the database %v", err) utils.Fatal("Error connecting to the database %v", err)
@@ -125,12 +122,12 @@ func RestoreDatabase(db *dbConfig, conf *RestoreConfig) {
utils.Info("Restoring database...") utils.Info("Restoring database...")
extension := filepath.Ext(filepath.Join(tmpPath, conf.file)) extension := filepath.Ext(filepath.Join(tmpPath, conf.file))
// Restore from compressed file / .sql.gz // Restore from a compressed file / .sql.gz
if extension == ".gz" { if extension == ".gz" {
str := "zcat " + filepath.Join(tmpPath, conf.file) + " | mariadb -h " + db.dbHost + " -P " + db.dbPort + " -u " + db.dbUserName + " " + db.dbName str := "zcat " + filepath.Join(tmpPath, conf.file) + " | mariadb " + fmt.Sprintf("--defaults-file=%s", mysqlClientConfig) + " " + db.dbName
_, err := exec.Command("sh", "-c", str).Output() output, err := exec.Command("sh", "-c", str).Output()
if err != nil { 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("Restoring database... done")
utils.Info("Database has been restored") utils.Info("Database has been restored")
@@ -138,11 +135,11 @@ func RestoreDatabase(db *dbConfig, conf *RestoreConfig) {
deleteTemp() deleteTemp()
} else if extension == ".sql" { } else if extension == ".sql" {
// Restore from sql file // Restore from SQL file
str := "cat " + filepath.Join(tmpPath, conf.file) + " | mariadb -h " + db.dbHost + " -P " + db.dbPort + " -u " + db.dbUserName + " " + db.dbName str := "cat " + filepath.Join(tmpPath, conf.file) + " | mariadb " + fmt.Sprintf("--defaults-file=%s", mysqlClientConfig) + " " + db.dbName
_, err := exec.Command("sh", "-c", str).Output() output, err := exec.Command("sh", "-c", str).Output()
if err != nil { 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("Restoring database... done")
utils.Info("Database has been restored") utils.Info("Database has been restored")

View File

@@ -24,7 +24,10 @@ SOFTWARE.
package pkg package pkg
import "time" import (
"path/filepath"
"time"
)
const tmpPath = "/tmp/backup" const tmpPath = "/tmp/backup"
const gpgHome = "/config/gnupg" const gpgHome = "/config/gnupg"
@@ -43,6 +46,7 @@ var (
backupSize int64 = 0 backupSize int64 = 0
startTime = time.Now() startTime = time.Now()
backupRescueMode = false backupRescueMode = false
mysqlClientConfig = filepath.Join(tmpPath, "my.cnf")
) )
// dbHVars Required environment variables for database // dbHVars Required environment variables for database