Any project requires high-quality data along with accuracy for success. Protecting information requires users to create a backup of their MySQL database. In this article, we will look at several main ways to create backups.
The mysqldump tool is widely used to create backups of MySQL databases. Using SQL script commands, you can export both the database structure and data to successfully restore information.
Execute the following command in the terminal:
mysqldump -u [username] -p [database_name] > [path_to_file]/backup.sql
After execution, the system will prompt for the MySQL user password.
To create backups of multiple databases, use the --databases parameter:
mysqldump -u [username] -p --databases [database1] [database2] > [path_to_file]/backup.sql
To create a backup of all databases on the server, use the --all-databases parameter:
mysqldump -u [username] -p --all-databases > [path_to_file]/all_databases_backup.sql
To save disk space, you can compress the backup file using gzip:
mysqldump -u [username] -p [database_name] | gzip > [path_to_file]/backup.sql.gz
To avoid overwriting files and maintain a backup history, add a timestamp to the file name:
mysqldump -u [username] -p [database_name] > [path_to_file]/backup_$(date +%Y%m%d).sql
phpMyAdmin provides a convenient interface for managing MySQL databases. To create a backup using phpMyAdmin, follow these steps:
At this stage, select the export method:
MySQL Workbench offers a graphical interface for database management, including backups. To create a backup, follow these steps:
After completion, the backup file will be saved to the specified location
Execute the following command in the terminal:
mysql -u [username] -p [database_name] < [path_to_file]/backup.sql
After logging in, select the target database in phpMyAdmin, choose the backup file for import, and click "Go" to start the recovery process.
After connecting to the server, go to "Server" > "Data Import," select the backup file, and click "Start Import."
openssl enc -aes-256-cbc -salt -in backup.sql.gz -out backup.sql.gz.enc -k "password"
0 2 * * * mysqldump -u [username] -p[password] [database_name] | gzip > /backup/mysql_backup_$(date +\%Y\%m\%d).sql.gz
This command will run every night at 2:00 AM, saving the backup in a compressed format.
Every organization values data as its most critical corporate resource. Since backup systems attract malicious actors, data protection is a top priority.
For maximum data security, check out our article "MySQL Security on Hosting: A Comprehensive Approach to Protecting Your Database".