By using this site you agree to the use of cookies by Brugbart and our partners.

Learn more

Using gzip directly in mysqldump

How you can use gzip directly in the exec command of your php scripts.

Edited: 2013-04-15 21:38

This Tutorial shows how to use gzip and mysqldump from php scripts. Note that there is no space between the -p and the password – for whatever stupid reason. To gzip your mysqldump, you may use the below command:

/opt/lampp/bin/mysqldump --opt -h $dbhost -u $dbuser -p$dbpass DatabaseName| gzip > $backupFile

Note the /opt/lampp/bin/mysqldump part, on some systems you might need the whole path, while on other systems it might be fine to just use mysqldump – its down to configuration of course – but never mind that now.

To use mysqldump without gzip, you can use the below command – note the mising | gzip after the DatabaseName.

/opt/lampp/bin/mysqldump --opt -h $dbhost -u $dbuser -p$dbpass DatabaseName > $backupFile

The full PHP code would be something similar to the below.

$dbhost = '';
$dbuser = 'username';
$dbpass = 'password';
$backupFile = 'DatabaseName.sql.gz';

exec("/opt/lampp/bin/mysqldump --opt -h $dbhost -u $dbuser -p$dbpass DatabaseName| gzip > $backupFile");

More Tutorials

  1. Backup all databases on server with PHP