rsync --exclude 'folder', copies that folder anyway
<p>There are a lot of posts about this, I know, but I tried all and still can't get it working.</p>

<p>If this is my folder to backup: <code>/home/user/thingstobackup</code>
the script will create a "backup" folder here and inside another folder named as the date of today. The daily backup is copied inside. </p>

<p>No matter how I use rsync, the "backup" folder will be always copied inside itself starting from the 2nd run of the script.</p>

<p>1st run:</p>

<pre><code>/home/user/thingstobackup
/home/user/thingstobackup/backup/2016-01-13 #and correct file inside
</code></pre>

<p>2nd run:</p>

<pre><code>/home/user/thingstobackup/backup/2016-01-13 #with correct file inside
/home/user/thingstobackup/backup/2016-01-14 #with correct file inside
</code></pre>

<p>I will shorten the path here..</p>

<pre><code>../backup/2016-01-14/2016-01-13/and backed up file inside..
../backup/2016-01-14/backup/
../backup/2016-01-14/backup/2016-01-13/and backed up file inside..
../backup/2016-01-14/backup/2016-01-14/empty
</code></pre>

<p>After the 2nd run, the backup folder is copied inside every daily backup folder.</p>

<p>The script:</p>

<pre><code>#!/bin/bash

export PATH=$PATH:/bin:/usr/bin:/usr/local/bin

# directory to backup
TOSAVE=/home/user/thingstobackup
TODAY=`date +%F`
BDIR=backup
BACKUPDIR=$TOSAVE/$BDIR/$TODAY/

# options for rsync
OPTS="-aq --exclude='backup/*'"

# find daily new file
FIND="`find $TOSAVE -mindepth 1 -mtime -1 -print`"

# MAIN #

# copy daily found inside new created daily folder
[ -d $TOSAVE/$BDIR/$TODAY ] || mkdir -p $TOSAVE/$BDIR/$TODAY
rsync $OPTS $FIND $BACKUPDIR

# delete file older than 2 weeks = 14 days
find $TOSAVE -mtime +14 -exec rm -rf {} \;
</code></pre>

<p>No matter how I use <code>--exclude='backup/*'"</code> <code>--exclude='backup'</code> || <code>--exclude 'backup/*'</code> || <code>--exclude 'backup'</code></p>

<p>It does not exclude that folder.. Yes I read the rsync manual: <code>--exclude=PATTERN       exclude files matching PATTERN</code></p>

<p>I'm sure I'm missing something but I just can't find it! Thanks in advance mates</p>