Reputation:
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
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
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
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