Skip to content

Commit

Permalink
Merge pull request #40 from jkaninda/develop
Browse files Browse the repository at this point in the history
refactor: refactoring of code
  • Loading branch information
jkaninda authored Jan 19, 2024
2 parents 40f2a2c + 3525a90 commit a086921
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 30 deletions.
22 changes: 11 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down Expand Up @@ -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()
}
}
Expand All @@ -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
Expand Down Expand Up @@ -218,5 +218,5 @@ func scheduledMode() {
func s3Restore() {
// Restore database from S3
pkg.MountS3Storage(s3Path)
pkg.Restore(file)
pkg.RestoreDatabase(file)
}
6 changes: 3 additions & 3 deletions pkg/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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"))

Expand Down
4 changes: 2 additions & 2 deletions pkg/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
6 changes: 2 additions & 4 deletions pkg/s3fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 10 additions & 10 deletions pkg/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
Expand All @@ -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)
Expand Down

0 comments on commit a086921

Please sign in to comment.