Skip to content

Commit 9588c5b

Browse files
authored
Merge pull request #49 from jkaninda/develop
Develop
2 parents b95601a + e0457a4 commit 9588c5b

File tree

7 files changed

+67
-49
lines changed

7 files changed

+67
-49
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ jobs:
3535
file: "./docker/Dockerfile"
3636
platforms: linux/amd64,linux/arm64
3737
tags: |
38-
"${{env.BUILDKIT_IMAGE}}:v0.6"
38+
"${{env.BUILDKIT_IMAGE}}:v0.7"
3939
"${{env.BUILDKIT_IMAGE}}:latest"

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ docker-build:
1717
docker build -f docker/Dockerfile -t jkaninda/mysql-bkup:latest .
1818

1919
docker-run: docker-build
20-
docker run --rm --network internal --privileged --device /dev/fuse --name mysql-bkup -e "DB_HOST=${DB_HOST}" -e "DB_NAME=${DB_NAME}" -e "DB_USERNAME=${DB_USERNAME}" -e "DB_PASSWORD=${DB_PASSWORD}" jkaninda/mysql-bkup bkup backup
20+
docker run --rm --network internal --privileged --device /dev/fuse --name mysql-bkup -e "DB_HOST=${DB_HOST}" -e "DB_NAME=${DB_NAME}" -e "DB_USERNAME=${DB_USERNAME}" -e "DB_PASSWORD=${DB_PASSWORD}" jkaninda/mysql-bkup bkup backup --prune --keep-last 2
2121

2222

2323
docker-run-scheduled: docker-build
24-
docker run --rm --network internal --privileged --device /dev/fuse --name mysql-bkup -e "DB_HOST=${DB_HOST}" -e "DB_NAME=${DB_NAME}" -e "DB_USERNAME=${DB_USERNAME}" -e "DB_PASSWORD=${DB_PASSWORD}" jkaninda/mysql-bkup bkup backup --mode scheduled --period "* * * * *"
24+
docker run --rm --network internal --privileged --device /dev/fuse --name mysql-bkup -e "DB_HOST=${DB_HOST}" -e "DB_NAME=${DB_NAME}" -e "DB_USERNAME=${DB_USERNAME}" -e "DB_PASSWORD=${DB_PASSWORD}" -v "./backup:/backup" jkaninda/mysql-bkup bkup backup --prune --keep-last=2 --mode scheduled --period "* * * * *"
2525

2626

2727
docker-run-scheduled-s3: docker-build

README.md

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,25 @@ MySQL Backup and Restoration tool. Backup database to AWS S3 storage or any S3 A
3838

3939
## Usage
4040

41-
| Options | Shorts | Usage |
42-
|-----------------------|--------|--------------------------------------------------------------------|
43-
| mysql-bkup | bkup | CLI utility |
44-
| backup | | Backup database operation |
45-
| restore | | Restore database operation |
46-
| history | | Show the history of backup |
47-
| --storage | -s | Set storage. local or s3 (default: local) |
48-
| --file | -f | Set file name for restoration |
49-
| --path | | Set s3 path without file name. eg: /custom_path |
50-
| --dbname | -d | Set database name |
51-
| --port | -p | Set database port (default: 3306) |
52-
| --mode | -m | Set execution mode. default or scheduled (default: default) |
53-
| --disable-compression | | Disable database backup compression |
54-
| --period | | Set crontab period for scheduled mode only. (default: "0 1 * * *") |
55-
| --timeout | -t | Set timeout (default: 60s) |
56-
| --help | -h | Print this help message and exit |
57-
| --version | -V | Print version information and exit |
41+
| Options | Shorts | Usage |
42+
|-----------------------|--------|-----------------------------------------------------------------------|
43+
| mysql-bkup | bkup | CLI utility |
44+
| backup | | Backup database operation |
45+
| restore | | Restore database operation |
46+
| history | | Show the history of backup |
47+
| --storage | -s | Set storage. local or s3 (default: local) |
48+
| --file | -f | Set file name for restoration |
49+
| --path | | Set s3 path without file name. eg: /custom_path |
50+
| --dbname | -d | Set database name |
51+
| --port | -p | Set database port (default: 3306) |
52+
| --mode | -m | Set execution mode. default or scheduled (default: default) |
53+
| --disable-compression | | Disable database backup compression |
54+
| --prune | | Delete old backup |
55+
| --keep-last | | keep all backup and delete within this time interval, default 7 days |
56+
| --period | | Set crontab period for scheduled mode only. (default: "0 1 * * *") |
57+
| --timeout | -t | Set timeout (default: 60s) |
58+
| --help | -h | Print this help message and exit |
59+
| --version | -V | Print version information and exit |
5860

5961

6062
## Environment variables

cmd/backup.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ func init() {
2323
//Backup
2424
BackupCmd.PersistentFlags().StringP("mode", "m", "default", "Set execution mode. default or scheduled")
2525
BackupCmd.PersistentFlags().StringP("period", "", "0 1 * * *", "Set schedule period time")
26+
BackupCmd.PersistentFlags().BoolP("prune", "", false, "Prune old backup")
27+
BackupCmd.PersistentFlags().IntP("keep-last", "", 7, "keep all backup and delete within this time interval, default 7 days")
2628
BackupCmd.PersistentFlags().BoolP("disable-compression", "", false, "Disable backup compression")
2729

2830
}

cmd/root.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ Copyright © 2024 Jonas Kaninda <jonaskaninda@gmail.com>
55
package cmd
66

77
import (
8-
"fmt"
98
"github.com/jkaninda/mysql-bkup/utils"
109
"github.com/spf13/cobra"
1110
"os"
@@ -18,16 +17,6 @@ var rootCmd = &cobra.Command{
1817
Long: `MySQL Database backup and restoration tool. Backup database to AWS S3 storage or any S3 Alternatives for Object Storage.`,
1918
Example: utils.MainExample,
2019
Version: appVersion,
21-
//TODO: To remove
22-
//For old user || To remove
23-
Run: func(cmd *cobra.Command, args []string) {
24-
if operation != "" {
25-
if operation == "backup" || operation == "restore" {
26-
fmt.Println(utils.Notice)
27-
utils.Fatal("New config required, please check --help")
28-
}
29-
}
30-
},
3120
}
3221
var operation = ""
3322
var s3Path = "/mysql-bkup"
@@ -49,11 +38,6 @@ func init() {
4938
rootCmd.PersistentFlags().IntP("port", "p", 3306, "Set database port")
5039
rootCmd.PersistentFlags().StringVarP(&operation, "operation", "o", "", "Set operation, for old version only")
5140

52-
rootCmd.PersistentFlags().StringP("mode", "m", "default", "Set execution mode. default or scheduled")
53-
rootCmd.PersistentFlags().StringP("period", "", "0 1 * * *", "Set schedule period time")
54-
rootCmd.PersistentFlags().BoolP("disable-compression", "", false, "Disable backup compression")
55-
rootCmd.PersistentFlags().StringP("file", "f", "", "File name of database")
56-
5741
rootCmd.AddCommand(VersionCmd)
5842
rootCmd.AddCommand(BackupCmd)
5943
rootCmd.AddCommand(RestoreCmd)

pkg/backup.go

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ import (
1111
"log"
1212
"os"
1313
"os/exec"
14+
"path/filepath"
1415
"time"
1516
)
1617

1718
func StartBackup(cmd *cobra.Command) {
18-
_, _ = cmd.Flags().GetString("operation")
19-
2019
//Set env
2120
utils.SetEnv("STORAGE_PATH", storagePath)
2221
utils.GetEnv(cmd, "dbname", "DB_NAME")
@@ -28,15 +27,17 @@ func StartBackup(cmd *cobra.Command) {
2827
storage = utils.GetEnv(cmd, "storage", "STORAGE")
2928
file = utils.GetEnv(cmd, "file", "FILE_NAME")
3029
disableCompression, _ = cmd.Flags().GetBool("disable-compression")
30+
keepLast, _ := cmd.Flags().GetInt("keep-last")
31+
prune, _ := cmd.Flags().GetBool("prune")
3132
executionMode, _ = cmd.Flags().GetString("mode")
3233

3334
if executionMode == "default" {
3435
if storage == "s3" {
3536
utils.Info("Backup database to s3 storage")
36-
s3Backup(disableCompression, s3Path)
37+
s3Backup(disableCompression, s3Path, prune, keepLast)
3738
} else {
3839
utils.Info("Backup database to local storage")
39-
BackupDatabase(disableCompression)
40+
BackupDatabase(disableCompression, prune, keepLast)
4041

4142
}
4243
} else if executionMode == "scheduled" {
@@ -72,7 +73,7 @@ func scheduledMode() {
7273
}
7374

7475
// BackupDatabase backup database
75-
func BackupDatabase(disableCompression bool) {
76+
func BackupDatabase(disableCompression bool, prune bool, keepLast int) {
7677
dbHost = os.Getenv("DB_HOST")
7778
dbPassword := os.Getenv("DB_PASSWORD")
7879
dbUserName := os.Getenv("DB_USERNAME")
@@ -140,6 +141,10 @@ func BackupDatabase(disableCompression bool) {
140141
log.Fatal(err)
141142
}
142143
utils.Done("Database has been backed up")
144+
//Delete old backup
145+
if prune {
146+
deleteOldBackup(keepLast)
147+
}
143148

144149
}
145150

@@ -155,8 +160,39 @@ func BackupDatabase(disableCompression bool) {
155160

156161
}
157162

158-
func s3Backup(disableCompression bool, s3Path string) {
163+
func s3Backup(disableCompression bool, s3Path string, prune bool, keepLast int) {
159164
// Backup Database to S3 storage
160165
MountS3Storage(s3Path)
161-
BackupDatabase(disableCompression)
166+
BackupDatabase(disableCompression, prune, keepLast)
167+
}
168+
169+
func deleteOldBackup(keepLast int) {
170+
utils.Info("Deleting old backups...")
171+
storagePath = os.Getenv("STORAGE_PATH")
172+
// Define the directory path
173+
backupDir := storagePath + "/"
174+
// Get current time
175+
currentTime := time.Now()
176+
// Walk through files in the directory
177+
err := filepath.Walk(backupDir, func(path string, info os.FileInfo, err error) error {
178+
if err != nil {
179+
return err
180+
}
181+
// Check if the file is older than defined day days
182+
if info.Mode().IsRegular() && info.ModTime().Before(currentTime.AddDate(0, 0, -keepLast)) {
183+
// Remove the file
184+
err := os.Remove(path)
185+
if err != nil {
186+
utils.Fatal("Error removing file ", path, err)
187+
} else {
188+
utils.Done("Removed file: ", path)
189+
}
190+
}
191+
return nil
192+
})
193+
194+
if err != nil {
195+
utils.Fatal("Error walking through directory: ", err)
196+
}
197+
162198
}

utils/constant.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
package utils
22

3-
const Notice = "Please remove --operation flag.\n" +
4-
"Use: \n" +
5-
"- backup for database backup operation [eg: bkup backup -d database_name ...]\n" +
6-
"- restore for database restore operation [eg. bkup restore -d database_name ...]\n" +
7-
"Example: bkup backup --storage s3 ...( instead of < bkup --operation backup >)\n" +
8-
"We are sorry for this inconvenient\n"
93
const RestoreExample = "mysql-bkup restore --dbname database --file db_20231219_022941.sql.gz\n" +
104
"bkup restore --dbname database --storage s3 --path /custom-path --file db_20231219_022941.sql.gz"
115
const BackupExample = "mysql-bkup backup --dbname database --disable-compression\n" +

0 commit comments

Comments
 (0)