18

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.

I'd like CREATE and INSERT commands to be created at the same time - basically, the same performance as mysqldump. Is SELECT INTO OUTFILE the right road to travel, or is there something else I'm overlooking - or maybe it's not possible?

Prix
  • 19,417
  • 15
  • 73
  • 132
Ben
  • 54,723
  • 49
  • 178
  • 224
  • 1
    Change your hosting options to get the MySQL dump. – Basile Starynkevitch Aug 11 '13 at 01:03
  • 1
    [**Are you saying you can't do this?**](http://stackoverflow.com/questions/6750531/using-a-php-file-to-generate-a-mysql-dump) [if that's so then I guess you want to replicate what phpMyAdmin does for exporting tables.](http://stackoverflow.com/questions/11388546/how-does-phpmyadmin-export-work) – Prix Aug 11 '13 at 01:05
  • @Prix: your bold link uses `mysqldump` which the OP wants to avoid. – Basile Starynkevitch Aug 11 '13 at 01:07
  • @BasileStarynkevitch I hope you can read the "can't" part of the bold link, also the follow up link is pretty straightforward as well. – Prix Aug 11 '13 at 01:09
  • @Steve: what mysql version is running? – bob-the-destroyer Aug 11 '13 at 01:14
  • @bob-the-destroyer - 5.0.91. Host is Fatcow. Problem is I can't access the darn thing without being logged in to the host's control panel. I've asked their support desk too and they've confirmed that's the only way. – Ben Aug 11 '13 at 01:16
  • @Prix and Basile - part of my question is exactly that, will `SELECT INTO OUTFILE` do pretty much exactly the same as `mysqldump`, or is there a different function I'm missing. – Ben Aug 11 '13 at 01:18
  • @Steve I don't know if the fields are escaped by default but in short it could be possible to use it to reproduce all the entries into a dump file. – Prix Aug 11 '13 at 01:20
  • @Prix - skimmed your link, looks like there should be an easier way to do it...maybe I'll abandon the dream. – Ben Aug 11 '13 at 01:21
  • @Prix - acknowledged, but with `CREATE` and `SELECT` and keys and all? – Ben Aug 11 '13 at 01:22
  • @Steve: That's a shame your hosting service provider won't give you at least remote access to the mysql port and a user with sufficient mysql permissions. Perhaps you can script this to output the sql commands to be ran in sequence on a backup host. You would of course need to take security precautions yourself to secure this remote dump that you create. What script languages are you allowed to use on this host? – bob-the-destroyer Aug 11 '13 at 01:24
  • @Steve with OUTFILE alone you won't be able to reproduce it all but using procedures you can recreate exactly for the records alone even so I would still prefer to do my own SELECT statement and output it to a file feels a lot easier then with OUTFILE + procedures. – Prix Aug 11 '13 at 01:24
  • bob - I know, right? Horrible. I can't connect from a desktop client or anything. AFAIK PHP and Perl are supported by default. Prix - OK, thanks for the tips. Dream abandoned :( will delete this question soon. – Ben Aug 11 '13 at 01:27
  • @Steve why not try using PHP with exec commands like on the first link I have sent you have you tried it? do they block those commands? – Prix Aug 11 '13 at 01:28
  • I think I've tried shell commands before and got shut down. But I'll try again. Back in 5. – Ben Aug 11 '13 at 01:29
  • @Steve I see, you could check http://search.cpan.org/ perl is full of modules perhaps there is one that would allow you to do that. – Prix Aug 11 '13 at 01:30
  • 2
    @Steve To be fairly honest with you I don't think you have to give up on it, its fairly easy to make your own with php or perl, list tables, show how they were created store, then query each table for all the data and pre format it. – Prix Aug 11 '13 at 01:36
  • Nope: `Can't connect to local MySQL server through socket` etc. – Ben Aug 11 '13 at 01:39
  • @Steve: what is your true concern? That you don't trust your hosting provider to regularly and sufficiently back up all their mysql data themselves (which would be shocking if they don't do this already), or that you're "unsatisfied with their service" to put it nicely? – bob-the-destroyer Aug 11 '13 at 01:45
  • I'm trying to make an easy link where I can dump the remote data onto my local machine for development. They do offer backup themselves, but I'd like to do it like Frank Sinatra - my way (and also without the login-control panel-mysql tab-backups rigamarole) – Ben Aug 11 '13 at 01:48
  • @Steve - so long as this is a box where, at a minimum, you can upload a PHP script and execute it on the host in question (via browser, no CLI i know) and also retrieve the outfile via browser or wget. It's all happening on the providers localhost and should work fine to get mysqldump output only from PHP. – cerd Aug 11 '13 at 03:05

3 Answers3

28

Use 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.

mysqldump-php - Pure PHP mysqldump on GitHub

PHP example:

<?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');
    ?>
diego
  • 529
  • 4
  • 6
cerd
  • 2,171
  • 1
  • 18
  • 28
  • Beauty this could be it. I've moved on to a different problem but I'll come back to this in a bit. Will have a good look, cheers. – Ben Aug 12 '13 at 04:00
  • That was really time saver. Thank you. – Mandrake Feb 05 '15 at 14:35
  • missing semicolon in 2nd line - how can I fix this without adding the required 60 characters? – theremin Jul 08 '15 at 15:21
  • very much appreciate this library. I was having a hard time fixing mysqldump permission and pathing issues when moving server. I just changed it up to use this. – shababhsiddique Sep 01 '19 at 12:29
4

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 SHOW TABLES.

  • Step 2: For each table name returned by the above query, run SHOW CREATE TABLE to 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 tblX statement 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.

bob-the-destroyer
  • 3,164
  • 2
  • 23
  • 30
  • 1
    You should be sorry, bob. >:( Just kidding. Thanks for your help. It seems that there's something basic I'm forgetting, but can't find what it is. Ah well. – Ben Aug 11 '13 at 01:40
  • Yeah, not user accounts, thanks though, I want to dump the whole thing with its keys and create statements. – Ben Aug 11 '13 at 01:41
  • Note to future users: although I've accepted this answer as the most likely way to do it, I haven't actually tried it (sorry). Thanks though bob. – Ben Aug 11 '13 at 01:57
  • @Steve: no problem. It was meant as a last-resort answer. Hopefully someone else has a better solution for your case. – bob-the-destroyer Aug 11 '13 at 02:01
1

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!

user266926
  • 121
  • 2
  • 1
    Note that you do not always have PHP available and do not necessarily want to install it. Some might also have security concerns if PHP serivce is just running for administration purposes - so when installing be sure to configure it correctly. – Alex Feb 05 '14 at 16:47
  • phpmyadmin wont be good option for automated backups. Also, for shared databases, the mysqldump is of no value to be an option to use. – MohanBabu Mar 18 '20 at 10:38