2024-08-03 16:03:17 +02:00
---
2025-01-13 14:56:08 +01:00
title: Encrypt backups using GPG
2024-08-03 16:03:17 +02:00
layout: default
parent: How Tos
2024-09-30 00:40:35 +02:00
nav_order: 8
2024-08-03 16:03:17 +02:00
---
2025-01-13 14:56:08 +01:00
# Encrypt Backup
2024-08-03 16:03:17 +02:00
2025-01-13 14:56:08 +01:00
The image supports encrypting backups using one of two methods: **GPG with a passphrase** or **GPG with a public key** . When a `GPG_PASSPHRASE` or `GPG_PUBLIC_KEY` environment variable is set, the backup archive will be encrypted and saved as a `.sql.gpg` or `.sql.gz.gpg` file.
2024-10-08 23:05:10 +02:00
2025-01-13 14:56:08 +01:00
{: .warning }
To restore an encrypted backup, you must provide the same GPG passphrase or private key used during the backup process.
2024-10-08 23:05:10 +02:00
2025-01-13 14:56:08 +01:00
---
2024-08-03 16:03:17 +02:00
2025-01-13 14:56:08 +01:00
## Key Features
2024-08-03 16:03:17 +02:00
2025-01-13 14:56:08 +01:00
- **Cipher Algorithm**: `aes256`
- **Automatic Restoration**: Backups encrypted with a GPG passphrase can be restored automatically without manual decryption.
- **Manual Decryption**: Backups encrypted with a GPG public key require manual decryption before restoration.
2024-08-03 16:03:17 +02:00
2025-01-13 14:56:08 +01:00
---
2024-10-08 23:05:10 +02:00
2025-01-13 14:56:08 +01:00
## Using GPG Passphrase
2024-08-03 16:03:17 +02:00
2025-01-13 14:56:08 +01:00
To encrypt backups using a GPG passphrase, set the `GPG_PASSPHRASE` environment variable. The backup will be encrypted and can be restored automatically.
2024-08-03 16:03:17 +02:00
2025-01-13 14:56:08 +01:00
### Example Configuration
2024-08-03 16:03:17 +02:00
2025-01-13 14:56:08 +01:00
```yaml
2024-08-03 16:03:17 +02:00
services:
mysql-bkup:
2025-01-13 14:56:08 +01:00
# In production, lock your image tag to a specific release version
# instead of using `latest` . Check https://github.com/jkaninda/mysql-bkup/releases
# for available releases.
2024-08-03 16:03:17 +02:00
image: jkaninda/mysql-bkup
container_name: mysql-bkup
2024-08-10 10:50:00 +02:00
command: backup -d database
2024-08-03 16:03:17 +02:00
volumes:
- ./backup:/backup
environment:
- DB_PORT=3306
- DB_HOST=mysql
- DB_NAME=database
- DB_USERNAME=username
- DB_PASSWORD=password
## Required to encrypt backup
- GPG_PASSPHRASE=my-secure-passphrase
2025-01-13 14:56:08 +01:00
# Ensure the pg-bkup container is connected to the same network as your database
2024-08-03 16:03:17 +02:00
networks:
- web
2025-01-13 14:56:08 +01:00
2024-08-03 16:03:17 +02:00
networks:
web:
2024-10-09 12:45:55 +02:00
```
2025-01-13 14:56:08 +01:00
---
2024-10-09 12:45:55 +02:00
## Using GPG Public Key
2025-01-13 14:56:08 +01:00
To encrypt backups using a GPG public key, set the `GPG_PUBLIC_KEY` environment variable to the path of your public key file. Backups encrypted with a public key require manual decryption before restoration.
### Example Configuration
```yaml
2024-10-09 12:45:55 +02:00
services:
mysql-bkup:
2025-01-13 14:56:08 +01:00
# In production, lock your image tag to a specific release version
# instead of using `latest` . Check https://github.com/jkaninda/mysql-bkup/releases
# for available releases.
2024-10-09 12:45:55 +02:00
image: jkaninda/mysql-bkup
container_name: mysql-bkup
command: backup -d database
volumes:
- ./backup:/backup
2025-01-13 14:56:08 +01:00
- ./public_key.asc:/config/public_key.asc
2024-10-09 12:45:55 +02:00
environment:
- DB_PORT=3306
- DB_HOST=mysql
- DB_NAME=database
- DB_USERNAME=username
- DB_PASSWORD=password
## Required to encrypt backup
- GPG_PUBLIC_KEY=/config/public_key.asc
2025-01-13 14:56:08 +01:00
# Ensure the pg-bkup container is connected to the same network as your database
2024-10-09 12:45:55 +02:00
networks:
- web
2025-01-13 14:56:08 +01:00
2024-10-09 12:45:55 +02:00
networks:
web:
2025-01-13 14:56:08 +01:00
```
---
## Manual Decryption
If you encrypted your backup using a GPG public key, you must manually decrypt it before restoration. Use the `gnupg` tool for decryption.
### Decrypt Using a Passphrase
```bash
gpg --batch --passphrase "my-passphrase" \
--output database_20240730_044201.sql.gz \
--decrypt database_20240730_044201.sql.gz.gpg
```
### Decrypt Using a Private Key
```bash
gpg --output database_20240730_044201.sql.gz \
--decrypt database_20240730_044201.sql.gz.gpg
```
---
## Key Notes
- **Automatic Restoration**: Backups encrypted with a GPG passphrase can be restored directly without manual decryption.
- **Manual Decryption**: Backups encrypted with a GPG public key require manual decryption using the corresponding private key.
- **Security**: Always keep your GPG passphrase and private key secure. Use Kubernetes Secrets or other secure methods to manage sensitive data.