Ubuntu: Using rsync with exclude/include


I'm running 2 ubuntu 9.10 servers. I have to synchronize the changes between these two machines. The following are the directories I've to synchronize:

/usr, /etc, /var, /bin, /sbin, /lib

I'm using rsync like the below for replication:

rsync -avP /usr

rsync -avP /etc ------here I'll have to exclude many files like /etc/udev/*, /etc/hosts, /etc/hostname,...

I tried using rsync -avP --exclude '/etc/fstab' /etc but it overwritten entire /etc directory

Can any one edit and give me rsync to one line command which synchronizes specified directories and not which are excluded?. Need help!


The exclude pattern is relative to the source files, not the destination files. You're actually excluding, which does not exist.

You should use:

rsync -avP --exclude '/fstab' /etc  


You should use something like this:

$ sudo rsync "$backup_options" --log-file="$backup_log_file" --include-from="$backup_incl_from_file" --exclude-from="$backup_excl_from_file" "$backup_src_folder" "$backup_dest_folder"  

where the text file $backup_incl_from_file should include:

usr/***  etc/***  var/***  bin/***  sbin/***  lib/***  ...  

and the text file $backup_excl_from_file should include:

etc/udev/  etc/hosts  etc/hostname  ...  

In this example

backup_src_folder=""  backup_dest_folder=""  

It's important you read the rsync man page before you use this example to meet your needs, paying special attention to the INCLUDE/EXCLUDE PATTERN RULES section.


The following is the rsync command to sync multiple directories in one-line and is worked for me. Please let me know if it is the right way to use.

This should be run on SOURCE system:

rsync -avP --exclude 'udev' --exclude 'hosts' --exclude 'hostname' --exclude 'any-account-under-home' /etc /home /usr /var /lib /bin /sbin  

We can exclude many files/directories from specified directories.

If we have a file with the same name let say 'test' under /etc/ and /home and you want to exclude this file only from /etc, then placing a exclude entry excluding 'test' from all .


Try this in the Advanced Options window to exclude a directory and its sub directories

--exclude "/user/pathtodirectory*/" where 'user' is your user name and 'pathtodirectoy' is the the directory you want excluded, along with all its sub directories. Make sure you include the double quotes and forward slashes.

Example: --exclude "/johndoe/downloads*/"

Tested on both Ubuntu 14.04 and Ubuntu Gnome 14.04.

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