Files
mysql-bkup/pkg/azure.go

122 lines
3.8 KiB
Go
Raw Normal View History

2024-12-06 18:27:25 +01:00
/*
MIT License
Copyright (c) 2023 Jonas Kaninda
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
2024-12-06 20:53:46 +01:00
package pkg
2024-12-06 18:27:25 +01:00
import (
"fmt"
"github.com/jkaninda/go-storage/pkg/azure"
"github.com/jkaninda/mysql-bkup/utils"
"os"
"path/filepath"
"time"
)
func azureBackup(db *dbConfig, config *BackupConfig) {
2024-12-08 00:00:24 +01:00
utils.Info("Backup database to Azure Blob Storage")
2024-12-06 18:27:25 +01:00
startTime = time.Now().Format(utils.TimeFormat())
// Backup database
BackupDatabase(db, config.backupFileName, disableCompression)
finalFileName := config.backupFileName
if config.encryption {
encryptBackup(config)
finalFileName = fmt.Sprintf("%s.%s", config.backupFileName, "gpg")
}
2024-12-06 20:53:46 +01:00
utils.Info("Uploading backup archive to Azure Blob storage ...")
utils.Info("Backup name is %s", finalFileName)
2024-12-06 18:27:25 +01:00
azureConfig := loadAzureConfig()
azureStorage, err := azure.NewStorage(azure.Config{
ContainerName: azureConfig.containerName,
AccountName: azureConfig.accountName,
AccountKey: azureConfig.accountKey,
RemotePath: config.remotePath,
LocalPath: tmpPath,
})
if err != nil {
2024-12-06 20:53:46 +01:00
utils.Fatal("Error creating Azure storage: %s", err)
2024-12-06 18:27:25 +01:00
}
err = azureStorage.Copy(finalFileName)
if err != nil {
2024-12-06 20:53:46 +01:00
utils.Fatal("Error copying backup file: %s", err)
2024-12-06 18:27:25 +01:00
}
2024-12-06 20:53:46 +01:00
utils.Info("Backup saved in %s", filepath.Join(config.remotePath, finalFileName))
2024-12-06 18:27:25 +01:00
// Get backup info
fileInfo, err := os.Stat(filepath.Join(tmpPath, finalFileName))
if err != nil {
2024-12-06 20:53:46 +01:00
utils.Error("Error: %s", err)
2024-12-06 18:27:25 +01:00
}
backupSize = fileInfo.Size()
// Delete backup file from tmp folder
err = utils.DeleteFile(filepath.Join(tmpPath, finalFileName))
if err != nil {
2024-12-06 20:53:46 +01:00
utils.Error("Error deleting file: %v", err)
2024-12-06 18:27:25 +01:00
}
if config.prune {
err := azureStorage.Prune(config.backupRetention)
if err != nil {
2024-12-06 20:53:46 +01:00
utils.Fatal("Error deleting old backup from %s storage: %s ", config.storage, err)
2024-12-06 18:27:25 +01:00
}
}
2024-12-06 20:53:46 +01:00
utils.Info("Uploading backup archive to Azure Blob storage ... done ")
2024-12-06 18:27:25 +01:00
// Send notification
utils.NotifySuccess(&utils.NotificationData{
File: finalFileName,
BackupSize: backupSize,
Database: db.dbName,
Storage: config.storage,
BackupLocation: filepath.Join(config.remotePath, finalFileName),
StartTime: startTime,
EndTime: time.Now().Format(utils.TimeFormat()),
})
// Delete temp
deleteTemp()
2024-12-06 20:53:46 +01:00
utils.Info("Backup completed successfully")
2024-12-06 18:27:25 +01:00
}
func azureRestore(db *dbConfig, conf *RestoreConfig) {
2024-12-06 20:53:46 +01:00
utils.Info("Restore database from Azure Blob storage")
2024-12-06 18:27:25 +01:00
azureConfig := loadAzureConfig()
azureStorage, err := azure.NewStorage(azure.Config{
ContainerName: azureConfig.containerName,
AccountName: azureConfig.accountName,
AccountKey: azureConfig.accountKey,
RemotePath: conf.remotePath,
LocalPath: tmpPath,
})
if err != nil {
2024-12-06 20:53:46 +01:00
utils.Fatal("Error creating SSH storage: %s", err)
2024-12-06 18:27:25 +01:00
}
err = azureStorage.CopyFrom(conf.file)
if err != nil {
2024-12-06 20:53:46 +01:00
utils.Fatal("Error downloading backup file: %s", err)
2024-12-06 18:27:25 +01:00
}
RestoreDatabase(db, conf)
}