Local

  • Source directory: dir1
  • Destination directory: dir2
  • Testing: rsync -n ...

Synchronize a directory in another directory

_$: ls -lar dir1
total 20
drwxr-xr-x 2 <user> <user> 4096 Jan  9 09:06 sub12
drwxr-xr-x 2 <user> <user> 4096 Jan  9 09:06 sub11
-rw-r--r-- 1 <user> <user>    2 Jan  9 09:06 a
drwxr-xr-x 4 <user> <user> 4096 Jan  9 09:06 ..
drwxr-xr-x 4 <user> <user> 4096 Jan  9 09:06 .
_$: rsync -av dir1/ dir2/
sending incremental file list
./
a
sub11/
sub12/

sent 163 bytes  received 42 bytes  410.00 bytes/sec
total size is 2  speedup is 0.01

_$: ls -lar dir2/
total 20
drwxr-xr-x 2 <user> <user> 4096 Jan  9 09:06 sub12
drwxr-xr-x 2 <user> <user> 4096 Jan  9 09:06 sub11
-rw-r--r-- 1 <user> <user>    2 Jan  9 09:06 a
drwxr-xr-x 4 <user> <user> 4096 Jan  9 09:06 ..
drwxr-xr-x 4 <user> <user> 4096 Jan  9 09:06 .

Synchronize a directory in another non-empty directory

_$: ls -lar dir1/
total 20
drwxr-xr-x 2 <user> <user> 4096 Jan  9 09:06 sub12
drwxr-xr-x 2 <user> <user> 4096 Jan  9 09:06 sub11
-rw-r--r-- 1 <user> <user>    2 Jan  9 09:06 a
drwxr-xr-x 4 <user> <user> 4096 Jan  9 09:06 ..
drwxr-xr-x 4 <user> <user> 4096 Jan  9 09:06 .

_$: ls -lar dir2/
total 12
-rw-r--r-- 1 <user> <user>    2 Jan  9 09:14 b
drwxr-xr-x 4 <user> <user> 4096 Jan  9 09:06 ..
drwxr-xr-x 2 <user> <user> 4096 Jan  9 09:14 .

_$: rsync -av dir1/ dir2/
sending incremental file list
./
a
sub11/
sub12/

sent 163 bytes  received 42 bytes  410.00 bytes/sec
total size is 2  speedup is 0.01
_$: ls -lar dir2/
total 24
drwxr-xr-x 2 <user> <user> 4096 Jan  9 09:06 sub12
drwxr-xr-x 2 <user> <user> 4096 Jan  9 09:06 sub11
-rw-r--r-- 1 <user> <user>    2 Jan  9 09:14 b
-rw-r--r-- 1 <user> <user>    2 Jan  9 09:06 a
drwxr-xr-x 4 <user> <user> 4096 Jan  9 09:06 ..
drwxr-xr-x 4 <user> <user> 4096 Jan  9 09:06 .

Synchronize a directory in another directory excluding .git files

_$: ls -lar dir1/
total 24
drwxr-xr-x 2 <user> <user> 4096 Jan  9 09:06 sub12
drwxr-xr-x 2 <user> <user> 4096 Jan  9 09:06 sub11
-rw-r--r-- 1 <user> <user>    0 Jan  9 09:22 .git1
drwxr-xr-x 2 <user> <user> 4096 Jan  9 09:22 .git
-rw-r--r-- 1 <user> <user>    2 Jan  9 09:06 a
drwxr-xr-x 4 <user> <user> 4096 Jan  9 09:06 ..
drwxr-xr-x 5 <user> <user> 4096 Jan  9 09:22 .

_$: rsync -av --exclude=".git*" dir1/ dir2/
sending incremental file list
./
a
sub11/
sub12/

sent 163 bytes  received 42 bytes  410.00 bytes/sec
total size is 2  speedup is 0.01

_$: ls -lar dir2/
total 20
drwxr-xr-x 2 <user> <user> 4096 Jan  9 09:06 sub12
drwxr-xr-x 2 <user> <user> 4096 Jan  9 09:06 sub11
-rw-r--r-- 1 <user> <user>    2 Jan  9 09:06 a
drwxr-xr-x 4 <user> <user> 4096 Jan  9 09:06 ..
drwxr-xr-x 4 <user> <user> 4096 Jan  9 09:22 .

Synchronize a directory in another directory excluding .git and .pyc files

_$: ls -lar dir1/
total 24
drwxr-xr-x 2 <user> <user> 4096 Jan  9 09:06 sub12
drwxr-xr-x 2 <user> <user> 4096 Jan  9 09:06 sub11
-rw-r--r-- 1 <user> <user>    0 Jan  9 09:29 script.pyc
-rw-r--r-- 1 <user> <user>    0 Jan  9 09:29 script.py
-rw-r--r-- 1 <user> <user>    0 Jan  9 09:22 .git1
drwxr-xr-x 2 <user> <user> 4096 Jan  9 09:22 .git
-rw-r--r-- 1 <user> <user>    2 Jan  9 09:06 a
drwxr-xr-x 4 <user> <user> 4096 Jan  9 09:06 ..
drwxr-xr-x 5 <user> <user> 4096 Jan  9 09:29 .

_$: rsync -av --exclude=".git*" --exclude="*.pyc" dir1/ dir2/
sending incremental file list
./
a
script.py
sub11/
sub12/

sent 224 bytes  received 61 bytes  570.00 bytes/sec
total size is 2  speedup is 0.01

_$: ls -lar dir2/
total 20
drwxr-xr-x 2 <user> <user> 4096 Jan  9 09:06 sub12
drwxr-xr-x 2 <user> <user> 4096 Jan  9 09:06 sub11
-rw-r--r-- 1 <user> <user>    0 Jan  9 09:29 script.py
-rw-r--r-- 1 <user> <user>    2 Jan  9 09:06 a
drwxr-xr-x 4 <user> <user> 4096 Jan  9 09:06 ..
drwxr-xr-x 4 <user> <user> 4096 Jan  9 09:29 .

Remote

  • Source host: src
  • Source user: userSRC
  • Source directory: dirSRC
  • Destination host: dst
  • Destination user: userDST
  • Destination directory: dirDST
  • Testing: rsync -n ...

Synchronize a directory in a remote host

a) Pull from dst of src:

(userDST@dst)_$: rsync -av -e "ssh" userSRC@src:/dirSRC/ /dirDST

b) Push from src to dst:

(userSRC@src)_$: rsync -av -e "ssh" /dirSRC/ userDST@dst:/dirDST

Synchronize all files starting with main_ from a directory in a remote host

Push from src to dst:

(userSRC@src)_$: rsync -av -e "ssh" \
  --include="main_*" --exclude="*" \
  /dirSRC/ userDST@dst:/dirDST

Troubleshooting

1) The user userDST is not the owner of /dir

If userDST is not root, add userDST to the sudoers file with:

backup        ALL= NOPASSWD: /usr/bin/rsync

2) The user root is the owner of /dir

Add userDST to the sudoers file with:

backup        ALL= NOPASSWD: /usr/bin/rsync

Add the flag --rsync-path="sudo rsync" to the command line:

_$: rsync <options> --rsync-path="sudo rsync" <src> <dst>