In this guide, I will show you how to back up and restore MySQL or MariaDB databases using the mysqldump command line from the terminal.
Table of Contents
MySQL Backup Strategies
- Physical Backups: Directly copying the database files. Fast recovery but requires downtime.
- Logical Backups: Extracting data as SQL statements, allowing for portability but slower to restore.
- Incremental Backups: Only saving changes since the last backup, reducing time and storage needs.
- Automation: Using tools like cron for regular and consistent backups without manual intervention.
- Verification: Regularly checking backups for integrity to ensure they are usable.
- Recovery Testing: Regularly testing the recovery process to ensure minimal downtime during actual data loss.
Here’s an illustrated image representing MySQL backup strategies.
Backup a Single MySQL Database
To back up a single MySQL database, utilize the command line. Here’s how you can perform the backup using the root user and save it to a specific folder:
mysqldump -u root -p LINUXOPERATINGSYSTEM > /tmp/LINUXOPERATINGSYSTEM-$(date +%Y%m%d).sql
Replace DATABASE_NAME
with your actual database name and /path/to/backup/
with the actual path where you want to save the backup.
Restore a Single MySQL Database
To restore a single MySQL database to a new host, follow these steps. Here’s an example:
Example my Database
- New Database: LINUXOPERATINGSYSTEM
- File dump: /tmp/LINUXOPERATINGSYSTEM-$(date +%Y%m%d).sql
mysql -u root -p -e "create database LINUXOPERATINGSYSTEM";
mysql -u root -p LINUXOPERATINGSYSTEM < /tmp/LINUXOPERATINGSYSTEM-$(date +%Y%m%d).sql
mysqldump syntax
mysqldump <OPTIONS> > dumpfile.sql
Where OPTIONS
include backing up restoring, and dumpfile.sql
is the name of the file to store the database backup.
Conclusion
Following these steps, you can effectively back up and restore MySQL databases using the mysqldump command line. This knowledge is crucial for database management and ensuring that your data is securely backed up. Thank you for following this tutorial from DevopsRoles page!