rsync远程同步(定期同步、实时同步)

关于rsync .

一款快速增量备份工具

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

配置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:根据对象的校验和来决定是否跳过文件

配置源的两种标识方法:br/>格式1:用户名@主机地址::共享模块名
格式2:rsync://用户名@主机地址/共享模块名
[[email protected] ~]# rsync -avz
[email protected] 192.168.4.200::wwwroot /root
[[email protected] ~]# rsync -avz
rsync://[email protected] 192.1 68.4.200/wwwroot /root

实验操作:

环境准备:两台主机

CentOS 7-4作为同步源:192.168.18.148

CentOS 7-5作为客户机:192.168.18.145

CentOS 7-4源端的操作:

[[email protected] ~]# hostnamectl set-hostname rsyncd
[[email protected] ~]# su
[[email protected] ~]# rpm -q rsync
rsync-3.0.9-18.el7.x86_64
[[email protected] ~]# 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保存退出

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

[[email protected] ~]# systemctl stop firewalld.service
[[email protected] ~]# setenforce 0
[[email protected] ~]# yum install httpd -y
[[email protected] html]# cd ..
[[email protected] www]# chmod 777 html/
[[email protected] 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客户机的操作:

[[email protected] ~]# hostnamectl set-hostname client
[[email protected] ~]# su
[[email protected] ~]# systemctl stop firewalld.service
[[email protected] ~]# setenforce 0
[[email protected] ~]# rpm -q rsync
rsync-3.0.9-18.el7.x86_64
[[email protected] ~]# yum install httpd -y
[[email protected] ~]# cd /var/www/html/
#此时文件中是空的没有文件的
[[email protected] html]# cd ..
[[email protected] www]# chmod 777 html/
[[email protected] 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

#同步方法一:
[[email protected] www]#  rsync -avz [email protected]::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
[[email protected] www]# cd html/
[[email protected] html]# ls      #此时index.html文件被同步
index.html
[[email protected] html]# cat index.html
this is test web

#同步方法二:
[[email protected] www]#  rsync -avz rsync://[email protected]::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
[[email protected] www]# cd html/
[[email protected] html]# ls      #此时index.html文件被同步
index.html
[[email protected] html]# cat index.html
this is test web

使用计划性任务时,免交互的配置

[[email protected] html]# vim /etc/server.pass
abc123
#写入密码信息后按Esc退出插入模式,输入:wq保存退出
[[email protected] html]# chmod 600 /etc/server.pass
[[email protected] html]# rsync -avz --delete --password-file=/etc/server.pass [email protected]::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
[[email protected] html]# ls
index.html
[[email protected] 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+inotify实时同步

调整inotify内核参数:

    max_queue_events:监控队列大小
    maxuser instances:最多监控实例数
    max_ user_watches::每个实例最多监控文件数
[[email protected] ~ ]# vi /etc/sysctl.conf
......
fs.inotify.max_ queued events = 16384
fs.inotify.max user_ instances = 1024
fs.inotify.max user watches = 1048576
[[email protected] ~]# sysctl -p

安装inotify-tools辅助工具

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

[[email protected] ~]# inotifywait -mrq -e modify,create,move,delete
/var/www/html
Setting up watches. Beware: since -r was given, this may take a while!
Watches established.
/var/www/html/ CREATE index.php
/var/www/html/ MODIFY index.php
/var/www/html/ MOVED_FROM index.php
/var/www/htmI/ MOVED_TO test.php
-m:持续进行监控

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

-q:简化输出信息

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

实验操作:

在client发起端中的操作:

[[email protected] 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保存退出

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

#加载inotofy管理工具
[[email protected] html]# mkdir /aaa
[[email protected] html]# mount.cifs //192.168.10.190/rpm /aaa
Password for [email protected]//192.168.10.190/rpm:
[[email protected] html]# cd /aaa
[[email protected] 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
[[email protected] aaa]# tar zxvf inotify-tools-3.14.tar.gz -C /opt/
[[email protected] opt]# cd /opt/inotify-tools-3.14/
[[email protected] 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
[[email protected] inotify-tools-3.14]# yum install gcc gc-c++ make -y
[[email protected] inotify-tools-3.14]# ./configure
[[email protected] inotify-tools-3.14]# make && make install
[[email protected] inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html/
#进入监控状态,监控本地的html文件中的更新,创建,移动,删除

此时无法进行操作,我们需要再开一个远程连接以进行操作

[[email protected] ~]# cd /var/www/html/
[[email protected] html]# ls
index.html
[[email protected] html]# touch abc       #创建新的abc文件
[[email protected] html]# rm -rf abc      #删除abc

此时会在监控的操作界面显示同步到了此动作:

/var/www/html/ CREATE abc       #同步到创建动作
/var/www/html/ DELETE abc       #同步到删除动作

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

在监控客户先使用Ctrl+c停止监控,然后创建脚本,操作如下:

[[email protected] inotify-tools-3.14]# cd /opt/
[[email protected] opt]# ls
inotify-tools-3.14  rh
[[email protected] 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/ [email protected]::wwwroot/"
$INOTIFY_CMD | while read DIRECTORY EVEVT FILE
do
        if [ $(pgrep rsync | wc -l) -le 0 ] ; then
            $RSYNC_CMD
        fi
done
#添加完成后按Esc退出插入模式,输入:wq保存退出
[[email protected] opt]# chmod +x inotify.sh
[[email protected] 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服务器端的配置文件:

[[email protected] www]# vim /etc/rsyncd.conf
read only = no      #关闭只读功能
#修改完成后按Esc退出插入模式,输入:wq保存退出

执行脚本
[[email protected] ~]# netstat -ntap | grep rsync
tcp        0      0 192.168.18.148:873      0.0.0.0:*          LISTEN      2768/rsync
[[email protected] ~]# kill -9 2768       #杀死该进程
[[email protected] ~]# rsync --daemon     #启动rsync
[[email protected] ~]# failed to create pid file /var/run/rsyncd.pid: File exists
#提示有pid文件存在
[[email protected] ~]# cd /var/run/
[[email protected] 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
[[email protected] run]# cat rsyncd.pid
2768
[[email protected] run]# rm -rf rsyncd.pid        #删除此pid文件
[[email protected] run]# rsync --daemon       #再次启动
[[email protected] run]# netstat -ntap | grep rsync       #此时会生成新的pid号
tcp        0      0 192.168.18.148:873      0.0.0.0:*         LISTEN      5416/rsync
[[email protected] run]# cat rsyncd.pid
5416
#此时正常运转rsync

在CentOS 7-5 client端开启监控:

[[email protected] opt]# ./inotify.sh
#此时监控开启

再打开另一个7-5的远程连接开始写内容:

[[email protected] html]# echo "this is test" > test.txt

此时文件同步到CentOS 7-4 rsync服务器端,我们可以进行查询:

[[email protected] run]# cd /var/www/html/
[[email protected] html]# ls
index.html  test.txt
[[email protected] html]# cat test.txt
this is test

rsync远程同步(定期同步、实时同步)

原文地址:https://blog.51cto.com/14449528/2460870

时间: 2024-08-12 11:15:48

rsync远程同步(定期同步、实时同步)的相关文章

rsync+inotify 实现服务器文件实时同步

rsync+inotify 实现服务器文件实时同步 操作系统:CentOS 6.X 源服务器:192.168.80.132 目标服务器:192.168.80.128 目的:把源服务器上/data/app目录实时同步到目标服务器的/data/app下 具体操作: 第一部分:在目标服务器192.168.80.128上操作 一.在目标服务器安装Rsync服务端 1.关闭SELINUX vi /etc/selinux/config #SELINUX=enforcing #SELINUXTYPE=targ

rsync、inotify实现web实时同步

. rsync.inotify实现web实时同步,布布扣,bubuko.com

Rsync+lsync实现触发式实时同步

使用rsync+lsync实现触发式实时同步 服务器信息 centos6.5 主:192.168.5.4 搭建lsync 从:192.168.5.3 搭建rsync 1.1 从服务器设置 # yum -y install rsync xinetd # cp /etc/xinetd.d/rsync /etc/xinetd.d/rsync.blk # vim /etc/xinetd.d/rsync # default: off # description: The rsync server is a

rhel下文件的同步:sersync实时同步和drbd双向同步

sersync(873端口)实时同步(单向同步) 需要注意:纯粹的使用rsync做单向同步时,rsynx的守护进程是运行在文件推送服务器上,而接收的服务器是运行rsync客户端.使用sersync做文件实时同步刚好相反,用于接收文件的服务器运行rsync守护进程. sersync主要用于服务器同步,web镜像等功能.基于boost1.43.0,inotify api,rsync command.开发.目前使用的比较多的同步解决方案是inotify-tools+rsync ,另外一个是google

rsync+inotify实现数据的实时同步

一,简介: 1. rsync是类unix系统下的数据镜像备份工具--remote sync.一款快速增量备份工具 Remote Sync,远程同步 支持本地复制,或者与其他SSH.rsync主机同步.与传统的cp.tar备份方式相比,rsync具有安全性高.备份迅速.支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器数据到远端服务器,对本地磁盘定期做数据镜像等.随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业

rsync+inotify节点间文件实时同步

说明: 操作系统:CentOS 7.2 server服务器(代码.数据检入)server: SLB-1:10.171.63.120 client服务器(数据检出.主动推送)client:WWW:10.163.0.233 目的:把client服务器上/www/web目录实时同步到server服务器的/www/web下 ============================================================ 具体操作: 第一部分:在server--SLB-1_10.1

rsync+inotify-tools实现文件的实时同步

rsync简介: Rsync 是一个远程数据同步工具,使用所谓的"Rsync 演算法"来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部分,而不是每次都整份传送,因此速度相当快.运行 Rsync server 的机器也叫 backup server,一个 Rsync server 可同时备份多个 client 的数据:也可以多个Rsync server 备份一个 client 的数据.Rsync 可以搭配 rsh 或 ssh 甚至使用 daemon 模式.Rsyn

rsync+inotify实现Git数据实时同步备份

定时备份和实时备份 说到备份,无疑于定时备份和实时同步备份.定时备份可以通过脚本或者Crontab来实现,而实时同步备份可以通过某些接口监控文件的各种变化情况来实现的(比如内核接口inotify):通过对比可以发现对数据信息要求高的环境使用实时同步备份可以更好更有利的保护数据的安全性. 软件介绍之rsync rsync说明 rsync 远程同步,同步是把数据从缓冲区同步到磁盘上去的.数据在内存缓存区完成之后还没有写入到磁盘中去.所以有时候要同步到磁盘中去的,而rsync说白了和复制差不多.能将一

配置rsync+inotify实现站点文件实时同步

一.rsync简介 rsync是linux系统下的数据镜像备份工具.可以在不同主机之间镜像同步整个目录树,支持增量备份,保持链接和权限,且采用优化的同步算法,在传输前执行压缩,因此非常适用于异地备份.镜像服务器等应用. rsync的官方站点为http:rsync.samba.org/ 二.使用rsync备份工具 2.1.rsync命令的基本用法 用法类似于cp命令,例如将文件/etc/fstab 和目录/boot/grub同步备份到.opt目录下,其中-r表示递归整个目录,-l选项用来备份链接文

rsync+inotify实现数据单向实时同步

主服务器端,向从服务器端实时同步数据 master端IP地址:192.168.1.39(node1) slave端IP地址:192.168.1.40 (node2) 一.配置从服务器 在从服务器安装rsync,创建并配置rsync文件. [[email protected] ~]# yum install -y rsync hosts allow = 192.168.1.39 hosts deny = * list = true uid = root gid = root pid file =