|
| 1 | +# Database Backup |
| 2 | + |
| 3 | +🐳 A Docker image for automated database backups from Managed Databases to S3-compatible storage. |
| 4 | + |
| 5 | +Available images: |
| 6 | +- `latest`: normal version. |
| 7 | + |
| 8 | +## Commands |
| 9 | + |
| 10 | +This image supports multiple commands: |
| 11 | + |
| 12 | +- `backup`: creates backups of managed DBs (PostgreSQL, MySQL) and uploads them to any S3-compatible storage (Scaleway Object Storage, AWS S3, MinIO, etc.) |
| 13 | +- fallback: execute the provided command. |
| 14 | + |
| 15 | +## Configuration |
| 16 | + |
| 17 | +Configuration is done using environment variables, and may depend on the command. |
| 18 | + |
| 19 | +### Command `backup` |
| 20 | + |
| 21 | +You'll need to select a DB provider, and also: |
| 22 | +- Provider-specific configuration, see below for example for [Provider scw](#provider-scw). |
| 23 | +- Configure [general S3 remote](#general-s3-remote). |
| 24 | + |
| 25 | +| Variable | Description | Required | |
| 26 | +|----------|-------------|----------| |
| 27 | +| `DB_PROVIDER` | Hosting provider (i.e. `scw`) | Yes | |
| 28 | +| `DB_INSTANCE_ID` | Database instance ID | Yes | |
| 29 | +| `DB_NAME` | Name of the database to backup | Yes | |
| 30 | + |
| 31 | +### Provider `scw` |
| 32 | + |
| 33 | +| Variable | Description | Required | |
| 34 | +|----------|-------------|----------| |
| 35 | +| `SCW_ACCESS_KEY` | Scaleway API access key | Yes | |
| 36 | +| `SCW_SECRET_KEY` | Scaleway API secret key | Yes | |
| 37 | +| `SCW_DEFAULT_ORGANIZATION_ID` | Scaleway organization ID | Yes | |
| 38 | +| `SCW_DEFAULT_PROJECT_ID` | Scaleway project ID | Yes | |
| 39 | +| `SCW_DEFAULT_REGION` | Scaleway region (e.g., `nl-ams`, `fr-par`) | Yes | |
| 40 | + |
| 41 | +### General S3 Remote |
| 42 | + |
| 43 | +Will be used by `rclone` to address a S3-bucket as a remote. |
| 44 | + |
| 45 | +| Variable | Description | Required | Example | |
| 46 | +|----------|-------------|----------|---------| |
| 47 | +| `S3_PROVIDER` | S3 provider (e.g. `Scaleway`, see rclone docs) | Yes | |
| 48 | +| `S3_ACCESS_KEY_ID` | S3 access key | Yes | - | |
| 49 | +| `S3_SECRET_ACCESS_KEY` | S3 secret key | Yes | - | |
| 50 | +| `S3_REGION` | S3 region | Yes | `nl-ams` | |
| 51 | +| `S3_ENDPOINT` | S3 endpoint URL | Yes | `s3.nl-ams.scw.cloud` | |
| 52 | +| `S3_BUCKET` | S3 bucket name | Yes | `my-backups` | |
| 53 | +| `S3_PATH` | Path prefix in bucket | Yes | `database/production` | |
| 54 | +| `S3_STORAGE_CLASS` | S3 storage class | No | Scaleway: `STANDARD`, `ONEZONE_IA`, `GLACIER` | |
| 55 | + |
| 56 | +### Webhook configuration |
| 57 | + |
| 58 | +A heartbeat url can be triggered when the job has been succesfully completed. |
| 59 | +If the environment variable is not configured, this step will be skipped. |
| 60 | + |
| 61 | +| Variable | Description | Required | Example | |
| 62 | +|----------|-------------|----------|---------| |
| 63 | +| `HEARTBEAT_URL` | Heartbeat webhook url | no | - | |
| 64 | + |
| 65 | +## Backup File Format |
| 66 | + |
| 67 | +Backups are stored with the following naming convention: |
| 68 | + |
| 69 | +``` |
| 70 | +{DB_NAME}_{TIMESTAMP}.custom |
| 71 | +``` |
| 72 | + |
| 73 | +Example: `production_20251226T01.custom` |
| 74 | + |
| 75 | +The `.custom` format is PostgreSQL's custom format, which can be restored using `pg_restore`. |
| 76 | + |
| 77 | +## Restoring Backups |
| 78 | + |
| 79 | +### PostgreSQL |
| 80 | + |
| 81 | +```bash |
| 82 | +# Restore to database |
| 83 | +pg_restore \ |
| 84 | + -h ip \ |
| 85 | + -p port \ |
| 86 | + -U user \ |
| 87 | + -d database \ |
| 88 | + --no-owner \ |
| 89 | + --no-privileges \ |
| 90 | + --single-transaction \ |
| 91 | + --exclude-schema=_timescaledb_internal \ |
| 92 | + --exclude-schema=_timescaledb_cache \ |
| 93 | + --exclude-schema=_timescaledb_catalog \ |
| 94 | + --exclude-schema=_timescaledb_config \ |
| 95 | + production_20251226T010203Z.custom |
| 96 | +``` |
| 97 | + |
| 98 | +### MySQL/MariaDB |
| 99 | + |
| 100 | +For MySQL databases, the backup format may differ. Consult documentation for the exact format. |
| 101 | + |
| 102 | +## Error Handling & Heartbeat |
| 103 | + |
| 104 | +The script will exit with code 1 and provide error details if: |
| 105 | + |
| 106 | +- Backup creation fails |
| 107 | +- S3 upload fails |
| 108 | +- Any critical step encounters an error |
| 109 | + |
| 110 | +Backup cleanup run after a successful sync to avoid accumulating backups. If configured, a heartbeat webhook will be triggered. |
0 commit comments