Ubuntu: How to exclude a folder from rsync



Question:

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/ admin@192.168.1.65: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?


Solution:1

The problem is that you are specifying complete directory paths.

With rsync, all exclude (or include) paths beginning with / are 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' / admin@192.168.1.65:

...then your exclusions should work.

But since you're just trying to sync your home directory, and there is no subdirectory of /home/chris named home/chris/Downloads, it ignores your exclusion.

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: If you didn't specify the leading /, and you had a directory called /home/chris/special-stuff/Downloads, that 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 /.

Incidentally, if you had (only) the line /Downloads/* in your file, it would ensure that a Downloads directory exists on the target, but would not copy any of its contents!

Actually, if you only need to exclude one directory, it would seem easier to just do this (no exclusion file at all):

rsync -Paz --exclude /Downloads /home/chris/ admin@192.168.1.65:LinuxHome

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).


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »