Reputation: 73
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
Upvotes: 5
Views: 1283
Reputation: 3919
Because crontab use sh as default shell , it not support brace expansion
so the --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"}
not work
you can add
SHELL=/bin/bash
above your crontab job.
for example
SHELL=/bin/bash
1 1 * * * cp /test/{*txt,*sh} /tmp/
Upvotes: 6
Reputation: 72639
Check three things:
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.
Upvotes: 2