๐Ÿ” MySQL Backup and Restore on AWS EC2 (Day 21 of 90 Days of Cloud)

Pratik DasPratik Das
4 min read

Whether youโ€™re running a personal blog or a production-grade application, backing up your MySQL databases is non-negotiable. Today, I focused entirely on learning and mastering MySQL backup and restore using both logical and physical methods.


โ˜๏ธ Why Backups Matter

Databases are the heart of dynamic applications. A single error, crash, or vulnerability can result in data loss. With proper backup strategies:

  • You protect against accidental deletions or updates.

  • You can quickly recover from system crashes.

  • You simplify migrations or cloning of databases.


๐Ÿ› ๏ธ My Setup

  • EC2 instance running Amazon Linux 2

  • MySQL 8.x or MariaDB

  • Database name: cloudapp_db

  • User: admin, Password: securepass123


โœ… Types of MySQL Backups

Backup TypeMethodUse Case
Logical BackupmysqldumpLightweight, portable SQL dump
Physical BackupFile copy (data dir)Full copy of raw data files (slower to move)

๐Ÿ”น 1. Backup Using mysqldump (Logical Backup)

This method generates .sql files you can use to recreate the DB schema and data.

โœ… Full Database Backup

mysqldump -u admin -p cloudapp_db > cloudapp_backup.sql

Youโ€™ll be prompted for the password. This exports everything: tables, structure, and data.


โœ… Backup Specific Tables

mysqldump -u admin -p cloudapp_db users orders > selected_tables.sql

This is helpful if you're only interested in a subset of data.


โœ… Backup All Databases

mysqldump -u admin -p --all-databases > all_databases_backup.sql

โœ… Add Timestamps for Automation

mysqldump -u admin -p cloudapp_db > backup_$(date +%F).sql

This appends the current date to your filename (e.g., backup_2025-07-23.sql).


๐Ÿ”„ 2. Restore a Backup

โœ… Restore from a .sql file

mysql -u admin -p cloudapp_db < cloudapp_backup.sql

This re-imports the full dump into the existing database. Ensure the DB exists before restoring:

mysql -u admin -p -e "CREATE DATABASE IF NOT EXISTS cloudapp_db;"

๐Ÿ” Backup + Restore via Compression

Compressing backups can save storage, especially with large datasets.

๐Ÿ”ธ Compressed Backup

mysqldump -u admin -p cloudapp_db | gzip > cloudapp_backup.sql.gz

๐Ÿ”ธ Restore From Compressed File

gunzip < cloudapp_backup.sql.gz | mysql -u admin -p cloudapp_db

๐Ÿงฑ Physical Backup (Advanced)

Physical backups involve copying the MySQL data directory, typically:

Edit/var/lib/mysql

โš ๏ธ You must stop MySQL before copying these files to avoid corruption.

sudo systemctl stop mysqld
sudo cp -r /var/lib/mysql /backup/mysql_raw/
sudo systemctl start mysqld

Only use this method when MySQL is down or in cold backup situations.


๐Ÿ” Automating Backups with Cron

Schedule automatic daily backups:

crontab -e

Add this line to backup every night at 2 AM:

Edit0 2 * * * mysqldump -u admin -pYourPassword cloudapp_db > /home/ec2-user/backups/backup_$(date +\%F).sql

โœ… Tip: Donโ€™t hard-code passwords โ€” use .my.cnf or env variables!


โ˜๏ธ Uploading Backups to S3 (Bonus)

aws s3 cp backup_2025-07-23.sql s3://my-db-backups/cloudapp/

Make sure your EC2 has IAM role permissions to upload to S3.


๐Ÿง  Best Practices

  • Always test your restore after taking a backup.

  • Use compression to save space.

  • Automate daily/weekly backups.

  • Store copies in multiple locations (e.g., S3, EBS, or RDS snapshots).

  • Use versioning or timestamps to avoid overwrites.


โœ… Summary

Todayโ€™s goal was to gain confidence in MySQL data protection. I now understand how to:

  • Take full and partial logical backups

  • Restore databases from dumps

  • Use compression for efficient storage

  • Schedule and automate the process

  • Push backups to cloud storage like S3


๐Ÿ“… Whatโ€™s Next?

Day 22 โ†’ Deploying a Node.js App on AWS EC2

Tomorrow, Iโ€™ll shift gears and deploy a custom Node.js application on an EC2 instance. This will help me understand:

๐Ÿ”น How modern backend apps run in the cloud
๐Ÿ”น Installing Node.js and setting up a production-ready environment
๐Ÿ”น Using PM2 to keep the app alive
๐Ÿ”น Exposing the app via NGINX as a reverse proxy

Weโ€™re now stepping into real-world cloud app deployment โ€” dynamic, scalable, and custom-built!

0
Subscribe to my newsletter

Read articles from Pratik Das directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Pratik Das
Pratik Das