π Automated MariaDB database backups with S3-compatible storage integration
- ποΈ MariaDB Backup to S3
# Using pre-built binary (check Releases page for latest version)
curl -LO https://github.com/capcom6/mariadb-backup-s3/releases/latest/download/mariadb-backup-s3_Linux_x86_64.tar.gz
tar -xzf mariadb-backup-s3_Linux_x86_64.tar.gz
chmod +x mariadb-backup-s3
./mariadb-backup-s3 --help
# Or via go install
go install github.com/capcom6/mariadb-backup-s3@latest
# Configure & run
cp .env.example .env
nano .env # Edit with your credentials
./mariadb-backup-s3- π‘οΈ Full database backups using
mariabackup - ποΈ Compression to
.tar.gzformat - π Optional encryption using AES-256-GCM
- βοΈ Multiple storage backends (S3-compatible, FTP, filesystem)
- π Pluggable storage interface for extensibility
- π Automatic backup rotation
- π³ Docker container support
The backup process follows these steps:
- π Create temporary working directory
- πΎ Perform MariaDB backup using
mariabackup --backup - π§ Prepare backup for consistency using
mariabackup --prepare - ποΈ Compress backup to
.tar.gzarchive - π Encrypt archive using AES-256-GCM
- π Upload archive to configured storage backend
- π§Ή Clean up old backups based on retention policy
The restore process follows these steps:
- π₯ Download the backup file from the specified storage backend
- π Decrypt the backup using AES-256-GCM
- ποΈ Decompress the
.tar.gzarchive - π Restore the database files to specified directory
The tool uses a temporary working directory to store intermediate files. By default, it uses the system's default temporary directory (e.g., /tmp). If for some reason you need to use a different directory, you can specify it via the TMPDIR environment variable.
export TMPDIR=/mnt/data/backup
./mariadb-backup-s3 backup- Go 1.23+ (for building from source)
- MariaDB server
- At least 2x the actual database size in free space (for successful backup)
- Storage backend credentials (depending on chosen storage type)
- Visit the Releases page
- Download the appropriate binary for your OS
- Make executable:
chmod +x mariadb-backup-s3 - Move to PATH:
sudo mv mariadb-backup-s3 /usr/local/bin/
go install github.com/capcom6/mariadb-backup-s3@latestdocker pull ghcr.io/capcom6/mariadb-backup-s3:latestNote The Docker image uses MariaDB's
ltsversion. For specific versions:
- Clone the repository
- Modify
Dockerfilebase image- Build custom image:
docker build -t custom-backup-image .
git clone https://github.com/capcom6/mariadb-backup-s3.git
cd mariadb-backup-s3
go build -o mariadb-backup-s3The tool supports loading configuration from multiple sources:
.envfile in the current directory- Environment variables
- Command-line flags
The priority order is: .env > Environment variables > Command-line flags
The logging system can be configured via environment variables:
LOG_LEVEL: Set log level (debug, info, warn, error, fatal). Default: infoLOG_FORMAT: Set format (human, json). Default: humanLOG_OUTPUT: Set output destination:stdout(default): Standard outputstderr: Standard error- Any file path: Write logs to the specified file
NO_COLOR: When set (any non-empty value), disables colored output for human format
mariadb-backup-s3 [global options] command [command options] [arguments...]The tool offers the following commands:
| Command | Description |
|---|---|
backup |
Perform a backup of the MariaDB database |
restore |
Restore the database files to specified directory |
mariadb-backup-s3 backup [options]Options:
| Option | Env Var | Description | Default value |
|---|---|---|---|
| Database | |||
--db-host, --host |
MARIADB__HOST |
MariaDB hostname | localhost |
--db-port, --port |
MARIADB__PORT |
MariaDB port | 3306 |
--db-user, --user |
MARIADB__USER |
MariaDB username | root |
--db-password, --password |
MARIADB__PASSWORD |
MariaDB password | "" |
| Storage | |||
--storage, --storage-url |
STORAGE__URL |
Storage URL, see Storage Types | required |
| Encryption | |||
--encryption-key |
ENCRYPTION__KEY |
Encryption key | "" |
| mariadb-backup | |||
--db-backup-binary |
MARIADB__BACKUP_BINARY |
MariaDB backup binary path | mariadb-backup |
--db-backup-options |
MARIADB__BACKUP_OPTIONS |
MariaDB backup options | "" |
| Retention | |||
--backup-limits-max-count |
BACKUP__LIMITS__MAX_COUNT |
Number of backups to keep, 0 = unlimited | 0 |
Example:
./mariadb-backup-s3 backup \
--db-host=mariadb.example.com \
--db-user=backup \
--storage-url="file:///var/backups/mariadb"mariadb-backup-s3 restore [options] filenameOptions:
| Option | Env Var | Description | Default value |
|---|---|---|---|
| Storage | |||
--storage, --storage-url |
STORAGE__URL |
Storage URL, see Storage Types | required |
| Encryption | |||
--encryption-key |
ENCRYPTION__KEY |
Encryption key | "" |
| Restore | |||
--target-dir |
RESTORE__TARGET_DIR |
Target directory to restore files to | required |
Arguments:
| Argument | Description |
|---|---|
filename |
Backup file name |
Example:
./mariadb-backup-s3 restore \
--storage-url="file:///var/backups/mariadb" \
--target-dir=/var/lib/mariadb \
backup_name.tar.gzFor S3-compatible storage (including AWS S3, MinIO, DigitalOcean Spaces, etc.):
STORAGE__URL=s3://bucket-name/path?endpoint=https://s3.example.comRequired for S3:
AWS_ACCESS_KEY: Your access keyAWS_SECRET_KEY: Your secret keyAWS_REGION: AWS region (or any region for non-AWS S3)
Query Parameters:
endpoint: S3 endpoint URLs3-force-path-style: Set to "true" to use path-style URLs
For FTP servers:
STORAGE__URL=ftp://username:password@host:port/pathRequired for FTP:
username: FTP username (defaults to "anonymous" if not provided)password: FTP password (optional for anonymous)host: FTP server hostnameport: FTP port (defaults to 21)
For local or mounted filesystem storage:
STORAGE__URL=file:///absolute/path/to/backup/directoryExamples:
- Linux/macOS:
file:///var/backups/mariadb - Windows:
file://C:/backups/mariadb - Docker volume:
file:///data/backups
The backup system supports client-side encryption using AES-256 in Galois/Counter Mode (GCM) to ensure your database backups remain confidential and secure. This method provides both confidentiality and integrity verification.
Features:
- 256-bit key strength
- Authenticated encryption with additional data (AEAD)
- Automatic nonce generation for each backup
Configuration:
# base64-encoded encryption key
ENCRYPTION__KEY=Av2cfWJ3enCHTyzPdzowfAXshvJtbEsvwgPjV46wnjc=- Never commit encryption keys to version control
- Store keys in secure environment variables or dedicated secret management systems
- Implement proper access controls for key storage
Generate secure encryption keys using cryptographically secure methods:
# Generate a 32-byte (256-bit) key for AES256-GCM
openssl rand -base64 32
# Alternative method using /dev/urandom
head -c 32 /dev/urandom | base64- systemd Service Example: examples/systemd-service
- Docker Swarm CRON Example: examples/docker-cron-backup
- Simple CRON Example: examples/simple-cron-backup
- Advanced CRON Example: examples/advanced-cron-backup
- Encryption Example: examples/encryption-example
We welcome contributions! Please follow these steps:
- π΄ Fork the repository
- πΏ Create a feature branch:
git checkout -b feat/amazing-feature - πΎ Commit changes:
git commit -m 'Add amazing feature' - π Push to branch:
git push origin feat/amazing-feature - π Create a Pull Request
Apache 2.0 - See LICENSE for details.
π‘ Need Help? Open an issue for support.