Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to write a script that backs up databases on my old server and moves them to my new server. I have about 150 to so need this to be automated. I have found this:

$ mysqldump -u username -p'password' db-name | ssh mysql -u username -p'password' db-name

Which will allow me to do this, however, it requires me to specify a password every time I run it. How can I make the ssh command take a password parameter? I tried ssh --help but couldn't see anything.

If this command cannot accept a password, can I do this another way?

share|improve this question
up vote 11 down vote accepted

You want to use key-based auth for this. There are dozens of questions here on SF on the topic as well as countless tutorials on the web that can walk you through how to get this set up.

Not only will using key auth allow for the automation you need, but it is also a more secure means of authentication (assuming that you disable password auth at some point).

The basic steps are:

  1. Run ssh-keygen on the backup server, as the user that will be doing the backups. This will generate a keypair (public and private parts) and store them in ~/.ssh/.
  2. Copy the public key to the server being backed up. There should be a single line of text in the public key file. Copy/paste that public key into ~/.ssh/authorized_keys of whatever user you're using on that server to perform the backups.
  3. After you've done this, you should be able to connected from the backup server to the old db server without using a password.
share|improve this answer
The directory ~/.ssh/ doesn't exist, do I need to create it? Does it matter where I put it? – webnoob Jun 6 '11 at 20:22
Yes. $ mkdir ~/.ssh will create that folder in the user's home directory, which is where it needs to go for any of this to work. – EEAA Jun 6 '11 at 20:28
Got it, just found a good in depth tutorial on it and it worked like a charm from Putty. Will probably break when I run my script because of user issues but will sort it. Many thanks. – webnoob Jun 6 '11 at 20:46
In Red Hat based distros you can use ssh-copy-id to copy the public key to the destination server – ghm1014 Jun 6 '11 at 22:27

You need to setup key based authentication. Using a private key file on your computer and the matching public key file listed under authorized agents on the receiving side, you can login with ssh without entering a password.

share|improve this answer
Did you mean ~/.ssh/authorized_keys? – EEAA Jun 6 '11 at 20:06
I was describing the solution conceptually so @webnoob could get his head around the solution. I specifically wasn't mentioning implementation details because as you pointed out those can be better found in a thousand tutorials and other answer, I was just pointing the direction he would be moving. – Caleb Jun 6 '11 at 20:09
Teach a man to fish, etc. etc. :) – EEAA Jun 6 '11 at 20:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.