Home » Linux » mysqldump from remote host

mysqldump from remote host

Posted by: admin November 30, 2017 Leave a comment

Questions:

Is it possible to dump a database from a remote host through an ssh connection and have the backup file on my local computer.

If so how can this be achieved?

I am assuming it will be some combination of piping output from the ssh to the dump or vice versa but cant figure it out.

Answers:

This would dump, compress and stream over ssh into your local file

ssh -l user remoteserver "mysqldump -mysqldumpoptions database | gzip -3 -c" > /localpath/localfile.sql.gz 

Questions:
Answers:

Starting from @MichelFeldheim’s solution, I’d use:

$ ssh [email protected] "mysqldump -u user -p database | gzip -c" | gunzip > db.sql

Questions:
Answers:

ssh -f [email protected] -L 3306:server.com:3306 -N

then:

mysqldump -hlocalhost > backup.sql

assuming you also do not have mysql running locally. If you do you can adjust the port to something else.

Questions:
Answers:

I have created a script to make it easier to automate mysqldump commands on remote hosts using the answer provided by Michel Feldheim as a starting point:

mysqldump-remote

The script allows you to fetch a database dump from a remote host with or without SSH and optionally using a .env file containing environment variables.

I plan to use the script for automated database backups. Feel free to create issues / contribute – hope this helps others as well!