• Provides more functions than simple scp/cp commands.
  • Also used for local directory syncing.

Basic usage

Lang: sh
rsync \[OPTIONS\] \$SRC \$DST

Advanced usage

Rsync used to sync large source codes.

Include/Exclude patterns on command line:

Lang: sh
rsync -zamvh \
--include "*/" \
--include="*.cpp" \
--include="*.c" \
--include="*.h" \
--include="*.am" \
--include="*.cfg" \
--exclude="build/*" \
--exclude=".git/*" \
$SRC $DST

rsync options used above:

Lang: text
-z, --compress, compress file data during the transfer\
-a, --archive, archive mode; equals -rlptgoD (no -H,-A,-X)\
-m, --prune-empty-dirs, prune empty directory chains from file-list\
-v, --verbose, increase verbosity\
-h, --human-readable, output numbers in a human-readable format\
-l, --links, copy symlinks as symlinks\
-L, --copy-links, transform symlink into referent file/dir

Include/Exclude patterns from text file:

Lang: sh
rsync -zauvh \
--include-from=rsync_include_patterns.txt \
--exclude-from=rsync_exclude_patterns.txt \
$SRC $DST

Common rsync include/exclude patterns:

Lang: sh
$ cat rsync_include_patterns.txt
*/
*.h
*.hh
*.hxx
*.c
*.cc
*.cpp
*.cxx
*.am
*.sh
*.py
configure.ac
CMakeLists.txt
CMakeLists.txt.in

$ cat rsync_exclude_patterns.txt
.git
CVS
*

Aliases

Create rsync aliases for long rsync commands

Lang: sh
$ export RSYNC_PATTERNS_DIR=<path-to-rsync-inc-exc-pat-files>

$ alias rsync-m="rsync -zamvh --no-l \
--include-from=$RSYNC_PATTERNS_DIR/rsync_include_patterns.txt \
--exclude-from=$RSYNC_PATTERNS_DIR/rsync_exclude_patterns.txt"

$ alias rsync-n="rsync -n -zamvh --no-l \
--include-from=$RSYNC_PATTERNS_DIR/rsync_include_patterns.txt \
--exclude-from=$RSYNC_PATTERNS_DIR/rsync_exclude_patterns.txt"

$ alias rsync-lm="rsync -zamvh --no-l -L \
--include-from=$RSYNC_PATTERNS_DIR/rsync_include_patterns.txt \
--exclude-from=$RSYNC_PATTERNS_DIR/rsync_exclude_patterns.txt"

$ alias rsync-ln="rsync -n -zamvh --no-l -L \
--include-from=$RSYNC_PATTERNS_DIR/rsync_include_patterns.txt \
--exclude-from=$RSYNC_PATTERNS_DIR/rsync_exclude_patterns.txt"

$ alias rsync-lmw="rsync -zamvh --no-l -L \
--exclude=$EXCLUDE_PATTERN \
--include-from=$RSYNC_PATTERNS_DIR/rsync_include_patterns.txt \
--exclude-from=$RSYNC_PATTERNS_DIR/rsync_exclude_patterns.txt"

$ alias rsync-lnw="rsync -n -zamvh --no-l -L \
--exclude=$EXCLUDE_PATTERN \
--include-from=$RSYNC_PATTERNS_DIR/rsync_include_patterns.txt \
--exclude-from=$RSYNC_PATTERNS_DIR/rsync_exclude_patterns.txt"

Examples

Lang: sh
cd ~/workspace
rsync-lm remote:~/remote-repos/repo_1/ repo_1/

Fast copy over ssh

refer:

Lang: sh
rsync -Pzv -e "ssh -i $SSH_KEY -p $SSH_PORT -T -x -o Compression=no" \
<local-files> <remote-files>