2

With this cron script I am trying to get Rsync to work

*/1 * *    * * /root/backup.sh  `date +today/\%M`

And a shell script as the one below

#!/bin/bash -x
PATH=/bin:/usr/bin:/sbin:/usr/sbin
REMOTE="REMOTEADDRESS"
RSYNC=/usr/bin/rsync

Comment: This works

$RSYNC -aqz --exclude-from '/home/root/backups/backup-exclude.txt' /var/www/html $REMOTE:backups/

Comment: This fails

$RSYNC -aqz  --exclude-from '/home/root/backups/backup-exclude.txt' /var/www/html $REMOTE:$1/

I am unable to understand why the passed parameter does not get passed correctly to the script. In the log everything looks as it should.

UPDATE The reason for the cron to pass the parameter and not have it in the script is to have a backup schedule that allows to have : - a backup each hour overwritten each day - a backup each day overwritten each month - a persistent Backup each month

The error when passing the parameter is that RSync can not mkdir on the remote server. It can with any static line. The log shows the correct directory passed when using the current cron.

closed as off-topic by Isaac, RalfFriedl, G-Man, Stephen Harris, Archemar Dec 2 '18 at 17:24

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Isaac, RalfFriedl, Stephen Harris, Archemar
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    What is the error message of the failed rsync command? – rudimeier Nov 30 '18 at 18:24
  • Not really related, but why do you set the path and use an absolute path to rsync? – Kusalananda Nov 30 '18 at 19:47
  • The error from rsync is that I can't mkdir on the remote server. It can with the static command. I have been testing how to create a backup schedule where it backups every hour overwriting each day and then backing up each day overwriting each month and lastly have a persistent Backup each month. So I can't hardcode the path because it needs to vary with 3 cron scripts for the hour, day and month. – Scorekaj22 Nov 30 '18 at 20:38
  • So it's a permission problem unrelated to cron. – Kusalananda Nov 30 '18 at 20:46
  • How can it be that? The cron runs it perfectly with the static line? So why should the permission be different run it with a parameter? – Scorekaj22 Nov 30 '18 at 20:48
0

You must ensure that the directory where you are trying to copy to (the dest/dir) exists in the remote computer:

rsync -aqz ./ user@host:dest/dir

At minimum, all the directories (except the last one) should already exist. That is the same behavior as mkdir: mkdir will only create the last directory (by default, the -p option can change that). That is: rsync will fail unless /home/user/dest/dir exists on the remote computer.

That is with relative directories.

The same also happens with absolute directories (ones that start with an /):

rsync -aqz ./ user@host:/home/user/dest/dir

The exact same rule but the directory is not assumed to be at the user home directory but could be anywhere (the user must have permissions to write to it, of course).


In additional testing I found that if rsync is going to use a directory like:

newdir/testdir

At least the directory newdir must exist. That is exactly the same as mkdir does. A mkdir newdir/testdir will fail if there is no newdir dir.

I tested it with the same script as you report:

  • using with a fixed dir,
  • a given dir on a parameter,
  • a given dir on the script $1 parameter
  • all the above repeated with and without cron.

In all cases, missing the initial directory of a two parts directory gets rsync to fail.

  • 1
    Thank you so much for this answer - I don't know how I succeeded in other tests. I must have somehow not deleted the directory. Thank you! – Scorekaj22 Dec 2 '18 at 12:46
  • You are welcome, glad that you found a good solution. … I must make a note that you should quote $REMOTE:$1/ as "$REMOTE:$1/" to avoid any future problems if the path could (at some future time) have spaces (any space or tab). :-) – Isaac Dec 2 '18 at 21:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.