mirror of
https://github.com/jkaninda/mysql-bkup.git
synced 2025-12-06 21:49:40 +01:00
refactor: refactoring of code
This commit is contained in:
22
main.go
22
main.go
@@ -72,10 +72,10 @@ func init() {
|
|||||||
disableCompression = *disableCompressionFlag
|
disableCompression = *disableCompressionFlag
|
||||||
|
|
||||||
flag.Usage = func() {
|
flag.Usage = func() {
|
||||||
fmt.Print("MySQL Backup and Restoration tool. Backup database to AWS S3 storage or any S3 Alternatives for Object Storage.\n\n")
|
fmt.Print("MySQL BackupDatabase and Restoration tool. BackupDatabase 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(" Restore: bkup -o restore -d databasename -f db_20231217_051339.sql.gz ...\n\n")
|
fmt.Print(" RestoreDatabase: bkup -o restore -d databasename -f db_20231217_051339.sql.gz ...\n\n")
|
||||||
flag.PrintDefaults()
|
flag.PrintDefaults()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -159,18 +159,18 @@ func start() {
|
|||||||
if executionMode == "default" {
|
if executionMode == "default" {
|
||||||
if operation != "backup" {
|
if operation != "backup" {
|
||||||
if storage != "s3" {
|
if storage != "s3" {
|
||||||
utils.Info("Restore from local")
|
utils.Info("RestoreDatabase from local")
|
||||||
pkg.Restore(file)
|
pkg.RestoreDatabase(file)
|
||||||
} else {
|
} else {
|
||||||
utils.Info("Restore from s3")
|
utils.Info("RestoreDatabase from s3")
|
||||||
s3Restore()
|
s3Restore()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if storage != "s3" {
|
if storage != "s3" {
|
||||||
utils.Info("Backup to local storage")
|
utils.Info("BackupDatabase to local storage")
|
||||||
pkg.Backup(disableCompression)
|
pkg.BackupDatabase(disableCompression)
|
||||||
} else {
|
} else {
|
||||||
utils.Info("Backup to s3 storage")
|
utils.Info("BackupDatabase to s3 storage")
|
||||||
s3Backup()
|
s3Backup()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -182,9 +182,9 @@ func start() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func s3Backup() {
|
func s3Backup() {
|
||||||
// Backup to S3 storage
|
// Backup Database to S3 storage
|
||||||
pkg.MountS3Storage(s3Path)
|
pkg.MountS3Storage(s3Path)
|
||||||
pkg.Backup(disableCompression)
|
pkg.BackupDatabase(disableCompression)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run in scheduled mode
|
// Run in scheduled mode
|
||||||
@@ -218,5 +218,5 @@ func scheduledMode() {
|
|||||||
func s3Restore() {
|
func s3Restore() {
|
||||||
// Restore database from S3
|
// Restore database from S3
|
||||||
pkg.MountS3Storage(s3Path)
|
pkg.MountS3Storage(s3Path)
|
||||||
pkg.Restore(file)
|
pkg.RestoreDatabase(file)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,8 +22,8 @@ var (
|
|||||||
storagePath = "/backup"
|
storagePath = "/backup"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Backup backup database
|
// BackupDatabase backup database
|
||||||
func Backup(disableCompression bool) {
|
func BackupDatabase(disableCompression bool) {
|
||||||
dbHost = os.Getenv("DB_HOST")
|
dbHost = os.Getenv("DB_HOST")
|
||||||
dbPassword = os.Getenv("DB_PASSWORD")
|
dbPassword = os.Getenv("DB_PASSWORD")
|
||||||
dbUserName = os.Getenv("DB_USERNAME")
|
dbUserName = os.Getenv("DB_USERNAME")
|
||||||
@@ -35,7 +35,7 @@ func Backup(disableCompression bool) {
|
|||||||
utils.Fatal("Please make sure all required environment variables for database are set")
|
utils.Fatal("Please make sure all required environment variables for database are set")
|
||||||
} else {
|
} else {
|
||||||
utils.TestDatabaseConnection()
|
utils.TestDatabaseConnection()
|
||||||
// Backup database
|
// Backup Database database
|
||||||
utils.Info("Backing up database...")
|
utils.Info("Backing up database...")
|
||||||
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"))
|
||||||
|
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Restore restore database
|
// RestoreDatabase restore database
|
||||||
func Restore(file string) {
|
func RestoreDatabase(file string) {
|
||||||
dbHost = os.Getenv("DB_HOST")
|
dbHost = os.Getenv("DB_HOST")
|
||||||
dbPassword = os.Getenv("DB_PASSWORD")
|
dbPassword = os.Getenv("DB_PASSWORD")
|
||||||
dbUserName = os.Getenv("DB_USERNAME")
|
dbUserName = os.Getenv("DB_USERNAME")
|
||||||
|
|||||||
@@ -21,15 +21,13 @@ var (
|
|||||||
s3Endpoint = ""
|
s3Endpoint = ""
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
// MountS3Storage Mount s3 storage using s3fs
|
||||||
|
func MountS3Storage(s3Path string) {
|
||||||
accessKey = os.Getenv("ACCESS_KEY")
|
accessKey = os.Getenv("ACCESS_KEY")
|
||||||
secretKey = os.Getenv("SECRET_KEY")
|
secretKey = os.Getenv("SECRET_KEY")
|
||||||
bucketName = os.Getenv("BUCKETNAME")
|
bucketName = os.Getenv("BUCKETNAME")
|
||||||
s3Endpoint = os.Getenv("S3_ENDPOINT")
|
s3Endpoint = os.Getenv("S3_ENDPOINT")
|
||||||
}
|
|
||||||
|
|
||||||
// MountS3Storage Mount s3 storage using s3fs
|
|
||||||
func MountS3Storage(s3Path string) {
|
|
||||||
if accessKey == "" || secretKey == "" || bucketName == "" {
|
if accessKey == "" || secretKey == "" || bucketName == "" {
|
||||||
utils.Fatal("Please make sure all environment variables are set")
|
utils.Fatal("Please make sure all environment variables are set")
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -11,14 +11,14 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
const cronLogFile = "/var/log/mysql-bkup.log"
|
||||||
|
const backupCronFile = "/usr/local/bin/backup_cron.sh"
|
||||||
|
|
||||||
}
|
|
||||||
func CreateCrontabScript(disableCompression bool, storage string) {
|
func CreateCrontabScript(disableCompression bool, storage string) {
|
||||||
task := "/usr/local/bin/backup_cron.sh"
|
//task := "/usr/local/bin/backup_cron.sh"
|
||||||
touchCmd := exec.Command("touch", task)
|
touchCmd := exec.Command("touch", backupCronFile)
|
||||||
if err := touchCmd.Run(); err != nil {
|
if err := touchCmd.Run(); err != nil {
|
||||||
utils.Fatalf("Error creating file %s: %v\n", task, err)
|
utils.Fatalf("Error creating file %s: %v\n", backupCronFile, err)
|
||||||
}
|
}
|
||||||
var disableC = ""
|
var disableC = ""
|
||||||
if disableCompression {
|
if disableCompression {
|
||||||
@@ -39,13 +39,13 @@ bkup --operation backup --dbname %s --port %s %v
|
|||||||
`, os.Getenv("DB_NAME"), os.Getenv("DB_PORT"), disableC)
|
`, os.Getenv("DB_NAME"), os.Getenv("DB_PORT"), disableC)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := utils.WriteToFile(task, scriptContent); err != nil {
|
if err := utils.WriteToFile(backupCronFile, scriptContent); err != nil {
|
||||||
utils.Fatalf("Error writing to %s: %v\n", task, err)
|
utils.Fatalf("Error writing to %s: %v\n", backupCronFile, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
chmodCmd := exec.Command("chmod", "+x", "/usr/local/bin/backup_cron.sh")
|
chmodCmd := exec.Command("chmod", "+x", "/usr/local/bin/backup_cron.sh")
|
||||||
if err := chmodCmd.Run(); err != nil {
|
if err := chmodCmd.Run(); err != nil {
|
||||||
utils.Fatalf("Error changing permissions of %s: %v\n", task, err)
|
utils.Fatalf("Error changing permissions of %s: %v\n", backupCronFile, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
lnCmd := exec.Command("ln", "-s", "/usr/local/bin/backup_cron.sh", "/usr/local/bin/backup_cron")
|
lnCmd := exec.Command("ln", "-s", "/usr/local/bin/backup_cron.sh", "/usr/local/bin/backup_cron")
|
||||||
@@ -60,8 +60,8 @@ bkup --operation backup --dbname %s --port %s %v
|
|||||||
utils.Fatalf("Error creating file %s: %v\n", cronJob, err)
|
utils.Fatalf("Error creating file %s: %v\n", cronJob, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cronContent := fmt.Sprintf(`%s root exec /bin/bash -c ". /run/supervisord.env; /usr/local/bin/backup_cron.sh >> /var/log/mysql-bkup.log"
|
cronContent := fmt.Sprintf(`%s root exec /bin/bash -c ". /run/supervisord.env; /usr/local/bin/backup_cron.sh >> %s"
|
||||||
`, os.Getenv("SCHEDULE_PERIOD"))
|
`, os.Getenv("SCHEDULE_PERIOD"), cronLogFile)
|
||||||
|
|
||||||
if err := utils.WriteToFile(cronJob, cronContent); err != nil {
|
if err := utils.WriteToFile(cronJob, cronContent); err != nil {
|
||||||
utils.Fatalf("Error writing to %s: %v\n", cronJob, err)
|
utils.Fatalf("Error writing to %s: %v\n", cronJob, err)
|
||||||
|
|||||||
Reference in New Issue
Block a user