Skip to content

Commit 40557af

Browse files
authored
Merge pull request #130 from jkaninda/refactor
Refactor
2 parents c16ee3a + 1dcb958 commit 40557af

File tree

13 files changed

+44
-11
lines changed

13 files changed

+44
-11
lines changed

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ TZ=Europe/Paris
2727

2828
### Backup Cron Expression
2929
#BACKUP_CRON_EXPRESSION=@midnight
30+
##Delete old backup created more than specified days ago
31+
#BACKUP_RETENTION_DAYS=7
3032

3133
####SSH Storage
3234
#SSH_HOST_NAME=

cmd/backup.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var BackupCmd = &cobra.Command{
2020
if len(args) == 0 {
2121
pkg.StartBackup(cmd)
2222
} else {
23-
utils.Fatal("Error, no argument required")
23+
utils.Fatal(`"backup" accepts no argument %q`, args)
2424
}
2525
},
2626
}

cmd/migrate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var MigrateCmd = &cobra.Command{
1919
if len(args) == 0 {
2020
pkg.StartMigration(cmd)
2121
} else {
22-
utils.Fatal("Error, no argument required")
22+
utils.Fatal(`"migrate" accepts no argument %q`, args)
2323

2424
}
2525

cmd/restore.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var RestoreCmd = &cobra.Command{
1414
if len(args) == 0 {
1515
pkg.StartRestore(cmd)
1616
} else {
17-
utils.Fatal("Error, no argument required")
17+
utils.Fatal(`"restore" accepts no argument %q`, args)
1818

1919
}
2020

docs/how-tos/backup-to-s3.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ services:
7474
- AWS_ACCESS_KEY=xxxx
7575
- AWS_SECRET_KEY=xxxxx
7676
# - BACKUP_CRON_EXPRESSION=0 1 * * * # Optional
77+
#Delete old backup created more than specified days ago
78+
#- BACKUP_RETENTION_DAYS=7
7779
## In case you are using S3 alternative such as Minio and your Minio instance is not secured, you change it to true
7880
- AWS_DISABLE_SSL="false"
7981
# mysql-bkup container must be connected to the same network with your database

docs/how-tos/backup-to-ssh.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ services:
7979
- REMOTE_PATH=/home/jkaninda/backups
8080
- SSH_IDENTIFY_FILE=/tmp/id_ed25519
8181
# - BACKUP_CRON_EXPRESSION=0 1 * * * # Optional
82+
#Delete old backup created more than specified days ago
83+
#- BACKUP_RETENTION_DAYS=7
8284
## We advise you to use a private jey instead of password
8385
#- SSH_PASSWORD=password
8486
# mysql-bkup container must be connected to the same network with your database

docs/how-tos/backup.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ services:
7575
- DB_USERNAME=username
7676
- DB_PASSWORD=password
7777
- BACKUP_CRON_EXPRESSION=0 1 * * *
78+
#Delete old backup created more than specified days ago
79+
#- BACKUP_RETENTION_DAYS=7
7880
# mysql-bkup container must be connected to the same network with your database
7981
networks:
8082
- web

docs/reference/index.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ Backup, restore and migrate targets, schedule and retention are configured using
2626
| --dbname | -d | Database name |
2727
| --port | -p | Database port (default: 3306) |
2828
| --disable-compression | | Disable database backup compression |
29-
| --prune | | Delete old backup, default disabled |
30-
| --keep-last | | Delete old backup created more than specified days ago, default 7 days |
3129
| --cron-expression | | Backup cron expression, eg: (* * * * *) or @daily |
3230
| --help | -h | Print this help message and exit |
3331
| --version | -V | Print version information and exit |
@@ -52,6 +50,7 @@ Backup, restore and migrate targets, schedule and retention are configured using
5250
| GPG_PASSPHRASE | Optional, required to encrypt and restore backup | GPG passphrase |
5351
| GPG_PUBLIC_KEY | Optional, required to encrypt backup | GPG public key, used to encrypt backup (/config/public_key.asc) |
5452
| BACKUP_CRON_EXPRESSION | Optional if it was provided from the `--cron-expression` flag | Backup cron expression for docker in scheduled mode |
53+
| BACKUP_RETENTION_DAYS | Optional | Delete old backup created more than specified days ago |
5554
| SSH_HOST | Optional, required for SSH storage | ssh remote hostname or ip |
5655
| SSH_USER | Optional, required for SSH storage | ssh remote user |
5756
| SSH_PASSWORD | Optional, required for SSH storage | ssh remote user's password |

pkg/backup.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func StartBackup(cmd *cobra.Command) {
4646
func scheduledMode(db *dbConfig, config *BackupConfig) {
4747
utils.Info("Running in Scheduled mode")
4848
utils.Info("Backup cron expression: %s", config.cronExpression)
49+
utils.Info("The next scheduled time is: %v", utils.CronNextTime(config.cronExpression).Format(timeFormat))
4950
utils.Info("Storage type %s ", config.storage)
5051

5152
//Test backup
@@ -58,6 +59,8 @@ func scheduledMode(db *dbConfig, config *BackupConfig) {
5859

5960
_, err := c.AddFunc(config.cronExpression, func() {
6061
BackupTask(db, config)
62+
utils.Info("Next backup time is: %v", utils.CronNextTime(config.cronExpression).Format(timeFormat))
63+
6164
})
6265
if err != nil {
6366
return
@@ -119,6 +122,7 @@ func startMultiBackup(bkConfig *BackupConfig, configFile string) {
119122
if utils.IsValidCronExpression(bkConfig.cronExpression) {
120123
utils.Info("Running MultiBackup in Scheduled mode")
121124
utils.Info("Backup cron expression: %s", bkConfig.cronExpression)
125+
utils.Info("The next scheduled time is: %v", utils.CronNextTime(bkConfig.cronExpression).Format(timeFormat))
122126
utils.Info("Storage type %s ", bkConfig.storage)
123127

124128
//Test backup
@@ -132,6 +136,8 @@ func startMultiBackup(bkConfig *BackupConfig, configFile string) {
132136
_, err := c.AddFunc(bkConfig.cronExpression, func() {
133137
// Create a channel
134138
multiBackupTask(conf.Databases, bkConfig)
139+
utils.Info("Next backup time is: %v", utils.CronNextTime(bkConfig.cronExpression).Format(timeFormat))
140+
135141
})
136142
if err != nil {
137143
return

pkg/config.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ func initAWSConfig() *AWSConfig {
172172
func initBackupConfig(cmd *cobra.Command) *BackupConfig {
173173
utils.SetEnv("STORAGE_PATH", storagePath)
174174
utils.GetEnv(cmd, "cron-expression", "BACKUP_CRON_EXPRESSION")
175-
utils.GetEnv(cmd, "period", "BACKUP_CRON_EXPRESSION")
176175
utils.GetEnv(cmd, "path", "REMOTE_PATH")
177176
//Get flag value and set env
178177
remotePath := utils.GetEnvVariable("REMOTE_PATH", "SSH_REMOTE_PATH")

pkg/var.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const algorithm = "aes256"
1212
const gpgHome = "/config/gnupg"
1313
const gpgExtension = "gpg"
1414
const workingDir = "/config"
15+
const timeFormat = "2006-01-02 at 15:04:05"
1516

1617
var (
1718
storage = "local"

utils/notification.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"encoding/json"
77
"fmt"
88
"github.com/go-mail/mail"
9-
"github.com/robfig/cron/v3"
109
"html/template"
1110
"io/ioutil"
1211
"net/http"
@@ -177,7 +176,3 @@ func getTgUrl() string {
177176
return fmt.Sprintf("https://api.telegram.org/bot%s", os.Getenv("TG_TOKEN"))
178177

179178
}
180-
func IsValidCronExpression(cronExpr string) bool {
181-
_, err := cron.ParseStandard(cronExpr)
182-
return err == nil
183-
}

utils/utils.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ package utils
88

99
import (
1010
"fmt"
11+
"github.com/robfig/cron/v3"
1112
"github.com/spf13/cobra"
1213
"io"
1314
"io/fs"
1415
"os"
1516
"strconv"
17+
"time"
1618
)
1719

1820
// FileExists checks if the file does exist
@@ -187,3 +189,26 @@ func EnvWithDefault(envName string, defaultValue string) string {
187189
}
188190
return value
189191
}
192+
193+
// IsValidCronExpression verify cronExpression and returns boolean
194+
func IsValidCronExpression(cronExpr string) bool {
195+
// Parse the cron expression
196+
_, err := cron.ParseStandard(cronExpr)
197+
return err == nil
198+
}
199+
200+
// CronNextTime returns cronExpression next time
201+
func CronNextTime(cronExpr string) time.Time {
202+
// Parse the cron expression
203+
schedule, err := cron.ParseStandard(cronExpr)
204+
if err != nil {
205+
Error("Error parsing cron expression:", err)
206+
return time.Time{}
207+
}
208+
// Get the current time
209+
now := time.Now()
210+
// Get the next scheduled time
211+
next := schedule.Next(now)
212+
//Info("The next scheduled time is: %v\n", next)
213+
return next
214+
}

0 commit comments

Comments
 (0)