Skip to content

Commit df0efd2

Browse files
authored
Merge pull request #116 from jkaninda/fix-notification
chore: fix infinity calling Fatal, add a backup reference
2 parents 12fbb67 + e5dd7e7 commit df0efd2

File tree

9 files changed

+133
-59
lines changed

9 files changed

+133
-59
lines changed

docs/how-tos/receive-notification.md

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ layout: default
44
parent: How Tos
55
nav_order: 12
66
---
7-
Send Email or Telegram notifications on success or failed backup.
7+
Send Email or Telegram notifications on successfully or failed backup.
88

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

1212
```yaml
1313
services:
@@ -27,9 +27,13 @@ services:
2727
- MAIL_PORT=587
2828
- MAIL_USERNAME=
2929
- MAIL_PASSWORD=!
30-
- MAIL_FROM=sender@example.com
30+
- MAIL_FROM=
3131
- MAIL_TO=me@example.com,team@example.com,manager@example.com
3232
- MAIL_SKIP_TLS=false
33+
## Time format for notification
34+
- TIME_FORMAT=2006-01-02 at 15:04:05
35+
## Backup reference, in case you want to identifier every backup instance
36+
- BACKUP_REFERENCE=database/Paris cluster
3337
networks:
3438
- web
3539
networks:
@@ -54,6 +58,10 @@ services:
5458
- DB_PASSWORD=password
5559
- TG_TOKEN=[BOT ID]:[BOT TOKEN]
5660
- TG_CHAT_ID=
61+
## Time format for notification
62+
- TIME_FORMAT=2006-01-02 at 15:04:05
63+
## Backup reference, in case you want to identifier every backup instance
64+
- BACKUP_REFERENCE=database/Paris cluster
5765
networks:
5866
- web
5967
networks:
@@ -62,12 +70,13 @@ networks:
6270
6371
### Customize notifications
6472
65-
The body of the notifications can be tailored to your needs using Go templates.
73+
The title and body of the notifications can be tailored to your needs using Go templates.
6674
Template sources must be mounted inside the container in /config/templates:
6775
6876
- email.template: Email notification template
6977
- telegram.template: Telegram notification template
70-
- error.template: Error notification template
78+
- email-error.template: Error notification template
79+
- telegram-error.template: Error notification template
7180
7281
### Data
7382
@@ -78,7 +87,7 @@ Here is a list of all data passed to the template:
7887
- `Storage`: Backup storage
7988
- `BackupLocation`: Backup location
8089
- `BackupSize`: Backup size
81-
90+
- `BackupReference`: Backup reference(eg: database/cluster name or server name)
8291

8392
> email.template:
8493

@@ -88,19 +97,20 @@ Here is a list of all data passed to the template:
8897
<html lang="en">
8998
<head>
9099
<meta charset="UTF-8">
91-
<title>[✅ Database Backup Notification – {{.Database}}</title>
100+
<title>✅ Database Backup Notification – {{.Database}}</title>
92101
</head>
93102
<body>
94103
<h2>Hi,</h2>
95104
<p>Backup of the {{.Database}} database has been successfully completed on {{.EndTime}}.</p>
96105
<h3>Backup Details:</h3>
97106
<ul>
98-
<li>Database Name: {{.Database}}</li>
99-
<li>Backup Start Time: {{.StartTime}}</li>
100-
<li>Backup End Time: {{.EndTime}}</li>
101-
<li>Backup Storage: {{.Storage}}</li>
102-
<li>Backup Location: {{.BackupLocation}}</li>
103-
<li>Backup Size: {{.BackupSize}} bytes</li>
107+
<li>Database Name: {{.Database}}</li>
108+
<li>Backup Start Time: {{.StartTime}}</li>
109+
<li>Backup End Time: {{.EndTime}}</li>
110+
<li>Backup Storage: {{.Storage}}</li>
111+
<li>Backup Location: {{.BackupLocation}}</li>
112+
<li>Backup Size: {{.BackupSize}} bytes</li>
113+
<li>Backup Reference: {{.BackupReference}} </li>
104114
</ul>
105115
<p>Best regards,</p>
106116
</body>
@@ -110,7 +120,7 @@ Here is a list of all data passed to the template:
110120
> telegram.template
111121

112122
```html
113-
[✅ Database Backup Notification – {{.Database}}
123+
✅ Database Backup Notification – {{.Database}}
114124
Hi,
115125
Backup of the {{.Database}} database has been successfully completed on {{.EndTime}}.
116126
@@ -121,9 +131,32 @@ Backup Details:
121131
- Backup Storage: {{.Storage}}
122132
- Backup Location: {{.BackupLocation}}
123133
- Backup Size: {{.BackupSize}} bytes
134+
- Backup Reference: {{.BackupReference}}
135+
```
136+
137+
> email-error.template
138+
139+
```html
140+
<!DOCTYPE html>
141+
<html lang="en">
142+
<head>
143+
<meta charset="UTF-8">
144+
<title>🔴 Urgent: Database Backup Failure Notification</title>
145+
</head>
146+
<body>
147+
<h2>Hi,</h2>
148+
<p>An error occurred during database backup.</p>
149+
<h3>Failure Details:</h3>
150+
<ul>
151+
<li>Error Message: {{.Error}}</li>
152+
<li>Date: {{.EndTime}}</li>
153+
<li>Backup Reference: {{.BackupReference}} </li>
154+
</ul>
155+
</body>
156+
</html>
124157
```
125158

126-
> error.template
159+
> telegram-error.template
127160

128161

129162
```html

pkg/backup.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ func BackupDatabase(db *dbConfig, backupFileName string, disableCompression bool
218218
}
219219
func localBackup(db *dbConfig, config *BackupConfig) {
220220
utils.Info("Backup database to local storage")
221-
startTime = time.Now().Format("2006-01-02 15:04:05")
221+
startTime = time.Now().Format(utils.TimeFormat())
222222
BackupDatabase(db, config.backupFileName, disableCompression)
223223
finalFileName := config.backupFileName
224224
if config.encryption {
@@ -241,7 +241,7 @@ func localBackup(db *dbConfig, config *BackupConfig) {
241241
Storage: config.storage,
242242
BackupLocation: filepath.Join(config.remotePath, finalFileName),
243243
StartTime: startTime,
244-
EndTime: time.Now().Format("2006-01-02 15:04:05"),
244+
EndTime: time.Now().Format(utils.TimeFormat()),
245245
})
246246
//Delete old backup
247247
if config.prune {
@@ -256,7 +256,7 @@ func s3Backup(db *dbConfig, config *BackupConfig) {
256256
bucket := utils.GetEnvVariable("AWS_S3_BUCKET_NAME", "BUCKET_NAME")
257257
s3Path := utils.GetEnvVariable("AWS_S3_PATH", "S3_PATH")
258258
utils.Info("Backup database to s3 storage")
259-
startTime = time.Now().Format("2006-01-02 15:04:05")
259+
startTime = time.Now().Format(utils.TimeFormat())
260260

261261
//Backup database
262262
BackupDatabase(db, config.backupFileName, disableCompression)
@@ -301,7 +301,7 @@ func s3Backup(db *dbConfig, config *BackupConfig) {
301301
Storage: config.storage,
302302
BackupLocation: filepath.Join(config.remotePath, finalFileName),
303303
StartTime: startTime,
304-
EndTime: time.Now().Format("2006-01-02 15:04:05"),
304+
EndTime: time.Now().Format(utils.TimeFormat()),
305305
})
306306
//Delete temp
307307
deleteTemp()
@@ -310,7 +310,7 @@ func s3Backup(db *dbConfig, config *BackupConfig) {
310310
}
311311
func sshBackup(db *dbConfig, config *BackupConfig) {
312312
utils.Info("Backup database to Remote server")
313-
startTime = time.Now().Format("2006-01-02 15:04:05")
313+
startTime = time.Now().Format(utils.TimeFormat())
314314

315315
//Backup database
316316
BackupDatabase(db, config.backupFileName, disableCompression)
@@ -353,7 +353,7 @@ func sshBackup(db *dbConfig, config *BackupConfig) {
353353
Storage: config.storage,
354354
BackupLocation: filepath.Join(config.remotePath, finalFileName),
355355
StartTime: startTime,
356-
EndTime: time.Now().Format("2006-01-02 15:04:05"),
356+
EndTime: time.Now().Format(utils.TimeFormat()),
357357
})
358358
//Delete temp
359359
deleteTemp()
@@ -362,7 +362,7 @@ func sshBackup(db *dbConfig, config *BackupConfig) {
362362
}
363363
func ftpBackup(db *dbConfig, config *BackupConfig) {
364364
utils.Info("Backup database to the remote FTP server")
365-
startTime = time.Now().Format("2006-01-02 15:04:05")
365+
startTime = time.Now().Format(utils.TimeFormat())
366366

367367
//Backup database
368368
BackupDatabase(db, config.backupFileName, disableCompression)
@@ -405,7 +405,7 @@ func ftpBackup(db *dbConfig, config *BackupConfig) {
405405
Storage: config.storage,
406406
BackupLocation: filepath.Join(config.remotePath, finalFileName),
407407
StartTime: startTime,
408-
EndTime: time.Now().Format("2006-01-02 15:04:05"),
408+
EndTime: time.Now().Format(utils.TimeFormat()),
409409
})
410410
//Delete temp
411411
deleteTemp()

templates/email-error.template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<ul>
1212
<li>Error Message: {{.Error}}</li>
1313
<li>Date: {{.EndTime}}</li>
14+
<li>Backup Reference: {{.BackupReference}} </li>
1415
</ul>
1516
<p>©2024 <a href="github.com/jkaninda/mysql-bkup">mysql-bkup</a></p>
1617
</body>

templates/email.template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<li>Backup Storage: {{.Storage}}</li>
1616
<li>Backup Location: {{.BackupLocation}}</li>
1717
<li>Backup Size: {{.BackupSize}} bytes</li>
18+
<li>Backup Reference: {{.BackupReference}} </li>
1819
</ul>
1920
<p>Best regards,</p>
2021
<p>©2024 <a href="github.com/jkaninda/mysql-bkup">mysql-bkup</a></p>

templates/telegram-error.template

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Hi,
33
An error occurred during database backup.
44
Failure Details:
5-
Date: {{.EndTime}}
6-
Error Message: {{.Error}}
5+
- Date: {{.EndTime}}
6+
- Backup Reference: {{.BackupReference}}
7+
- Error Message: {{.Error}}
78

templates/telegram.template

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
✅ Database Backup Notification – {{.Database}}
1+
[✅ Database Backup Notification – {{.Database}}
22
Hi,
33
Backup of the {{.Database}} database has been successfully completed on {{.EndTime}}.
44

@@ -8,4 +8,5 @@ Backup Details:
88
- Backup EndTime: {{.EndTime}}
99
- Backup Storage: {{.Storage}}
1010
- Backup Location: {{.BackupLocation}}
11-
- Backup Size: {{.BackupSize}} bytes
11+
- Backup Size: {{.BackupSize}} bytes
12+
- Backup Reference: {{.BackupReference}}

utils/config.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,23 @@ type MailConfig struct {
1212
SkipTls bool
1313
}
1414
type NotificationData struct {
15-
File string
16-
BackupSize int64
17-
Database string
18-
StartTime string
19-
EndTime string
20-
Storage string
21-
BackupLocation string
15+
File string
16+
BackupSize int64
17+
Database string
18+
StartTime string
19+
EndTime string
20+
Storage string
21+
BackupLocation string
22+
BackupReference string
2223
}
2324
type ErrorMessage struct {
24-
Database string
25-
EndTime string
26-
Error string
25+
Database string
26+
EndTime string
27+
Error string
28+
BackupReference string
2729
}
2830

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

4043
}
4144

45+
// TimeFormat returns the format of the time
46+
func TimeFormat() string {
47+
format := os.Getenv("TIME_FORMAT")
48+
if format == "" {
49+
return "2006-01-02 at 15:04:05"
50+
51+
}
52+
return format
53+
}
54+
55+
func backupReference() string {
56+
return os.Getenv("BACKUP_REFERENCE")
57+
}
58+
4259
const templatePath = "/config/templates"

utils/constant.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
**/
77
package utils
88

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

1414
const MainExample = "mysql-bkup backup --dbname database --disable-compression\n" +

0 commit comments

Comments
 (0)