Beginners Guide: Backup And Restore Compressed MySQL Dump File

As you know your website data is stored in your database server, in this case MySQL. To prevent disaster, backing up your data regularly is a must for you and every website owner. There are many ways to backup MySQL data, one of them is using mysqldump to dump your MySQL data to a file.

But mysqldump output is plain text files full of SQL commands. In Linux you can compress it using gzip with a pipelining command.

To backup and compress a MySQL dump file:

$ mysqldump -u [username] -p[pass] --complete-insert [dbname] | gzip -9 > [backupfilename.sql.gz]

Explanation: mysqldump will dump MySQL data according to the credentials given; the output will be piped to gzip to compress it with the highest compression rate and output as backupfilename.sql.gz.

Note: –complete-insert will remove the old table and create a new one, then fill the data.

To restore from a compressed MySQL dump file:

gunzip < [backupfile.sql.gz] | mysql -u [uname] -p[pass] [dbname]

Explanation: gunzip will extract the compressed file and the output (the SQL commands) will be run in the MySQL database with the credentials given. The SQL commands will create or overwrite the existing table and data.

Wise man said: Backup regularly, backup often.