Reputation: 1527
I have an SSH access to production server of the Rails app.
I want to make a mysqldump of production database to my Mac. Please help me to achieve this.
Upvotes: 40
Views: 66862
Reputation: 319
An alternative way to backup via SSH is to create an SSH tunnel, like this:
ssh -f -N -L 3307:localhost:3306 [email protected]
here, -L
is used to specify the ports (in the format port:host:hostport
) to create the tunnel;
the -f
option backgrounds ssh; and
-N
means no command execution, just forward ports
after that you can connect to the database as if it were a local server
mysqldump -u user -ppassword -h 127.0.0.1 -P 3307 --all-databases > backup.sql
note the port number -P
matches the one specified earlier
Run a restore process is even easier
mysql -h 127.0.0.1 -u user -ppassword < backup.sql
This approach can be convenient when you don’t want to put one command into another (for example, due to confusion with the quotes).
Examples with mysql backup via SSH connection:
Access Your Database Remotely Through an SSH Tunnel
Over SSH – Remote MySQL Backup
Upvotes: 3
Reputation: 53
I couldn't get the other ones to work.
This is the solution I found for linux:
ssh username@ipadress "mysqldump -u USERNAME -pPASSWORD DB_NAME " > ~/dump.sql
This will prompt you to enter a password for your ssh server, and then dumps the database to the specified location on the SSH server.
Upvotes: 3
Reputation: 1426
Direct method to dump mysql data from remote server to your local computer is:
ssh root@ipaddress "mysqldump -u dbuser -p dbname | gzip -9" > dblocal.sql.gz
Or
ssh -l root ipaddress "mysqldump -u dbuser -p dbname | gzip -9" > dblocal.sql.gz
Both command does the same work.
If you have password for ssh and database access there will two prompt for password or if you have no password for ssh then you will be asked to enter you database password.
Similarly, if you are using key from aws or cloud other service you can incorporate the key in the command as:
ssh -i key.pem root@ipaddress "mysqldump -u dbuser -p dbname | gzip -9" > dblocal.sql.gz
Upvotes: 112
Reputation: 1527
ssh remote_username@remote_host
mysqldump -u username -ppassword -h host database > dump.sql
scp remote_username@remote_host:/path/to/dump.sql /Users/YourName/Documents/dump.sql
rm dump.sql
Upvotes: 4