refactore: refactoring of code

This commit is contained in:
2024-01-19 14:29:37 +01:00
parent aaad8a010c
commit 2ae78fec57
7 changed files with 13 additions and 9 deletions

View File

@@ -30,9 +30,6 @@ func Execute() {
}
func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.mysql-bkup.yaml)")

View File

@@ -16,6 +16,7 @@ var VersionCmd = &cobra.Command{
},
}
// Version display application version
func Version() {
fmt.Printf("Version: %s \n", appVersion)
fmt.Print()

View File

@@ -155,6 +155,7 @@ func start() {
utils.Info("Restore database from local")
pkg.RestoreDatabase(file)
} else {
//Restore from S3
utils.Info("Restore database from s3")
s3Restore()
}
@@ -163,6 +164,7 @@ func start() {
utils.Info("Backup database to local storage")
pkg.BackupDatabase(disableCompression)
} else {
//Backup to S3
utils.Info("Backup database to s3 storage")
s3Backup()
}

View File

@@ -37,10 +37,14 @@ func BackupDatabase(disableCompression bool) {
utils.TestDatabaseConnection()
// Backup Database database
utils.Info("Backing up database...")
//Generate file name
bkFileName := fmt.Sprintf("%s_%s.sql.gz", dbName, time.Now().Format("20060102_150405"))
// Verify is compression is disabled
if disableCompression {
//Generate file name
bkFileName = fmt.Sprintf("%s_%s.sql", dbName, time.Now().Format("20060102_150405"))
// Execute mysqldump
cmd := exec.Command("mysqldump",
"-h", dbHost,
"-P", dbPort,
@@ -53,6 +57,7 @@ func BackupDatabase(disableCompression bool) {
log.Fatal(err)
}
// save output
file, err := os.Create(fmt.Sprintf("%s/%s", storagePath, bkFileName))
if err != nil {
log.Fatal(err)
@@ -66,6 +71,7 @@ func BackupDatabase(disableCompression bool) {
utils.Done("Database has been backed up")
} else {
// Execute mysqldump
cmd := exec.Command("mysqldump", "-h", dbHost, "-P", dbPort, "-u", dbUserName, "--password="+dbPassword, dbName)
stdout, err := cmd.StdoutPipe()
if err != nil {

View File

@@ -25,7 +25,7 @@ func RestoreDatabase(file string) {
utils.TestDatabaseConnection()
extension := filepath.Ext(fmt.Sprintf("%s/%s", storagePath, file))
// GZ compressed file
// Restore from compressed file / .sql.gz
if extension == ".gz" {
str := "zcat " + fmt.Sprintf("%s/%s", storagePath, file) + " | mysql -h " + os.Getenv("DB_HOST") + " -P " + os.Getenv("DB_PORT") + " -u " + os.Getenv("DB_USERNAME") + " --password=" + os.Getenv("DB_PASSWORD") + " " + os.Getenv("DB_NAME")
_, err := exec.Command("bash", "-c", str).Output()
@@ -36,7 +36,7 @@ func RestoreDatabase(file string) {
utils.Done("Database has been restored")
} else if extension == ".sql" {
//SQL file
//Restore from sql file
str := "cat " + fmt.Sprintf("%s/%s", storagePath, file) + " | mysql -h " + os.Getenv("DB_HOST") + " -P " + os.Getenv("DB_PORT") + " -u " + os.Getenv("DB_USERNAME") + " --password=" + os.Getenv("DB_PASSWORD") + " " + os.Getenv("DB_NAME")
_, err := exec.Command("bash", "-c", str).Output()
if err != nil {

View File

@@ -44,6 +44,8 @@ func MountS3Storage(s3Path string) {
}
//Change file permission
utils.ChangePermission(s3fsPasswdFile, 0600)
//Mount object storage
utils.Info("Mounting Object storage in", s3MountPath)
if isEmpty, _ := utils.IsDirEmpty(s3MountPath); isEmpty {
cmd := exec.Command("s3fs", bucketName, s3MountPath,

View File

@@ -19,10 +19,6 @@ func Info(v ...any) {
func Done(v ...any) {
fmt.Println("✔ ", fmt.Sprint(v...))
}
func Warning(v ...any) {
fmt.Println("[Warning: ] ", fmt.Sprint(v...))
}
func Fatal(v ...any) {
fmt.Println("✘ ", fmt.Sprint(v...))
os.Exit(1)