1 rsync简介
一种远程同步工具,实现了一种高效的文件同步算法,具体可以搜索相关论文研究学习,可以实现文件增量、全量同步,删除等操作,是一种用途广泛的运维工具
2 安装
下载目录 https://download.samba.org/pub/rsync/src/
wget https://download.samba.org/pub/rsync/src/rsync-3.1.2.tar.gz
tar -zxvf rsync-3.1.2.tar.gz
cd rsync-3.1.2
./configure --prefix=/usr/local/rsync
make && make install
源和目标机器都要安装rsync
3 配置
配置文件为/etc/rsyncd.conf
,打开可以看到相关配置
# /etc/rsyncd: configuration file for rsync daemon mode
# See rsyncd.conf man page for more options.
# uid = nobody
# gid = nobody
uid = 0 #运行rsync守护进程的用户
gid = 0 #运行rsync守护进程的组
use chroot = yes #使用chroot安全开关
max connections = 4 #最大连接数
readonly = no #可写权限
# pid file = /var/run/rsyncd.pid #进程文件
# exclude = lost+found/
log file = /tmp/rsync.log #日志文件路径
transfer logging = yes #
timeout = 900 #连接超时时长
# ignore nonreadable = yes
# dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
[ftp]
path = /home/ftp
comment = ftp export area
[test] #定义test模块
path = /opt/projects #指定模块的目录,具体传输到哪个目录,可在传输参数上指定
readonly = false #可写权限
comment = rsync files #注释
4 启动
/usr/bin/rsync --daemon --config=/etc/rsyncd.conf
rsync在复制大文件时,目标机上首先会生成指定文件名称+随机字符串的临时文件,当文件传输完成后,会重命名为指定文件名称
5 文件传输过程
#指定传输文件到test模块下的bak目录,结合rsyncd.conf的test模块配置,目的目录即/opt/projects/bak
rsync -avz 2.mp4 root@10.2.7.146::test/bak
源机器
[root@xx.xx.xx.xx]$ rsync -avz 2.mp4 root@10.2.44.75::test/bak
sending incremental file list 2.mp4
sent 2045238 bytes received 27 bytes 1363510.00 bytes/sec
total size is 2044471 speedup is 1.00
目标机器
2021/11/06 11:46:25 [22369] name lookup failed for xx.xx.xx.xx: Name or service not known
2021/11/06 11:46:25 [22369] connect from UNKNOWN (xx.xx.xx.xx)
2021/11/06 03:46:25 [22369] rsync to test/bak from UNKNOWN (xx.xx.xx.xx)
2021/11/06 03:46:25 [22369] receiving file list
2021/11/06 03:46:26 [22369] recv UNKNOWN [xx.xx.xx.xx] test () bak/2.mp4 2044471
2021/11/06 03:46:26 [22369] sent 39 bytes received 2047254 bytes total size 2044471