I'm trying to could backup one of tables from database using php only. I'd like to save result in some directory at my server or (the best) send it via e-mail.
My problem is that:
- exec() is disabled
- system() is disabled
I'm tried to use "SELECT * INTO OUTFILE" and "LOAD DATA INFILE" but I got blank page (with my "error" message) each time.
I use PHP 5.2 (but I checked it also under version: 4, 5.3.2 and 6). There's no error raport from PHP side (all errors display options are turned on).
"Backup" directory have chmod 777 so that's not problem.
My files looks this way:
<?php
$host = "localhost"; // Host name
$username = "username"; // Mysql username
$password = "password"; // Mysql password
$db_name = "MyDbName"; // Database name
$tableName = "MyTestTable"; // Table name
// Connect to server and select database.
mysql_connect($host, $username, $password)or die("cannot connect");
mysql_select_db($db_name)or die("cannot select DB");
$backupFile = "/home/myusername/domains/mydomain.com/public_html/admin/backup/mybackup.sql";
$query = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName";
$result = mysql_query($query);
if($result){
echo "success";
} else {
echo "error";
}
// close connection
mysql_close();
?>
<?php
$host = "localhost"; // Host name
$username = "username"; // Mysql username
$password = "password"; // Mysql password
$db_name = "MyDbName"; // Database name
$tableName = "MyTestTable"; // Table name
// Connect to server and select database.
mysql_connect($host, $username, $password)or die("cannot connect");
mysql_select_db($db_name)or die("cannot select DB");
$backupFile = "/home/myusername/domains/mydomain.com/public_html/admin/backup/mybackup.sql";
$query = "LOAD DATA INFILE 'backupFile' INTO TABLE $tableName";
$result = mysql_query($query);
if($result){
echo "success";
} else {
echo "error";
}
// close connection
mysql_close();
?>