|
| 1 | +# PostgreSQL Backup to S3 with Docker |
| 2 | + |
| 3 | +This application automates the process of backing up PostgreSQL databases and uploading them to an S3-compatible storage service, utilizing Docker for easy deployment and scheduling. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Easy deployment with Docker and Docker Compose. |
| 8 | +- Support for multiple PostgreSQL databases. |
| 9 | +- Customizable backup intervals. |
| 10 | +- Direct upload of backups to an S3-compatible storage bucket. |
| 11 | +- Environment variable and command-line configuration for flexibility. |
| 12 | +- Secure handling of database and S3 credentials. |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | + |
| 16 | +- Docker and Docker Compose installed on your system. |
| 17 | +- Access to a PostgreSQL database. |
| 18 | +- Access to an S3-compatible storage service. |
| 19 | + |
| 20 | +## Configuration |
| 21 | + |
| 22 | +Before running the application, you need to configure it either by setting environment variables in a `.env` file or by using command-line flags in the `docker-compose.yml`. |
| 23 | + |
| 24 | +### Environment Variables |
| 25 | + |
| 26 | +Create a `.env` file in the project directory with the following variables: |
| 27 | + |
| 28 | +- `URLS`: Comma-separated list of PostgreSQL database URLs to backup. Format: `postgres://<user>:<password>@<host>[:<port>]/<dbname>` |
| 29 | +- `S3_ENDPOINT`: The endpoint URL of your S3-compatible storage service. |
| 30 | +- `S3_BUCKET`: The name of the bucket where backups will be stored. |
| 31 | +- `S3_ACCESS_KEY`: Your S3 access key. |
| 32 | +- `S3_SECRET_KEY`: Your S3 secret key. |
| 33 | +- `INTERVAL`: How often to run the backup (e.g., `24h` for daily backups). |
| 34 | + |
| 35 | +### Docker Compose |
| 36 | + |
| 37 | +Alternatively, you can specify the configuration directly in the `docker-compose.yml` file under the `environment` section of your service: |
| 38 | + |
| 39 | +```yaml |
| 40 | +services: |
| 41 | + app: |
| 42 | + build: . |
| 43 | + environment: |
| 44 | + URLS: "postgres://user:password@host:port/dbname" |
| 45 | + S3_ENDPOINT: "your_s3_endpoint" |
| 46 | + S3_BUCKET: "your_s3_bucket" |
| 47 | + S3_ACCESS_KEY: "your_s3_access_key" |
| 48 | + S3_SECRET_KEY: "your_s3_secret_key" |
| 49 | + INTERVAL: "24h" |
| 50 | +``` |
| 51 | +
|
| 52 | +## Running the Application with Docker |
| 53 | +
|
| 54 | +There is an image available on `ghcr.io/thedevminertv/postgres_s3_backup` that you can use. |
| 55 | + |
| 56 | +Alternatively, you can build the image yourself: |
| 57 | + |
| 58 | +1. Build the Docker image: |
| 59 | + |
| 60 | + ```sh |
| 61 | + docker compose build |
| 62 | + ``` |
| 63 | + |
| 64 | +2. Start the application: |
| 65 | + |
| 66 | + ```sh |
| 67 | + docker compose up -d |
| 68 | + ``` |
| 69 | + |
| 70 | +This will start the application in the background. It will automatically perform backups based on the configured interval and upload them to the specified S3 bucket. |
| 71 | + |
| 72 | +## Monitoring and Logs |
| 73 | + |
| 74 | +To monitor the application's activity and view logs: |
| 75 | + |
| 76 | +```sh |
| 77 | +docker compose logs -f |
| 78 | +``` |
| 79 | + |
| 80 | +This command will follow the log output of the container. Press `Ctrl+C` to exit log following. |
| 81 | + |
| 82 | +## Updating Configuration |
| 83 | + |
| 84 | +If you need to update the configuration, modify the `.env` file or the `docker-compose.yml` as necessary and restart the service: |
| 85 | + |
| 86 | +```sh |
| 87 | +docker compose down |
| 88 | +docker compose up -d |
| 89 | +``` |
0 commit comments