mysqldump a zipped dump

Till now I've created and zipped mysql dumps this way:

mysqldump -u root -p --all-databases > /var/www/html/db-$(date +\%F-\%T).sql
zip /var/www/html/db-$(date +\%F-\%T).zip /var/www/html/db-*.sql
rm /var/www/html/db-*.sql

Is there a way to have it zipped directly from mysqldump and save these 2 extra rows?


Try this:

mysqldump -u root -p --all-databases | zip /var/www/html/db-$(date +\%F-\%T).zip

You will only have one file into the zip archive having - as file name.


The other two answers have already stated the following solution (so consider this an extension of the other two answers):

mysqldump -u root -p --all-databases | zip /var/www/html/db-$(date +\%F-\%T).zip -

This is good, but this works only on stdin/stdout. This means that the file will be stored as - inside the archive, and can only normally be extracted sanely to stdin. This means to actually extract this data from the ZIP, you will have to do this:

unzip -p myZipFile.zip > myBackup.sql

If you want to make a "normal" ZIP file (where things have a real name), you will have to do some really complicated things or just do what you're already doing so there's no net gain.

As MySQL itself is stdin-aware, you can restore directly from a backup using the below command (as an example):

unzip -p myDataBackup.zip | mysql -uroot -pMyInsecurePassword

You can try this

mysqldump -u root -p --all-databases | zip /var/www/html/db-$(date +\%F-\%T).zip -