Search code examples
ubuntucronrsync

rsync does not exclude extension when executed as a cron job


I'm trying to use rsync to backup all files and directories on my server, not really everything, I want to exclude a few things so I do:

/usr/bin/rsync -aAXh  --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} /* /mnt/backupdirectory

It works well and does exactly what it is supposed to do until I put it in a cron job to do it everyday.

30 23 * * * ~/scripts/backup_system.sh > ~/scripts/logs/backup_system.log 2>&1

When executed as a cron job, it doesn't exclude all the directories it is supposed to exclude. I don't understand why. Couldn't someone explain to me why?

I don't think it matters much but the system in use is Ubuntu Server 14.04.


The problem was pointed out in the answer below. The use of braces in cron is not valid and I had to replace the previous command by:

/usr/bin/rsync -aAXh --exclude=/dev/* --exclude=/proc/* --exclude=/sys/* --exclude=/tmp/* --exclude=/run/* --exclude=/mnt/* --exclude=/media/* --exclude=/lost+found /* /mnt/backupdirectory

Solution

  • Check three things:

    1. What is the error in the mail from cron?
    2. Do not use tilde expansion. Use full path names.
    3. Does your script set a PATH? Because cronjobs do not inherit any PATH.

    The last point is responsible for 99% of all cron surprises. Use something like

    PATH=$(/usr/bin/getconf PATH)
    

    at the beginning of your script. To test it on the command line, run it with an empty environment, e.g.

    /usr/bin/env -i /path/to/script.sh
    

    As long as that doesn't work, the cronjob won't.