Jump to content

Use a PHP file to backup a mysql database?


xc0n

Recommended Posts

hey i want a php file to backup my mysql database into a .sql file for me im using the code below, I save this code as backup.php and when i visit backup it says "Database Backup Completed" but the database backup folder is empty it doesnt create or write to the sql file.. ive also tryed giving the file and folder permissions 755, 775 & 777 and still none worked! not sure whats wrong?

 

<?php

// Connect To Database
$dbhost = 'localhost';
$dbuser = 'database_user';
$dbpass = '***********';
$dbname = 'database_name';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error Connecting to MYSQL');
mysql_select_db($dbname);
// End 

$backupFile = 'backup_folder/backup_file.sql';

mysql_query("SELECT * FROM '$dbname' -> INTO OUTFILE '$backupFile'");
$result = mysql_query;

if($result) { echo "Database Backup Completed"; 
} else { echo "There Was A Error";
}
?>

cleaned up your code, try this..

 

<?php

ini_set("error_reporting", E_ALL);
ini_set("display_errors","ON");

// Connect To Database
$dbhost = 'localhost';
$dbuser = 'database_user';
$dbpass = '***********';
$dbname = 'database_name';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error Connecting to MYSQL: '.mysql_error());
mysql_select_db($dbname);
// End 

$backupFile = 'backup_folder/backup_file.sql';

$result = mysql_query("SELECT * FROM '$dbname' -> INTO OUTFILE '$backupFile'");

if($result) { echo "Database Backup Completed"; } else { echo "There Was A Error";}
?>

 

do you receive any errors after you update you code?

@AyKay47: hey thanks alot for the help but that code gave me white page saying: There Was A Error

 

@jcbones: that script works great and does everything i need it to do! thanks alot, it can even email me the backup zipped or send it to a remote ftp server! very nice!!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.