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{
|
var rootCmd = &cobra.Command{
|
||||||
Use: "mysql-bkup",
|
Use: "mysql-bkup",
|
||||||
Short: "MySQL Backup tool, backup database to S3 or Object Storage",
|
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
|
// Uncomment the following line if your bare application
|
||||||
// has an action associated with it:
|
// has an action associated with it:
|
||||||
// Run: func(cmd *cobra.Command, args []string) { },
|
// Run: func(cmd *cobra.Command, args []string) { },
|
||||||
@@ -50,5 +50,5 @@ func init() {
|
|||||||
rootCmd.PersistentFlags().IntP("port", "p", 3306, "Set database port")
|
rootCmd.PersistentFlags().IntP("port", "p", 3306, "Set database port")
|
||||||
rootCmd.PersistentFlags().BoolP("help", "h", false, "Print this help message")
|
rootCmd.PersistentFlags().BoolP("help", "h", false, "Print this help message")
|
||||||
rootCmd.PersistentFlags().BoolP("version", "v", false, "shows version information")
|
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()
|
||||||
|
}
|
||||||
26
main.go
26
main.go
@@ -1,13 +1,14 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
/*****
|
/*****
|
||||||
* MySQL Backup & Restore
|
* MySQL Backup & Restore
|
||||||
* @author Jonas Kaninda
|
* @author Jonas Kaninda
|
||||||
* @license MIT License <https://opensource.org/licenses/MIT>
|
* @license MIT License <https://opensource.org/licenses/MIT>
|
||||||
* @link https://github.com/jkaninda/mysql-bkup
|
* @link https://github.com/jkaninda/mysql-bkup
|
||||||
**/
|
**/
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/jkaninda/mysql-bkup/cmd"
|
||||||
"github.com/jkaninda/mysql-bkup/pkg"
|
"github.com/jkaninda/mysql-bkup/pkg"
|
||||||
"github.com/jkaninda/mysql-bkup/utils"
|
"github.com/jkaninda/mysql-bkup/utils"
|
||||||
flag "github.com/spf13/pflag"
|
flag "github.com/spf13/pflag"
|
||||||
@@ -15,8 +16,6 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
)
|
)
|
||||||
|
|
||||||
var appVersion string = os.Getenv("VERSION")
|
|
||||||
|
|
||||||
const s3MountPath string = "/s3mnt"
|
const s3MountPath string = "/s3mnt"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -72,10 +71,10 @@ func init() {
|
|||||||
disableCompression = *disableCompressionFlag
|
disableCompression = *disableCompressionFlag
|
||||||
|
|
||||||
flag.Usage = func() {
|
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("Usage: bkup --operation backup -storage s3 --dbname databasename --path /my_path ...\n")
|
||||||
fmt.Print(" bkup -o backup -d databasename --disable-compression ...\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()
|
flag.PrintDefaults()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,7 +85,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
if *versionFlag {
|
if *versionFlag {
|
||||||
startBackup = false
|
startBackup = false
|
||||||
version()
|
cmd.Version()
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
if *dbnameFlag != "" {
|
if *dbnameFlag != "" {
|
||||||
@@ -137,13 +136,7 @@ func init() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func version() {
|
|
||||||
fmt.Printf("Version: %s \n", appVersion)
|
|
||||||
fmt.Print()
|
|
||||||
}
|
|
||||||
func main() {
|
func main() {
|
||||||
//cmd.Execute()
|
|
||||||
|
|
||||||
err := os.Setenv("STORAGE_PATH", storagePath)
|
err := os.Setenv("STORAGE_PATH", storagePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@@ -159,18 +152,18 @@ func start() {
|
|||||||
if executionMode == "default" {
|
if executionMode == "default" {
|
||||||
if operation != "backup" {
|
if operation != "backup" {
|
||||||
if storage != "s3" {
|
if storage != "s3" {
|
||||||
utils.Info("RestoreDatabase from local")
|
utils.Info("Restore database from local")
|
||||||
pkg.RestoreDatabase(file)
|
pkg.RestoreDatabase(file)
|
||||||
} else {
|
} else {
|
||||||
utils.Info("RestoreDatabase from s3")
|
utils.Info("Restore database from s3")
|
||||||
s3Restore()
|
s3Restore()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if storage != "s3" {
|
if storage != "s3" {
|
||||||
utils.Info("BackupDatabase to local storage")
|
utils.Info("Backup database to local storage")
|
||||||
pkg.BackupDatabase(disableCompression)
|
pkg.BackupDatabase(disableCompression)
|
||||||
} else {
|
} else {
|
||||||
utils.Info("BackupDatabase to s3 storage")
|
utils.Info("Backup database to s3 storage")
|
||||||
s3Backup()
|
s3Backup()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -206,6 +199,7 @@ func scheduledMode() {
|
|||||||
utils.Info("Creating backup job...")
|
utils.Info("Creating backup job...")
|
||||||
pkg.CreateCrontabScript(disableCompression, storage)
|
pkg.CreateCrontabScript(disableCompression, storage)
|
||||||
|
|
||||||
|
//Start Supervisor
|
||||||
supervisordCmd := exec.Command("supervisord", "-c", "/etc/supervisor/supervisord.conf")
|
supervisordCmd := exec.Command("supervisord", "-c", "/etc/supervisor/supervisord.conf")
|
||||||
if err := supervisordCmd.Run(); err != nil {
|
if err := supervisordCmd.Run(); err != nil {
|
||||||
utils.Fatalf("Error starting supervisord: %v\n", err)
|
utils.Fatalf("Error starting supervisord: %v\n", err)
|
||||||
|
|||||||
@@ -63,14 +63,12 @@ func BackupDatabase(disableCompression bool) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
utils.Info("Database has been backed up")
|
utils.Done("Database has been backed up")
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
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 {
|
||||||
utils.Info("Mysql")
|
|
||||||
|
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
gzipCmd := exec.Command("gzip")
|
gzipCmd := exec.Command("gzip")
|
||||||
@@ -86,7 +84,7 @@ func BackupDatabase(disableCompression bool) {
|
|||||||
if err := gzipCmd.Wait(); err != nil {
|
if err := gzipCmd.Wait(); err != nil {
|
||||||
log.Fatal(err)
|
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.Fatal("Error, in restoring the database")
|
||||||
}
|
}
|
||||||
|
|
||||||
utils.Info("Database has been restored")
|
utils.Done("Database has been restored")
|
||||||
|
|
||||||
} else if extension == ".sql" {
|
} else if extension == ".sql" {
|
||||||
//SQL file
|
//SQL file
|
||||||
@@ -43,7 +43,7 @@ func RestoreDatabase(file string) {
|
|||||||
utils.Fatal("Error, in restoring the database", err)
|
utils.Fatal("Error, in restoring the database", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
utils.Info("Database has been restored")
|
utils.Done("Database has been restored")
|
||||||
} else {
|
} else {
|
||||||
utils.Fatal("Unknown file extension ", extension)
|
utils.Fatal("Unknown file extension ", extension)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,23 +14,21 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Info(v ...any) {
|
func Info(v ...any) {
|
||||||
fmt.Println("[INFO] ", fmt.Sprint(v...))
|
fmt.Println("⒤ ", fmt.Sprint(v...))
|
||||||
}
|
}
|
||||||
func Infof(msg string, v ...any) {
|
func Done(v ...any) {
|
||||||
fmt.Printf("[INFO] "+msg, v...)
|
fmt.Println("✔ ", fmt.Sprint(v...))
|
||||||
}
|
}
|
||||||
func Warning(message string) {
|
func Warning(v ...any) {
|
||||||
fmt.Println("[WARNING]", message)
|
fmt.Println("[Warning: ] ", fmt.Sprint(v...))
|
||||||
}
|
|
||||||
func Warningf(msg string, v ...any) {
|
|
||||||
fmt.Printf("[WARNING] "+msg, v...)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Fatal(v ...any) {
|
func Fatal(v ...any) {
|
||||||
fmt.Println("[ERROR] ", fmt.Sprint(v...))
|
fmt.Println("✘ ", fmt.Sprint(v...))
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
func Fatalf(msg string, v ...any) {
|
func Fatalf(msg string, v ...any) {
|
||||||
fmt.Printf("[ERROR] "+msg, v...)
|
fmt.Printf("✘ "+msg, v...)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user