How can I backup a mysql database which is running on a remote server, I need to store the back up file in the local pc.

share|improve this question
up vote 63 down vote accepted

Try it with Mysqldump

#mysqldump --host=the.remotedatabase.com -u yourusername -p yourdatabasename > /User/backups/adump.sql
share|improve this answer

Have you got access to SSH?

You can use this command in shell to backup an entire database:

mysqldump -u [username] -p[password] [databasename] > [filename.sql]

This is actually one command followed by the > operator, which says, "take the output of the previous command and store it in this file."

Note: The lack of a space between -p and the mysql password is not a typo. However, if you leave the -p flag present, but the actual password blank then you will be prompted for your password. Sometimes this is recommended to keep passwords out of your bash history.

share|improve this answer
6  
man mysql: If you use the short option form (-p), you cannot have a space between the option and the password. – Tanky Woo Jan 9 '14 at 11:49
1  
Good one explaining the "no space" between -p and the actual password – okeyxyz Mar 15 '15 at 15:50

No one mentions anything about the --single-transaction option. People should use it by default for InnoDB tables to ensure data consistency. In this case:

mysqldump --single-transaction -h [remoteserver.com] -u [username] -p [password] [yourdatabase] > [dump_file.sql]

This makes sure the dump is run in a single transaction that's isolated from the others, preventing backup of a partial transaction.

For instance, consider you have a game server where people can purchase gears with their account credits. There are essentially 2 operations against the database:

  1. Deduct the amount from their credits
  2. Add the gear to their arsenal

Now if the dump happens in between these operations, the next time you restore the backup would result in the user losing the purchased item, because the second operation isn't dumped in the SQL dump file.

While it's just an option, there are basically not much of a reason why you don't use this option with mysqldump.

share|improve this answer
    
Could be a lifesaver, thanks a bunch !!! – Gaurav Kumar Mar 18 '14 at 19:31
    
Good addition!! – Ian Jamieson May 19 '14 at 15:18

This topic shows up on the first page of my google result, so here's a little useful tip for new comers.

You could also dump the sql and gzip it in one line:

mysqldump -u [username] -p[password] [database_name] | gzip > [filename.sql.gz]
share|improve this answer
mysqldump -h [domain name/ip] -u [username] -p[password] [databasename] > [filename.sql]
share|improve this answer
3  
Is it really -p[password] and not -p [password]? – Artjom B. Jun 16 '15 at 8:29
3  
@ArtjomB. Yes, and if you're password has special characters put it in quotes: -p'pa$$w0rd!!!!' – Ryan Taylor Jul 15 '15 at 1:06

you can choose an other way :

mysql -u [username] [databasename]

And choose your sql file like :

source mydumpdatabase.sql
share|improve this answer
    
That is how you restore a backup, not how you create one. – Quentin Sep 25 '15 at 14:45

protected by Quentin Sep 25 '15 at 14:45

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.