0

I have a problem with my backup script which is using keychain. The script works fine when fired via command line but doesn't work as expected when called via cron (does not transfer any data to the remote host). Both servers are running Ubuntu Server 14.04 LTS. My id_rsa use empty passphare, I can connect to the remote host without any password.

Cron task:

* * * * * bash /home/kris/sync.sh

Script:

#!/bin/bash
source $HOME/.keychain/${HOSTNAME}-sh
rsync -avz --delete --log-file=/home/kris/rsync.log  /var/www/owncloud/data/ [email protected]:/var/www/owncloud/data/

Output:

/home/kris/sync.sh: 2: /home/kris/sync.sh: source: not found
Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,password).
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at io.c(226) [sender=3.1.0]

.profile :

/usr/bin/keychain $HOME/.ssh/id_rsa
source $HOME/.keychain/${HOSTNAME}-sh

1 Answer 1

1

I have written the following script that connects to my PI ownclod server and backs it up on my laptop. The script is working perfectly. You can see if it will help you:

#!/bin/bash

echo "[`date +"%y-%m-%d %H:%I:%S"`] Sync started";
CURRENT_DATE=`date +%y-%m-%d`
BACKUP_DIR="/home/radoslav/Backups"
CURRENT_BACKUP_DIR="$BACKUP_DIR/backup-$CURRENT_DATE"
LOG_FILE="$BACKUP_DIR/backup-$CURRENT_DATE.log"

if [ ! -d "$CURRENT_BACKUP_DIR" ]; then
      mkdir "$CURRENT_BACKUP_DIR"
fi

if [ ! -f "$LOG_FILE" ]; then
      touch "$LOG_FILE"
fi

rsync -aAXvzlxH --exclude='/dev' --exclude='/proc' --exclude='/sys' --exclude='/lost+found' -e "ssh" --rsync-path="sudo rsync" [email protected]:/ "$CURRENT_BACKUP_DIR" >"$LOG_FILE" 2>&1

tar -czvf "$BACKUP_DIR/backup-$CURRENT_DATE.tar.gz" "$CURRENT_BACKUP_DIR"
rm -rf "$CURRENT_BACKUP_DIR"
echo "[`date +"%y-%m-%d %H:%I:%S"`] Sync ended";

Note that I am using --rsync-path="sudo rsync" in rsync command. Also under which user is set the corn to run? Under the root user or under your normal user where the ssh key is generated?

1
  • I've had keys generated as a normal user but I was trying to run the job via root's cron... and that was the reason. Thanks.
    – XorOrNor
    Apr 7, 2015 at 9:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .