Score:0

SCP/RSYNC don't work in Crontab but work when run standalone

cn flag

I need to copy a backup file to a backup server everyday. I am using PublicKey SSH authentication.

All of the things work but not together:

If I run this line in crontab

rsync /home/ubuntu/*date_backup.archive* ssh [email protected]/home/centos/backup

This works and sends the backup correctly

But since the date in the filename of the .archive changes, I need to run a script. Then I create rsync_backup.sh

#!/bin/bash

date= ...

rsync /home/ubuntu/*date_backup.archive* ssh [email protected]/home/centos/backup

Then, if I run this via terminal with bash rsync_backup.sh, it works and sends the backup correctly

BUT if I put

* * * * * bash /home/ubuntu/rsync_backup.sh

or

* * * * * bash /home/ubuntu/rsync_backup.sh

The backup doesn't reach the destination host.

Also, if I put in the script another test line on local (mkdir /home/centos/test) and launch with crontab, it works!

I tried also with SCP, still the same results!

Do you have some ideas? Source machine is Ubuntu and Destination is Centos

Many thanks in advance

user535733 avatar
cn flag
Did you try redirecting cron output to a logfile to capture error output?
hr flag
Are you sure your command works outside of crontab? it looks malformed to me - in particular, the remote destination needs a `:` in it (otherwise it will be interpreted as local) and if `ssh` is meant to specify the remote shell it needs to be `-e ssh`. Regardless, like the preceding comment said, add some logging (both stdout and stderr) ex. `* * * * * bash /home/ubuntu/rsync_backup.sh >/home/ubuntu/rsync_backup.log 2>&1` which should tell you why it's failing (my guess is failed ssh authentication).
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.