Chad
Chad

Reputation:

What would be the best way to backup and restore mysql dumps with just php?

I know how you do it from the console, and I know you can execute console commands with php, but would there be a way to recursively dump a database into a file, and then restore it from that file later, just using php? I want it to be able to work on windows and nix servers.

I am guessing it would need to loop through the tables and rows but how would I get a list of those? Or can it even be done? Seems like it would take a massive amount of memory if db was a good size. Thoughts?

Upvotes: 3

Views: 1541

Answers (5)

deizel.
deizel.

Reputation: 11212

This might help with primary keys

SHOW CREATE TABLE my_table

Upvotes: 0

nabrond
nabrond

Reputation: 1388

You can use the exec() function to call mysqldump. This would pipe the output of mysqldump (the database export) back to your PHP script and you can handle it there however is necessary. This can be used to export a single table or an entire database. The tool could be installed on a client machine, or the PHP script can be run on the MySQL server.

/path/to/mysqldump --host=[svr] --user=[user] --password=[passwd] [db]

Make sure that the user passed to MySQL has SELECT and LOCK TABLE permissions on each database to be backed up.

Upvotes: 1

Ian P
Ian P

Reputation: 12993

Look into the mysql database.

Upvotes: -2

Ian P
Ian P

Reputation: 12993

Run the SHOW TABLES; query. Walk through the results and run the queries listed by deizel for each table.

Good luck!

Upvotes: 0

deizel.
deizel.

Reputation: 11212

mysql_query("SELECT * INTO OUTFILE 'sql/backup.sql' FROM my_table");
mysql_query("LOAD DATA INFILE 'sql/backup.sql' INTO TABLE my_table");

Upvotes: 2

Related Questions