简体   繁体   中英

Bash shell cron script connecting to EC2 — ssh-add failing

The goal is to get a bash script that connects to EC2 to download offsite backups. I have this, but I have to keep running them from the terminal manually as they fail from the crontab with the error "Permission denied (publickey)", so the key is obviously failing to add.

There's obviously a hole in my understanding. I've been tinkering for a couple of hours trawling Google and still haven't got it working. What am I doing wrong in terms of getting the script to add the keys itself?

#!/bin/sh
# Add the client key so we can SSH in
eval "$(ssh-agent)"
ssh-add ec2-key.pem

# Download the web file backup.
# Example file web1_2013-06-30_00-30.tar.gz
WEBDATE=`date +"%Y-%m-%d"`;
echo $(date) " - Web Backup >> Running secure copy: scp {user}@{ip}:/var/backup/web1/web1_$WEBDATE* site-files/"
scp {user}@{ip}:/var/backup/web1/web1_$WEBDATE* site-files/
... etc ...

Use the full path to the key the way you use the full path to the script and the output files and it should work.

If you want to have the script load the key from the same directory as the script automatically then see Getting the source directory of a Bash script from within and http://mywiki.wooledge.org/BashFAQ/028 for ways/how to do that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM