Backup MySQL database using cron and php

Here is a little PHP script that I use to backup MySQL databases on Hostgator Linux web servers. Simply save the .php file to a non publicly accessible part of the server and run regularly using crontab:

/usr/bin/php /home4/username/my-backups/site-name/backup-task.php

View source
<?php
$DATABASE="db_name";
$DBUSER="db_user";
$DBPASSWD="db_password";
$PATH="/home4/username/my-backups/site-name/";
$FILE_NAME="site-name-backup-" . date("Y-m-d") . ".sql.gz";
exec('/usr/bin/mysqldump -u '.$DBUSER.' -p'.$DBPASSWD.' '.$DATABASE.' | gzip --best > '.$PATH.$FILE_NAME);
echo "Database(".$DATABASE.") backup completed. File name: ".$FILE_NAME;
?>



Newsletter

Subscribe to receive occasional updates on new posts.
Your email will not be used for any other purpose and you can unsubscribe at any time.
Please wait
Privacy Policy