1. MagicESP Test
  2. Ubuntu Server 22.04
  3. SSH Transfer Files Using rsync

SSH Transfer Files Using rsync

Synonyms:
  1. rsync

rsync can be used to synchronize files between two Ubuntu servers. It is more complicated than scp.

Prerequisite

$[username]$

  • SSH connect using the same username without password as SSH Connect from Ubuntu said.
  • Login to $username$ account of server A.

Synchronize Directory

$[a_path]$

$[b_hostname]$

$[b_path]$

$[option_filter_exclude]$

$[option_delete]$

$[option_filter_protect]$

Push Directory from Server A to Server B

$ show : !_.isEmpty(option_filter_excludes) $

The following files/directories in Server A will be excluded:

$ repeat : excludes $

$ end : show $

$ show : option_delete.value $

Files/directories exist in Server B but aren't present in Server A will be deleted, with exception of the following patterns: {class="red-text"}

$ repeat : protects $

$ end : show $

sudo rsync -avz -e "sudo -u $username$ ssh" --rsync-path="sudo rsync" $option$ $a_path$ $b_hostname$:$b_path$

Pull Directory from Server B to Server A

$ show : !_.isEmpty(option_filter_excludes) $

The following files/directories in Server B will be excluded.

$ repeat : excludes $

$ end : show $

$ show : option_delete.value $

Files/directories exist in Server A but aren't present in Server B will be deleted, with exception of the following patterns: {class="red-text"}

$ repeat : protects $

$ end : show $

sudo rsync -avz -e "sudo -u $username$ ssh" --rsync-path="sudo rsync" $option$ $b_hostname$:$b_path$ $a_path$

parallel by directories (Pull Directory from Server B to Server A)

$[job]$

sudo rsync --dry-run --filter "+ /*/" --filter "- *" -avz -e "sudo -u $username$ ssh" --rsync-path="sudo rsync" $option$ $b_hostname$:$b_path$ $a_path$ > /tmp/transfer.log
sed -i '1d' /tmp/transfer.log
sed -i '$d' /tmp/transfer.log
sed -i '$d' /tmp/transfer.log
sed -i '$d' /tmp/transfer.log

# 在删除一部分?(需要修改)
#sed -i '/\/$/d' /tmp/transfer.log

# dry-run测试
tail /tmp/transfer.log
cat /tmp/transfer.log | parallel --will-cite --dry-run -j $job$ sudo rsync -avz -e \"sudo -u $username$ ssh\" --rsync-path=\"sudo rsync\" $option$ $b_hostname$:$b_path${} $a_path${} > /tmp/result.log
tail /tmp/result.log

cat /tmp/transfer.log | parallel --will-cite -j $job$ sudo rsync -avz -e \"sudo -u $username$ ssh\" --rsync-path=\"sudo rsync\" $option$ $b_hostname$:$b_path${} $a_path${} > /tmp/result.log
tail /tmp/result.log

Reference

rsync Filter Rules

Search FILTER RULES in man page of rsync.