Add delete /tmp directory after backup or restore and update docs

This commit is contained in:
2024-08-11 09:38:31 +02:00
parent 27b9ab5f36
commit a3fc58af96
8 changed files with 59 additions and 10 deletions

View File

@@ -50,6 +50,12 @@ To run a one time backup, bind your local volume to `/backup` in the container a
Alternatively, pass a `--env-file` in order to use a full config as described below.
```yaml
docker run --rm --network your_network_name \
--env-file your-env-file
-v $PWD/backup:/backup/ \
jkaninda/mysql-bkup backup -d database_name
```
### Simple backup in docker compose file

View File

@@ -7,7 +7,7 @@ nav_order: 3
# Backup to SSH remote server
As described for s3 backup section, to change the storage of you backup and use S3 as storage. You need to add `--storage ssh` or `--storage remote`.
As described for s3 backup section, to change the storage of your backup and use SSH Remote server as storage. You need to add `--storage ssh` or `--storage remote`.
You need to add the full remote path by adding `--path /home/jkaninda/backups` flag or using `SSH_REMOTE_PATH` environment variable.
{: .note }

View File

@@ -11,7 +11,7 @@ The image supports encrypting backups using GPG out of the box. In case a `GPG_P
{: .warning }
To restore an encrypted backup, you need to provide the same GPG passphrase used during backup process.
To decrypt manually, you need to install gnupg
To decrypt manually, you need to install `gnupg`
### Decrypt backup

View File

@@ -45,6 +45,13 @@ To run a one time backup, bind your local volume to `/backup` in the container a
Alternatively, pass a `--env-file` in order to use a full config as described below.
```yaml
docker run --rm --network your_network_name \
--env-file your-env-file
-v $PWD/backup:/backup/ \
jkaninda/mysql-bkup backup -d database_name
```
### Simple backup in docker compose file
```yaml

View File

@@ -1,4 +1,4 @@
piVersion: batch/v1
apiVersion: batch/v1
kind: CronJob
metadata:
name: bkup-job

View File

@@ -203,6 +203,8 @@ func localBackup(backupFileName string, disableCompression bool, prune bool, bac
if prune {
deleteOldBackup(backupRetention)
}
//Delete temp
deleteTemp()
}
func s3Backup(backupFileName string, disableCompression bool, prune bool, backupRetention int, encrypt bool) {
@@ -216,7 +218,7 @@ func s3Backup(backupFileName string, disableCompression bool, prune bool, backup
encryptBackup(backupFileName)
finalFileName = fmt.Sprintf("%s.%s", backupFileName, "gpg")
}
utils.Info("Uploading backup file to S3 storage...")
utils.Info("Uploading backup archive to remote storage S3 ... ")
utils.Info("Backup name is %s", finalFileName)
err := utils.UploadFileToS3(tmpPath, finalFileName, bucket, s3Path)
if err != nil {
@@ -237,7 +239,9 @@ func s3Backup(backupFileName string, disableCompression bool, prune bool, backup
utils.Fatal("Error deleting old backup from S3: %s ", err)
}
}
utils.Done("Database has been backed up and uploaded to s3 ")
utils.Done("Uploading backup archive to remote storage S3 ... done ")
//Delete temp
deleteTemp()
}
func sshBackup(backupFileName, remotePath string, disableCompression bool, prune bool, backupRetention int, encrypt bool) {
utils.Info("Backup database to Remote server")
@@ -248,7 +252,7 @@ func sshBackup(backupFileName, remotePath string, disableCompression bool, prune
encryptBackup(backupFileName)
finalFileName = fmt.Sprintf("%s.%s", backupFileName, "gpg")
}
utils.Info("Uploading backup file to remote server...")
utils.Info("Uploading backup archive to remote storage ... ")
utils.Info("Backup name is %s", finalFileName)
err := CopyToRemote(finalFileName, remotePath)
if err != nil {
@@ -268,9 +272,10 @@ func sshBackup(backupFileName, remotePath string, disableCompression bool, prune
}
utils.Done("Database has been backed up and uploaded to remote server ")
utils.Done("Uploading backup archive to remote storage ... done ")
//Delete temp
deleteTemp()
}
func encryptBackup(backupFileName string) {
gpgPassphrase := os.Getenv("GPG_PASSPHRASE")
err := Encrypt(filepath.Join(tmpPath, backupFileName), gpgPassphrase)

View File

@@ -71,4 +71,28 @@ func deleteOldBackup(retentionDays int) {
utils.Fatal(fmt.Sprintf("Error: %s", err))
return
}
utils.Done("Deleting old backups...done")
}
func deleteTemp() {
utils.Info("Deleting %s ...", tmpPath)
err := filepath.Walk(tmpPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Check if the current item is a file
if !info.IsDir() {
// Delete the file
err = os.Remove(path)
if err != nil {
return err
}
}
return nil
})
if err != nil {
utils.Error("Error deleting files: %v", err)
} else {
utils.Info("Deleting %s ... done", tmpPath)
}
}

View File

@@ -44,7 +44,7 @@ func restoreFromS3(file, bucket, s3Path string) {
utils.Info("Restore database from s3")
err := utils.DownloadFile(tmpPath, file, bucket, s3Path)
if err != nil {
utils.Fatal(fmt.Sprintf("Error download file from s3 %s %s", file, err))
utils.Fatal("Error download file from s3 %s %v", file, err)
}
RestoreDatabase(file)
}
@@ -52,7 +52,7 @@ func restoreFromRemote(file, remotePath string) {
utils.Info("Restore database from remote server")
err := CopyFromRemote(file, remotePath)
if err != nil {
utils.Fatal(fmt.Sprintf("Error download file from remote server: ", filepath.Join(remotePath, file), err))
utils.Fatal("Error download file from remote server: %s %v ", filepath.Join(remotePath, file), err)
}
RestoreDatabase(file)
}
@@ -94,6 +94,7 @@ func RestoreDatabase(file string) {
if utils.FileExists(fmt.Sprintf("%s/%s", tmpPath, file)) {
utils.TestDatabaseConnection()
utils.Info("Restoring database...")
extension := filepath.Ext(fmt.Sprintf("%s/%s", tmpPath, file))
// Restore from compressed file / .sql.gz
@@ -103,7 +104,10 @@ func RestoreDatabase(file string) {
if err != nil {
utils.Fatal("Error, in restoring the database %v", err)
}
utils.Info("Restoring database... done")
utils.Done("Database has been restored")
//Delete temp
deleteTemp()
} else if extension == ".sql" {
//Restore from sql file
@@ -112,7 +116,10 @@ func RestoreDatabase(file string) {
if err != nil {
utils.Fatal(fmt.Sprintf("Error in restoring the database %s", err))
}
utils.Info("Restoring database... done")
utils.Done("Database has been restored")
//Delete temp
deleteTemp()
} else {
utils.Fatal(fmt.Sprintf("Unknown file extension %s", extension))
}