I have used the following script for automated MySQL backups. Just create it as a PHP file on your account, and configure the settings. Set the cron to run on the same days every month, and will will rotate them out by unlinking them - as it creates the file based on the day. So if you ran it daily, it would keep 30 days backups and rotate them. If you ran it on the 5th only, it would rotate that out on the next 5th.
PHP Code:
<?php $emailaddress = "XXXXXX@yourdomain.com"; $host="XXXXXX"; // database host $dbuser="XXXXXX"; // database user name $dbpswd="XXXXXX"; // database password $mysqldb="XXXXXX"; // name of database $path = "/home/user/dbbackups"; // full server path to the directory where you want the backup files (no trailing slash) // modify the above values to fit your environment $filename = $path . "/backup" . date("d") . ".sql"; if ( file_exists($filename) ) unlink($filename); system("mysqldump --user=$dbuser --password=$dbpswd --host=$host $mysqldb > $filename",$result); $size = filesize($filename); switch ($size) { case ($size>=1048576): $size = round($size/1048576) . " MB"; break; case ($size>=1024): $size = round($size/1024) . " KB"; break; default: $size = $size . " bytes"; break; } $message = "The database backup for " . $mysqldb . " has been run.\n\n"; $message .= "The return code was: " . $result . "\n\n"; $message .= "The file path is: " . $filename . "\n\n"; $message .= "Size of the backup: " . $size . "\n\n"; $message .= "Server time of the backup: " . date(" F d h:ia") . "\n\n"; mail($emailaddress, "Database Backup Message" , $message, "From: Website <>"); ?>
And to set up the cron, let's say you saved the file as dbbackup.php, then you would enter the following in the crontab to run it daily at midnight.
0 * * * * php -q /home/user/dbbackup.php
__________________
Please login or register to view this content. Registration is FREE - Premium free cPanel hosting!
|