diff --git a/pkg/backup.go b/pkg/backup.go index f0a4144..282a9f8 100644 --- a/pkg/backup.go +++ b/pkg/backup.go @@ -26,6 +26,7 @@ SOFTWARE. package pkg import ( + "errors" "fmt" "github.com/jkaninda/encryptor" "github.com/jkaninda/go-storage/pkg/local" @@ -207,7 +208,7 @@ func BackupDatabase(db *dbConfig, backupFileName string, disableCompression bool } err = testDatabaseConnection(db) if err != nil { - return fmt.Errorf("failed to connect to the database: %v", err) + return errors.New(err.Error()) } // Backup Database database utils.Info("Backing up database...") @@ -223,7 +224,7 @@ func BackupDatabase(db *dbConfig, backupFileName string, disableCompression bool ) output, err := cmd.Output() if err != nil { - return fmt.Errorf("failed to backup database: %v", err) + return fmt.Errorf("failed to backup database: %v output: %v", err, output) } // save output @@ -249,7 +250,7 @@ func BackupDatabase(db *dbConfig, backupFileName string, disableCompression bool cmd := exec.Command("mysqldump", "-h", db.dbHost, "-P", db.dbPort, "-u", db.dbUserName, db.dbName) stdout, err := cmd.StdoutPipe() if err != nil { - return fmt.Errorf("failed to backup database: %v", err) + return fmt.Errorf("failed to backup database: %v output: %v", err, stdout) } gzipCmd := exec.Command("gzip") gzipCmd.Stdin = stdout @@ -358,8 +359,9 @@ func recoverMode(err error, msg string) { utils.Error("Backup rescue mode is enabled") utils.Error("Backup will continue") } else { - utils.Error("Error: %s", msg) - utils.Fatal("Error: %v", err) + utils.Error("Error 10: %s", msg) + utils.Fatal("Error 10: %v", err) + return } } diff --git a/pkg/helper.go b/pkg/helper.go index 4ca5030..98efe7c 100644 --- a/pkg/helper.go +++ b/pkg/helper.go @@ -65,25 +65,28 @@ func deleteTemp() { } } -// TestDatabaseConnection tests the database connection +// TestDatabaseConnection tests the database connection func testDatabaseConnection(db *dbConfig) error { - err := os.Setenv("MYSQL_PWD", db.dbPassword) - if err != nil { + // 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) } 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") // Capture the output var out bytes.Buffer cmd.Stdout = &out cmd.Stderr = &out - err = cmd.Run() - if err != nil { - return fmt.Errorf("failed to connect to %s database: %v", db.dbName, err) + // Run the command + if err := cmd.Run(); err != nil { + return fmt.Errorf("failed to connect to database %s: %v, output: %s", db.dbName, err, out.String()) } + utils.Info("Successfully connected to %s database", db.dbName) return nil } @@ -183,3 +186,14 @@ func RemoveLastExtension(filename string) string { } return filename } + +// Create mysql client config file +func createMysqlClientConfigFile(db dbConfig) error { + // 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) + if err := os.WriteFile(mysqlClientConfigFile, []byte(mysqlClientConfig), 0644); err != nil { + return fmt.Errorf("failed to create mysql client config file: %v", err) + } + return nil +}