Linux----rsync同步服务

rsync概述

Remote Sync,远程同步,它是一个开源的快速备份工具,可以在不同主机之间镜像同步整个目录树
支持增量备份、保持连接和权限,且采用优化的同步算法,传输前执行压缩,因此分床适用于异地备份、镜像服务器等应用
支持本地复制,或者与其他SSH、rsync主机同步

官方网站:http://rsync.samba.org

搭建rsync

实验环境

主机 IP地址
rsync同步源 192.168.179.137
rsync客户机 192.168.179.128
  • 用户backuper,允许下行同步,操作的目录为 /var/www/html/
yum -y install httpd
  • 配置rsync
yum -y install rsync
  • 更改配置文件
vi /etc/rsyncd.conf               ####在同步源服务器上配置

uid = nobody                       
gid = nobody
#禁锢在源目录
use chroot = yes
#监听地址			
address = 192.168.179.137
#监听端口号   		
port 873
#日志文件位置				
log file = /var/log/rsyncd.log
#存放进程PID文件位置	
pid file = /var/run/rsyncd.pid
#允许访问的客户端地址
hosts allow = 192.168.179.0/24
#共享模块名称
[wwwroot]
#源目录的实际路径
path = /var/www/html
comment = Document Root of www1.bdqn.com
#是否只读
read only = yes
#同步时压缩的文件类型
dont comperss = *.gz *.bz2 *.tgz *.zip *.rar *.z
#授权账户
auth users = backuper
#存放账户信息的数据文件
secrets file = /etc/rsyncd_users.db
  • rsync账号文件
  • 采用“用户名:密码”的记录格式,每行一个用户记录独立的账号数据,不依赖于系统账号
vi /etc/rsyncd_users.db
backuper:pwd123

//修改权限
chmod 600 /etc/rsyncd_users.db
  • 启用rsync服务
rsync --daemon 

停止服务  
kill $(cat /var/run/rsyncd.pid)

//查看服务状态
netstat -anpt |grep rsync

测试

  • 在/var/www/html目录中,新建文件(内容随意)
cd /var/www/html/

echo "abc" 1.html

echo "aaa" 2.html
  • 客户机实验rsync备份工具同步
rsync命令的用法:rsync [选项] 原始位置 目标位置

[root@metalogger ~]# rsync /etc/fstab /opt
[root@metalogger ~]# cd /opt/
[root@metalogger opt]# ll 
总用量 8
-rw-r--r--.  1 root root   595 3月  24 20:08 fstab
drwxr-xr-x. 20  501 games 4096 3月  24 13:36 moosefs-3.0.100
drwxr-xr-x.  2 root root     6 3月  26 2015 rh
  • 常用选项
  • -a:归档模式,递归并保留对象属性,等同于 -rlptgoD
    -v:显示同步过程的详细(verbose)信息
    -z:在传输文件时进行压缩(compress)
    -H:保留硬连接文件
    -A:保留ACL属性信息
    --delete:删除目标位置有而原始位置没有的文件
    --checksum:根据对象的校验和来决定是否跳过文件

配置源的两种表示方法

  • 方法1:用户名@主机地址::共享模块名
rsync -avz [email protected]::wwwroot /opt
Password:pwd123               //在服务端创建那个文件的密码

  • 方法2:rsync://用户名@主机地址/共享模块名
rsync -avz rsync://[email protected]/wwwroot /root
Password: 

rsync同步操作示例

  • 客户机操作
[root@metalogger opt]# mkdir /opt/myweb
[root@metalogger opt]# rsync -avzH --delete [email protected]::wwwroot  /opt/myweb
Password: 
receiving incremental file list
./
1.html
2.html

sent 68 bytes  received 167 bytes  94.00 bytes/sec
total size is 8  speedup is 0.03

[root@metalogger opt]# ll myweb/
总用量 8
-rw-r--r--. 1 root root 4 3月  24 20:13 1.html
-rw-r--r--. 1 root root 4 3月  24 20:13 2.html

rsync源的免交互处理
使用 --password-file= 密码文件

//示例
vi /etc/server.pass
pwd123
cat /etc/server.pass
pwd123
chmod 600 /etc/server.pass
rsync -az --delete --password-file=/etc/server.pass [email protected]::wwwroot /opt/myweb/  
###测试命令是否正常###
crontab -e   ####每天晚上10点半对服务器网站目录更新一次
30 22 * * * /usr/bin/rsync -az --delete --password-file=/etc/server.pass [email protected]::wwwroot /opt/myweb/

rsync实时同步

1.定期同步的不足

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

2.实时同步的优点

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

3.调整inotify内核参数

  • max_queue_events:监控队列大小
  • max_user_instances:最多监控实例数
  • max_user_watches:每个实例最多监控文件数
vi /etc/sysctl.conf

fs.inotify.max_queued_events = 32768
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576

sysctl -p
  • 客户端安装inotify-tools辅助工具
cd /opt
tar xzvf inotify-tools-3.14.tar.gz -C /opt
cd inotify-tools-3.14/

/编译安装
./configure
make && make install
  • 开启监控
  • inotifywait:用于持续监控,实时输出结果
  • inotifywatch:用于短期监控,任务完成后再出结果
inotifywait -mrq -e modify,create,move,delete  /opt/myweb
  • 客户端编写脚本
  • 通过inotifywait触发rsync同步操作,使用while、read持续获取监控结果,根据结果可以作进一步判断,决定执行何种操作
vim /opt/inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e create,delete,move,modify,attrib /opt/myweb/"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /opt/myweb/ [email protected]::wwwroot"
 
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
    if [ $(pgrep rsync | wc -l) -le 0 ] ; then
        $RSYNC_CMD
#       echo "${FILE} was rsynced" >>/opt/inotify_rsync.log

    fi
done
  • 更改同步源
//权限设置
在同步源 192.168.179.137更改
vi /etc/rsyncd.conf   
read only =no                 ###这个地方原来的yes  改成 no    这个地方的模块可以读写了

kill $(cat /var/run/rsyncd.pid)      ###关闭rsyncd服务
rsync --daemon                              ###启动rsyncd服务
netstat -anpt |grep rsync
  • 客户端操作
在客户机 192.168.179.128
/opt/inotify.sh 
chmod 777 /opt/myweb/     ###需要在开一个终端
vi /opt/myweb/110.html      ###里面插入任意数据


在同步源 192.168.179.137
ll /var/www/html/         ##  会发现这个地方会多个110.html文件

发布了78 篇原创文章 · 获赞 5 · 访问量 2565

猜你喜欢

转载自blog.csdn.net/qq397750142/article/details/105080635
今日推荐