开发者

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

开发者 https://www.devze.com 2023-02-16 11:04 出处:网络
Ok Im having trouble finding out how to select a full database for backup as an *.sql file rather than only an individual table.

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, 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 -开发者_StackOverflowh $dbhost -u $dbuser -p $dbpass $dbname | gzip
            > $backupFile";
system($command);

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.


You've missed mysqldump! Its a command line utility designed specifically for what you are trying to do.

Writing a backup file manually is rather hard. Writing one that you will consistently be able to recover from is very hard. Check out the docs, it has a lot of options that you can use to specify what exactly you want to save.

0

精彩评论

暂无评论...
验证码 换一张
取 消