rsync是类unix系统下的数据镜像备份工工具,一般linux系统都自带了 [可以确认一下:shell>rpm -qa|grep rsync]

服务端:192.168.1.2  同步目录:/home/source

客户端:192.168.1.3  同步目录:/home/receive

1、服务端配置

shell>vi /etc/rsyncd.conf   #创建配置文件rsyncd.conf

配置文件内容:

uid=nobody
gid=nobody
use chroot = no
max connections = 10
pid file = /var/run/rsyncde.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
Timeout = 300
Log format = %t %a %m %f %b
[backup]
path=/a/system_build/leia/
ignore errors
read only = yes
list = no
auth users = rsync
secrets file = /etc/rsyncd.secrets
hosts allow = 9.115.249.158
hosts deny = 0.0.0.0/0

  

shell>vi etc/rsyncd.secrets #创建密码文件

内容:

rsync:rsync //这里用户名和密码都定义为rsync

shell>chmod 0600 /etc/rsyncd.secrets  #更改密码文件权限

shell>rsync --daemon;  #启动服务,默认在873端口监听(可以自己修改)  or  /usr/bin/rsync --daemon

检查进程是否存在,

ps -aux |grep rsync
root 4406 0.0 0.0 4228 588 ? Ss May14 0:00 /usr/local/bin/rsync --daemon
netstat -an |grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN

出现以上结果,表明rsync服务器端已经启动。

打开防火墙

iptables -i INPUT -p tcp --dport 873 -j ACCEPT
iptables -L
结果如下
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere tcp dpt:rsync

rsync开机启动
echo "/usr/bin/rsync --daemon" >> /etc/rc.local

(开机自动启动rsync服务)

注意事项
1) 提示密码文件不能读,需要手工输入密码时,可能就是密码文件权限不是600,或者格式不对,或者是路径不对。
2) 提示要创建新目录或文件传输失败时,可能是欲同步的目录没有权限,最好把欲同步的目录权限修改为744。
3) 从客户端同步文件到服务端时,最好单个文件目录传输,否则易出错。


2、客户端

客户端就不用启动rsync服务了

shell>vi /etc/rsyncd.secrets #这里也是创建密码文件,文件名字自定义,但内容只需要填目标服务授权密码

内容:

rsync //这里是服务端rsync服务授权密码

shell>chmod 0600 /etc/rsyncd.secrets #更改文件权限

测试:(在服务端/home/source/ 先创建test文件 )

shell>rsync -vzrtopg --delete --progress --password-file=/etc/rsyncd.secrets rsync@192.168.1.2::backup /home/receive

v:传输时的进度等信息, z:表示压缩, r:是递归, t:保持文件原有时间, o:保持文件原有属主, P:传输进度, g:保持文件原有用户组
--progress 指显示

--delete 指如果服务器端删除了这一文件,那么客户端也相应把文件删除,保持真正的一致

--password-file=/etc/rsyncd.secrets 认证密码

rsync 认证用户

backup 认证模块

如果客户端/home/receive/下产生了test文件代表同步成功

下一步写一个脚本文件实现真正的同步......

启动脚本:rsync.sh

shell>vi /home/rsync.sh

#!/bin/sh
rsync -vzrtopg --delete --progress --password-file=/etc/rsyncd.secrets rsync@192.168.1.2::backup /home/receive

关闭脚本:killrsync.sh

shell>vi /home/killrsync.sh

#!/bin/sh
RSYNC_PID=`ps auxww|grep rsync |grep -v grep|awk '{print $2}'`
kill -9 $RSYNC_PID

设置定时任务 (crontab?google can help you!)

shell>crontab -e

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
0 3 * * * /home/rsync.sh //每天晚上3点执行一次同步
0 6 * * * /home/killrsync.sh //每天早上6点强制终止同步(如果还没完成)

shell>crontab -l #可以查看任务设置情况

shell>service crond status #查看crond是否已启动,若启动了能看到PID

shell>service crond start #启动crond服务


--------------------------------------------------------
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 /home/admin/security/rsync.pass
应该差不多就可以了

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.scrt 的档案权限属性不对, 应设为 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/rsync.pas这个文件,而在使用rsync命令的时候,加了这个参数--password-file=/etc/rsync.scrt

8.rsync: recv_generator: mkdir "/teacherclubBackup/rsync……" failed: No space left on device (28)
*** Skipping any contents from this failed directory ***
磁盘空间满了

9.rsync: opendir "/kexue" (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 tee
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
原因:
服务器端该模块(tee)需要验证用户名密码,但客户端没有提供正确的用户名密码,认证失败。提供正确的用户名密码解决此问题。

18.@ERROR: Unknown module ‘tee_nonexists’
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
原因:
服务器不存在指定模块。提供正确的模块名或在服务器端修改成你要的模块以解决问题。

19.权限无法复制。去掉同步权限的参数即可。(这种情况多见于Linux向Windows的时候)

rsync实现文件同步的更多相关文章

  1. rsync+inotfiy文件同步

    rsync+inotfiy文件同步 1.部署rsync服务 yum install rsync #安装rsync,如果嫌yum版本过低也可以源码安装 2.vim /etc/rsyncd.conf #默 ...

  2. rsync+inotify文件同步 - 同步慢的问题

    rsync+inotify文件同步 - 同步慢的问题 我们来看网上的教程,我加了注释.(网上所有的教程基本都一模一样,尽管写法不一样,致命点都是一样的) #!/bin/bash /usr/bin/in ...

  3. rsync+inotify文件同步

    rsync+inotify文件同步 在服务器中,通常结合计划任务.shell脚本来执行本地备份.为了进一步提高备份的可靠性,使用异地备份也是非常重要的,利用rsync工具,可以实现快速.高效的异地备份 ...

  4. Linux下简单粗暴使用rsync实现文件同步备份【转】

    这篇来说说如何安全的备份,还有一点不同的是上一篇是备份服务器拉取数据,这里要讲的是主服务器如何推送数据实现备份. 一.备份服务器配置rsync文件 vim /etc/rsyncd.conf #工作中指 ...

  5. Linux Rsync实现文件同步备份(转载)

    原文地址:Linux Rsync实现文件同步备份作者:夷北 转自:http://www.mike.org.cn/blog/index.php?load=read&id=639###pp=0 [ ...

  6. inotify配合rsync实现文件同步

    一.slave端rsync设置在此部署rsync服务和rsync daemon 1.安装rsync   2.配置rsyncd.conf文件#vi /etc/rsyncd.conf配置文件uid = r ...

  7. CentOS安装和配置Rsync进行文件同步

    Liunx系统实现文件同步不需要搭建FTP这类的工具,只需要按照Rsync配置下文件就可以. 本文以Centos7.0为例. 1. 首先关闭SELINUX(不关闭无法同步,权限太高了) vi /etc ...

  8. centos7 使用rsync 实现文件同步

    一.服务端(192.168.8.81): 安装软件: yum -y install rsync 创建需同步的目录: mkdir -p /home/root/rsync 编辑配置文件:vim /etc/ ...

  9. Rsync实现文件同步的算法(转载)

    Rsync文件同步的核心算法 文章出处:http://coolshell.cn/articles/7425.html#more-7425 rsync是unix/linux下同步文件的一个高效算法,它能 ...

  10. Centos7利用rsync实现文件同步

    0x01 测试环境 CentOS 7.4 Rsync服务端:192.168.204.130 CentOS 7.4 Rsync客户端:192.168.204.168 0x02 rsync同步方式 第一种 ...

随机推荐

  1. hdu 3015

    这个题给你一堆树,每棵树的位置x和高度h都给你 f[i]代表这棵树的位置排名,s[i]代表这棵树的高度排名 问你任意两棵树的(f[i] - f[j])*min(s[i],s[j])和 (f[i]-f[ ...

  2. mac系统下安装Windows(7,8,10都一样的步骤)

    1.下载纯净版window10镜像文件(ISO) 注意:必须是纯净版,不是ghost版     2.打开bootcamp软件(位置:launchpad-其他) 注意:硬盘不要分区,若分过区,请合并. ...

  3. 元组(tuple)基本操作

    1.定义元组,Python的元组和列表类似,不同之处在于元组中的元素不能修改(因此元组又称为只读列表),且元组使用小括号而列表使用中括号 dimensions.py , ) print(dimensi ...

  4. FormatSQL

    核心提示:在一些论坛,常看到有人拼接SQL的时候,喜欢直接硬拼,结果就是出现一大堆加号和单引号,不仅写起来麻烦(你得小心该连续写多少个单引号),SQL的可读性也相当差....稍微好一点的方法是,使用Q ...

  5. 一致性hash(整理版)

    简单解释: 简单解释一致性hash的原理:网上通篇都是用服务器做的举例,我这里也如此,主要是便于理解. 通常:有N个客户端请求服务器,假设有M台web服务器,通常为了均衡访问会进行N%M的取模,然后分 ...

  6. python使用httplib2访问REST服务的例子

    首先你需要安装httplib2,这个可以在github上找到: 然后你需要获得一个http连接的对象: con = httplib2.Http()   然后你需要发起连接: (6)resp, (5)c ...

  7. 机器学习实战-ch3-决策树

    决策树是一种新算法: 优点:计算复杂度不高,输出结果易于理解,对中间值的缺失不敏感,可以处理不相关特征数据.缺点:可能会产生过度匹配问题. 决策树算法可用于数据类型:数值型和标称型. 决策树的核心在于 ...

  8. Kali Linux渗透测试实战 2.1 DNS信息收集

    目录 2.1 DNS信息收集1 2.1.1 whois查询3 2.1.2 域名基本信息查询4 Dns服务器查询4 a记录查询4 mx记录查询5 2.1.3 域名枚举5 fierse 5 dnsdict ...

  9. UWP 取消GridView、ListView鼠标选中、悬停效果

    因为经常碰到ListView或者ListBox之类的选中.鼠标悬停样式和自己设置的主题颜色不搭,这时就需要改变这些样式了. 而这里我通过ListView来说明,大致思路其实就是重新定义Item的Tem ...

  10. Shell命令之文本操作

    前言 在Linux中,文本处理操作是最常见的,应用非常广泛,如果能熟练掌握,可以大大提高开发效率. awk/sed/grep是文本操作领域的“三剑客”,学会了这3个命令就可以应对绝大多数文本处理场景. ...