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


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

 

 


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *