Here are three ways to backup your MySQL database(s) and compress the backup on the fly:
1. Dump database ‘dbname’ into a compressed file
mysqldump \ -u user \ -ppassword \ --host=mysql-server \ dbname | \ gzip > dbname_backup.sql.gz
To dump multiple databases, you can either write a ‘for loop’ on a ‘SHOW DATABASES’ query result or use the next command to backup to a single file.
2. Dump all databases into a compressed file
mysqldump \ --all-databases \ -u user \ -ppassword \ --host=mysql-server | \ gzip > dbname_backup.sql.gz
3. Dump database ‘dbname’ except for some tables into a compressed file
mysqldump \ --ignore-table=dbname.ignoredtable1 \ --ignore-table=dbname.ignoredtable2 \ --ignore-table=dbname.ignoredtable3 \ -ppassword \ -u user \ --host=mysql-server \ dbname | \ gzip > dbname_backup.sql.gz
This allows you to skip certain tables that are unimportant (for example cached data) or static (imported from somewhere else). You can skip as many tables as you like. Just repeat the line for each table you want to ignore.
The pipe to gzip in the last line of all examples, makes sure the backup gets compressed before written to disk. You can safely do so to save some disk space. Just remove it to have an uncompressed backup.