views:

958

answers:

4

I have the following:

 mysqldump -u xxxx 
           -h localhost 
           --password=xxxxx databasename | 
           ssh [email protected] "dd of=httpdocs/backup`date +'%Y-%m-%d-%H-%M-%S'`.sql"

...which SSH's a mysqldump to a remote machine.

I need to compress the mysqldump before it is SSH'd as the dump is 500mb and its eating up my bandwidth allowance.

+5  A: 

You can add the -C flag to the ssh call to automatically compress the transmitted data.

sth
This is interesting, I was thinking only of doing a zip of the file on the fly.
Mike Buckbee
That will be a joke of compression, though.
Michael Krelin - hacker
@hacker: Any special reason that you think this compression will be not good enough? Just that you would prefer a `-9` flag for gzip?
sth
sth, Actually, I would prefer -9 for bzip2 if I want to save bandwidth ;-) (and you can specify gipz level to ssh, anyway), but this is not what I meant here. Now that I think of it I am not sure if I was right, anyway. But my guess is that ssh does compression in shorter chunks - otherwise it wouldn't be half as responsive as it is. And doing compression in small chunks of course would prevent it from efficiently compressing the stream. If you seriously know what you're talking about I would love to learn I'm wrong here (if I am).
Michael Krelin - hacker
+4  A: 

mysqldump ... | gzip -9 | ssh ...

or

mysqldump ... | bzip2 -9 | ssh ...

or, if you want it uncompressed on the other end

mysqldump ... | bzip2 -9 | ssh machine "bzip2 -d >..."

mysqldump ... | gzip -9 | ssh machine "gzip -d >..."

Michael Krelin - hacker
+2  A: 

You need to call gzip between mysqldump and ssh, like:

mysqldump [mysql options] | gzip | ssh [ssh options]

I would recommend changing the saved file extension to ".sql.gz" as well.

Mike Buckbee
should be `.sql.gz` then - the outermost encapsulation at the end.
Michael Krelin - hacker
Good catch, I typo'd that in accidently. Fixed.
Mike Buckbee
A: 

This has already been answered and accepted, but I thought you might find this an interesting alternative.

Percona's OpenSource xtrabackup application will perform compressed (TAR) backups on the fly - along with lots of other interesting things.

I couldn't find an anchor on the page, but scroll down to "Compressed Backups".

Mike Buckbee