Backup database with mysqldump

Prerequisites

PHP with safe_mode disabled (when safe mode is enabled, you can only execute files within the safe_mode_exec_dir).

$db_user = 'db_user';
$db_host = 'localhost';
$db_password = 'db_password';
$db_name = 'db_name';

// creating a file name with the current date and time
$file_name = $db_name.'_'.date('ymdhis').'.sql';

// writing to the file
exec("mysqldump --host='$db_host' --add-drop-table --user='$db_user' --password='$db_password' $db_name > $file_name");

From command line (Linux)

create a gzip compressed backup with the current timestamp

mysqldump -u userName -p"myPassword" databaseName | gzip -c > databaseName-`date "+%Y.%m.%d-%H.%M.%S"`.sql.gz