Shell script zips a folder, but exclude parameters not working IF its run from cron.daily

The script works fine if I run it with sh filename, but it ignores all the --exclude parameters if its run by cron:

#!/bin/sh
TIMESTAMP=$(date +%u)

WEEK=`date +"%V"`
if [ $(($WEEK%2)) -eq 0 ];
then
    echo "even";
    BACKUP_DIR="/mnt/backup/2_$TIMESTAMP"
    mkdir -p "$BACKUP_DIR/www"

else
    echo "odd";
    BACKUP_DIR="/mnt/backup/1_$TIMESTAMP"
    mkdir -p "$BACKUP_DIR/www"  
fi

if [ $(date +%u) -gt 6 ];
then
    zip -r -y $BACKUP_DIR/www/web.zip /var/www/ --exclude='var/www/\.opcache/*' --exclude='var/www/utils/phpThumb/cache/*'
else
    zip -r -y $BACKUP_DIR/www/web.zip /var/www/ --exclude='var/www/mycompany/files/*' --exclude='var/www/\.opcache/*' --exclude='var/www/mycompany/szamlazz/xml_szamlak/*' --exclude='var/www/mycompany/files_admin/szamlazz/*' --exclude='var/www/mycompany/files_admin/chatFiles/*' --exclude='var/www/mycompany/utils/phpThumb/cache/*' --exclude='*/backup-guard/*' --exclude='var/www/mycompany/mailer/*' --exclude='var/www/mycompany/attachments/*'
fi

exit 0

The above script should create a larger backup on sunday, and a partial on weekdays, but it just zips everything.


Your zip command will update the zip file if it exists. You write to directories in a cyclic manner, they are reused eventually. If you have an old zip file created without exclusions, the update won't remove old versions of files you want to exclude from the archive.

Straightforward solution: remove the old zip file before you run zip.