66

I am trying to backup my home folder on my NAS drive. I am giving this:

rsync -Paz --exclude-from 'rsync-exclude.txt' /home/chris/ [email protected]:LinuxHome

where rsync-exclude.txt has this content:

/home/chris/Downloads/*
/home/chris/Downloads/
/home/chris/Downloads/*.*

and it is in the same folder I execute rsync (home folder).

However the rsync tries to copy this folder, too.

What am I doing wrong?

2 Answers 2

101

You are providing absolute paths in your exclude list.

With rsync, all exclude (or include!) paths beginning with / are anchored to the "root of transfer".

The root of transfer in this case is /home/chris. If you did:

rsync -Paz --exclude-from 'rsync-exclude.txt' / [email protected]:`

…then your exclusions should work (but you'd be copying everything else on that filesystem!).

But since you're just trying to sync your home directory, and there is no subdirectory of /home/chris named "home/chris/Downloads", rsync finds nothing that matches.

So try removing the /home/chris parts from your rsync-exclude.txt file.

Actually, you should just need a single line in the file:

/Downloads

Note that if you don't specify the leading /, and you happen to have other directories named "Downloads", those would also be excluded. I'm assuming you only want to exclude your "top-level" (relative to the source directory, aka the "root of transfer") Downloads directory, so you'll want the leading /.

The easiest way (to exclude only a few paths)

If you only need to exclude one directory, just do this (avoiding a separate file):

rsync -Paz --exclude /Downloads /home/chris/ [email protected]:LinuxHome

You can also chain together --exclude tags, like so:

rsync -Paz --exclude /Downloads --exclude '/Pictures' --exclude .cache \
  /home/chris/ [email protected]:LinuxHome

Note that in this example, since .cache has no leading slash, rsync will exclude any file or directory named .cache from every directory it transfers. You can leave out the backslash (\), as long as you type everything on a single line. It has nothing to do with rsync.

If you have more than a few exclusions, you're better off with --exclude-from and a file.

Note

I see that you got it right, but those new to rsync should note the slash at the end of /home/chris/

To quote the rsync man page, "You can think of a trailing / on a source as meaning 'copy the contents of this directory' as opposed to 'copy the directory by name'."

So if you left off that trailing slash, you would end up with a directory called chris within the target directory, containing everything from /home/chris (except the original Downloads directory, of course!).

1
  • Aah, at least a fine and definitive answer about excluding absolute paths, thanks.
    – Déjà vu
    Commented Oct 5, 2020 at 7:40
5

This might be easier on the eyes, and just a note on excluding directories and syntax:

SRC='/home/username'
DST='/run/media/username/EasyStoreRT/rsync'

rsync -avrh --stats                                 \
    --log-file=/home/username/log/rsync-home.log    \
    --exclude='/username/.cache'                    \
    --exclude='/username/.local/share/Trash'        \
    $SRC \
    $DST \

It will exclude those directories and all files within them. For whatever reason, rysnc wasn't appending /home/username to /.cache. Only home/ would anchor itself, so I had to add /username to each excluded directory.

The man pages say, "if the pattern starts with a / then it is anchored to a particular spot in the hierarchy of files, otherwise it is matched against the end of the pathname." But, that hierarchy seems to only be the first directory in the source directory. I'm using bash, version 4.4.23(1)-release.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .