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 db.example.com dbname < db-backup.sql

You can type this instead:

gunzip < db-backup.sql.gz | mysql -p -h db.example.com 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

  1. 

    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

Leave a reply to vasu Cancel reply