Selecting a whole database over an individual table to output to file

Posted by Daniel Wrigley on Stack Overflow See other posts from Stack Overflow or by Daniel Wrigley
Published on 2011-03-11T15:44:37Z Indexed on 2011/03/11 16:10 UTC
Read the original article Hit count: 299

Filed under:
|
|

:::::::: EDIT ::::::::

New code for people to have a look at, one question I have with this is where do I set were the *.gz file is saved?

$backupFile = $dbname . date("Y-m-d-H-i-s") . '.gz';
$command = "mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname | gzip
            > $backupFile";
system($command);

Also why the hell can you not reply yo your own post with answering it? :(

:::::::: EDIT ::::::::

Ok Im having trouble finding out how to select a full database for backup as an *.sql file rather than only an individual table.

On the localhost I have several databases with one named "foo" and it is that which I want to backup and not any of the individual tables inside the database "foo".

The code to connect to the database;

//Database Information
$dbhost = "localhost";
$dbname = "foo";
$dbuser = "bar";
$dbpass = "rulz";

//Connect to database
mysql_connect ($dbhost, $dbuser, $dbpass)
    or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());

The code to backup the database;

// Grab the time to know when this post was submitted
$time = date('Y-m-d-H-i-s');

$tableName  = 'foo';
$backupFile = '/sql/backup/'. $time .'.sql';

$query  = "SELECT * INTO OUTFILE '". $backupFile ."'
           FROM ". $tableName ."";
$result = mysql_query($query)or die("Database query died: " . mysql_error());

My brain is hurting near to the end of the day so no doubts i've missed something out very obvious.

Thanks in advance to anyone helping me out.

© Stack Overflow or respective owner

Related posts about mysql

Related posts about into