Using mysqldump to back up a database

If you want to make a super quick backup of a MySQL database, the best tool for the job is the native mysqldump. Its also a command line utility, so you can use it from a telnet or SSH connection, etc…

The basic command structure of mysqldump is:

mysqldump -u {username} -p{password} {database} > {backup-file}

I.e. If you had root access, and you wanted to backup all the tables within the “customers” database you could run this command:

mysqldump -u root -p customers > mybackup.sql

This would dump all the data to the mybackup.sql file. Note I didn’t enter a password. Mysql will ask you for your password in this instance. If you wanted to enter the password (where it could be seen, or saved into a BASH history) then you use the command -pABC123 where ABC123 is the password (note the lack of a space between -p and ABC123 – this is important!)

But if you are security conscious, omit the actual password from the command line, and merely use -p as this will prompt you for the password and therefore BASH or DOS will not store it.

If you wanted to only dump certain tables from the database, specify them after the database. For example, to back up the “address” and “billing” tables:

mysqldump -u root -p customers address billing > mybackup.sql

Restoring a mysqldump file

If you want to restore a mysqldump’ed file into MySQL – you can simply run the following command:

mysql -u {username} -p{password} {database} < {backup-file}

Note the change of direction for the < symbol and the fact we’re calling the mysql command, and not the mysqldump command!!!

So, to squirt my backupfile back into the customers database

mysql -u root -pABC123 customers < mybackup.sql

NB. This will overwrite your current database tables with whatever is in the backupfile!

Using mysqldump to backup ALL databases

If you have lots of databases, then you simply use the –all-databases command to back them all up into one file:

mysqldump -u root -pABC123 --all-databases > mytotalbackup.sql

Using mysqldump to backup multiple databases

By using the –databases switch, we can state which databases we want backed up. Separate each database name with a space.

So to backup “customers”, “orders” and “misc” we use the following syntax:

mysqldump -u root -pABC123 --databases customers orders misc > multiple-databases.sql

Advanced techniques

Compressed backups

More often than not, a database dump will be several hundred MB in size, if not more. MySQL can zip this for you automatically using the gzip command:

mysqldump -u root -pABC123 customers | gzip > compressed-backup.sql.gz

The pipe before the gzip tells mysqldump to send the output to the next command, which is gzip and that, in turn, is asked to create “compressed-backup.sql.gz”

Dumping the data definitions

If you wanted a “skeleton” structure of the entire database, then mysqldump will allow this, by using the –no-data switch:

mysqldump -u root -pABC123 --no-data customers > customers-structure.sql

By admin

2 thought on “How to use mysqldump”
  1. “mysql -u root -pABC123 –no-data customers > customers-structure.sql” – I think there’s a typo there, it’s meant to by mysqldump.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.