One-liner: restore compressed MySQL backup

8 October 2012 — 2 Comments

One-liners are always fun. Linux has pipes, streams and redirects which you can combine to do many things in just one line. Today I had to restore a MySQL backup that was gzipped. Instead of typing:

gunzip db-backup.sql.gz
mysql -p -h dbname < db-backup.sql

You can type this instead:

gunzip < db-backup.sql.gz | mysql -p -h dbname

The .sql.gz file is input to gunzip, the resulting (unzipped) data is piped to mysql and is used as input there.

It will preserve the .gz file and saves diskspace as the unzipped file is not saved to disk. When using big databases, this is also a time saver. When using two commands you have to wait for the file to unzip, before starting the actual import. No intervention is needed when using the one-liner.

2 responses to One-liner: restore compressed MySQL backup


    Thank you man! Take that
    mysqldump -h your-host -u y-user -py-pass –hex-blob –routines –triggers dbname | gzip > ~/dumps/dump-`date +%F`.sql.gz

What do you think?

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s