diff --git a/Makefile b/Makefile index c84016c..279044d 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ docker-build: docker build -f docker/Dockerfile -t ${IMAGE_NAME}:latest . docker-run: docker-build - docker run --rm --network web --name pg-bkup -v "./backup:/backup" -e "DB_HOST=${DB_HOST}" -e "DB_NAME=${DB_NAME}" -e "DB_USERNAME=${DB_USERNAME}" -e "DB_PASSWORD=${DB_PASSWORD}" -e "GPG_PASSPHRASE=${GPG_PASSPHRASE}" ${IMAGE_NAME} backup --prune --keep-last 2 + docker run --rm --network web --name pg-bkup -v "./backup:/backup" -e "DB_HOST=${DB_HOST}" -e "DB_NAME=${DB_NAME}" -e "DB_USERNAME=${DB_USERNAME}" -e "DB_PASSWORD=${DB_PASSWORD}" -e "GPG_PASSPHRASE=${GPG_PASSPHRASE}" -e "GPG_PASSPHRASE=${GPG_PASSPHRASE}" ${IMAGE_NAME} backup --prune --keep-last 2 docker-restore: docker-build docker run --rm --network web --user 1000:1000 --name pg-bkup -v "./backup:/backup" -e "DB_HOST=${DB_HOST}" -e "DB_NAME=${DB_NAME}" -e "DB_USERNAME=${DB_USERNAME}" -e "DB_PASSWORD=${DB_PASSWORD}" -e "GPG_PASSPHRASE=${GPG_PASSPHRASE}" ${IMAGE_NAME} restore -f ${FILE_NAME} diff --git a/docs/how-tos/backup-to-ssh.md b/docs/how-tos/backup-to-ssh.md index 2ba01d9..c42c2b3 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 4dea4b1..e564c49 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 gpg +To decrypt manually, you need to install `gnupg` ```shell gpg --batch --passphrase "my-passphrase" \ diff --git a/docs/index.md b/docs/index.md index 320f55b..30a36da 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/pg-bkup backup -d database_name +``` + ### Simple backup in docker compose file ```yaml diff --git a/pkg/backup.go b/pkg/backup.go index 8248ecb..f0a3afa 100644 --- a/pkg/backup.go +++ b/pkg/backup.go @@ -214,6 +214,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) { @@ -227,11 +229,12 @@ 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 { - utils.Fatal("Error uploading file to S3: %s ", err) + utils.Fatal("Error uploading backup archive to S3: %s ", err) } @@ -248,7 +251,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") @@ -259,8 +264,8 @@ 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("Backup name is %s", backupFileName) + utils.Info("Uploading backup archive to remote storage ... ") + utils.Info("Backup name is %s", finalFileName) err := CopyToRemote(finalFileName, remotePath) if err != nil { utils.Fatal("Error uploading file to the remote server: %s ", err) @@ -279,7 +284,9 @@ 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) { diff --git a/pkg/helper.go b/pkg/helper.go index 3f86ff2..65c4d39 100644 --- a/pkg/helper.go +++ b/pkg/helper.go @@ -70,4 +70,27 @@ func deleteOldBackup(retentionDays int) { utils.Fatal("Error:", 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 d24fd0a..07b62c6 100644 --- a/pkg/restore.go +++ b/pkg/restore.go @@ -98,6 +98,7 @@ func RestoreDatabase(file string) { return } utils.TestDatabaseConnection() + utils.Info("Restoring database...") extension := filepath.Ext(fmt.Sprintf("%s/%s", tmpPath, file)) // Restore from compressed file / .sql.gz @@ -107,7 +108,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 @@ -116,7 +120,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 { utils.Fatal("Unknown file extension: %s", extension) }