简介
1 |
rsync英文称为remote synchronizetion,从软件的名称就可以看出来,rsync具有可使本地和远程两台主机之间的数据快速复制同步镜像、远程备份的功能,这个功能类似于ssh带的scp命令,但是又优于scp命令的功能,scp每次都是全量拷贝,而rsync可以增量拷贝。当然,rsync还可以在本地主机的不同分区或目录之间全量及增量的复制数据,这又类似cp命令。但是同样也优于cp命令,cp每次都是全量拷贝,而rsync可以增量拷贝。在同步数据的时候,默认情况下,rsync通过其独特的“quick check”算法,它仅同步大小或者最后修改时间发生变化的文件或目录,当然也可根据权限、属主等属性的变化同步,但是需要制定相应的参数,甚至可以实现只同步一个文件里有变化的内容部分,所以,可是实现快速的同步备份数据。 |
rsync特性
1 2 3 4 5 6 7 |
支持拷贝特殊文件,如连接文件、设备等。 可以有排除指定文件或目录同步的功能,相当于打包命令tar的排除功能。 可以做到保持原文件或目录的权限、时间、软硬链接、属主、组等所有属性均不改变 –p。 可以实现增量同步,既只同步发生变化的数据,因此数据传输效率很高(tar-N)。 可以使用rcp、rsh、ssh等方式来配合传输文件(rsync本身不对数据加密)。 可以通过socket(进程方式)传输文件和数据(服务端和客户端)*****。 支持匿名的活认证(无需系统用户)的进程模式传输,可以实现方便安全的进行数据备份和镜像。 |
rsync生产场景
1 2 3 4 5 |
1.借助cron+rsync把所有客户端服务器数据同步到备份服务器。 2.针对公司重要数据备份混乱状况和领导提出备份全网数据的解决方案。 3.通过本地打包备份,然后rsync结合inotify应用把全网数统一备份到一个固定存储服务器,然后在存储服务器上通过脚本检查并报警管理员备份结果。 4.定期将IDC机房的数据 备份公司的内部服务器,防止机房地震及火灾问题导致数据丢失。 5.实时同步,解决存储服务器等的单点问题。 |
rsync命令选项
1)常用选项
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
-a #归档模式传输, 等于-tropgDl -t -r -o -p -g -D -l -v #详细模式输出, 打印速率, 文件数量等 -z #传输时进行压缩以提高效率 -r #递归传输目录及子目录,即目录下得所有目录都同样传输。 -t #保持文件时间信息 -o #保持文件属主信息 -p #保持文件权限 -g #保持文件属组信息 -l #保留软连接 -P #显示同步的过程及传输时的进度等信息 -D #保持设备文件信息 -L #保留软连接指向的目标文件 -e #使用的信道协议,指定替代rsh的shell程序 --exclude=PATTERN #指定排除不需要传输的文件模式 --exclude-from=file #文件名所在的目录文件 --partial #断点续传 --password-file=xxx #使用密码文件 |
2)限速演示
1 2 3 4 5 6 7 |
--bwlimit=100 #限速传输 [root@web01 /tmp]# rsync -avP --bwlimit 100 /tmp/ rsync_backup@172.16.1.41::backup sending incremental file list ./ data.txt 5,537,792 1% 100.04kB/s |
3)数据一致
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
--delete #让目标目录和源目录数据保持一致 #推:数据与推送数据的源数据一致 [root@web01 /tmp]# rsync -avP --delete /tmp/ rsync_backup@172.16.1.41::backup sending incremental file list deleting file2 deleting file1 sent 223 bytes received 43 bytes 532.00 bytes/sec total size is 0 speedup is 0.00 #推:数据与拉取数据的源数据一致 [root@web01 /tmp]# rsync -a -v -P rsync_backup@172.16.1.41::backup /tmp --delete receiving incremental file list deleting file4 deleting file3 ./ sent 32 bytes received 203 bytes 470.00 bytes/sec total size is 0 speedup is 0.00 |
远程推拉方式(类似于scp)
pull拉取数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#语法: 命令 选项 用户 @ 主机:源文件 目标地址 rsync [OPTION...] [USER@]HOST:SRC... [DEST] #如果用户不写,则以当前用户去连接远程机器 #示例 [root@web01 ~]# rsync -avz root@172.16.1.41:/root/1.txt ./ receiving incremental file list 1.txt sent 43 bytes received 84 bytes 28.22 bytes/sec total size is 0 speedup is 0.00 #拆分语句 rsync #命令 -avz #选项 root #用户 @ #分隔符 172.16.1.41: #主机地址 /root/1.txt #源文件,要拉取得文件 ./ #拉取到的目录地址 #注意: #拉取远程服务器目录及目录下的文件 [root@web01 ~]# rsync -avz root@172.16.1.41:/tmp ./ #拉取远程服务器目录下的文件,不包含目录本身 [root@web01 ~]# rsync -avz root@172.16.1.41:/tmp/ ./ |
push推送数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#语法: rsync [OPTION...] SRC... [USER@]HOST:DEST #示例: [root@web01 ~]# rsync -avz file1 root@172.16.1.41:/root/ root@172.16.1.41's password: sending incremental file list file1 sent 83 bytes received 35 bytes 78.67 bytes/sec total size is 0 speedup is 0.00 #命令拆分 rsync #命令 -avz #选项 file1 #源文件 root #用户 @ #分隔符 172.16.1.41: #主机 /root/ #目标地址 |
安装rsync
1 |
[root@backup ~]# yum install -y rsync |
服务端配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[root@backup ~]# vim /etc/rsyncd.conf uid = rsync #指定运行程序的用户 gid = rsync #运行程序的用户组 port = 873 #服务的监听端口 fake super = yes #不需要root用户启动,安全机制 use chroot = no #限制操作目录 max connections = 200 #设置最大连接数 timeout = 600 #超时时间 ignore errors #忽略错误 read only = false #关闭只读权限 list = false #查看模块列表 auth users = rsync_backup #指定授权的虚拟用户 secrets file = /etc/rsync.passwd #指定虚拟用户使用的密码 log file = /var/log/rsyncd.log #日志文件 ##################################### [backup] #模块名字 comment = welcome to oldboyedu backup! #注释 path = /backup #真实文件目录 |
服务端创建用户
1 |
[root@backup ~]# useradd rsync -s /sbin/nologin -M |
服务端创建密码文件
1 2 3 4 5 6 7 |
[root@backup ~]# vim /etc/rsync.passwd rsync_backup:123456 [root@backup ~]# echo "rsync_backup:123456" > /etc/rsync.passwd #授权 [root@backup ~]# chmod 600 /etc/rsync.passwd |
服务端创建真实文件目录
1 2 |
[root@backup ~]# mkdir /backup [root@backup ~]# chown -R rsync.rsync /backup/ |
服务端启动服务
1 2 3 4 5 6 7 8 9 10 |
[root@backup ~]# systemctl start rsyncd #验证启动 [root@backup ~]# netstat -lntp tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 18673/rsync tcp6 0 0 :::873 :::* LISTEN 18673/rsync [root@backup ~]# ps -ef | grep rsync root 18673 1 0 17:01 ? 00:00:00 /usr/bin/rsync --daemon --no-detach root 18680 7850 0 17:02 pts/0 00:00:00 grep --color=auto rsync |
客户端验证推送
1)输入密码的方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#推送数据 [root@web01 ~]# rsync -avz file2 rsync_backup@172.16.1.41::backup Password: 123456 sending incremental file list file2 sent 87 bytes received 43 bytes 37.14 bytes/sec total size is 0 speedup is 0.00 #拉取数据 [root@web01 ~]# rsync -avz rsync_backup@172.16.1.41::backup ./ Password: receiving incremental file list ./ qiudaodsb sent 53 bytes received 173 bytes 64.57 bytes/sec total size is 0 speedup is 0.00 |
2)指定密码文件的方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#客户端配置密码文件 [root@web01 ~]# vim /etc/rsyncd.password 123456 [root@web01 ~]# echo "123456" > /etc/rsyncd.password #授权 [root@web01 ~]# chmod 600 /etc/rsyncd.password #推送数据 [root@web01 ~]# rsync -avz file3 rsync_backup@172.16.1.41::backup --password-file=/etc/rsyncd.password sending incremental file list file3 sent 87 bytes received 43 bytes 86.67 bytes/sec total size is 0 speedup is 0.00 #拉取数据 [root@web01 ~]# rsync -avz rsync_backup@172.16.1.41::backup ./ --password-file=/etc/rsyncd.password receiving incremental file list ./ qiudaocsb sent 50 bytes received 211 bytes 174.00 bytes/sec total size is 0 speedup is 0.00 |
3)配置环境变量的方式
1 2 3 4 5 6 7 |
[root@web01 ~]# export RSYNC_PASSWORD=123456 #拉取数据 [root@web01 ~]# rsync -avz rsync_backup@172.16.1.41::backup ./ #推送数据 [root@web01 ~]# rsync -avz file3 rsync_backup@172.16.1.41::backup |
rsync常见报错
1)报错1
1 2 3 4 5 6 |
#报错: [root@web01 ~]# rsync -avz rsync_backup@172.16.1.41::backu ./ @ERROR: Unknown module 'backu' #原因: 模块名字与服务端配置没有对应 |
2)报错2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#报错: [root@web01 ~]# rsync -avz /tmp/ rsync_backup@172.16.1.41::backup sending incremental file list rsync: failed to write xattr user.rsync.%stat for "." (in backup): Permission denied (13) rsync: failed to set times on "." (in backup): Operation not permitted (1) ./ sent 176 bytes received 205 bytes 762.00 bytes/sec total size is 0 speedup is 0.00 rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1179) [sender=3.1.2] #原因: 1.权限不足,服务端目录权限过高 2.selinux |
3)报错3
1 2 3 4 5 6 7 8 9 |
#错误: [root@web01 ~]# rsync -avz /tmp/ rsync_back@172.16.1.41::backup @ERROR: auth failed on module backup #原因: 1.虚拟用户错误 2.密码错误 3.密码文件权限不是600 4.服务端密码文件不存在 |
4)报错4
1 2 3 4 5 6 |
#错误: [root@web01 ~]# rsync -avz /tmp/ rsync_backup@172.16.1.41::/backup ERROR: The remote path must start with a module name not a / #原因: 守护进程模式::后面跟的是模块名字,而不是目录名字 |
5)报错5
1 2 3 4 5 6 7 |
#错误: [root@web01 ~]# rsync -avz /tmp/ rsync_backup@172.16.1.41::backup rsync: failed to connect to 172.16.1.41 (172.16.1.41): Connection refused (111) rsync error: error in socket IO (code 10) at clientserver.c(125) [sender=3.1.2] #原因: 服务端服务没有启动 |
6)报错6
1 2 3 4 5 6 7 |
#错误: [root@web01 ~]# rsync -avz /tmp/ rsync_backup@172.16.1.41::backup rsync: failed to connect to 172.16.1.41 (172.16.1.41): No route to host (113) rsync error: error in socket IO (code 10) at clientserver.c(125) [sender=3.1.2] #原因: 防火墙开启 |
7)报错7
1 2 3 4 5 6 |
#错误 [root@web01 ~]# rsync -avz /tmp/ rsync_backup@172.16.1.41::backup @ERROR: chdir failed #原因: 服务端文件目录不存在 |