mirror of
https://github.com/jkaninda/mysql-bkup.git
synced 2025-12-06 13:39:41 +01:00
refactore: refactoring of code
This commit is contained in:
@@ -30,9 +30,6 @@ func Execute() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
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)")
|
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.mysql-bkup.yaml)")
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ var VersionCmd = &cobra.Command{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Version display application version
|
||||||
func Version() {
|
func Version() {
|
||||||
fmt.Printf("Version: %s \n", appVersion)
|
fmt.Printf("Version: %s \n", appVersion)
|
||||||
fmt.Print()
|
fmt.Print()
|
||||||
|
|||||||
2
main.go
2
main.go
@@ -155,6 +155,7 @@ func start() {
|
|||||||
utils.Info("Restore database from local")
|
utils.Info("Restore database from local")
|
||||||
pkg.RestoreDatabase(file)
|
pkg.RestoreDatabase(file)
|
||||||
} else {
|
} else {
|
||||||
|
//Restore from S3
|
||||||
utils.Info("Restore database from s3")
|
utils.Info("Restore database from s3")
|
||||||
s3Restore()
|
s3Restore()
|
||||||
}
|
}
|
||||||
@@ -163,6 +164,7 @@ func start() {
|
|||||||
utils.Info("Backup database to local storage")
|
utils.Info("Backup database to local storage")
|
||||||
pkg.BackupDatabase(disableCompression)
|
pkg.BackupDatabase(disableCompression)
|
||||||
} else {
|
} else {
|
||||||
|
//Backup to S3
|
||||||
utils.Info("Backup database to s3 storage")
|
utils.Info("Backup database to s3 storage")
|
||||||
s3Backup()
|
s3Backup()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,10 +37,14 @@ func BackupDatabase(disableCompression bool) {
|
|||||||
utils.TestDatabaseConnection()
|
utils.TestDatabaseConnection()
|
||||||
// Backup Database database
|
// Backup Database database
|
||||||
utils.Info("Backing up database...")
|
utils.Info("Backing up database...")
|
||||||
|
//Generate file name
|
||||||
bkFileName := fmt.Sprintf("%s_%s.sql.gz", dbName, time.Now().Format("20060102_150405"))
|
bkFileName := fmt.Sprintf("%s_%s.sql.gz", dbName, time.Now().Format("20060102_150405"))
|
||||||
|
|
||||||
|
// Verify is compression is disabled
|
||||||
if disableCompression {
|
if disableCompression {
|
||||||
|
//Generate file name
|
||||||
bkFileName = fmt.Sprintf("%s_%s.sql", dbName, time.Now().Format("20060102_150405"))
|
bkFileName = fmt.Sprintf("%s_%s.sql", dbName, time.Now().Format("20060102_150405"))
|
||||||
|
// Execute mysqldump
|
||||||
cmd := exec.Command("mysqldump",
|
cmd := exec.Command("mysqldump",
|
||||||
"-h", dbHost,
|
"-h", dbHost,
|
||||||
"-P", dbPort,
|
"-P", dbPort,
|
||||||
@@ -53,6 +57,7 @@ func BackupDatabase(disableCompression bool) {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// save output
|
||||||
file, err := os.Create(fmt.Sprintf("%s/%s", storagePath, bkFileName))
|
file, err := os.Create(fmt.Sprintf("%s/%s", storagePath, bkFileName))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
@@ -66,6 +71,7 @@ func BackupDatabase(disableCompression bool) {
|
|||||||
utils.Done("Database has been backed up")
|
utils.Done("Database has been backed up")
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
// Execute mysqldump
|
||||||
cmd := exec.Command("mysqldump", "-h", dbHost, "-P", dbPort, "-u", dbUserName, "--password="+dbPassword, dbName)
|
cmd := exec.Command("mysqldump", "-h", dbHost, "-P", dbPort, "-u", dbUserName, "--password="+dbPassword, dbName)
|
||||||
stdout, err := cmd.StdoutPipe()
|
stdout, err := cmd.StdoutPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ func RestoreDatabase(file string) {
|
|||||||
utils.TestDatabaseConnection()
|
utils.TestDatabaseConnection()
|
||||||
|
|
||||||
extension := filepath.Ext(fmt.Sprintf("%s/%s", storagePath, file))
|
extension := filepath.Ext(fmt.Sprintf("%s/%s", storagePath, file))
|
||||||
// GZ compressed file
|
// Restore from compressed file / .sql.gz
|
||||||
if extension == ".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")
|
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()
|
_, err := exec.Command("bash", "-c", str).Output()
|
||||||
@@ -36,7 +36,7 @@ func RestoreDatabase(file string) {
|
|||||||
utils.Done("Database has been restored")
|
utils.Done("Database has been restored")
|
||||||
|
|
||||||
} else if extension == ".sql" {
|
} 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")
|
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()
|
_, err := exec.Command("bash", "-c", str).Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -44,6 +44,8 @@ func MountS3Storage(s3Path string) {
|
|||||||
}
|
}
|
||||||
//Change file permission
|
//Change file permission
|
||||||
utils.ChangePermission(s3fsPasswdFile, 0600)
|
utils.ChangePermission(s3fsPasswdFile, 0600)
|
||||||
|
|
||||||
|
//Mount object storage
|
||||||
utils.Info("Mounting Object storage in", s3MountPath)
|
utils.Info("Mounting Object storage in", s3MountPath)
|
||||||
if isEmpty, _ := utils.IsDirEmpty(s3MountPath); isEmpty {
|
if isEmpty, _ := utils.IsDirEmpty(s3MountPath); isEmpty {
|
||||||
cmd := exec.Command("s3fs", bucketName, s3MountPath,
|
cmd := exec.Command("s3fs", bucketName, s3MountPath,
|
||||||
|
|||||||
@@ -19,10 +19,6 @@ func Info(v ...any) {
|
|||||||
func Done(v ...any) {
|
func Done(v ...any) {
|
||||||
fmt.Println("✔ ", fmt.Sprint(v...))
|
fmt.Println("✔ ", fmt.Sprint(v...))
|
||||||
}
|
}
|
||||||
func Warning(v ...any) {
|
|
||||||
fmt.Println("[Warning: ] ", fmt.Sprint(v...))
|
|
||||||
}
|
|
||||||
|
|
||||||
func Fatal(v ...any) {
|
func Fatal(v ...any) {
|
||||||
fmt.Println("✘ ", fmt.Sprint(v...))
|
fmt.Println("✘ ", fmt.Sprint(v...))
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|||||||
Reference in New Issue
Block a user