mirror of
https://github.com/jkaninda/mysql-bkup.git
synced 2025-12-06 13:39:41 +01:00
chore: clean up code
This commit is contained in:
@@ -14,7 +14,7 @@ import (
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "mysql-bkup",
|
||||
Short: "MySQL Backup tool, backup database to S3 or Object Storage",
|
||||
Long: `MySQL Backup and Restoration tool. Backup database to AWS S3 storage or any S3 Alternatives for Object Storage.`,
|
||||
Long: `MySQL Database backup and restoration tool. Backup database to AWS S3 storage or any S3 Alternatives for Object Storage.`,
|
||||
// Uncomment the following line if your bare application
|
||||
// has an action associated with it:
|
||||
// Run: func(cmd *cobra.Command, args []string) { },
|
||||
@@ -50,5 +50,5 @@ func init() {
|
||||
rootCmd.PersistentFlags().IntP("port", "p", 3306, "Set database port")
|
||||
rootCmd.PersistentFlags().BoolP("help", "h", false, "Print this help message")
|
||||
rootCmd.PersistentFlags().BoolP("version", "v", false, "shows version information")
|
||||
|
||||
rootCmd.AddCommand(VersionCmd)
|
||||
}
|
||||
|
||||
22
cmd/version.go
Normal file
22
cmd/version.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
"os"
|
||||
)
|
||||
|
||||
var appVersion = os.Getenv("VERSION")
|
||||
|
||||
var VersionCmd = &cobra.Command{
|
||||
Use: "version",
|
||||
Short: "Show version",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
Version()
|
||||
},
|
||||
}
|
||||
|
||||
func Version() {
|
||||
fmt.Printf("Version: %s \n", appVersion)
|
||||
fmt.Print()
|
||||
}
|
||||
24
main.go
24
main.go
@@ -8,6 +8,7 @@ package main
|
||||
**/
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/jkaninda/mysql-bkup/cmd"
|
||||
"github.com/jkaninda/mysql-bkup/pkg"
|
||||
"github.com/jkaninda/mysql-bkup/utils"
|
||||
flag "github.com/spf13/pflag"
|
||||
@@ -15,8 +16,6 @@ import (
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
var appVersion string = os.Getenv("VERSION")
|
||||
|
||||
const s3MountPath string = "/s3mnt"
|
||||
|
||||
var (
|
||||
@@ -72,10 +71,10 @@ func init() {
|
||||
disableCompression = *disableCompressionFlag
|
||||
|
||||
flag.Usage = func() {
|
||||
fmt.Print("MySQL BackupDatabase and Restoration tool. BackupDatabase database to AWS S3 storage or any S3 Alternatives for Object Storage.\n\n")
|
||||
fmt.Print("MySQL Database backup and restoration tool. Backup database to AWS S3 storage or any S3 Alternatives for Object Storage.\n\n")
|
||||
fmt.Print("Usage: bkup --operation backup -storage s3 --dbname databasename --path /my_path ...\n")
|
||||
fmt.Print(" bkup -o backup -d databasename --disable-compression ...\n")
|
||||
fmt.Print(" RestoreDatabase: bkup -o restore -d databasename -f db_20231217_051339.sql.gz ...\n\n")
|
||||
fmt.Print(" Restore: bkup -o restore -d databasename -f db_20231217_051339.sql.gz ...\n\n")
|
||||
flag.PrintDefaults()
|
||||
}
|
||||
|
||||
@@ -86,7 +85,7 @@ func init() {
|
||||
}
|
||||
if *versionFlag {
|
||||
startBackup = false
|
||||
version()
|
||||
cmd.Version()
|
||||
os.Exit(0)
|
||||
}
|
||||
if *dbnameFlag != "" {
|
||||
@@ -137,13 +136,7 @@ func init() {
|
||||
|
||||
}
|
||||
|
||||
func version() {
|
||||
fmt.Printf("Version: %s \n", appVersion)
|
||||
fmt.Print()
|
||||
}
|
||||
func main() {
|
||||
//cmd.Execute()
|
||||
|
||||
err := os.Setenv("STORAGE_PATH", storagePath)
|
||||
if err != nil {
|
||||
return
|
||||
@@ -159,18 +152,18 @@ func start() {
|
||||
if executionMode == "default" {
|
||||
if operation != "backup" {
|
||||
if storage != "s3" {
|
||||
utils.Info("RestoreDatabase from local")
|
||||
utils.Info("Restore database from local")
|
||||
pkg.RestoreDatabase(file)
|
||||
} else {
|
||||
utils.Info("RestoreDatabase from s3")
|
||||
utils.Info("Restore database from s3")
|
||||
s3Restore()
|
||||
}
|
||||
} else {
|
||||
if storage != "s3" {
|
||||
utils.Info("BackupDatabase to local storage")
|
||||
utils.Info("Backup database to local storage")
|
||||
pkg.BackupDatabase(disableCompression)
|
||||
} else {
|
||||
utils.Info("BackupDatabase to s3 storage")
|
||||
utils.Info("Backup database to s3 storage")
|
||||
s3Backup()
|
||||
}
|
||||
}
|
||||
@@ -206,6 +199,7 @@ func scheduledMode() {
|
||||
utils.Info("Creating backup job...")
|
||||
pkg.CreateCrontabScript(disableCompression, storage)
|
||||
|
||||
//Start Supervisor
|
||||
supervisordCmd := exec.Command("supervisord", "-c", "/etc/supervisor/supervisord.conf")
|
||||
if err := supervisordCmd.Run(); err != nil {
|
||||
utils.Fatalf("Error starting supervisord: %v\n", err)
|
||||
|
||||
@@ -63,14 +63,12 @@ func BackupDatabase(disableCompression bool) {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
utils.Info("Database has been backed up")
|
||||
utils.Done("Database has been backed up")
|
||||
|
||||
} else {
|
||||
cmd := exec.Command("mysqldump", "-h", dbHost, "-P", dbPort, "-u", dbUserName, "--password="+dbPassword, dbName)
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
utils.Info("Mysql")
|
||||
|
||||
log.Fatal(err)
|
||||
}
|
||||
gzipCmd := exec.Command("gzip")
|
||||
@@ -86,7 +84,7 @@ func BackupDatabase(disableCompression bool) {
|
||||
if err := gzipCmd.Wait(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
utils.Info("Database has been backed up")
|
||||
utils.Done("Database has been backed up")
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ func RestoreDatabase(file string) {
|
||||
utils.Fatal("Error, in restoring the database")
|
||||
}
|
||||
|
||||
utils.Info("Database has been restored")
|
||||
utils.Done("Database has been restored")
|
||||
|
||||
} else if extension == ".sql" {
|
||||
//SQL file
|
||||
@@ -43,7 +43,7 @@ func RestoreDatabase(file string) {
|
||||
utils.Fatal("Error, in restoring the database", err)
|
||||
}
|
||||
|
||||
utils.Info("Database has been restored")
|
||||
utils.Done("Database has been restored")
|
||||
} else {
|
||||
utils.Fatal("Unknown file extension ", extension)
|
||||
}
|
||||
|
||||
@@ -14,23 +14,21 @@ import (
|
||||
)
|
||||
|
||||
func Info(v ...any) {
|
||||
fmt.Println("[INFO] ", fmt.Sprint(v...))
|
||||
fmt.Println("⒤ ", fmt.Sprint(v...))
|
||||
}
|
||||
func Infof(msg string, v ...any) {
|
||||
fmt.Printf("[INFO] "+msg, v...)
|
||||
func Done(v ...any) {
|
||||
fmt.Println("✔ ", fmt.Sprint(v...))
|
||||
}
|
||||
func Warning(message string) {
|
||||
fmt.Println("[WARNING]", message)
|
||||
}
|
||||
func Warningf(msg string, v ...any) {
|
||||
fmt.Printf("[WARNING] "+msg, v...)
|
||||
func Warning(v ...any) {
|
||||
fmt.Println("[Warning: ] ", fmt.Sprint(v...))
|
||||
}
|
||||
|
||||
func Fatal(v ...any) {
|
||||
fmt.Println("[ERROR] ", fmt.Sprint(v...))
|
||||
fmt.Println("✘ ", fmt.Sprint(v...))
|
||||
os.Exit(1)
|
||||
}
|
||||
func Fatalf(msg string, v ...any) {
|
||||
fmt.Printf("[ERROR] "+msg, v...)
|
||||
fmt.Printf("✘ "+msg, v...)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user