I’d like to dump my databases to a file.
Certain website hosts don’t allow remote or command line access, so I have to do this using a series of queries.
All of the related questions say “use
mysqldump” which is a great tool but I don’t have command line access to this database.
INSERT commands to be created at the same time – basically, the same performance as
SELECT INTO OUTFILE the right road to travel, or is there something else I’m overlooking – or maybe it’s not possible?
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
mysqldump-php a pure-PHP solution to replicate the function of the
mysqldump executable for basic to med complexity use cases – I understand you may not have remote CLI and/or mysql direct access, but so long as you can execute via an HTTP request on a httpd on the host this will work:
So you should be able to just run the following purely PHP script straight from a secure-directory in /www/ and have an output file written there and grab it with a wget.
<?php require('database_connection.php'); require('mysql-dump.php') $dumpSettings = array( 'include-tables' => array('table1', 'table2'), 'exclude-tables' => array('table3', 'table4'), 'compress' => CompressMethod::GZIP, /* CompressMethod::[GZIP, BZIP2, NONE] */ 'no-data' => false, 'add-drop-table' => false, 'single-transaction' => true, 'lock-tables' => false, 'add-locks' => true, 'extended-insert' => true ); $dump = new MySQLDump('database','database_user','database_pass','localhost', $dumpSettings); $dump->start('forum_dump.sql.gz'); ?>
With your hands tied by your host, you may have to take a rather extreme approach. Using any scripting option your host provides, you can achieve this with just a little difficulty. You can create a secure web page or strait text dump link known only to you and sufficiently secured to prevent all unauthorized access. The script to build the page/text contents could be written to follow these steps:
For each database you want to back up:
Step 1: Run
Step 2: For each table name returned by the above query, run
SHOW CREATE TABLEto get the create statement that you could run on another server to recreate the table and output the results to the web page. You may have to prepend “DROP TABLE X IF EXISTS;” before each create statement generated by the results of these queryies (!not in your query input!).
Step 3: For each table name returned from step 1 again, run a
SELECT *query and capture full results. You will need to apply a bulk transformation to this query result before outputing to screen to convert each line into an
INSERT INTO tblXstatement and output the final transformed results to the web page/text file download.
The final web page/text download would have an output of all create statements with “drop table if exists” safeguards, and insert statements. Save the output to your own machine as a “.sql” file, and execute on any backup host as needed.
I’m sorry you have to go through with this. Note that preserving mysql user accounts that you need is something else entirely.
Use / Install PhpMySQLAdmin on your web server and click export. Many web hosts already offer you this as a service pre-configured, and it’s easy to install if you don’t already have it (pure php): http://www.phpmyadmin.net/
This allows you to export your database(s), as well as perform other otherwise tedious database operations very quickly and easily — and it works for older versions of PHP < 5.3 (unlike the Mysqldump.php offered as another answer here).
I am aware that the question states ‘using query’ but I believe the point here is that any means necessary is sought when shell access is not available — that is how I landed on this page, and PhpMyAdmin saved me!