实验环境

(1) Rsync服务器:10.0.10.158

(2) Rsync客户端:10.0.10.173


Rsync服务器端的配置

1. 安装xinetd和rsync

# yum install xinetd
# yum install rsync

2. 创建配置目录和文件

# mkdir /etc/rsync

该目录下包含3个文件:

  • rsyncd.conf           # rsync主配置文件
  • rsyncd.secrets      # 密码文件
  • rsyncd.motd         # 服务信息定义文件

下面逐一编辑这些文件:

2.1 编辑主配置文件

# vim /etc/rsync/rsyncd.conf
pid file = /var/run/rsyncd.pid #pid文件的存放位置
port = 873                    #通信的端口
address = 10.0.10.158           #监听的地址/服务器的地址
uid = root                       #运行rsync守护进程的用户
gid = root                       #运行rsync守护进程的用户组
use chroot = yes                 #是否使用chroot
read only = no                   #是否只读
hosts allow=10.0.0.0/255.255.0.0 #设置允许访问的主机(可以是一个范围也可以是一个IP地址)
hosts deny= *                    #设置拒绝访问的主机(这里的*表示除了allow以外的都拒绝)
max connections = 51             #最大连接数
motd file = /etc/rsync/rsyncd.motd #指定信息显示文件
log file = /var/log/rsync.log     #指定日志文件
log format = %t %a %m %f %b %l     #设置日志文件格式
syslog facility = local3          #设置日志级别
timeout = 600                     #设置连接超时时间(单位:s) [webroot]            #目录的标识/认证模块名
path = /var/www/html   #要同步的目录名
list=yes              #是否允许列出文件
ignore errors          #忽略一般的IO错误
auth users = admin   #认证的用户名
secrets file = /etc/rsync/rsyncd.secrets #密码文件
comment = web root directory            #说明信息
exclude = secret/ #需要排除的目录(排除后就不同步它了)

2.2 编辑密码配置文件

# vim /etc/rsync/rsyncd.secrets
admin:123123 #格式是“用户名:密码”

2.3 编辑服务信息定义文件

# vim /etc/rsync/rsyncd.motd
welcome access

3. 修改密码配置文件的权限

# chmod  rsyncd.secrets

4. 启动Rsync服务

# /usr/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf
# vim /etc/xinetd.d/rsync
service rsync
{
disable = no
flags = IPv6
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon --config=/etc/rsync/rsyncd.conf
log_on_failure += USERID
}
# service xinetd restart

Rsync客户端配置

1. 创建目录、编辑密码文件

# mkdir /etc/rsync
# vim /etc/rsync/password
123123 #格式是“密码”
# chmod 600 /etc/rsync/password #修改密码文件的权限

Rsync同步命令的参数

-v  表示verbose详细显示

-z  表示压缩(reduces the amount of data being transmitted)

-r   表示recursive递归

-t   表示保持原文件创建时间

-o  表示保持原文件属主

-p  表示保持原文件的参数

-g  表示保持原文件的所属组

-a  归档模式

-P  表示代替-partial和-progress两者的选项功能

-e    ssh建立起加密的连接

--partial    阻止rsync在传输中断时删除已拷贝的部分(如果在拷贝文件的过程中,传输被中断,rsync的默认操作是撤消前操作,即从目标机上删除已拷贝的部分文件)

--progress  是指显示出详细的进度情况

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

--exclude  不包含指定目录

--size-only  这个参数用在两个文件夹中的差别仅是源文件夹中有一些新文件,不存在重名且被修改过的文件,因为这种文件有可能会因为内容被修改可大小一样,而被略过。这个参数可以大大地提高同步的效率,因为它不需要检查同名文件的内容是否相同

--password-file  来指定密码文件,内容包含server端指定认证用户的密码


验证

1. 同步服务器的文件到本地

# ll /var/www/html/   #看服务器上的/var/www/html目录下有啥
总用量 8
-rw-r--r--. 1 root root 0 12月 1 01:05 index.html
-rw-r--r--. 1 root root 0 12月 1 01:05 page.html
# rsync -vzrtopg --progress --delete admin@10.0.10.158::webroot /var/www/html/ --password-file=/etc/rsync/password   #在客户机,执行同步操作,使得获得服务器的文件
welcome access receiving incremental file list
./
index.html
0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=1/3)
page.html
0 100% 0.00kB/s 0:00:00 (xfer#2, to-check=0/3) sent 105 bytes received 227 bytes 664.00 bytes/sec
total size is 0 speedup is 0.00
# ll /var/www/html/            #在服务器端我们试着添加一下新的目录和文件(包括设置了不同步的目录secret)
总用量 8
drwxr-xr-x. 2 root root 4096 12月 1 01:41 haha
-rw-r--r--. 1 root root 0 12月 1 01:05 index.html
-rw-r--r--. 1 root root 0 12月 1 01:40 page_2.html
-rw-r--r--. 1 root root 0 12月 1 01:05 page.html
drwxr-xr-x. 2 root root 4096 12月 1 01:40 secret
# rsync -vzrtopg --progress --delete admin@10.0.10.158::webroot /var/www/html/ --password-file=/etc/rsync/password  #在客户端执行同步命令,以获得服务器的更新
welcome access receiving incremental file list
./
page_2.html
0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/4) sent 80 bytes received 207 bytes 191.33 bytes/sec
total size is 0 speedup is 0.00 # rsync -vzrtopg --progress --delete admin@10.0.10.158::webroot /var/www/html/ --password-file=/etc/rsync/password
welcome access receiving incremental file list
./
haha/ sent 65 bytes received 203 bytes 536.00 bytes/sec
total size is 0 speedup is 0.00 # ll /var/www/html/ --------->#确实没有同步secret这个目录
总用量 4
drwxr-xr-x. 2 root root 4096 12月 1 2009 haha
-rw-r--r--. 1 root root 0 12月 1 2009 index.html
-rw-r--r--. 1 root root 0 12月 1 2009 page_2.html
-rw-r--r--. 1 root root 0 12月 1 2009 page.html

2. 同步本地的文件到服务器

# touch new.html    #在本地新增一个文件
# rsync -vzrtopg --progress --partial --password-file=/etc/rsync/password /var/www/html/ admin@10.0.10.158::webroot #在本地执行同步命令,使本地的更新同步到服务器
welcome access sending incremental file list
./
new.html
0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=3/6) sent 165 bytes received 31 bytes 392.00 bytes/sec
total size is 0 speedup is 0.00
# ls /var/www/html    #在服务器上看到了这个new.html
haha index.html new.html page_2.html page.html secret

常见错误

问题一:
@ERROR: chroot failed
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
原因:服务器端的目录不存在或无权限。创建目录并修正权限可解决问题。检查服务器那个密码文件的权限是否为600

问题二:
@ERROR: auth failed on module tee
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
原因:服务器端该模块(tee)需要验证用户名密码,但客户端没有提供正确的用户名密码,认证失败。提供正确的用户名密码解决此问题。
 
问题三:
@ERROR: Unknown module ‘tee_nonexists’
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
原因:服务器不存在指定模块。提供正确的模块名或在服务器端修改成你要的模块以解决问题。
 
问题四:
password file must not be other-accessible
continuing without password file
Password:
原因:这是因为rsyncd.pwd rsyncd.secrets的权限不对,应该设置为600。如:chmod 600 rsyncd.pwd
 
问题五:
rsync: failed to connect to 218.107.243.2: No route to host (113)
rsync error: error in socket IO (code 10) at clientserver.c(104) [receiver=2.6.9]
原因:对方没开机、防火墙阻挡、通过的网络上有防火墙阻挡,都有可能。关闭防火墙,其实就是把tcpudp的873端口打开。

问题六:
rsync error: error starting client-server protocol (code 5) at main.c(1524) [Receiver=3.0.7]
原因:/etc/rsyncd.conf配置文件内容有错误。请正确核对配置文件。
 
问题七:
rsync: chown "" failed: Invalid argument (22)
原因:权限无法复制。去掉同步权限的参数即可。(这种情况多见于Linux向Windows的时候)

问题八:
@ERROR: daemon security issue -- contact admin
rsync error: error starting client-server protocol (code 5) at main.c(1530) [sender=3.0.6]
原因:同步的目录里面有软连接文件,需要服务器端的/etc/rsyncd.conf打开use chroot = yes。掠过软连接文件

[rsync]——rsync文件同步和备份的更多相关文章

  1. rsync 文件同步和备份

    rsync 是同步文件的利器,一般用于多个机器之间的文件同步与备份,同时也支持在本地的不同目录之间互相同步文件.在这种场景下,rsync 远比 cp 命令和 ftp 命令更加合适,它只会同步需要更新的 ...

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

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

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

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

  4. rsync+inotify文件同步

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

  5. rsync+inotfiy文件同步

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

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

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

  7. CentOS利用inotify+rsync实现文件同步

    1.环境部署 inotify-master 10.10.6.208 inotify-slave 10.10.6.149 2.两台服务器都安装rsync yum install -y rsync 3.i ...

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

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

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

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

  10. rsync实现文件同步

    rsync是类unix系统下的数据镜像备份工工具,一般linux系统都自带了 [可以确认一下:shell>rpm -qa|grep rsync] 服务端:192.168.1.2  同步目录:/h ...

随机推荐

  1. SID与GUID的区别

    1.在AD里面创建一个用户或者组都会为其分配一个SID,同时也会为这些对象分配一个GUID,GUID是一个128位的字符串,一个标识符,GUID不仅在整个域里面是唯一的,并且在全世界的范围内都是唯一的 ...

  2. PayPal 开发详解(一):注册PayPal帐号

    1.注册paypal帐号 https://www.paypal.com 2.使用刚才注册的paypal帐号登录3.进入开发者中心 4.登录开发者中心 5.登录 查看我们paypal Sandbox测试 ...

  3. Count and Say [LeetCode 38]

    1- 问题描述 The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211 ...

  4. 捣蛋phpwind之WindFrameWork

    一直都有关注phpwind这个开源产品,从9.0开始就好关注拉,因为官方说把之前的代码重写了一遍,融入了windFramework这个框架,代码真的挺优美的,今日在做社区的一些功能,心血来潮就参考了p ...

  5. 在C#中对Datatable排序【DefaultView的Sort方法】

    在C#中对Datatable排序,[DefaultView的Sort方法] 代码如下: DataTable dt = new DataTable(); dt.Columns.Add("ID& ...

  6. JS数组(Array)处理函数总结

    1.concat() 连接两个或更多的数组该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本.例如: <script type="text/javascript"&g ...

  7. c#中判断对象为空的几种方式(字符串等)

    (1)先了解几个与空类型相关的关键字和对象  Null : 关键字表示不引用任何对象的空引用,它是所有引用类型变量的默认值,在2.0版本之前也就只有引用变量类型可以为null,如(string a=n ...

  8. [leetcode]_Longest Substring Without Repeating Characters

    问题:求一个字符串中最长不重复子串的长度. 直接思路:以每个字符为出发计算最长不重复子串.TLE.O(n2),HashMap存储字符出现的位置. 代码: public int lengthOfLong ...

  9. 在Windows下Mysql如何重置root用户密码

    原文链接:http://www.feeldesignstudio.com/2013/05/windows-mysql-root-password-reset 网上的很多在Windows下重置root用 ...

  10. SublimeText快捷键大全(附GIF演示图)

    Sublime Text是码农必备之神器,有助于码农快速开垦,如果掌握了Sublime强大的快捷键就可以飞起来了.下面下载吧小编汇总了SublimeText支持的全部快捷键(适用SublimeText ...