This script automates the process of backing up all user databases from a MySQL server and uploading the backups to a Cloudflare R2 bucket.
- Connects to a MySQL server using provided credentials.
- Fetches a list of all databases and filters out system databases.
- Creates a compressed backup for each user database.
- Uploads each backup file to a specified Cloudflare R2 bucket.
- Uses environment variables for configuration, ensuring security and flexibility.
- Bun installed on your system.
mysqldump
command-line utility available in your system's PATH.- Access to a MySQL server.
- A Cloudflare R2 bucket and API credentials.
-
Clone the repository:
git clone https://github.com/yandearta/db2r2.git cd db2r2
-
Install dependencies:
bun install
-
Configure environment variables:
Create a
.env
file in the root of the project by copying the example file:cp .env.example .env
Then, edit the
.env
file with your database and R2 credentials:DB_HOST=your_database_host DB_PORT=your_database_port DB_USER=your_database_user DB_PASSWORD=your_database_password R2_ACCESS_KEY=your_r2_access_key R2_SECRET_KEY=your_r2_secret_key R2_ENDPOINT=your_r2_endpoint R2_BUCKET=your_r2_bucket
To run the backup script, use the following command:
bun start
This will execute the src/backup.ts
script, which performs the database backups and uploads them to your R2 bucket.
This project uses ESLint for code quality. To run the linter, use:
bun lint
To automatically fix linting issues, run:
bun lint:fix