Rsync具有可使本地和远程两台主机之间的数据快速复制同步镜像、远程备份的功能。
cp,scp等工具拷贝均为完整的拷贝,而rsync除了可以完整拷贝外,还具有增量拷贝的功能。
官方文档:https://www.samba.org/ftp/rsync/rsync.html
1) rsync+crontab 数据同步
2)实时数据同步rsync+inotify
1)单个主机本地直接的数据传输
rsync -avz /etc/hosts /tmp 相当于cp
rsync -avz --delete /null/ /tmp/ 相当于rm
2)remote shell
push: rsync -avzP -e ' ssh -p 22' /tmp/ rsync@192.168.1.100 :/tmp/
pull : rsync -avzP -e ' ssh -p 22' rsync@192.168.1.100 :/tmp/ /tmp/
3)rsync daemon
1)vi /etc/rsyncd.conf(需要手动生成)
uid = root
gid = root
user chroot = no
max connections = 20
timeout = 60
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
motd file = /etc/rsyncd.motd
log file = /var/log/rsyncd.log
[backup]
path = /backup
ignore errors
read only = no
list = no
hosts allow = 192.168.1.101
auth users = rsync
secrets file =/etc/rsyncd.pwd
2)创建rsync用户及共享目录/backup
useradd rsync -s /sbin/nologin -M
id rsync
mkdir /backup
chown -R rsync /backup
3)创建密码文件
echo "rsync:123456">/etc/rsyncd.pwd
chmod 600 /etc/rsync.password
4)启动服务
/usr/bin/rsync --deamon
netstat -lntup |grep rsync
ps -ef |grep rsync |grep -v grep
5)加入开机自启动
echo "/usr/bin/rsync --deamon">>/etc/rc.local
cat /etc/rc.local
1、客户端不用配置,直接使用rsync命令就可以,
rsync -vzrtopg --progress --delete rsync@192.168.1.100::backup /data/
rsync -vzrtopg --progress --delete /data/ rsync@192.168.1.100::backup
2、rsync无密码登陆,客户端需配置密码文件,只包含服务器端auth user的密码,不需要配置用户名 。
vim /etc/rsyncd.pwd
123456
chmod 600 /etc/rsyncd.pwd
注:此处密码一定要与rsync服务器端密码文件中密码保持一致。
rsync -vzrtopg --progress --delete --password-file=/etc/rsyncd.pwd rsync@192.168.1.100::backup /data/
rsync -vzrtopg --progress --delete --password-file=/etc/rsyncd.pwd /data/ rsync@192.168.1.100::backup
3、rsync定时任务(备份本地目录/data/到服务端192.168.1.100)
crontab -e
0 1 * * * rsync -vzrtopg --progress --delete --password-file=/etc/rsyncd.pwd /data/ rsync@192.168.1.100::backup
4、Rsync 同步参数说明
-vzrtopg里的v是verbose,z是压缩,r是recursive,topg都是保持文件原有属性如属主、时间的参数。
–progress是指显示出详细的进度情况
–delete是指如果服务器端删除了这一文件,那么客户端也相应把文件删除
5、rsync常用参数:
#rsync [option] 源路径 目标路径
其中[option]为:
a:使用archive模式,等于-rlptgoD,即保持原有的文件权限
z:表示传输时压缩数据
v:显示到屏幕中
e:使用远程shell程序(可以使用rsh或ssh)
–delete:精确保存副本,源主机删除的文件,目标主机也会同步删除
–include=PATTERN:不排除符合PATTERN的文件或目录
–exclude=PATTERN:排除所有符合PATTERN的文件或目录
–password-file:指定用于rsync服务器的用户验证密码
1、
rsync: failed to connect to 118.244.216.177: No route to host (113)
rsync error: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.6]
原因:防火墙屏蔽了端口
解决:打开873段考
iptables -i INPUT -p tcp --dport 873 -j ACCEPT
iptables -L
如果以上指令不行,可以直接停掉防火墙
/etc/init.d/iptables stop
2、
@ERROR: auth failed on module backup
rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.7]
检查密码文件设置权限 chmod 600 /etc/rsyncd.pwd
3、
@ERROR: auth failed on module xxxxx
rsync: connection unexpectedly closed (90 bytes read so far)
rsync error: error in rsync protocol data stream (code 12) at io.c(150)
这是因为密码设错了, 无法登入成功, 请检查一下 rsyncd.scrt 中的密码, 二端是否一致?
4、
password file must not be other-accessible
continuing without password file
Password:
这表示 rsyncd.pwd 的档案权限属性不对, 应设为 600。
5、
@ERROR: chroot failed
rsync: connection unexpectedly closed (75 bytes read so far)
rsync error: error in rsync protocol data stream (code 12) at io.c(150)
这通常是您的 rsyncd.conf 中的 path 路径所设的那个目录并不存在所致.请先用 mkdir开设好要备份目录
6、
@ERROR: access denied to www from unknown (192.168.1.123)
rsync: connection unexpectedly closed (0 bytes received so far) [receiver]
rsync error: error in rsync protocol data stream (code 12) at io.c(359)
最后原因终于找到了。因为有两个网段都需要同步该文件夹内容,但没有在hosts allow 后面添加另一个IP段
hosts allow = 192.168.1.0/24
改为
hosts allow = 192.168.1.0/24 192.168.2.0/24
重新启动rsync服务,问题解决
7、
@ERROR: auth failed on module backup
rsync error: error starting client-server protocol (code 5) at
main.c(1506) [Receiver=3.0.7]
client端没有设置/etc/rsyncd.pwd这个文件,而在使用rsync命令的时候,加了这个参数–password-file=/etc/rsyncd.pwd
8、
rsync: recv_generator: mkdir "/teacherclubBackup/rsync……" failed: No space left on device (28)
*** Skipping any contents from this failed directory ***
磁盘空间满了
9、
rsync: opendir "/backup" (in dtsChannel) failed: Permission denied (13)
同步目录的权限设置不对,改为755
10、
rsync: read error: Connection reset by peer (104)
rsync error: error in rsync protocol data stream (code 12) at io.c(759) [receiver=3.0.5]
未启动xinetd守护进程
[root@CC02 /]# service xinetd start
11、
rsync: unable to open configuration file "/etc/rsyncd.conf": No such file or directory
xnetid查找的配置文件位置默认是/etc下,在/etc下找不到rsyncd.conf文件
12、
rsync: failed to connect to 203.100.192.66: Connection timed out (110)
rsync error: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.5]
连接服务器超时,检查服务器的端口netstat –tunlp,远程telnet测试
13、我需要在防火墙上开放哪些端口以适应rsync?
视情况而定。rsync可以直接通过873端口的tcp连接传文件,也可以通过22端口的ssh来进行文件传递,但你也可以通过下列命令改变它的端口:
rsync --port 8730 otherhost::
或者
rsync -e 'ssh -p 2002' otherhost:
14、我如何通过rsync只复制目录结构,忽略掉文件呢?
rsync -av --include '*/' --exclude '*' source-dir dest-dir
15、为什么我总会出现”Read-only file system”的错误呢?
看看是否忘了设”read only = no”了
16、
@ERROR: chroot failed
rsync error: error starting client-server protocol (code 5) at
main.c(1522) [receiver=3.0.3]
原因:
服务器端的目录不存在或无权限。创建目录并修正权限可解决问题。
17、
@ERROR: auth failed on module backup
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
原因:
服务器端该模块(tee)需要验证用户名密码,但客户端没有提供正确的用户名密码,认证失败。提供正确的用户名密码解决此问题。
18、
@ERROR: Unknown module ‘bcakup’
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
原因:
服务器不存在指定模块。提供正确的模块名或在服务器端修改成你要的模块以解决问题。
19、权限无法复制。去掉同步权限的参数即可。(这种情况多见于Linux向Windows的时候)
(备份本地目录/data/到服务端192.168.1.100)