Automated MySQL Backup
· One min read
Implementation
Step 1: MySQL Backup
- Create a Bash script,
mysql_backup.sh
.- Runs
mysqldump
and compress and store the backup.
- Runs
- Delete old backups > 24 hours automatically.
- I also created a dedicated user, with minimal privileges (
SELECT
,LOCK TABLES
,EVENT
) to enhance security.
Step 2: S3 Synchronization
- Create a Bash script,
sync_to_s3.sh
.- Sync the backups to S3 using
aws s3 sync
aws s3 sync $BACKUP_DIR $S3_BUCKET --delete
--delete
: Ensures S3 mirrors the local backup directory, keeping only the latest files.- IAM roles granting least-privilege access (
s3:PutObject
,s3:GetObject
).
- Sync the backups to S3 using
Step 3: RDS Restore
- Download the backup from S3 using
aws s3 cp
- Use
mysql -h $RDS_ENDPOINT -u $RDS_USER -p$RDS_PASS < $TEMP_DIR/backup.sql
to restore the backup to the RDS instance.
Step 4: Automation
- Schedule the pipeline using cron jobs on the server: backups at :00, S3 sync at :05, and RDS restore at :15 each hour.
- This staggered schedule ensures each step completes before the next begins, maintaining data consistency.