Merge pull request #40 from jkaninda/develop

refactor: refactoring of code
This commit is contained in:
2024-01-19 06:57:00 +01:00
committed by GitHub
5 changed files with 28 additions and 30 deletions

22
main.go
View File

@@ -72,10 +72,10 @@ func init() {
disableCompression = *disableCompressionFlag
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(" 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()
}
@@ -159,18 +159,18 @@ func start() {
if executionMode == "default" {
if operation != "backup" {
if storage != "s3" {
utils.Info("Restore from local")
pkg.Restore(file)
utils.Info("RestoreDatabase from local")
pkg.RestoreDatabase(file)
} else {
utils.Info("Restore from s3")
utils.Info("RestoreDatabase from s3")
s3Restore()
}
} else {
if storage != "s3" {
utils.Info("Backup to local storage")
pkg.Backup(disableCompression)
utils.Info("BackupDatabase to local storage")
pkg.BackupDatabase(disableCompression)
} else {
utils.Info("Backup to s3 storage")
utils.Info("BackupDatabase to s3 storage")
s3Backup()
}
}
@@ -182,9 +182,9 @@ func start() {
}
func s3Backup() {
// Backup to S3 storage
// Backup Database to S3 storage
pkg.MountS3Storage(s3Path)
pkg.Backup(disableCompression)
pkg.BackupDatabase(disableCompression)
}
// Run in scheduled mode
@@ -218,5 +218,5 @@ func scheduledMode() {
func s3Restore() {
// Restore database from S3
pkg.MountS3Storage(s3Path)
pkg.Restore(file)
pkg.RestoreDatabase(file)
}

View File

@@ -22,8 +22,8 @@ var (
storagePath = "/backup"
)
// Backup backup database
func Backup(disableCompression bool) {
// BackupDatabase backup database
func BackupDatabase(disableCompression bool) {
dbHost = os.Getenv("DB_HOST")
dbPassword = os.Getenv("DB_PASSWORD")
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")
} else {
utils.TestDatabaseConnection()
// Backup database
// Backup Database database
utils.Info("Backing up database...")
bkFileName := fmt.Sprintf("%s_%s.sql.gz", dbName, time.Now().Format("20060102_150405"))

View File

@@ -8,8 +8,8 @@ import (
"path/filepath"
)
// Restore restore database
func Restore(file string) {
// RestoreDatabase restore database
func RestoreDatabase(file string) {
dbHost = os.Getenv("DB_HOST")
dbPassword = os.Getenv("DB_PASSWORD")
dbUserName = os.Getenv("DB_USERNAME")

View File

@@ -21,15 +21,13 @@ var (
s3Endpoint = ""
)
func init() {
// MountS3Storage Mount s3 storage using s3fs
func MountS3Storage(s3Path string) {
accessKey = os.Getenv("ACCESS_KEY")
secretKey = os.Getenv("SECRET_KEY")
bucketName = os.Getenv("BUCKETNAME")
s3Endpoint = os.Getenv("S3_ENDPOINT")
}
// MountS3Storage Mount s3 storage using s3fs
func MountS3Storage(s3Path string) {
if accessKey == "" || secretKey == "" || bucketName == "" {
utils.Fatal("Please make sure all environment variables are set")
} else {

View File

@@ -11,14 +11,14 @@ import (
"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) {
task := "/usr/local/bin/backup_cron.sh"
touchCmd := exec.Command("touch", task)
//task := "/usr/local/bin/backup_cron.sh"
touchCmd := exec.Command("touch", backupCronFile)
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 = ""
if disableCompression {
@@ -39,13 +39,13 @@ bkup --operation backup --dbname %s --port %s %v
`, os.Getenv("DB_NAME"), os.Getenv("DB_PORT"), disableC)
}
if err := utils.WriteToFile(task, scriptContent); err != nil {
utils.Fatalf("Error writing to %s: %v\n", task, err)
if err := utils.WriteToFile(backupCronFile, scriptContent); err != nil {
utils.Fatalf("Error writing to %s: %v\n", backupCronFile, err)
}
chmodCmd := exec.Command("chmod", "+x", "/usr/local/bin/backup_cron.sh")
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")
@@ -60,8 +60,8 @@ bkup --operation backup --dbname %s --port %s %v
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"
`, os.Getenv("SCHEDULE_PERIOD"))
cronContent := fmt.Sprintf(`%s root exec /bin/bash -c ". /run/supervisord.env; /usr/local/bin/backup_cron.sh >> %s"
`, os.Getenv("SCHEDULE_PERIOD"), cronLogFile)
if err := utils.WriteToFile(cronJob, cronContent); err != nil {
utils.Fatalf("Error writing to %s: %v\n", cronJob, err)