Taking database backup on a regular basis is one of the crucial steps after making the project live. The backup frequency could be either once in a day or week. The daily backup is preferred for the applications in which database changes are more frequent. In the case of high-availability databases, appropriate cluster setup must be considered instead of vertical scaling. This tutorial explains the usage of the
Notes: This tutorial only covers the most common options used with the
Local Backup
Backup - This section explains taking local backup in a file by exporting either single or multiple databases.
Permissions -
- SELECT - to dump tables
- SHOW VIEW - to dump views
- TRIGGER - to dump triggers
- LOCK TABLES - in case --single-transaction option is not used
Command - Below mentioned is the usage of
# Regular command - Usage - Windows, Linux
mysqldump [options] db_name [tbl_name ...] > <file path>
mysqldump [options] --databases db_name ... > <file path>
mysqldump [options] --all-databases > <file path>
The examples to use the regular command are as mentioned below. These example commands are using the user enterprise to take backups of the database enterprise. There is no need to specify user and password in case the user account is using auth_socket algorithm to login to the MySQL server. The system user can directly access the database. You may refer How To Use Native Password With MySQL 5.7 and How To Install MySQL 8 on Ubuntu to learn more about password algorithms.
Notes: There is no need to use the option -p, in case no password is assigned to the user.
Below mentioned are the examples to back up a single database with username, host, and port. The password must not be provided with these commands for security purpose. You may specify the password for
# All tables - with username, without password - prompts for password
mysqldump -u enterprise -p enterprise > enterprise.sql
# All tables - with username, with password - insecure
mysqldump -u enterprise -p'mypwd' enterprise > enterprise.sql
# All tables - with username, with password - Plesk way
mysqldump -u enterprise -p`cat /etc/psa/.psa.shadow` dbname > dbname.sql
# All tables - with host, with port, with username, without password - prompts for password
mysqldump -h localhost -p 3306 -u enterprise -p enterprise > enterprise.sql
# Single table(user) - with username, without password - prompts for password
mysqldump -u enterprise -p enterprise [user] > enterprise.sql
# Multiple tables(user, profile) - with username, without password - prompts for password
mysqldump -u enterprise -p enterprise user profile > enterprise.sql
Below mentioned are the examples to back up multiple databases with username, host, and port.
# Multiple databases - with username, without password - prompts for password
mysqldump -u enterprise -p --databases enterprise vblog mshop > mydbs.sql
# Multiple databases - with username, with password
mysqldump -u enterprise -p'mypwd' --databases enterprise vblog mshop > mydbs.sql
Below mentioned is the example to back up all the databases with username, host, and port.
# All databases - with username, without password - prompts for password
mysqldump -u enterprise -p --all-databases > mydbs.sql
# All databases - with username, with password
mysqldump -u enterprise -p'mypwd' --all-databases > mydbs.sql
The PowerShell users on Windows will need different command as mentioned below.
# PowerShell on Windows
shell> mysqldump [options] db_name [tbl_name ...] --result-file=<file path>
shell> mysqldump [options] --databases db_name ... --result-file=<file path>
shell> mysqldump [options] --all-databases --result-file=<file path>
# Example
shell> mysqldump -u enterprise -p enterprise --result-file=mydump.sql
The above-mentioned commands will export the selected database and tables to the file location specified in the command.
Database Backup
This section explains taking the database backup to another database used for backup purpose using the Shell Pipes. We can either export the database to the local file and then use this file to import to another database or directly export to another database using shell pipes.
The below-mentioned command can be used to export the database directly to another database. Also, make sure that you have access to both the servers before executing the command.
# Export database to another database - Same server
mysqldump -u enterprise -p enterprise | mysql enterprise_backup
# Export database to another database - Different server
mysqldump -u enterprise -p enterprise | mysql -h host.example.com -p 3306 enterprise
This is how we can export or backup a database.