You can use the STDIN / STDOUT redirection function in 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. Using > dbname.sql you write the STDOUT stream to a local file.
You can even direct output through gzip (or any other compression tool) to reduce bandwidth if you want:
$ ssh user@remote "mysqldump -h host -u username -p dbname | gzip" | gunzip > dbname.sql
You can also use the channel in reverse to restore the database from a backup:
$ gzip dbname.sql | ssh user@remote "gunzip | mysql -h host -u username -p dbname"
Or remotely recover without compression (not recommended):
$ dbname.sql > ssh user@remote "mysql -h host -u username -p dbname"
source share