Internet Marketing Forum

Internet Marketing Forum


For Webmasters and Entrepreneurs
|
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 21, 2012, 02:59:11 PM

Login with username, password and session length

You have to have confidence in your ability, and then be tough enough to follow through.


- Rosalynn Smith Carter

|-   Internet Marketing Forum > Online Business Discussions > Website Design and Maintenance > Database
+  

Need an example of how to use mysqldump

0 Members and 1 Guest are viewing this topic.
Pages: [1] Reply to Thread
Author Topic: Need an example of how to use mysqldump  (Read 1450 times)
OfflineGalaxian
Senior Member
Galaxian is on a distinguished road
Joined: Oct 2007
Posts: 286


Avatar of Galaxian

The internet is my playground.

View Galaxian\s Profile
Gender: MaleAries United States
notepad Feb 08, 2008, 11:55:30 AM #1
Can anyone please write out a complete php working example of how to do a mysqldump on an entire database?

Please, I would like to automate my backupprocess with a PHP script.
OfflineMrMango
Junior Member
MrMango is on a distinguished road
Joined: Jan 2007
Posts: 63


Avatar of MrMango

View MrMango\s Profile
United Kingdom
notepad Feb 08, 2008, 01:00:46 PM #2
 The most common use of mysqldump is probably for making a backup of an
       entire database:

       shell> mysqldump --opt db_name > backup-file.sql

       You can read the dump file back into the server like this:

       shell> mysql db_name < backup-file.sql

       Or like this:

       shell> mysql -e "source /path-to-backup/backup-file.sql" db_name

OfflineAceCoder
Junior Member
AceCoder is on a distinguished road
Joined: Oct 2007
Posts: 79



View AceCoder\s ProfileWWW
United States
notepad Feb 10, 2008, 07:58:15 PM #3
That's fine if you have access to the command line but how can you do these in PHP?

Latest Blog Post : PHP – Pass by Reference or Pass by Value

OfflineCode4Gold
Administrator
Code4Gold is a jewel in the roughCode4Gold is a jewel in the roughCode4Gold is a jewel in the roughCode4Gold is a jewel in the rough
Joined: Jan 2006
Posts: 6215


Avatar of Code4Gold

Administrator

View Code4Gold\s ProfileWWW
Gender: MaleGemini United States
notepad Feb 10, 2008, 10:30:38 PM #4
You could do something like this in PHP using the PHP system() command

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

Otherwise you could use a SQL statement to dump the data

Code:
$backupFile = '/path/to/backup/mytable.sql';
$query      = "SELECT * INTO OUTFILE '$backupFile' FROM myTable";
$result = mysql_query($query);

You could read that back in like this...

Code:
$backupFile = 'mytable.sql';
$query      = "LOAD DATA INFILE '$backupFile' INTO TABLE myTable";
$result = mysql_query($query);

If you have any question, ask away...

Latest Blog Post : 8 Tips for Creating a Marketing Buzz

728x90 Take the First Step
Pages: [1] Reply to Thread


Code4Gold Internet Marketing Forum © 2006-2011 Resdaz Media LLC - All Rights Reserved
Forum Software Powered by SMF - © 2001-2008, Lewis Media. All Rights Reserved.