rsync remote synchronization (theory + practical)

About rsync

A fast incremental backup tool

Remote Sync, remote synchronization
support local replication, or with other SSH, rsync host synchronization
official website: http://rsync.samba.org

Configuring rsync server source

rsync synchronization source

指备份操作的远程服务器,也称为备份源

rsync remote synchronization (theory + practical)

Configuring rsync source

The basic idea

建立rsync.conf配置文件,独立的账号文件
启用rsync的--daemon模式

Application Examples

用户backuper,允许下行同步
操作的目录为/var/www/html

Profile rsyncd.conf

需手动建立,语法类似于Samba配置
认证配置auth users,secrets file,不加则为匿名

rsync account file

采用“用户名:密码”的记录格式,每行一个用户记录
独立的账号数据,不依赖于系统账号

Enabling rsync service

通过--daemon独自提供服务
执行kill $(cat /var/run/rsync.pid)关闭rsync服务

Use rsync backup tool

Rsync command usage

rsync [选项] 原始位置 目标位置

Common options

-a:归档模式,递归并保留对象属性,等用于-rlptgoD
-v:显示同步过程的详细信息
-z:在传输文件时进行压缩
-H:保留硬连接文件
-A:保留ACL属性信息
--delete:删除目标位置有而原始位置没有的文件
--checksum:根据对象的校验和来决定是否跳过文件

Configure two representations source

格式1:用户名@主机地址::共享模块名
格式2:rsync://用户名@主机地址/共享模块名

rsync real-time synchronization

Regular synchronization inadequate

执行备份的时间固定,延迟明显,实时性差
当同步源长期不变化时,密集的定期任务是不必要的

The advantage of real-time synchronization

一旦同步源出现变化,立即启动备份
只要同步源无变化,则不执行备份

About inotify (installed at the originating end)

Inotify 是一个 Linux特性,它监控文件系统操作,比如读取、写入和创建。
Inotify 反应灵敏,用法非常简单,并且比 cron 任务的繁忙轮询高效得多。
可以监控文件系统的变化情况,并作出通知响应;
辅助软件:inotify-tools

rsync remote synchronization (theory + practical)

lab environment

rsyncd:192.168.52.134
client:192.168.52.148

1, modify the configuration file on the server rsyncd

[root@rsyncd ~]# rpm -q rsync   ##检查是否安装rsync,没有用yum安装
rsync-3.0.9-18.el7.x86_64
[root@rsyncd ~]# vim /etc/rsyncd.conf
uid = nobody     ##匿名用户
gid = nobody
use chroot = yes   ##禁锢家目录
pid file = /var/run/rsyncd.pid  ##pid文件
address = 192.168.13.128    ##监听地址
port = 873    ##监听端口号
log file = /var/log/rsyncd.log   ##日志文件路径
hosts allow = 192.168.13.0/24   ##允许地址段访问
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2   ##不需要压缩的类型

[wwwroot]      ##共享模块名
path = /var/www/html    ##路径
comment = www.kgc.com  ##定义名称
read only = yes    ##开启只读
auth users = backuper   ##身份验证用户名
secrets file = /etc/rsyncd_users.db    ##密码文件

[root@rsyncd ~]# vim /etc/rsyncd_users.db  ##创建密码文件
backuper:abc123  ##用户名:密码
[root@rsyncd ~]# chmod 600 /etc/rsyncd_users.db   ##给root用户读写权限
[root@rsyncd ~]# rsync --daemon   ##开启rsync服务
[root@rsyncd ~]# netstat -ntap | grep rsync   ##查看端口
tcp        0      0 192.168.52.134:873      0.0.0.0:*               LISTEN      15471/rsync      
[root@rsyncd ~]# systemctl stop firewalld.service   ##关闭防火墙
[root@rsyncd ~]# setenforce 0
[root@rsyncd ~]# yum install httpd -y   ##安装httpd服务
[root@rsyncd ~]# cd /var/www/html/
[root@rsyncd html]# echo "this is test web" > index.html   ##创建网页信息
[root@rsyncd html]# cd ../
[root@rsyncd www]# chmod 777 html/   ##给最大权限,方便任意用户操作
[root@rsyncd www]# ll   ##查看权限
总用量 0
drwxr-xr-x. 2 root root  6 8月   8 19:42 cgi-bin
drwxrwxrwx. 2 root root 24 12月 13 15:11 html
[root@rsyncd www]# 

2, on the client server, pulling synchronization source rsyncd


[root@client ~]# rpm -q rsync  ##检查是否安装rsync服务
rsync-3.0.9-18.el7.x86_64
[root@client ~]# systemctl stop firewalld.service  ##关闭防火墙
[root@client ~]# setenforce 0
[root@client ~]# yum install httpd -y  ##安装httpd服务
[root@client ~]# cd /var/www/
[root@client www]# chmod 777 html/  ##给最大权限
[root@client www]# ls -l   ##查看去哪先
总用量 0
drwxr-xr-x. 2 root root 6 8月   8 19:42 cgi-bin
drwxrwxrwx. 2 root root 6 8月   8 19:42 html

##同步格式一:
[root@client www]# rsync -avz [email protected]::wwwroot /var/www/html/
##拉取共享模块
Password:   ##输入密码  
./
index.html

sent 83 bytes  received 172 bytes  46.36 bytes/sec
total size is 17  speedup is 0.07
[root@client www]# ls
cgi-bin  html
[root@client www]# cd html/
[root@client html]# ls
index.html
[root@client html]# cat index.html     ##查看同步情况
this is test web
[root@client html]# 
[root@client www]# cat html/index.html
this is test web

##同步格式二:
[root@client html]# rm -rf index.html    ##删除同步过来的文件
[root@client html]# ls
[root@client html]# rsync -avz rsync://[email protected]/wwwroot /var/www/html/
##拉取共享模块
Password:    ##输入密码  
receiving incremental file list
./
index.html

sent 83 bytes  received 172 bytes  72.86 bytes/sec
total size is 17  speedup is 0.07
[root@client html]# ls
index.html
[root@client html]# cat index.html     ##查看同步情况
this is test web
[root@client html]# 

##免交互同步:
[root@client html]# rm -rf index.html     ##删除同步过来的文件
[root@client html]# touch abc.html   ##在目录下创建一个abc.html文件
[root@client html]# ls
abc.html
[root@client html]# 
[root@client html]# vim /etc/server.pass  ##创建本地的密码文件
abc123
[root@client html]# chmod 600 /etc/server.pass    ##给权限
[root@client html]# 
[root@client html]# rsync -avz --delete --password-file=/etc/server.pass [email protected]::wwwroot /var/www/html/
##指定本地密码文件,删除目标位置有而原始位置没有的文件,实现免交互
receiving incremental file list
deleting abc.html
./
index.html

sent 83 bytes  received 172 bytes  170.00 bytes/sec
total size is 17  speedup is 0.07
[root@client html]# ls   ##可以看到,abc.html被删除了,因为加了--delete选项
index.html
[root@client html]# cat index.html 
this is test web
[root@client html]# 

3, installed on the client monitoring inotify

[root@client html]# cd ../
[root@client www]# vim /etc/sysctl.conf   ##修改内核参数文件
fs.inotify.max_queued_events = 16384   ##队列
fs.inotify.max_user_instances = 1024     ##每个队列中的实例数
fs.inotify.max_user_watches = 1048576  ##每个实例中的文件数
[root@client www]# sysctl -p  ##加载
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
[root@client www]# mount.cifs //192.168.100.100/tools /mnt/tools/  ##挂载
Password for root@//192.168.100.100/tools:  
[root@client www]# cd /mnt/tools/inotify/
[root@client inotify]# tar xf inotify-tools-3.14.tar.gz -C /opt/   ##解压inotify到/opt下
[root@client inotify]# cd /opt/inotify-tools-3.14/
[root@client inotify-tools-3.14]# yum install gcc gcc-c++ make -y   ##安装环境必要的组件
[root@client inotify-tools-3.14]# ./configure    ##配置
[root@client inotify-tools-3.14]# make && make install  ##编译安装
[root@client inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html/    
##进行监控

##重启开启一个客户机的终端
[root@client ~]# cd /var/www/html/
[root@client html]# ls
index.html
[root@client html]# touch abc
[root@client html]# rm -rf abc 
[root@client html]# 

##在监控上的客户机上查看
/var/www/html/ CREATE abc
/var/www/html/ DELETE abc

4, create a script on the client machine, triggered by inotifywait rsync synchronization script

[root@client inotify-tools-3.14]# cd /opt/
[root@client opt]# vim inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,move,delete /var/www/html/"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /var/www/html/ [email protected]::wwwroot/"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE  
 do
   if [ $(pgrep rsync | wc -l) -le 0 ]; then    
      $RSYNC_CMD
   fi
done
[root@client opt]# chmod +x inotify.sh  ##给执行权限
##确保服务端和客户端的权限都为最大

5, modify the configuration file on the server rsyncd

[root@rsyncd www]# vim /etc/rsyncd.conf
read only = no  ##关闭只读
[root@rsyncd www]# pkill -9 rsync   ##关闭
[root@rsyncd www]# netstat -ntap | grep rsync
[root@rsyncd www]# 
[root@rsyncd www]# rm -rf /var/run/rsyncd.pid   ##删除pid文件
[root@rsyncd www]# rsync --daemon    ##开启rsync服务
[root@rsyncd www]# netstat -ntap | grep rsync
tcp        0      0 192.168.52.134:873      0.0.0.0:*               LISTEN      50571/rsync         
[root@rsyncd www]#

6, the implementation inotify script file on the client

##客户端执行脚本
[root@client opt]# ./inotify.sh
##进入监控状态

##重新开启一个客户机终端
[root@client ~]# cd /var/www/html/
[root@client html]# ls
index.html
[root@client html]# echo "this is test" > test.txt  ##添加文本

##查看监控服务信息
[root@client opt]# ./inotify.sh 
rsync: failed to set times on "/." (in wwwroot): Operation not permitted (1)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1052) [sender=3.0.9]

##在rsync服务器上查看
[root@rsyncd www]# cd html/
[root@rsyncd html]# ls
index.html  test.txt   ##同步完成

##在新开的客户机终端
[root@client html]# rm -rf test.txt 
[root@client html]# ls
index.html
[root@client html]# 

##在rsync服务器上查看
[root@rsyncd html]# ls
index.html
[root@rsyncd html]# 
##删除也是同步的

Guess you like

Origin blog.51cto.com/14449541/2461162