rsync

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了rsync相关的知识,希望对你有一定的参考价值。

简介

rsync是Linux系统下的数据镜像备份系统。可以远程同步,支持本地复制,或者与其他SSH,rsync主机同步。

特性

  • 同步小文件
  • 可以镜像保存整个目录树和文件系统
  • 无需特殊权限即可安装
  • 快速:第一次同步会复制全部内容,但下一次只传输修改过的文件;传输过程可以实行压缩解压缩,占用带宽少
  • 安全:可以使用scp、ssh。也可以直接socket
  • 支持匿名传输(已做好互信),方便网站同步

rsync命令

// rsync 的命令格式常用的有一下三种:
rsync [OPTION]... SRC DEST(源地址)
//拷贝本地文件至本地

[[email protected] ~]# ls
anaconda-ks.cfg  Documents  initial-setup-ks.cfg  Pictures  Templates
Desktop          Downloads  Music                 Public    Videos
[[email protected] ~]# rsync -avz anaconda-ks.cfg rsync-copy
sending incremental file list
anaconda-ks.cfg

sent 995 bytes  received 31 bytes  2052.00 bytes/sec
total size is 1614  speedup is 1.57
[[email protected] ~]# ls
anaconda-ks.cfg  Documents  initial-setup-ks.cfg  Pictures  rsync-copy 

rsync [OPTION]... SRC [[email protected]]HOST:DEST
//拷贝本地文件至远程主机

[[email protected] etc]# rsync -avz passwd [email protected]:/tmp/
[email protected]‘s password: 
sending incremental file list
passwd

sent 830 bytes  received 31 bytes  114.80 bytes/sec
total size is 1968  speedup is 2.29

rsync [OPTION]... [[email protected]]HOST:SRC DEST
//拷贝远程主机文件至本地

[[email protected] ~]# rsync -avz [email protected]:/etc/yum.repos.d /root
[email protected]‘s password: 
receiving incremental file list
yum.repos.d/
yum.repos.d/iso.repo

sent 34 bytes  received 167 bytes  44.67 bytes/sec
total size is 61  speedup is 0.30
[[email protected] ~]# ls
anaconda-ks.cfg  Downloads             Pictures    Templates
Desktop          initial-setup-ks.cfg  Public      Videos
Documents        Music                 rsync-copy  yum.repos.d

//rsync常用选项:
-a, --archive //归档
-v, --verbose //啰嗦模式
-q, --quiet //静默模式
-r, --recursive //递归
-p, --perms //保持原有的权限属性
-z, --compress //在传输时压缩,节省带宽,加快传输速度
--delete //在源服务器上做的删除操作也会在目标服务器上同步

rsync+inotify

在目标服务器上做以下操作:

//关闭防火墙与SELINUX
[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# systemctl disable firewalld
[[email protected] ~]# getenforce
Enforcing
[[email protected] ~]# setenforce 0
[[email protected] ~]# sed -ri ‘s/^(SELINUX=).*/1disabled/g‘ /etc/sysconfig/selinux

//安装rsync服务端软件
[[email protected] ~]# yum -y install rsync
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
myrepo                                          | 4.1 kB     00:00
(1/2): myrepo/group_gz                            | 137 kB   00:00
(2/2): myrepo/primary_db                          | 4.0 MB   00:00
Resolving Dependencies
--> Running transaction check
---> Package rsync.x86_64 0:3.0.9-18.el7 will be installed
......
myrepo/productid                                | 1.6 kB     00:00
  Verifying  : rsync-3.0.9-18.el7.x86_64                           1/1

Installed:
  rsync.x86_64 0:3.0.9-18.el7

Complete!

//设置rsyncd.conf配置文件

[email protected] ~]# cat >> /etc/rsyncd.conf <<EOF
log file = /var/log/rsyncd.log //日志文件位置,启动rsync后自动产生这个文件,无需提前创建
pidfile = /var/run/rsyncd.pid //pid文件的存放位置
lock file = /var/run/rsync.lock //支持max connections参数的锁文件
secrets file = /etc/rsync.pass //用户认证配置文件,里面保存用户名称和密码,必须手动创建这个文件

[etcfromclient] //自定义同步名称
path = /tmp/ //rsync服务端数据存放路径,客户端的数据将同步至此目录
comment = sync etc from client
uid = root //设置rsync运行权限为root
gid = root //设置rsync运行权限为root
port = 873 //默认端口
ignore errors //表示出现错误忽略错误
use chroot = no //默认为true,修改为no,增加对目录文件软连接的备份
read only = no //设置rsync服务端为读写权限
list = no //不显示rsync服务端资源列表
max connections = 200 //最大连接数
timeout = 600 //设置超时时间
auth users = admin //执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
hosts allow = 172.16.30.254 //允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
hosts deny = 192.168.1.1 //禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开

EOF


//创建用户认证文件
[[email protected] ~]# echo ‘admin:123456‘ &gt; /etc/rsync.pass
[[email protected] ~]# cat /etc/rsync.pass
admin:123456

//设置文件权限
[[email protected] ~]# chmod 600 /etc/rsync
[[email protected] ~]# ll /etc/rsync

-rw-------. 1 root root 802 Aug 9 22:45 /etc/rsyncd.conf
-rw-------. 1 root root 13 Aug 9 22:47 /etc/rsync.pass

//启动rsync服务并设置开机自启动
[[email protected] ~]# systemctl start rsyncd
[[email protected] ~]# systemctl enable rsyncd
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.
[[email protected] ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 :22 :
LISTEN 0 100 127.0.0.1:25
:
LISTEN 0 5
:873 :
LISTEN 0 128 :::22 :::
LISTEN 0 100 ::1:25 :::

LISTEN 0 5 :::873 :::*


![](https://s1.51cto.com/images/blog/201902/19/2a97b5ca61ccff2d055d084474f70815.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

**在原服务器上**
//关闭防火墙和SELINUX
[[email protected] ~]# systemctl stop firewalld 
[[email protected] ~]# systemctl disable firewalld 
rm ‘/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service‘
rm ‘/etc/systemd/system/basic.target.wants/firewalld.service‘
[[email protected] ~]# getenforce 
Permissive
[[email protected] ~]# yum -y install rsync//安装rsync
//创建认证密码文件
[[email protected] ~]# echo ‘123456‘ > /etc/rsync.pass
//设置文件权限,只设置文件所有者具有读取、写入权限
[[email protected] ~]# chmod 600 /etc/rsync.pass
[[email protected] ~]# ll /etc/rsync.pass 
-rw-------. 1 root root 7 Feb 19 19:59 /etc/rsync.pass

//在源服务器上创建测试目录,然后在源服务器运行

[[email protected] ~]# mkdir -pv /root/etc/test
mkdir: created directory ‘/root/etc’
mkdir: created directory ‘/root/etc/test’

[[email protected] ~]# rsync -avH --port 873 --progress --delete /root/etc/ [email protected]::etc_from_client --password-file=/etc/rsync.pass
sending incremental file list
./
deleting systemd-private-ySPPll/tmp/
deleting systemd-private-ySPPll/
...
![](https://s1.51cto.com/images/blog/201902/20/951ffb3acbe823abb1d5f92ce2ca8f2d.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

//安装inotify-tools工具,实时触发rsync进行同步
//查看服务器内核是否支持inotify
[[email protected] ~]# ll /proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 Aug 10 11:19 max_queued_events
-rw-r--r-- 1 root root 0 Aug 10 11:19 max_user_instances
-rw-r--r-- 1 root root 0 Aug 10 11:19 max_user_watches
//如果有这三个max开头的文件则表示服务器内核支持inotify

//安装inotify-tools
[[email protected] ~]# yum -y install make gcc gcc-c++
....
[[email protected] ~]# yum -y install inotify-tools
....

//写同步脚本,此步乃最最重要的一步,请慎之又慎。让脚本自动检测我们制定的目录下文件发生的变化,然后再执行rsync的命令把它同步到我们的服务器端去
[[email protected] ~]# mkdir /scripts
[[email protected] ~]# touch /scripts/inotify.sh
[[email protected] ~]# chmod 755 /scripts/inotify.sh
[[email protected] ~]# ll /scripts/inotify.sh
-rwxr-xr-x 1 root root 0 Aug 10 13:02 /scripts/inotify.sh
[[email protected] ~]# vim /scripts/inotify.sh
host=172.16.30.254      //目标服务器的ip(备份服务器)
src=/etc        //在源服务器上所要监控的备份目录(此处可以自定义,但是要保证存在)
des=etc_from_client     //自定义的模块名,需要与目标服务器上定义的同步名称一致
password=/etc/rsync.pass        //执行数据同步的密码文件
user=admin          //执行数据同步的用户名
inotifywait=/usr/bin/inotifywait

$inotifywait -mrq --timefmt ‘%Y%m%d %H:%M‘ --format ‘%T %w%f%e‘ -e modify,delete,create,attrib $src | while read files ;do
    rsync -avzP --delete  --timeout=100 --password-file=${password} $src [email protected]$host::$des
    echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done

//启动脚本
[[email protected] ~]# nohup bash /scripts/inotify.sh &
[1] 86871
[[email protected] ~]# nohup: ignoring input and appending output to ‘nohup.out’

[[email protected] ~]# ps -ef|grep inotify
root      86871   2143  0 14:52 pts/0    00:00:00 bash /scripts/inotify.sh
root      86872  86871  0 14:52 pts/0    00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /etc
root      86873  86871  0 14:52 pts/0    00:00:00 bash /scripts/inotify.sh
root      86875   2143  0 14:52 pts/0    00:00:00 grep --color=auto inotify

以上是关于rsync的主要内容,如果未能解决你的问题,请参考以下文章

svn 结合rsync 的代码发布系统

配置 rsync 报错

saltstack+git+rsync发布代码

配置rsync+inotify进行资源或代码同步

rsync

Rsync