2

I have created a bash script as follows and saved it as /usr/bin/db.sync.sh:

#!/bin/bash

rsync -ar --delete /files/database_backups [email protected]:/users/slyme/Documents/server_backups

I have generated the necessary pair of authentication keys to make this work without the need to enter a password.

When I run the command directly it works (hurrah!).

I have added the following to `/etc/rc.local':

/usr/bin/db.sync.sh >>/files/database_backups/db-sync.log.txt 2>&1

When I restart the machine this script fails and the log file suggests that the process asks for a password which, of course, it doesn't get so it fails. If I run /etc/rc.local from the command line then, sure enough, I get asked for a password and when I enter it the script works.

Anyone got any ideas why the script needs a password when run from rc.local even though it doesn't need a password when run directly?

2 Answers 2

3

The script /etc/rc.local is run as root. Make sure that you have the correct private key for user [email protected] installed in /root/.ssh/ (and not just in your user's $HOME/.ssh)

Alternatively, to make sure the correct private key is used, you can specify it on the rsync command line:

rsync -e "ssh -i /path/to/ssh_key" ...

Hope this helps.

1
  • I copied the key to the root account. It worked. Thank you. This was the last step in what (for me) was a long process of configuring a development server just the way I want it. I am very happy with the outcome.
    – Simon
    Jul 31, 2014 at 11:40
3

If you want to use /etc/rc.local you should run the script as your user:

su -l myuser -c /path/to/the/script

Otherwise, you can use the @startup option of the cron system; this way the job is safely executed as your user and you do not need to edit system files. See for example https://unix.stackexchange.com/questions/57852/crontab-job-start-1-min-after-reboot

1
  • Thanks for your suggestion re user, I shall try it out. I can't use a cron job for this without a lot of fiddling about because a script that this one relies on (to create the files in the first place) needs mysql to be up and running before it executes
    – Simon
    Jul 31, 2014 at 11:21

You must log in to answer this question.

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