conmen
conmen

Reputation: 2417

PHP MySQL database backup cron job

I wanted to have database backup cron in webserver, I use following codes to output sql file:

<?php
include('connectdb.php');

$backupFile = 'database_backup_'.date("YmdHis").'.sql';
$command = 'mysqldump $dbname --password=$dbpass --user=$dbuser --single-transaction >'.$backupFile;
system($command);

include('closedb.php');
?>

but when I open the specific sql file, its only show text like this:

Usage: mysqldump [OPTIONS] database [tables]
OR     mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR     mysqldump [OPTIONS] --all-databases [OPTIONS]
For more options, use mysqldump --help

What's wrong with my code?

Thanks.

Upvotes: 0

Views: 14089

Answers (4)

manish1706
manish1706

Reputation: 1599

If your site is live and using Database then it is very important to take backup of you Database in regular time interval. But it is not possible to take the backup manually every time. So lets create a simple PHP function to do this job for us and we can call that function using Cron Job in regular time interval.

<?php
include("connection.php");
function backup_db(){
  /* Store All Table name in an Array */

$allTables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result)){
 $allTables[] = $row[0];
}
foreach($allTables as $table){
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);
$return.= 'DROP TABLE IF EXISTS '.$table.';';
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$return.= "\n\n".$row2[1].";\n\n";
for ($i = 0; $i < $num_fields; $i++) {
while($row = mysql_fetch_row($result)){
   $return.= 'INSERT INTO '.$table.' VALUES(';
 for($j=0; $j<$num_fields; $j++){
   $row[$j] = addslashes($row[$j]);
   $row[$j] = str_replace("\n","\\n",$row[$j]);
   if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } 
   else { $return.= '""'; }
   if ($j<($num_fields-1)) { $return.= ','; }
 }
   $return.= ");\n";
}
}
$return.="\n\n";
}
// Create Backup Folder
$folder = 'DB_Backup/';
if (!is_dir($folder))
mkdir($folder, 0777, true);
chmod($folder, 0777);
$date = date('m-d-Y-H-i-s', time()); 
$filename = $folder."db-backup-".$date; 
$handle = fopen($filename.'.sql','w+');
fwrite($handle,$return);
fclose($handle);
}
// Call the function
backup_db();
?>

And for your connection you can use this file.

connection.php File

<?php
  $host="localhost";
  $uname="YourUserName";
  $pass="YourPassword";
  $database = "YourDatabaseName";
  $connection=mysql_connect($host,$uname,$pass);
or die("Database Connection Failed");
$selectdb=mysql_select_db($database) or die("Database could not be selected"); 
$result=mysql_select_db($database)
or die("database cannot be selected <br>");
?>

Save the above code in a PHP file and upload the file to your web server then run the file. And check if it create a folder called DB_Backup and create the backup .sql file inside DB_Backup folder. If the file works fine then you can assign the file to your Cron Job.

Upvotes: 1

user1864610
user1864610

Reputation:

Try your mysqldump command from the command line with the correct parameters. Does it work? What errors does it report, if any (use the --verbose switch)? Fix them and try again.

When you've debugged your command, submit it directly to cron - no need to run it through a PHP script.

Upvotes: 1

Henkealg
Henkealg

Reputation: 1516

The output you are getting us because you have an error in the mysqldump syntax.

This is a good syntax to follow:

mysqldump --opt -u [uname] -p[pass] [dbname] > [backupfile.sql]

I would run this as a cron job by itself instead of using PHP. Especially if the db is large. You might then get time outs depending on your php settings.

Upvotes: 1

user1864610
user1864610

Reputation:

This line should be quoted with double-quotes:

$command = 'mysqldump $dbname --password=$dbpass --user=$dbuser --single-transaction >'.$backupFile;

like this:

$command = "mysqldump $dbname --password=$dbpass --user=$dbuser --single-transaction >".$backupFile;

If not, the variable substitution that sets $dbnam, $dbpass etc won't happen

Upvotes: 2

Related Questions