diff --git a/README.md b/README.md index 9af9b87..db4a4ef 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/docs/how-tos/backup-to-ssh.md b/docs/how-tos/backup-to-ssh.md index 863f661..8a88999 100644 --- a/docs/how-tos/backup-to-ssh.md +++ b/docs/how-tos/backup-to-ssh.md @@ -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 } diff --git a/docs/how-tos/encrypt-backup.md b/docs/how-tos/encrypt-backup.md index a866de0..d136fb5 100644 --- a/docs/how-tos/encrypt-backup.md +++ b/docs/how-tos/encrypt-backup.md @@ -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 diff --git a/docs/index.md b/docs/index.md index 2cb1620..c5a69cb 100644 --- a/docs/index.md +++ b/docs/index.md @@ -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 diff --git a/examples/k8s-job.yaml b/examples/k8s-job.yaml index 6d77e1b..4a12771 100644 --- a/examples/k8s-job.yaml +++ b/examples/k8s-job.yaml @@ -1,4 +1,4 @@ -piVersion: batch/v1 +apiVersion: batch/v1 kind: CronJob metadata: name: bkup-job diff --git a/pkg/backup.go b/pkg/backup.go index 627320e..0762903 100644 --- a/pkg/backup.go +++ b/pkg/backup.go @@ -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) diff --git a/pkg/helper.go b/pkg/helper.go index b016789..e59296e 100644 --- a/pkg/helper.go +++ b/pkg/helper.go @@ -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) + } } diff --git a/pkg/restore.go b/pkg/restore.go index e54605a..592cddd 100644 --- a/pkg/restore.go +++ b/pkg/restore.go @@ -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)) }