rsync Ignoring Exclude Directories – Backup Issues

rsync

I know that there are few tickets with similar topic already but none of them helped with my issue.

I am using rsync to backup contents of/to 2nd HD that is mounted under /media/. Of course, I would like to have /media/* excluded, otherwise it will run into the indefinite loop.

My current rsync script that lives under /etc/cron.weekly

#!/bin/bash
rm -rf /tmp/*
rm -rf /media/LocalBackup/backup.3
mv /media/LocalBackup/backup.2 /media/LocalBackup/backup.3
mv /media/LocalBackup/backup.1 /media/LocalBackup/backup.2
cp -al /media/LocalBackup/backup.0 /media/LocalBackup/backup.1

# Backup Entire system incrementally
rsync -aAXS --inplace --delete / --exclude={'/var/log/*', '/dev/*','/proc/*','/sys/*', '/tmp/*','/run/*','/mnt/*','/media/*','/lost+found'} /media/LocalBackup/backup.0

Unfortunately, despite all my efforts, contents of /media/* IS included and rsync goes into an indefinite loop of backup up itself. I am running out of ideas.

When I run script, I am also getting this error, which might be related but I don't understand its meaning as all those directories do exist:

rsync: change_dir "/dev/*,/proc/*,/sys" failed: No such file or directory (2)
rsync: change_dir "/tmp/*,/run/*,/mnt/*,/media/*," failed: No such file or directory (2)
IO error encountered -- skipping file deletion

any ideas, suggestions?

Edit:
Changed single quotes to double quotes, but still, same issue (contents of /media/ is being backed up):

rsync -aAXS --inplace --delete / --exclude={"/var/log/*", "/dev/*","/proc/*","/sys/*", "/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} /media/LocalBackup/backup.0

Best Answer

I think @Gordon Davisson is on to the right answer in the comments: there should be not be any spaces in your exclude list. And quotes (single or double) are unnecessary. Ref: https://askubuntu.com/questions/320458/how-to-exclude-multiple-directories-with-rsync#320459

Also:

  • Exclude patterns are always relative to your source directory. So in general you should never use absolute paths in your exclude patterns. In your case the source directory is / so perhaps you get away with it (or not?) but it's still not a good idea.
  • --exclude and all other options are supposed to appear before the source directory in the rsync command line.
  • It may be easier to write and maintain your excludes if you use an --exclude-from=FILE option, where you list your exclude patterns, one per line, in an external FILE as described at the same AskUbuntu link above, and in the rsync man page.
  • The --dry-run option is very handy for showing what your rsync command will do, before actually doing it. Since that typically prints many lines, you can redirect its output to a text file and open that in an editor for easier review.
Related Topic