You are invited to Log in or Register a free Frihost Account!

using cron jobs and scp to automate backups

I've written a simple shell script that does a backup of my mysql databases everyday with cron jobs. My script works pretty well, but its only backing it up to directory on the same machine. I'm trying to figure out how to have the script send the file to a remote server using scp. for the life of me I can't figure out how to do this in a script since scp prompts for a password to the remote host. I know this can be done I just can't figure it out. I'm guessing it has something to do with the rsa keys.

Any help would be greatly appreciated.
Thanks in advanced
You can do it using expect. Expect is a unix tool which let you automate tasks that normally require actions of a user, like in your case typing in a password when prompted to do so.

Here is an example which automates an ftp session:


# Open an ftp session to a remote server, and wait for a username prompt.
spawn ftp $remote_server
expect "username:"
# Send the username, and then wait for a password prompt.
send "$my_user_id\r"
expect "password:"
# Send the password, and then wait for an ftp prompt.
send "$my_password\r"
expect "ftp>"
# Switch to binary mode, and then wait for an ftp prompt.
send "bin\r"
expect "ftp>"
# Turn off prompting.
send "prompt\r"
expect "ftp>"
# Get all the files
send "mget *\r"
expect "ftp>"
# Exit the ftp session, and wait for a special end-of-file character.
send "bye\r"
expect eof

It should be easy to change it to using scp instead of ftp.

See man expect for more details. There is also an expect module for Perl, if you prefer perl.
Thanks, I'm gonna check out expect. It sounds interesting. But I did find a solution using a different method. I just used ssh-keygen to generate a public and private ssh key. I put the public key on the remote machine in /root/.ssh/ and now it can connect through ssh to run the script. I'm not sure how secure this method is. I'm guessing that its still pretty secure since it still is sent through an encrypted ssh tunnel. All the ssh-keys do is pair the remote machine with a trusted machine to allow it to connect without a password.
Related topics
Cron Jobs
propos des cron jobs
[RESOLVED] cron jobs
Cron jobs
I don't have cron jobs enabled ?
cron jobs
Cron Jobs
Cron Jobs
Cron Jobs
Cron jobs - running a php file
Executing automated tasks without using cron jobs
cron jobs?
how do I auto backup using cron jobs
[Urgent] Cron Jobs
Reply to topic    Frihost Forum Index -> Computers -> Software

© 2005-2011 Frihost, forums powered by phpBB.