I had to move hosters recently and I have a couple of scripts run using CRON to backup my MySql database and manage the number of backups that I keep. I can ask the hosters to deprecate the version of PHO backwards but I don't want to lose the opportunity of features that may be available in the new version. The main script essentially performs a MySqlDump to a specified directory and then emails me to tell me that it completed successfully. The second script just deletes any files in the backup folder more than x days old.
I know mysqldump is a valid call so I should need to use anything other than that but I'm lost when it comes to seeing what is wrong with my scripts.
backup.php
<?
$datestamp = date("Y-m-d"); // Current date to append to filename of backup file in format of YYYY-MM-DD
// run the command php -q /home/mysite/public_html/scripts/ffbackup.php
/* CONFIGURE THE FOLLOWING SEVEN VARIABLES TO MATCH YOUR SETUP */
// require($_SERVER["DOCUMENT_ROOT"]."/config/db_config.php");
require("/home/mysite/config/db_config.php");
$connection = mysql_connect($db_host, $db_user, $db_password);
mysql_select_db($db_name, $connection);
//$dbuser = $db_user, $conection ; // Database username
//$dbpwd = ($db_password, $conection); // Database password
//$dbname = ($db_name, connection); // Database name. Use --all-databases if you have more than one
$backdir ="/home/mysite/forumbackup/";
$filename= "backup-$datestamp.sql.gz"; // The name (and optionally path) of the dump file
$to = "
[email protected]"; // Email address to send dump file to
$from = "
[email protected]"; // Email address message will show as coming from.
$subject = "Mysite MySQL backup file-$datestamp"; // Subject of email
$command = "mysqldump -u $db_user --password=$db_password $db_name | gzip > $filename";
$result = passthru($command);
$newfile = $filename;
copy ($filename, $backdir.$newfile);
$message = "Compressed database backup file $filename copied to backup directory.";
$headers = "From: $from\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to, $subject, $message, $headers);
unlink($filename); //delete the backup file from the server
?>
and delold.php
<?php
$path = "/home/mysite/forumbackup/";
//echo "script is running";
//echo "the path is $path ";
if (is_dir("$path") )
{
$handle=opendir($path);
while (false!==($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$Diff = (time() - filectime("$path/$file"))/60/60/24;
if ($Diff > 10) unlink("$path/$file");
}
}
closedir($handle);
}
?>