40

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.

Marty Wallace
  • 34,046
  • 53
  • 137
  • 200
  • I have replied to similar answer here: http://stackoverflow.com/questions/6564882/automate-mysqldump-to-local-computer-windows/13584425#13584425 – Ashwin A Nov 28 '12 at 09:39

4 Answers4

70

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 
Michel Feldheim
  • 17,625
  • 5
  • 60
  • 77
21

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

$ ssh user@host "mysqldump -u user -p database | gzip -c" | gunzip > db.sql
Pierre de LESPINAY
  • 44,700
  • 57
  • 210
  • 307
3

ssh -f user@server.com -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.

Michael
  • 3,568
  • 3
  • 37
  • 50
2

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!

Logan
  • 607
  • 8
  • 7