Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysqldump from a remote server

I need to copy mysql db from a remote server to a local server. The only way I can get access to that DB is by connecting to a remote computer (let's call it X) with ssh, and then from that computer I connect to mysql with mysql -h address -u username -p password. My limitation is that I cannot do the dump on the mysql server that runs the DB, and cannot do the dump to computer X (for administrative reason that cannot be change).

Is there a way to tell mysql to do the dump directly to my local server? (Perhaps using PIPE, though I'm not familiar with it). For what it's worth, my server runs on Ubuntu server, X is also running on linux.

I tried looking for a solution but couldn't find something to this exact scenario.

Appreciate any help.

Regards, Elad

like image 230
Elad Edri Avatar asked Jul 02 '15 15:07

Elad Edri


People also ask

What is @mysqldump and how to use it?

mysqldump allows you to dump databases that are hosted on a remote machine. Let's see how to do that for a single, multiple and all databases stored on a remote server.

How to dump a MySQL database on a remote server?

mysqldump allows you to dump databases that are hosted on a remote machine. Let's see how to do that for a single, multiple and all databases stored on a remote server. We need to know the MySQL user, password, host and port of the remote database. Let's assume the following values for the examples below: host: 14.212.331.87

How to copy MySQL db from remote server to local server?

I need to copy mysql db from a remote server to a local server. The only way I can get access to that DB is by connecting to a remote computer (let's call it X) with ssh, and then from that computer I connect to mysql with mysql -h address -u username -p password.

Why doesn’t mysqldump dump out to disk before archiving?

But the server didn’t have enough disk space to dump it out to disk before copying it off to a remote server for archiving. The first thought was to run mysqldump dump on the destination machine, and to access the database over the network. That however, doesn’t compress or encrypt the data.


1 Answers

You can use the STDIN/STDOUT redirection feature of SSH:

$ ssh user@remote "mysqldump -h host -u username -p dbname" > dbname.sql

With this command, mysqldump writes its dump to STDOUT, which is redirected to the STDOUT of your local shell. With > dbname.sql you write the stream of STDOUT to the local file.

You can even pipe the output via gzip (or any other compression tool) to reduce bandwidth if you like:

$ ssh user@remote "mysqldump -h host -u username -p dbname | gzip" | gunzip > dbname.sql

You can also use the pipe the other way round to restore a database from the backup:

$ gzip dbname.sql | ssh user@remote "gunzip | mysql -h host -u username -p dbname"

Or remotely restore without compression (not recommended):

$ dbname.sql > ssh user@remote "mysql -h host -u username -p dbname"
like image 193
Kaii Avatar answered Oct 11 '22 10:10

Kaii