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

$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;


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