mirror of
https://github.com/jkaninda/mysql-bkup.git
synced 2025-12-06 21:49:40 +01:00
This commit is contained in:
1
go.mod
1
go.mod
@@ -26,6 +26,7 @@ require (
|
||||
github.com/hashicorp/errwrap v1.1.0 // indirect
|
||||
github.com/hashicorp/go-multierror v1.1.1 // indirect
|
||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||
github.com/jkaninda/go-utils v0.0.0-20250122060806-26119182077a // indirect
|
||||
github.com/jlaffaye/ftp v0.2.0 // indirect
|
||||
github.com/jmespath/go-jmespath v0.4.0 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
|
||||
4
go.sum
4
go.sum
@@ -43,6 +43,10 @@ github.com/jkaninda/encryptor v0.0.0-20241111100652-926393c9437e h1:jtFKZHt/PLGQ
|
||||
github.com/jkaninda/encryptor v0.0.0-20241111100652-926393c9437e/go.mod h1:Y1EXpPWQ9PNd7y7E6ez3xgnzZc8fuDWXwX/1/dXNCE4=
|
||||
github.com/jkaninda/go-storage v0.1.3 h1:lEpHVgFLKSvjsi/6tAek96Y07za3vxmsXF2/+jiCMZU=
|
||||
github.com/jkaninda/go-storage v0.1.3/go.mod h1:zVRnLprBk/9AUz2+za6Y03MgoNYrqKLy3edVtjqMaps=
|
||||
github.com/jkaninda/go-utils v0.0.0-20250122054739-d330fecee150 h1:AgcKk58P/Z+u4DBE2MTyZ6kCweA89YUpX7TuttHS3oQ=
|
||||
github.com/jkaninda/go-utils v0.0.0-20250122054739-d330fecee150/go.mod h1:pf0/U6k4JbxlablM2G4eSTZdQ2LFshfAsCK5Q8qNfGo=
|
||||
github.com/jkaninda/go-utils v0.0.0-20250122060806-26119182077a h1:ZTpKujQGhEF266RWkD2cXnCsafk3R2+sGtfbzCQSs1s=
|
||||
github.com/jkaninda/go-utils v0.0.0-20250122060806-26119182077a/go.mod h1:pf0/U6k4JbxlablM2G4eSTZdQ2LFshfAsCK5Q8qNfGo=
|
||||
github.com/jlaffaye/ftp v0.2.0 h1:lXNvW7cBu7R/68bknOX3MrRIIqZ61zELs1P2RAiA3lg=
|
||||
github.com/jlaffaye/ftp v0.2.0/go.mod h1:is2Ds5qkhceAPy2xD6RLI6hmp/qysSoymZ+Z2uTnspI=
|
||||
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
|
||||
|
||||
@@ -27,6 +27,7 @@ package pkg
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/jkaninda/go-storage/pkg/azure"
|
||||
goutils "github.com/jkaninda/go-utils"
|
||||
"github.com/jkaninda/mysql-bkup/utils"
|
||||
|
||||
"os"
|
||||
@@ -36,7 +37,6 @@ import (
|
||||
|
||||
func azureBackup(db *dbConfig, config *BackupConfig) {
|
||||
utils.Info("Backup database to Azure Blob Storage")
|
||||
startTime = time.Now().Format(utils.TimeFormat())
|
||||
|
||||
// Backup database
|
||||
BackupDatabase(db, config.backupFileName, disableCompression)
|
||||
@@ -87,6 +87,8 @@ func azureBackup(db *dbConfig, config *BackupConfig) {
|
||||
utils.Info("Backup size: %s", utils.ConvertBytes(uint64(backupSize)))
|
||||
utils.Info("Uploading backup archive to Azure Blob storage ... done ")
|
||||
|
||||
duration := goutils.FormatDuration(time.Since(startTime), 0)
|
||||
|
||||
// Send notification
|
||||
utils.NotifySuccess(&utils.NotificationData{
|
||||
File: finalFileName,
|
||||
@@ -94,12 +96,11 @@ func azureBackup(db *dbConfig, config *BackupConfig) {
|
||||
Database: db.dbName,
|
||||
Storage: config.storage,
|
||||
BackupLocation: filepath.Join(config.remotePath, finalFileName),
|
||||
StartTime: startTime,
|
||||
EndTime: time.Now().Format(utils.TimeFormat()),
|
||||
Duration: duration,
|
||||
})
|
||||
// Delete temp
|
||||
deleteTemp()
|
||||
utils.Info("Backup completed successfully")
|
||||
utils.Info("Backup successfully completed in %s", duration)
|
||||
}
|
||||
func azureRestore(db *dbConfig, conf *RestoreConfig) {
|
||||
utils.Info("Restore database from Azure Blob storage")
|
||||
|
||||
@@ -29,6 +29,7 @@ import (
|
||||
"fmt"
|
||||
"github.com/jkaninda/encryptor"
|
||||
"github.com/jkaninda/go-storage/pkg/local"
|
||||
goutils "github.com/jkaninda/go-utils"
|
||||
"github.com/jkaninda/mysql-bkup/utils"
|
||||
"github.com/robfig/cron/v3"
|
||||
"github.com/spf13/cobra"
|
||||
@@ -107,6 +108,7 @@ func multiBackupTask(databases []Database, bkConfig *BackupConfig) {
|
||||
// BackupTask backups database
|
||||
func BackupTask(db *dbConfig, config *BackupConfig) {
|
||||
utils.Info("Starting backup task...")
|
||||
startTime = time.Now()
|
||||
// Generate file name
|
||||
backupFileName := fmt.Sprintf("%s_%s.sql.gz", db.dbName, time.Now().Format("20060102_150405"))
|
||||
if config.disableCompression {
|
||||
@@ -118,7 +120,7 @@ func BackupTask(db *dbConfig, config *BackupConfig) {
|
||||
localBackup(db, config)
|
||||
case "s3", "S3":
|
||||
s3Backup(db, config)
|
||||
case "ssh", "SSH", "remote":
|
||||
case "ssh", "SSH", "remote", "sftp":
|
||||
sshBackup(db, config)
|
||||
case "ftp", "FTP":
|
||||
ftpBackup(db, config)
|
||||
@@ -256,7 +258,6 @@ 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(utils.TimeFormat())
|
||||
BackupDatabase(db, config.backupFileName, disableCompression)
|
||||
finalFileName := config.backupFileName
|
||||
if config.encryption {
|
||||
@@ -279,6 +280,8 @@ func localBackup(db *dbConfig, config *BackupConfig) {
|
||||
utils.Info("Backup name is %s", finalFileName)
|
||||
utils.Info("Backup size: %s", utils.ConvertBytes(uint64(backupSize)))
|
||||
utils.Info("Backup saved in %s", filepath.Join(storagePath, finalFileName))
|
||||
duration := goutils.FormatDuration(time.Since(startTime), 0)
|
||||
|
||||
// Send notification
|
||||
utils.NotifySuccess(&utils.NotificationData{
|
||||
File: finalFileName,
|
||||
@@ -286,8 +289,7 @@ func localBackup(db *dbConfig, config *BackupConfig) {
|
||||
Database: db.dbName,
|
||||
Storage: config.storage,
|
||||
BackupLocation: filepath.Join(storagePath, finalFileName),
|
||||
StartTime: startTime,
|
||||
EndTime: time.Now().Format(utils.TimeFormat()),
|
||||
Duration: duration,
|
||||
})
|
||||
// Delete old backup
|
||||
if config.prune {
|
||||
@@ -299,7 +301,7 @@ func localBackup(db *dbConfig, config *BackupConfig) {
|
||||
}
|
||||
// Delete temp
|
||||
deleteTemp()
|
||||
utils.Info("Backup completed successfully")
|
||||
utils.Info("Backup successfully completed in %s", duration)
|
||||
}
|
||||
|
||||
func encryptBackup(config *BackupConfig) {
|
||||
|
||||
@@ -28,6 +28,7 @@ import (
|
||||
"fmt"
|
||||
"github.com/jkaninda/go-storage/pkg/ftp"
|
||||
"github.com/jkaninda/go-storage/pkg/ssh"
|
||||
goutils "github.com/jkaninda/go-utils"
|
||||
"github.com/jkaninda/mysql-bkup/utils"
|
||||
|
||||
"os"
|
||||
@@ -37,7 +38,6 @@ import (
|
||||
|
||||
func sshBackup(db *dbConfig, config *BackupConfig) {
|
||||
utils.Info("Backup database to Remote server")
|
||||
startTime = time.Now().Format(utils.TimeFormat())
|
||||
// Backup database
|
||||
BackupDatabase(db, config.backupFileName, disableCompression)
|
||||
finalFileName := config.backupFileName
|
||||
@@ -91,6 +91,8 @@ func sshBackup(db *dbConfig, config *BackupConfig) {
|
||||
|
||||
}
|
||||
utils.Info("Uploading backup archive to remote storage ... done ")
|
||||
duration := goutils.FormatDuration(time.Since(startTime), 0)
|
||||
|
||||
// Send notification
|
||||
utils.NotifySuccess(&utils.NotificationData{
|
||||
File: finalFileName,
|
||||
@@ -98,12 +100,11 @@ func sshBackup(db *dbConfig, config *BackupConfig) {
|
||||
Database: db.dbName,
|
||||
Storage: config.storage,
|
||||
BackupLocation: filepath.Join(config.remotePath, finalFileName),
|
||||
StartTime: startTime,
|
||||
EndTime: time.Now().Format(utils.TimeFormat()),
|
||||
Duration: duration,
|
||||
})
|
||||
// Delete temp
|
||||
deleteTemp()
|
||||
utils.Info("Backup completed successfully")
|
||||
utils.Info("Backup successfully completed in %s", duration)
|
||||
|
||||
}
|
||||
func remoteRestore(db *dbConfig, conf *RestoreConfig) {
|
||||
@@ -153,7 +154,6 @@ func ftpRestore(db *dbConfig, conf *RestoreConfig) {
|
||||
}
|
||||
func ftpBackup(db *dbConfig, config *BackupConfig) {
|
||||
utils.Info("Backup database to the remote FTP server")
|
||||
startTime = time.Now().Format(utils.TimeFormat())
|
||||
|
||||
// Backup database
|
||||
BackupDatabase(db, config.backupFileName, disableCompression)
|
||||
@@ -203,6 +203,7 @@ func ftpBackup(db *dbConfig, config *BackupConfig) {
|
||||
utils.Info("Backup name is %s", finalFileName)
|
||||
utils.Info("Backup size: %s", utils.ConvertBytes(uint64(backupSize)))
|
||||
utils.Info("Uploading backup archive to the remote FTP server ... done ")
|
||||
duration := goutils.FormatDuration(time.Since(startTime), 0)
|
||||
|
||||
// Send notification
|
||||
utils.NotifySuccess(&utils.NotificationData{
|
||||
@@ -211,10 +212,9 @@ func ftpBackup(db *dbConfig, config *BackupConfig) {
|
||||
Database: db.dbName,
|
||||
Storage: config.storage,
|
||||
BackupLocation: filepath.Join(config.remotePath, finalFileName),
|
||||
StartTime: startTime,
|
||||
EndTime: time.Now().Format(utils.TimeFormat()),
|
||||
Duration: duration,
|
||||
})
|
||||
// Delete temp
|
||||
deleteTemp()
|
||||
utils.Info("Backup completed successfully")
|
||||
utils.Info("Backup successfully completed in %s", duration)
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ package pkg
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/jkaninda/go-storage/pkg/s3"
|
||||
goutils "github.com/jkaninda/go-utils"
|
||||
"github.com/jkaninda/mysql-bkup/utils"
|
||||
|
||||
"os"
|
||||
@@ -37,7 +38,6 @@ import (
|
||||
func s3Backup(db *dbConfig, config *BackupConfig) {
|
||||
|
||||
utils.Info("Backup database to s3 storage")
|
||||
startTime = time.Now().Format(utils.TimeFormat())
|
||||
// Backup database
|
||||
BackupDatabase(db, config.backupFileName, disableCompression)
|
||||
finalFileName := config.backupFileName
|
||||
@@ -91,6 +91,7 @@ func s3Backup(db *dbConfig, config *BackupConfig) {
|
||||
}
|
||||
utils.Info("Backup saved in %s", filepath.Join(config.remotePath, finalFileName))
|
||||
utils.Info("Uploading backup archive to remote storage S3 ... done ")
|
||||
duration := goutils.FormatDuration(time.Since(startTime), 0)
|
||||
// Send notification
|
||||
utils.NotifySuccess(&utils.NotificationData{
|
||||
File: finalFileName,
|
||||
@@ -98,12 +99,11 @@ func s3Backup(db *dbConfig, config *BackupConfig) {
|
||||
Database: db.dbName,
|
||||
Storage: config.storage,
|
||||
BackupLocation: filepath.Join(config.remotePath, finalFileName),
|
||||
StartTime: startTime,
|
||||
EndTime: time.Now().Format(utils.TimeFormat()),
|
||||
Duration: duration,
|
||||
})
|
||||
// Delete temp
|
||||
deleteTemp()
|
||||
utils.Info("Backup completed successfully")
|
||||
utils.Info("Backup successfully completed in %s", duration)
|
||||
|
||||
}
|
||||
func s3Restore(db *dbConfig, conf *RestoreConfig) {
|
||||
|
||||
@@ -24,6 +24,8 @@ SOFTWARE.
|
||||
|
||||
package pkg
|
||||
|
||||
import "time"
|
||||
|
||||
const tmpPath = "/tmp/backup"
|
||||
const gpgHome = "/config/gnupg"
|
||||
const gpgExtension = "gpg"
|
||||
@@ -39,7 +41,7 @@ var (
|
||||
encryption = false
|
||||
usingKey = false
|
||||
backupSize int64 = 0
|
||||
startTime string
|
||||
startTime = time.Now()
|
||||
)
|
||||
|
||||
// dbHVars Required environment variables for database
|
||||
|
||||
@@ -52,8 +52,7 @@
|
||||
<h3>Backup Details:</h3>
|
||||
<ul>
|
||||
<li><strong>Database Name:</strong> {{.Database}}</li>
|
||||
<li><strong>Backup Start Time:</strong> {{.StartTime}}</li>
|
||||
<li><strong>Backup End Time:</strong> {{.EndTime}}</li>
|
||||
<li><strong>Backup Duration:</strong> {{.Duration}}</li>
|
||||
<li><strong>Backup Storage:</strong> {{.Storage}}</li>
|
||||
<li><strong>Backup Location:</strong> {{.BackupLocation}}</li>
|
||||
<li><strong>Backup Size:</strong> {{.BackupSize}}</li>
|
||||
|
||||
@@ -6,8 +6,7 @@ Please find the details below:
|
||||
|
||||
Backup Details:
|
||||
- Database Name: {{.Database}}
|
||||
- Backup Start Time: {{.StartTime}}
|
||||
- Backup EndTime: {{.EndTime}}
|
||||
- Backup Duration: {{.Duration}}
|
||||
- Backup Storage: {{.Storage}}
|
||||
- Backup Location: {{.BackupLocation}}
|
||||
- Backup Size: {{.BackupSize}}
|
||||
|
||||
@@ -39,8 +39,7 @@ type NotificationData struct {
|
||||
File string
|
||||
BackupSize string
|
||||
Database string
|
||||
StartTime string
|
||||
EndTime string
|
||||
Duration string
|
||||
Storage string
|
||||
BackupLocation string
|
||||
BackupReference string
|
||||
|
||||
Reference in New Issue
Block a user