rsync 使用,关联

rsync 基本语法

1
rsync options source destination

常用选项:

  • -v : verbose
  • -r : copies data recursively (but don’t preserve timestamps and permission while transferring data.
  • -a : archive mode, which allows copying files recursively and it also preserves symbolic links, file permissions, user & group ownerships, and timestamps.
  • -z : compress file data.
  • -h : human-readable, output numbers in a human-readable format.
  • -P --progress 显示传输进度。

安装 rsync

1
2
3
4
5
$ sudo apt-get install rsync   [On Debian/Ubuntu & Mint] 
$ pacman -S rsync              [On Arch Linux]
$ emerge sys-apps/rsync        [On Gentoo]
$ sudo yum install rsync       [On Fedora/CentOS/RHEL and Rocky Linux/AlmaLinux]
$ sudo zypper install rsync    [On openSUSE]

依据列表文件拷贝

--files-from 从列表(一行一个文件路径)文件拷贝

1
rsync -a --files-from=/tmp/foo /usr remote:/backup

使用 ssh 协议传输

-e 来指定传输协议

-e ssh 指定 ssh 协议

1
rsync -avzhe ssh root@192.168.0.141:/root/anaconda-ks.cfg /tmp

--include --exclude 包含和排除要转移的文件

1
2
# include those files and directory only which starts with ‘R’ and exclude all other files and directory.
rsync -avze ssh --include 'R*' --exclude '*' root@192.168.0.141:/var/lib/rpm/ /root/rpm

设置文件大小的条件

1
2
# in this example, the Max file size is 200k, so this command will transfer only those files which are equal to or smaller than 200k.
rsync -avzhe ssh --max-size='200k' /var/lib/rpm/ root@192.168.0.151:/root/tmprpm

模拟运行,不产生实际效果 –dry-run

1
rsync --dry-run --remove-source-files -zvh backup.tar.gz root@192.168.0.151:/tmp/backups/