Skip to content

Commit

Permalink
Merge pull request #116 from jkaninda/fix-notification
Browse files Browse the repository at this point in the history
chore: fix infinity calling Fatal, add a backup reference
  • Loading branch information
jkaninda authored Oct 10, 2024
2 parents 12fbb67 + e5dd7e7 commit df0efd2
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 59 deletions.
63 changes: 48 additions & 15 deletions docs/how-tos/receive-notification.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ layout: default
parent: How Tos
nav_order: 12
---
Send Email or Telegram notifications on success or failed backup.
Send Email or Telegram notifications on successfully or failed backup.

### Email
To send out email notifications on failed backup runs, provide SMTP credentials, a sender and a recipient:
To send out email notifications on failed or successfully backup runs, provide SMTP credentials, a sender and a recipient:

```yaml
services:
Expand All @@ -27,9 +27,13 @@ services:
- MAIL_PORT=587
- MAIL_USERNAME=
- MAIL_PASSWORD=!
- MAIL_FROM=[email protected]
- MAIL_FROM=
- [email protected],[email protected],[email protected]
- MAIL_SKIP_TLS=false
## Time format for notification
- TIME_FORMAT=2006-01-02 at 15:04:05
## Backup reference, in case you want to identifier every backup instance
- BACKUP_REFERENCE=database/Paris cluster
networks:
- web
networks:
Expand All @@ -54,6 +58,10 @@ services:
- DB_PASSWORD=password
- TG_TOKEN=[BOT ID]:[BOT TOKEN]
- TG_CHAT_ID=
## Time format for notification
- TIME_FORMAT=2006-01-02 at 15:04:05
## Backup reference, in case you want to identifier every backup instance
- BACKUP_REFERENCE=database/Paris cluster
networks:
- web
networks:
Expand All @@ -62,12 +70,13 @@ networks:
### Customize notifications
The body of the notifications can be tailored to your needs using Go templates.
The title and body of the notifications can be tailored to your needs using Go templates.
Template sources must be mounted inside the container in /config/templates:
- email.template: Email notification template
- telegram.template: Telegram notification template
- error.template: Error notification template
- email-error.template: Error notification template
- telegram-error.template: Error notification template
### Data
Expand All @@ -78,7 +87,7 @@ Here is a list of all data passed to the template:
- `Storage`: Backup storage
- `BackupLocation`: Backup location
- `BackupSize`: Backup size

- `BackupReference`: Backup reference(eg: database/cluster name or server name)

> email.template:

Expand All @@ -88,19 +97,20 @@ Here is a list of all data passed to the template:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>[✅ Database Backup Notification – {{.Database}}</title>
<title>✅ Database Backup Notification – {{.Database}}</title>
</head>
<body>
<h2>Hi,</h2>
<p>Backup of the {{.Database}} database has been successfully completed on {{.EndTime}}.</p>
<h3>Backup Details:</h3>
<ul>
<li>Database Name: {{.Database}}</li>
<li>Backup Start Time: {{.StartTime}}</li>
<li>Backup End Time: {{.EndTime}}</li>
<li>Backup Storage: {{.Storage}}</li>
<li>Backup Location: {{.BackupLocation}}</li>
<li>Backup Size: {{.BackupSize}} bytes</li>
<li>Database Name: {{.Database}}</li>
<li>Backup Start Time: {{.StartTime}}</li>
<li>Backup End Time: {{.EndTime}}</li>
<li>Backup Storage: {{.Storage}}</li>
<li>Backup Location: {{.BackupLocation}}</li>
<li>Backup Size: {{.BackupSize}} bytes</li>
<li>Backup Reference: {{.BackupReference}} </li>
</ul>
<p>Best regards,</p>
</body>
Expand All @@ -110,7 +120,7 @@ Here is a list of all data passed to the template:
> telegram.template

```html
[✅ Database Backup Notification – {{.Database}}
✅ Database Backup Notification – {{.Database}}
Hi,
Backup of the {{.Database}} database has been successfully completed on {{.EndTime}}.
Expand All @@ -121,9 +131,32 @@ Backup Details:
- Backup Storage: {{.Storage}}
- Backup Location: {{.BackupLocation}}
- Backup Size: {{.BackupSize}} bytes
- Backup Reference: {{.BackupReference}}
```

> email-error.template

```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>🔴 Urgent: Database Backup Failure Notification</title>
</head>
<body>
<h2>Hi,</h2>
<p>An error occurred during database backup.</p>
<h3>Failure Details:</h3>
<ul>
<li>Error Message: {{.Error}}</li>
<li>Date: {{.EndTime}}</li>
<li>Backup Reference: {{.BackupReference}} </li>
</ul>
</body>
</html>
```

> error.template
> telegram-error.template


```html
Expand Down
16 changes: 8 additions & 8 deletions pkg/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func BackupDatabase(db *dbConfig, backupFileName string, disableCompression bool
}
func localBackup(db *dbConfig, config *BackupConfig) {
utils.Info("Backup database to local storage")
startTime = time.Now().Format("2006-01-02 15:04:05")
startTime = time.Now().Format(utils.TimeFormat())
BackupDatabase(db, config.backupFileName, disableCompression)
finalFileName := config.backupFileName
if config.encryption {
Expand All @@ -241,7 +241,7 @@ func localBackup(db *dbConfig, config *BackupConfig) {
Storage: config.storage,
BackupLocation: filepath.Join(config.remotePath, finalFileName),
StartTime: startTime,
EndTime: time.Now().Format("2006-01-02 15:04:05"),
EndTime: time.Now().Format(utils.TimeFormat()),
})
//Delete old backup
if config.prune {
Expand All @@ -256,7 +256,7 @@ func s3Backup(db *dbConfig, config *BackupConfig) {
bucket := utils.GetEnvVariable("AWS_S3_BUCKET_NAME", "BUCKET_NAME")
s3Path := utils.GetEnvVariable("AWS_S3_PATH", "S3_PATH")
utils.Info("Backup database to s3 storage")
startTime = time.Now().Format("2006-01-02 15:04:05")
startTime = time.Now().Format(utils.TimeFormat())

//Backup database
BackupDatabase(db, config.backupFileName, disableCompression)
Expand Down Expand Up @@ -301,7 +301,7 @@ func s3Backup(db *dbConfig, config *BackupConfig) {
Storage: config.storage,
BackupLocation: filepath.Join(config.remotePath, finalFileName),
StartTime: startTime,
EndTime: time.Now().Format("2006-01-02 15:04:05"),
EndTime: time.Now().Format(utils.TimeFormat()),
})
//Delete temp
deleteTemp()
Expand All @@ -310,7 +310,7 @@ func s3Backup(db *dbConfig, config *BackupConfig) {
}
func sshBackup(db *dbConfig, config *BackupConfig) {
utils.Info("Backup database to Remote server")
startTime = time.Now().Format("2006-01-02 15:04:05")
startTime = time.Now().Format(utils.TimeFormat())

//Backup database
BackupDatabase(db, config.backupFileName, disableCompression)
Expand Down Expand Up @@ -353,7 +353,7 @@ func sshBackup(db *dbConfig, config *BackupConfig) {
Storage: config.storage,
BackupLocation: filepath.Join(config.remotePath, finalFileName),
StartTime: startTime,
EndTime: time.Now().Format("2006-01-02 15:04:05"),
EndTime: time.Now().Format(utils.TimeFormat()),
})
//Delete temp
deleteTemp()
Expand All @@ -362,7 +362,7 @@ func sshBackup(db *dbConfig, config *BackupConfig) {
}
func ftpBackup(db *dbConfig, config *BackupConfig) {
utils.Info("Backup database to the remote FTP server")
startTime = time.Now().Format("2006-01-02 15:04:05")
startTime = time.Now().Format(utils.TimeFormat())

//Backup database
BackupDatabase(db, config.backupFileName, disableCompression)
Expand Down Expand Up @@ -405,7 +405,7 @@ func ftpBackup(db *dbConfig, config *BackupConfig) {
Storage: config.storage,
BackupLocation: filepath.Join(config.remotePath, finalFileName),
StartTime: startTime,
EndTime: time.Now().Format("2006-01-02 15:04:05"),
EndTime: time.Now().Format(utils.TimeFormat()),
})
//Delete temp
deleteTemp()
Expand Down
1 change: 1 addition & 0 deletions templates/email-error.template
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<ul>
<li>Error Message: {{.Error}}</li>
<li>Date: {{.EndTime}}</li>
<li>Backup Reference: {{.BackupReference}} </li>
</ul>
<p>©2024 <a href="github.com/jkaninda/mysql-bkup">mysql-bkup</a></p>
</body>
Expand Down
1 change: 1 addition & 0 deletions templates/email.template
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<li>Backup Storage: {{.Storage}}</li>
<li>Backup Location: {{.BackupLocation}}</li>
<li>Backup Size: {{.BackupSize}} bytes</li>
<li>Backup Reference: {{.BackupReference}} </li>
</ul>
<p>Best regards,</p>
<p>©2024 <a href="github.com/jkaninda/mysql-bkup">mysql-bkup</a></p>
Expand Down
5 changes: 3 additions & 2 deletions templates/telegram-error.template
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Hi,
An error occurred during database backup.
Failure Details:
Date: {{.EndTime}}
Error Message: {{.Error}}
- Date: {{.EndTime}}
- Backup Reference: {{.BackupReference}}
- Error Message: {{.Error}}

5 changes: 3 additions & 2 deletions templates/telegram.template
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
✅ Database Backup Notification – {{.Database}}
[✅ Database Backup Notification – {{.Database}}
Hi,
Backup of the {{.Database}} database has been successfully completed on {{.EndTime}}.

Expand All @@ -8,4 +8,5 @@ Backup Details:
- Backup EndTime: {{.EndTime}}
- Backup Storage: {{.Storage}}
- Backup Location: {{.BackupLocation}}
- Backup Size: {{.BackupSize}} bytes
- Backup Size: {{.BackupSize}} bytes
- Backup Reference: {{.BackupReference}}
37 changes: 27 additions & 10 deletions utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,23 @@ type MailConfig struct {
SkipTls bool
}
type NotificationData struct {
File string
BackupSize int64
Database string
StartTime string
EndTime string
Storage string
BackupLocation string
File string
BackupSize int64
Database string
StartTime string
EndTime string
Storage string
BackupLocation string
BackupReference string
}
type ErrorMessage struct {
Database string
EndTime string
Error string
Database string
EndTime string
Error string
BackupReference string
}

// loadMailConfig gets mail environment variables and returns MailConfig
func loadMailConfig() *MailConfig {
return &MailConfig{
MailHost: os.Getenv("MAIL_HOST"),
Expand All @@ -39,4 +42,18 @@ func loadMailConfig() *MailConfig {

}

// TimeFormat returns the format of the time
func TimeFormat() string {
format := os.Getenv("TIME_FORMAT")
if format == "" {
return "2006-01-02 at 15:04:05"

}
return format
}

func backupReference() string {
return os.Getenv("BACKUP_REFERENCE")
}

const templatePath = "/config/templates"
4 changes: 2 additions & 2 deletions utils/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
**/
package utils

const RestoreExample = "mysql-bkup restore --dbname database --file db_20231219_022941.sql.gz\n" +
const RestoreExample = "restore --dbname database --file db_20231219_022941.sql.gz\n" +
"restore --dbname database --storage s3 --path /custom-path --file db_20231219_022941.sql.gz"
const BackupExample = "mysql-bkup backup --dbname database --disable-compression\n" +
const BackupExample = "backup --dbname database --disable-compression\n" +
"backup --dbname database --storage s3 --path /custom-path --disable-compression"

const MainExample = "mysql-bkup backup --dbname database --disable-compression\n" +
Expand Down
Loading

0 comments on commit df0efd2

Please sign in to comment.