Wednesday, November 17, 2010

Bash scripting, test and OR

Rsync exits with 0 if the transfer was successful, or a non-zero value if there was a "problem". I say "problem" with quoties because one condition that can (and does) occur regularly on our mail server is when mail files get deleted while the backup is running - they "vanish". And this isn't really an error, but rsync exits with error code 24... and if your backup script does this:

rsync --lots-of-options $SRC $DEST
EXIT=$?
if [ $EXIT -eq 0 ]
then 
    echo "Yay, we are all good: [$EXIT]"
else
    echo "Oh noes, bad things happened: [$EXIT]"
fi

...you end up with spurious error reports giving you high blood pressure.

So the obvious solution is to make the if condition for error code 0 or 24, and only spit out "Oh Noes" if it was something different. It took me a while to get the syntax right:

if [ \( $EXIT -eq 0 -o $EXIT -eq 24 \) ]

2 comments:

  1. Have you ran into many error code 23s? I have to allow those as well, as I get them every run on a 'live' system hold home directories over an AFP share. From what I can tell, it's obscure permissions on certain files, bizarre characters in filenames, or files that were modified after rsync indexed. In any event, the integrity of the backup seems fine.

    ReplyDelete
  2. Joshua, that does sound familiar - looking at the revision history, we started to run into troubles with our old mail system, which would do wacky things with inactive user accounts, making them unreadable, or unwritable. Our solution was to start using --exclude-from to read the directories to skip from a file.

    I see a few other people out there suggesting that symlinks can also cause error code 23 - maybe try rsync -a to evade issues with symlinks?

    ReplyDelete