rsync服务

RSYNC服务

rsync简介

rsynclinux系统下的数据镜像同步备份工具,使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他sshrsync主机同步

rsync特性

1.可以镜像保存整个目录和文件系统
2.可以很容易做到保持原来的权限、时间、软硬链接
3.无须特殊权限即可安装
4.快速:第一时间同步时rsync会复制全部内容,但在下一次只传输修改的文件,rsync在传输数据的过程中实行压缩及解压缩操作,因此可以使用更少的宽带
5.安全:可以使用scpssh等方式来传输文件,当然也可以通过直接的socket(套接字)连接
6.支持匿名传输,以便进行网站镜像

rsync命令

  • 1.上传
rsync -avz /backup/ rsync_backup@192.168.69.116::backup/  --password=/etc/rsync.password
  • 2.下载
rsync -avz  rsync_backup@192.168.69.116::backup  /backup/  --password=/etc/rsync.password
  • 3.常用选项
    -a //归档
    -v //啰嗦模式
    -q //静默模式
    -r //递归
    -p //保持原有权限属性
    --delete //在源服务器上做的删除操作也会在目标服务器上同步

rsync+inotify

rsync与传统的cptar备份方式相比,rsync具有安全性高、备份迅速、支持增量备份等优点、通过rsync可以解决实时性要求不高的数据备份要求
inotify是一种强大的、细粒度的、异步的文件系统事件监控机制

实验要求

部署rsync+inotify同步/root/xiefei目录同步到目标服务的/tmp/xiefei目录下

  • 实验环境
主机名IP地址角色系统版本
server-rsync192.168.100.33目标服务器red-hat7
client-rsync192.168.100.60源服务器red-hat7
  • 关闭防火墙和selinux
[root@server-rysnc ]# systemctl start firewalld.service
[root@server-rysnc ]# systemctl enable firewalld.service
[root@server-rysnc ]# setenforce 0
[root@client-rysnc ]# systemctl start firewalld.service
[root@client-rysnc ]# systemctl enable firewalld.service
[root@client-rsync ]# setenforce 0

目标服务端配置

  • 1.安装rsync
[root@server-rysnc ~]# yum -y install rsync
  • 2.接下来修改配置主文件,在文件尾部添加如下配置
[root@server-rysnc ~]# vim /etc/rsyncd.conf 
pidfile = /var/run/rsyncd.pid                       //pid文件存放位置
lock file = /var/run/rsyncd.lock                  //支持max,锁文件
log file = /var/log/rsyncd.log                            //日志文件存放位置
secrets file = /etc/rsync.pass                             //用户认证配置文件,里面存放用户名和密码,必须手动添加
[xiefei]                                                           //自定义同步名称
path = /tmp/xiefei                                          //rsync服务端数据存放路径,客户端的数据同步至此目录
comment = This is client                              //信息
uid = root                                                    //设置rsync运行权限为root
gid = root                                                   
port = 873                                                       //默认端口
ignore errors                                                //表现出现错误忽略错误
use chroot = no                                                 //增加目录文件软连接的备份
read only = no                                                 //设置rsync服务端的读写权限
list = no                                                               //不显示rsync服务资源列表
max connections = 200                                     //最大连接数
timeout = 600                                                        //设置超时时间
auth users = xiefei                                            //执行数据同步的名称
hosts allow = 192.168.100.0/24                                          //允许进行数据同步的客户端的网段
hosts deny = 192.168.1.1                               //禁止进行数据同步客户端的IP地址
  • 3.创建用户认证文件,并设置文件权限
[root@server-rysnc ~]# cat /etc/rsync.pass 
xiefei:123
[root@server-rysnc ~]# chmod 600 /etc/rsync.pass
[root@server-rysnc ~]# ll /etc/rsync.pass
-rw------- 1 root root 11 9月  18 18:56 /etc/rsync.pass
  • 4.启动rsync服务并设置开机自启
[root@server-rysnc ~]# systemctl start rsyncd
[root@server-rysnc ~]# systemctl enable rsyncd
  • 查看端口,检测rsync服务是否启动
[root@server-rysnc ~]# systemctl enable rsyncd
[root@server-rysnc ~]# ss -anlt
State       Recv-Q Send-Q                   Local Address:Port                                  Peer Address:Port              
LISTEN      0      128                                  *:111                                              *:*                  
LISTEN      0      128                                  *:22                                               *:*                  
LISTEN      0      100                          127.0.0.1:25                                               *:*                  
LISTEN      0      5                                    *:873                                              *:*                  
LISTEN      0      128                                 :::111                                             :::*                  
LISTEN      0      128                                 :::22                                              :::*                  
LISTEN      0      100                                ::1:25                                              :::*                  
LISTEN      0      5                                   :::873                                             :::*    

源服务端配置

  • 1.安装rsync、inotify-tools
[root@client-rsync ~]# yum -y install rsync inotify-tools
//rsync只需要安装,不要启动和配置
  • 2.创建认证密码文件,并设置其权限为600
[root@client-rsync ~]# cat /etc/rsync.pass 
123
[root@client-rsync ~]# chmod 600 /etc/rsync.pass
  • 3.检测

在源服务端创建测试目录

[root@client-rsync ~]# mkdir /root/bbq
[root@client-rsync ~]# touch /root/bbq/xifei.test

数据同步

[root@client-rsync ~]# rsync -avH --port=873 --progress --delete /root/bbq/ xiefei@192.168.100.33::xiefei --password-file=/etc/rsync.pass 
sending incremental file list
./
xifei.test
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=0/2)

sent 119 bytes  received 46 bytes  110.00 bytes/sec
total size is 0  speedup is 0.00

目标服务端查看

[root@server-rysnc ~]# ls /tmp/xiefei/
xifei.test
  • 4.写同步脚本,让脚本自动去检测我们定制的目录下文件发生的变化,然后去执行rsync的命令把它同步到我们的服务端去
[root@client-rsync ~]# mkdir /scripts/
[root@client-rsync ~]# touch /scripts/inotify.sh
[root@client-rsync ~]# vim /scripts/inotify.sh 
host=192.168.100.33                     //目标服务器的IP地址
src=/root/xiefei                              //在源服务器上所要监控的备份目录
des=xiefei                                            //自定义模块,要去目标服务器上定义的同步名称一致
password=/etc/rsync.pass                              //执行数据同步的密码文件
user=xiefei                                              //执行数据同步的用户名
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 $user@$host::$des
 echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
  • 5.启动脚本
[root@client-rsync ~]# nohup bash /scripts/inotify.sh &
[1] 1318
[root@client-rsync ~]# nohup: ignoring input and appending output to ‘nohup.out’
[root@client-rsync ~]# ps -ef | grep inotify
root        932    870  0 09:22 pts/0    00:00:00 bash /scripts/inotify.sh
root        933    932  0 09:22 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 /root/xiefei
root        934    932  0 09:22 pts/0    00:00:00 bash /scripts/inotify.sh
  • 6.检测

在源服务器上生成新文件

[root@client-rsync ~]# cd xiefei/
[root@client-rsync xiefei]# mkdir aaa
[root@client-rsync xiefei]# echo "hello xiefei" >> aaa/bbb

在目标服务端查看

[root@server-rysnc ~]# cat /tmp/xiefei/xiefei/aaa/bbb 
hello xiefei
  • 7.设置脚本的开机自启动
[root@client-rsync ~]# touch /etc/rc.d/rc.local
[root@client-rsync ~]# chmod +x /etc/rc.d/rc.local
[root@client-rsync ~]# echo "nohup /bin/bash /scripts/inotify.sh" >> /etc/rc.d/rc.local 
[root@client-rsync ~]# tail /etc/rc.d/rc.local 
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local
nohup /bin/bash /scripts/inotify.sh

重启后查看

[root@client-rsync ~]#  ps -ef  | grep inotify
root        677    667  0 20:16 ?        00:00:00 /bin/bash /scripts/inotify.sh
root        690    677  0 20:16 ?        00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /root/xiefei
root        691    677  0 20:16 ?        00:00:00 /bin/bash /scripts/inotify.sh
root        891    858  0 20:20 pts/1    00:00:00 grep --color=autoinotify

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值