[rsync]——rsync文件同步和备份

实验环境

(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 600 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  表示压缩

-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 [email protected]::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 [email protected]::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 [email protected]::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/ [email protected]::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: auth failed on module webroot
   rsync error: error starting client-server protocol (code 5) at main.c(1503) [receiver=3.0.6]

检查服务器那个密码文件的权限,是否为 600

时间: 2024-08-05 20:33:11

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

【惊雷】Linux下Rsync的文件同步的配置过程

Rsync的文件同步实现 一.rsync 简介 Rsync(remote synchronize)是一个远程数据同步工具,简要的概括就是主机于主机之间的文件目录数据的一个同步. 它的特性如下: 可以镜像保存整个目录树和文件系统. 可以很容易做到保持原来文件的权限.时间.软硬链接等等. 无须特殊权限即可安装. 快速:第一次同步时 rsync 会复制全部内容,但在下一次只传输修改过的文件.rsync 在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽. 安全:可以使用scp.ssh等

rsync+sersync2文件同步(基于centos6.5)

rsync+sersync2文件同步 环境:centos6.5 软件:rsync+sersync2 源服务器:192.168.10.109 同步服务器:192.168.10.107,192.168.10.108 前提工作: 关闭SELINUX vi /etc/selinux/config #SELINUX=enforcing 修改 SELINUX=disabled 重启系统生效 iptables打开端口 vi /etc/sysconfig/iptables -A RH-Firewall-1-IN

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

rsync+inotify文件同步 - 同步慢的问题 我们来看网上的教程,我加了注释.(网上所有的教程基本都一模一样,尽管写法不一样,致命点都是一样的) #!/bin/bash /usr/bin/inotifywait -mrq --format '%w%f'-e create,close_write,delete /backup |while read file #把发生更改的文件列表都接收到file 然后循环,但有什么鬼用呢?下面的命令都没有引用这个$file 下面做的是全量rsync do

用rsync进行文件同步

实施目的: 实现将文件从10.0.0.40/wb/的文件同步到10.0.0.194/wb/下 实施流程: 1.在两台机器上安装rsync yum install rsync 2.配置服务端(被同步服务器),10.0.0.194 vi /etc/rsyncd.conf 添加以下内容: uid = tomcat #设置执行rsync的本地用户 gid = tomcat  #设置执行rsync的本地组 use chroot = yes #是否chroot max connections = 4  #最

利用Rsync进行文件同步

1 需求 先描述下需求,有时候后台经常要同步一些数据,然而又不想每次都全量同步.举个例子,有一个单点物理机,在上面部署了很多脚本和数据的目录,这些脚本又提供给外部来使用,这样的话,如果这台物理机器挂了,部署在上面的脚本都丢失了,这样导致外部调用都失败了,而且你不好恢复这些脚本,这些脚本都可能和目录结构有很强的关联性. 2 原始办法 每天定时打包脚本和数据目录,通过scp传到备份的机器上,来实现数据同步,这样有个缺点是,每次的数据同步都需要全量同步,如果数据文件目录很大的话,同步时间就会非常长,而

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

1.环境部署 inotify-master 10.10.6.208 inotify-slave 10.10.6.149 2.两台服务器都安装rsync yum install -y rsync 3.inotify-slave部署 新建rsync用户及模块目录并更改其用户组 useradd rsync -s /sbin/nologin #添加rsync用户mkdir /usr/local/backup #创建rsync daemon工作模式的模块目录chown rsync.rsync /usr/l

rsync + inotify 文件同步

rpm -e rsync-3.1.2-4.el7.x86_64 --nodeps rpm -ivh rsync-3.1.2-4.el7.x86_64.rpm 115 服务端,接收文件的地方 echo "web:123" > /usr/local/rsync/rsyncd.passwd vi /etc/rsyncd.conf # /etc/rsyncd: configuration file for rsync daemon mode # See rsyncd.conf man p

Centos7利用rsync实现文件同步

测试环境: CentOS 7.4 Rsync服务端:192.168.99.112 CentOS 7.4 Rsync客户端:192.168.99.136 第一种方式:rsync通过ssh方式同步 1.Rsync服务端和客户端都需要安装rsync [[email protected] ~]# yum -y install rsync 2.使用 前提:需知道远程服务器开启ssh端口和账号密码 A.推文件: [[email protected] tmp]# rsync -av /etc/passwd 

RSYNC实现文件同步

1.安装(客户端服务端都要安装) yum -y install rsync 2.安装xinetd yum -y install xinetd 3.修改xinetd配置文件 修改 /etc/xinetd.d/rsync  把 disable = yes  改为disable = no 4.创建rsync服务器配置文件/etc/rsyncd.conf vi /etc/rsyncd.conf uid = root gid = root port = 873 hosts allow = 192.168.