Skip to content

Commit

Permalink
Merge pull request #53 from jkaninda/docs
Browse files Browse the repository at this point in the history
Add delete /tmp directory after backup and restore and update docs
  • Loading branch information
jkaninda authored Aug 11, 2024
2 parents b53894e + 6ae3594 commit 8339f19
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
2 changes: 1 addition & 1 deletion docs/how-tos/backup-to-ssh.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion docs/how-tos/encrypt-backup.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" \
Expand Down
7 changes: 7 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 13 additions & 6 deletions pkg/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)

}

Expand All @@ -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")
Expand All @@ -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)
Expand All @@ -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) {
Expand Down
23 changes: 23 additions & 0 deletions pkg/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
7 changes: 7 additions & 0 deletions pkg/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
}
Expand Down

0 comments on commit 8339f19

Please sign in to comment.