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 [email protected] 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?
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:
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/
.~/.ssh/authorized_keys
of whatever user you're using on that server to perform the backups.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.