温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

rsync远程同步(定期同步,实时同步实战!)

发布时间:2020-07-26 05:39:06 来源:网络 阅读:3283 作者:JarryZ 栏目:数据库
本章结构

1.rsync同步简介
2.配置rsync备份源
3.rsync命令基本用法
4.rsync备份操作示例
5.rsync+inotify结合使用

关于rsync .

一款快速增量备份工具

1.Remote Sync,远程同步
2.支持本地复制,或者与其他SSH、rsync主机同步
3.官方网站: http://rsync.samba.org

rsync远程同步(定期同步,实时同步实战!)

配置rsync源服务器

rsync同步源:

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

rsync远程同步(定期同步,实时同步实战!)

配置rsync源

基本思路:

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

应用示例:

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

配置文件rsyncd.conf

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

rsync账号文件

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

启用rsync服务

1.通过--daemon独自提供服务 [执行kill $(catIvar/run/rsyncd.pid)关闭rsync服务]

使用rsync备份工具

rsync命令的用法:

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

1.常用选项:

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

配置源的两种标识方法:

rsync远程同步(定期同步,实时同步实战!)

Demo:

环境准备:两台主机

CentOS 7-4作为同步源:192.168.18.148

CentOS 7-5作为客户机:192.168.18.145

CentOS 7-4源端的操作:
[root@localhost ~]# hostnamectl set-hostname rsyncd
[root@localhost ~]# su
[root@rsyncd ~]# rpm -q rsync
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
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
#以上内容去掉注释

address = 192.168.18.148        #本机IP地址
port 873        #开端口
log file = /var/rsyncd.log      #指定日志文件 
hosts allow = 192.168.18.0/24   #允许网段访问
#在pid下行添加以上内容

[wwwroot]
path = /var/www/html
comment = www.kgc.com
read only = yes
auth users = backuper
secrets file = /etc/rsyncd_users.db
#在dont下一行插入以上内容:共享模块
#修改完成后按Esc退出插入模式,输入:wq保存退出

#添加密码文件
[root@rsyncd ~]# vim /etc/rsyncd_users.db
backuper:abc123     #需要和共享模块中的auth_users名称一致
#添加完成后按Esc退出插入模式,输入:wq保存退出
[root@rsyncd ~]# chmod 600 /etc/rsyncd_users.db
[root@rsyncd ~]# rsync --daemon
[root@rsyncd ~]# netstat -ntap | grep rsync
tcp        0      0 192.168.18.148:873      0.0.0.0:*          LISTEN      6150/rsync
#此时873端口开启

[root@rsyncd ~]# systemctl stop firewalld.service
[root@rsyncd ~]# setenforce 0
[root@rsyncd ~]# yum install httpd -y
[root@rsyncd html]# cd ..
[root@rsyncd www]# chmod 777 html/
[root@rsyncd www]# ls -l
总用量 0
drwxr-xr-x. 2 root root  6 8月   8 19:42 cgi-bin
drwxrwxrwx. 2 root root 24 12月 16 08:41 html
CentOS 7-5客户机的操作:
[root@localhost ~]# hostnamectl set-hostname client
[root@localhost ~]# su
[root@client ~]# systemctl stop firewalld.service
[root@client ~]# setenforce 0
[root@client ~]# rpm -q rsync
rsync-3.0.9-18.el7.x86_64
[root@client ~]# yum install httpd -y
[root@client ~]# cd /var/www/html/
#此时文件中是空的没有文件的
[root@client html]# cd ..
[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 backuper@192.168.18.148::wwwroot /var/www/html/
Password:       #此时输入密码abc123,敲回车
receiving incremental file list
./
index.html

sent 83 bytes  received 172 bytes  12.44 bytes/sec
total size is 17  speedup is 0.07
[root@client www]# cd html/
[root@client html]# ls      #此时index.html文件被同步
index.html
[root@client html]# cat index.html
this is test web

#同步方法二:
[root@client www]#  rsync -avz rsync://backuper@192.168.18.148::wwwroot /var/www/html/
Password:       #此时输入密码abc123,敲回车
receiving incremental file list
./
index.html

sent 83 bytes  received 172 bytes  12.44 bytes/sec
total size is 17  speedup is 0.07
[root@client www]# cd html/
[root@client html]# ls      #此时index.html文件被同步
index.html
[root@client html]# cat index.html
this is test web
此时我们如果使用计划性任务,就需要考虑到免交互的问题:
[root@client html]# vim /etc/server.pass
abc123
#写入密码信息后按Esc退出插入模式,输入:wq保存退出
[root@client html]# chmod 600 /etc/server.pass
[root@client html]# rsync -avz --delete --password-file=/etc/server.pass backuper@192.168.18.148::wwwroot /var/www/html/        #用此条命令可以直接进入
receiving incremental file list
./
index.html

sent 83 bytes  received 172 bytes  510.00 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

#后面就可以在crontab -e中添加计划任务了

rsync实时同步

定期同步的不足

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

实时同步的优点

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

关于inotify

Linux内核的inotify机制

1.从版本2.6.13开始提供
2.可以监控文件系统的变动情况,并作出通知响应
3.辅助软件: inotify-tools

rsync远程同步(定期同步,实时同步实战!)

rsync+inotify实时同步

调整inotify内核参数:

max_queue_events:监控队列大小
maxuser instances:最多监控实例数
max_ user_watches::每个实例最多监控文件数

rsync远程同步(定期同步,实时同步实战!)

安装inotify-tools辅助工具:

inotifywait:用于持续监控,实时输出结果
inotifywatch:用于短期监控,任务完成后再出结果

rsync远程同步(定期同步,实时同步实战!)

-m:持续进行监控

-r:递归监控所有子对象

-q:简化输出信息

-e:指定要监控哪些事件类型

Demo:

在client发起端中的操作:
[root@client html]# vim /etc/sysctl.conf
#需要在发起端开启监控
#在末行下插入以下内容
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
#添加完成后按Esc退出插入模式,输入:wq保存退出

[root@client html]# sysctl -p       #刷新数据
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576

#加载inotofy管理工具
[root@client html]# mkdir /aaa
[root@client html]# mount.cifs //192.168.10.190/rpm /aaa
Password for root@//192.168.10.190/rpm:
[root@client html]# cd /aaa
[root@client aaa]# ls
Discuz_X3.4_SC_UTF8.zip           nginx-1.12.2.tar.gz
error.png                         php
extundelete-0.2.4.tar.bz2         redis-5.0.7.tar.gz
haproxy-1.5.19.tar.gz             ruby-2.4.1.tar.gz
httpd-2.4.29.tar.bz2              ruby.png
hzw.jpeg                          squid
inotify-tools-3.14.tar.gz         TC
[root@client aaa]# tar zxvf inotify-tools-3.14.tar.gz -C /opt/
[root@client opt]# cd /opt/inotify-tools-3.14/
[root@client inotify-tools-3.14]# ls
aclocal.m4    config.h.in   COPYING     libinotifytools  man      src
AUTHORS       config.sub    depcomp     ltmain.sh        missing
ChangeLog     configure     INSTALL     Makefile.am      NEWS
config.guess  configure.ac  install-sh  Makefile.in      README
[root@client inotify-tools-3.14]# yum install gcc gc-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/
#进入监控状态,监控本地的html文件中的更新,创建,移动,删除
此时无法进行操作,我们需要再开一个远程连接以进行操作
[root@client ~]# cd /var/www/html/
[root@client html]# ls
index.html
[root@client html]# touch abc       #创建新的abc文件
[root@client html]# rm -rf abc      #删除abc
此时会在监控的操作界面显示同步到了此动作:
/var/www/html/ CREATE abc       #同步到创建动作
/var/www/html/ DELETE abc       #同步到删除动作

我们可以使用:监控触发动作,然后调取rsync进行同步

在监控客户先使用Ctrl+c停止监控,然后创建脚本,操作如下:
[root@client inotify-tools-3.14]# cd /opt/
[root@client opt]# ls
inotify-tools-3.14  rh
[root@client opt]# vim inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /var/www/html/ backuper@192.168.18.148::wwwroot/"
$INOTIFY_CMD | while read DIRECTORY EVEVT FILE
do
        if [ $(pgrep rsync | wc -l) -le 0 ] ; then
            $RSYNC_CMD
        fi
done
#添加完成后按Esc退出插入模式,输入:wq保存退出
[root@client opt]# chmod +x inotify.sh
[root@client opt]# ls -l /var/www/
总用量 0
drwxr-xr-x. 2 root root  6 8月   8 19:42 cgi-bin
drwxrwxrwx. 2 root root 24 12月 16 10:00 html
此时还需要注意到CentOS 7-4 rsync服务器端的配置文件:
[root@rsyncd www]# vim /etc/rsyncd.conf
read only = no      #关闭只读功能
#修改完成后按Esc退出插入模式,输入:wq保存退出

执行脚本
[root@rsyncd ~]# netstat -ntap | grep rsync
tcp        0      0 192.168.18.148:873      0.0.0.0:*          LISTEN      2768/rsync
[root@rsyncd ~]# kill -9 2768       #杀死该进程
[root@rsyncd ~]# rsync --daemon     #启动rsync
[root@rsyncd ~]# failed to create pid file /var/run/rsyncd.pid: File exists
#提示有pid文件存在
[root@rsyncd ~]# cd /var/run/
[root@rsyncd run]# ls
abrt          dhclient-ens33.pid  lock            radvd           syslogd.pid
alsactl.pid   dmeventd-client     log             rpcbind         systemd
atd.pid       dmeventd-server     lsm             rpcbind.sock    tmpfiles.d
auditd.pid    faillock            lvm             rsyncd.pid      tuned
avahi-daemon  firewalld           lvmetad.pid     samba           udev
certmonger    gdm                 mdadm           sepermit        udisks2
chrony        gssproxy.pid        media           setrans         user
chronyd.pid   gssproxy.sock       mount           setroubleshoot  utmp
console       httpd               named           sm-notify.pid   vmware
crond.pid     initramfs           netreport       spice-vdagentd  xtables.lock
cron.reboot   ksmtune.pid         NetworkManager  sshd.pid
cups          libvirt             plymouth        sudo
dbus          libvirtd.pid        ppp             sysconfig
[root@rsyncd run]# cat rsyncd.pid
2768
[root@rsyncd run]# rm -rf rsyncd.pid        #删除此pid文件
[root@rsyncd run]# rsync --daemon       #再次启动
[root@rsyncd run]# netstat -ntap | grep rsync       #此时会生成新的pid号
tcp        0      0 192.168.18.148:873      0.0.0.0:*         LISTEN      5416/rsync
[root@rsyncd run]# cat rsyncd.pid
5416
#此时正常运转rsync
在CentOS 7-5 client端开启监控:
[root@client opt]# ./inotify.sh
#此时监控开启
再打开另一个7-5的远程连接开始写内容:
[root@client html]# echo "this is test" > test.txt
此时文件同步到CentOS 7-4 rsync服务器端,我们可以进行查询:
[root@rsyncd run]# cd /var/www/html/
[root@rsyncd html]# ls
index.html  test.txt
[root@rsyncd html]# cat test.txt
this is test

以上就实现了实时同步!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI