I can't manage to backup my MySQL database. I use the following command:
mysqldump --user=user --password=password db > db.sql
This is what the output file looks like:
-- MySQL dump 10.11
--
-- Host: localhost Database: db
-- ------------------------------------------------------
-- Server version 5.0.96-community
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
That's it. Not any CREATE TABLE
and not any INSERT
... I also noticed in posts for similar problems that there always was a -- Dump completed on YYYY-MM-DD HH:MM:SS
at the end of the output, which is not the case here. My username and my password are correct and the user has all possible privileges.
What am I doing wrong?
Since asking this question, my web host deactivated the shell_exec()
command, so now I'm using a PHP script to backup my database. Here's the function I'm using:
function backup_tables($host, $user, $pass, $name, $tables='*', $backup_path) {
$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link);
//get all of the tables
if($tables == '*') {
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result)) {
$tables[] = $row[0];
}
}
else {
$tables = is_array($tables) ? $tables : explode(', ',$tables);
}
$droptables = 'DROP TABLE IF EXISTS ';
for ($i = sizeof($tables) - 1; $i >= 0; $i--) {
$droptables .= '`' . $tables[$i] . '`';
if ($i > 0) {
$droptables .= ', ';
} else {
$droptables .= ';';
}
}
//cycle through
foreach($tables as $table) {
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);
// $droptables.= '`' . $table . '`, ';
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$data .= "\n\n".$row2[1].";\n\n";
for ($i = 0; $i < $num_fields; $i++) {
while($row = mysql_fetch_row($result)) {
$data .= 'INSERT INTO '.$table.' VALUES(';
for ($j = 0; $j < $num_fields; $j++) {
if (isset($row[$j])) {
$row[$j] = addslashes($row[$j]);
$row[$j] = ereg_replace("\n","\\n",$row[$j]);
$data .= '"'.$row[$j].'"' ;
} else {
$data .= 'NULL';
}
if ($j < ($num_fields-1)) {
$data .= ',';
}
}
$data .= ");\n";
}
}
$data .= "\n\n\n";
}
$return = $droptables . $return;
//save file
$date = date('Ymd-His');
$filename = 'db-backup-' . $date . '.sql';
$handle = fopen($backup_path.$filename,'w+');
fwrite($handle, utf8_encode($return));
fclose($handle);
$gzfile = $filename . '.gz';
$handle = gzopen($backup_path.$gzfile, 'w9');
gzwrite($handle, file_get_contents($backup_path.$filename));
gzclose($handle);
}
Example usage: backup_tables('localhost','user','pass','database', 'users, posts, comments, subscriptions', '/home/user/db_backups/');
. You just have to remember to put the tables with foreign keys at the end of the list.