This script provides a simple, automated way to back up your databases to AWS S3. It supports multiple database types and can be easily configured to run on a schedule.
Supported databases:
postgres
mysql
mongodb
- Define multiple databases of different types in your configuration
- Automatic backup process for all databases in one execution
- Backups can be initiated on script execution or scheduled using a cron job
- Backups are compressed to reduce file size
- Clone this repository
- Install dependencies:
npm install
- Set up your
.env
file (see Configuration section) - Run the script:
npm run start
Standalone:
npm run start
or with Docker:
docker compose up --build
- Define database connection URI strings and backup schedule
- The script connects to the database(s) and performs a dump
- The dump is compressed and uploaded to your AWS S3 Bucket
- Dumps are cleaned up on the local file system
Create a .env
file in the root directory with the following variables:
RUN_ON_STARTUP=true
CRON=0 0 * * *
AWS_ACCESS_KEY_ID=<ACCESS_KEY_ID>
AWS_SECRET_ACCESS_KEY=<SECRET_ACCESS_KEY>
AWS_S3_BUCKET=<S3_BUCKET>
AWS_S3_REGION=<S3_REGION>
AWS_S3_ENDPOINT=<S3_ENDPOINT>
DATABASES="mysql://user:password@host:port/database,postgresql://user:password@host:port/database,mongodb://user:password@host:port"
Key | Description | Optional | Default Value |
---|---|---|---|
DATABASES |
Comma-separated connection strings list of database URIs that should be backed up. | No | [] |
RUN_ON_STARTUP |
Boolean value that indicates if the script should run immediately on startup. | Yes | false |
CRON |
Cron expression for scheduling when the backup job will run for all databases. See Crontab.guru for help setting up schedules. | Yes | |
AWS_ACCESS_KEY_ID |
AWS access key ID. | No | |
AWS_SECRET_ACCESS_KEY |
AWS secret access key. | No | |
AWS_S3_BUCKET |
Name of the S3 bucket. | No | |
AWS_S3_REGION |
Region of the S3 bucket. | No | |
AWS_S3_ENDPOINT |
Endpoint for the S3 service. | No |
A: If you're trying to backup databases that are running locally on the same server as this backup script, you may need to set the network mode to "host" in your Docker configuration. This allows the container to access the host's network stack directly. You can do this by adding --network host
when running your Docker container, or by setting network_mode: host
in your docker-compose.yml file.
Note: Using host network mode can have security implications, so only use it if necessary and understand the risks involved.
A: This error can occur due to various reasons, including incorrect connection details, network issues, firewalls, VPN configurations, or database server status. Double-check your connection string, ensure network accessibility, verify firewall settings, and confirm the database server is running and accepting connections from your container's environment.
Contributions are welcome! Please feel free to submit a Pull Request.