目录
一、rsync(同步)
一、概述
二、常用命令
| 常用选项 | 含义 |
|---|---|
| -r | 递归模式,包含目录及子目录中的所有文件 |
| -l | 对于符号链接文件仍然复制为符号链接文件 |
| -v | 显示同步过程的详细信息 |
| -z | 在传输文件时进行压缩 |
| -a | 归裆模式,递归并保留对象属性,等同于-rlptgoD |
| -p | 保留文件的权限标记 |
| -t | 保留文件的时间标记 |
| -g | 保留文件的属组标记(仅超级用户使用) |
| -o | 保留文件的属主标记(仅超级用户使用) |
| -H | 保留硬链接文件 |
| -A | 保留ACL属性信息 |
| -D | 保留设备文件及其他特殊文件 |
| –delete | 删除目标位置有而原始位置没有的文件 |
| –checksum | 根据对象的校验和来决定是否跳过文件 |
二、inotify(实时监控)
概述
常用选项
| 选项 | 含义 |
|---|---|
| -m | 持续进行监控 |
| -r | 递归监控所有子对象 |
| -q | 简化输出信息 |
| -e | 指定要监控哪些事件类型 |
三、服务组合过程

1、nfs服务器与mysql服务器建立共享同步,同步mysql服务器的data目录。
2、inotify监听同步目录,如同步目录有变化,通知rsync复制备份,备份到共享模块wwwroot指定的源目录中,实现实时同步,备份冗余。
四、实验
一、rsync
一、本地同步
第一种: [root@server ~]# cd / [root@server /]# mkdir /wh [root@server /]# cd /wh [root@server wh]# touch a.txt b.txt [root@server wh]# ls a.txt b.txt [root@server wh]# cd .. [root@server /]# rsync -avz /wh /opt/ #将wh目录加目录下的文件复制到opt目录 sending incremental file list wh/ wh/a.txt wh/b.txt sent 167 bytes received 58 bytes 450.00 bytes/sec total size is 0 speedup is 0.00 [root@server /]# ls /opt rh wh [root@server /]# ls /wh a.txt b.txt 第二种: [root@server /]# rm -rf /opt/wh [root@server /]# ls /opt rh [root@server /]# rsync -avz /wh/ /opt/ #只将wh目录下的文件复制到opt目录中 sending incremental file list ./ a.txt b.txt sent 155 bytes received 57 bytes 424.00 bytes/sec total size is 0 speedup is 0.00 [root@server /]# ls /opt a.txt b.txt rh
二、远程同步
实验环境 rsync:192.168.238.150 client:192.168.238.100
服务端
[root@localhost ~]# hostnamectl set-hostname rsync [root@localhost ~]# su [root@rsync ~]# ntpdate ntp.aliyun.com 10 Aug 21:44:34 ntpdate[23217]: adjust time server 203.107.6.88 offset 0.008105 sec [root@rsync ~]# rpm -q rsync #查询软件是否安装 [root@rsync ~]# yum -y install rsync #更新升级软件 [root@rsync ~]# vim /etc/rsyncd.conf #进入服务配置文件 原文件内容全部删除,重新写入以下内容 uid = nobody #禁锢的对象(用户的权限) gid = nobody #禁锢的对象(用户的权限) use chroot = yes #禁锢在源目录 address = 192.168.238.150 #监听的地址 port 873 #监听的端口udp/tcp873 log file = /var/log/rsyncd.log #日志文件路径 pid file = /var/run/rsyncd.pid #存放进程PID的文件路径 hosts allow = 192.168.238.0/24 #允许访问的客户机地址 [wwwroot] #共享模块 path = /var/www/html #源目录的路径
comment = Document Root of www.wh.com #给模块指定一个描述,该描述连同模块名在客户连接得到模块列表时显示给客户 read only = yes #指定客户对文件的权限,是否为只读 dont comperss = *.gz *.bz2 *.tgz *.zip *.rar *.z #指定同步传输时不再压缩的文件类型 auth users = backuper #授权账户,多个账号以空格分隔 secrets file = /etc/rsyncd_users.db #存放账户信息的数据文件

[root@rsync ~]# vim /etc/rsyncd_users.db #创建交互密码文件 [root@rsync ~]# cat /etc/rsyncd_users.db backuper:abc123 [root@rsync ~]# chmod 600 /etc/rsyncd_users.db #必须赋予600权限否则报错 [root@rsync ~]# mkdir -p /var/www/html #创建源目录
[root@rsync ~]# chmod +r /var/www/html/ #赋予所有用户对此目录的可读权限 [root@rsync ~]# ls -ld /var/www/html/ drwxr-xr-x. 2 root root 6 8月 13 11:01 /var/www/html/ [root@rsync ~]# rsync --daemon [root@rsync ~]# netstat -natp | grep rsync tcp 0 0 192.168.238.150:873 0.0.0.0:* LISTEN 79800/rsync [root@rsync ~]# echo "hello world" >> /var/www/html/1.tx
客户端
[root@localhost ~]# hostnamectl set-hostname client [root@localhost ~]# su [root@client ~]# ntpdate ntp.aliyun.com 10 Aug 21:21:43 ntpdate[18055]: adjust time server 203.107.6.88 offset -0.000536 sec [root@client ~]# cd / [root@client /]# mkdir wh [root@client /]# rsync -avz backuper@192.168.238.150::wwwroot /wh/

[root@client /]# cat /wh/1.txt hello world
免交互
客户端 [root@client /]# echo "abc123" > /etc/server.pass [root@client /]# chmod 600 /etc/server.pass 服务端 [root@rsync ~]# rm -rf /var/www/html/1.txt 客户端 [root@client /]# rsync -avz --delete --password-file=/etc/server.pass backuper@192.168.238.150::wwwroot /wh/

二、rsync+inotify
服务端
[root@rsync ~]# vim /etc/rsyncd.conf

[root@rsync ~]# kill $(cat /var/run/rsyncd.pid) #关闭服务 [root@rsync ~]# netstat -natp | grep rsync [root@rsync ~]# rsync --daemon #启动服务 [root@rsync ~]# netstat -natp | grep rsync tcp 0 0 192.168.238.150:873 0.0.0.0:* LISTEN 80550/rsync

客户端
[root@client /]# vim /etc/sysctl.conf #进入内核文件,内核优化 …… fs.inotify.max_queued_events = 32768 #监控时间队列,默认为16384 fs.inotify.max_user_instances = 1024 #最多监控实例数,默认为128 fs.inotify.max_user_watches = #每个实例最多监控文件数,默认为8192

[root@client /]# sysctl -p #刷新生效

[root@client /]# yum -y install gcc gcc-c++ #安装依赖环境 [root@client opt]# rz #上传软件包 [root@client opt]# tar zxvf inotify-tools-3.14.tar.gz #解包 [root@client opt]# cd /opt/inotify-tools-3.14/ [root@client inotify-tools-3.14]# ./configure [root@client inotify-tools-3.14]# make && make install [root@client inotify-tools-3.14]# echo "hello world" >> /wh/1.txt [root@client inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /wh #监控目录,跟踪屏幕输出结果 另开终端 [root@client ~]# mv /wh/1.txt /wh/2.txt

[root@client ~]# vim /opt/inotify.sh #!/bin/bash INOTIFY_CMD="inotifywait -mrq -e create,delete,move,modify,attrib /wh/" RSYNC_CMD="rsync -apzH --delete --password-file=/etc/server.pass /wh/ backuper@192.168.238.150::wwwroot/" $INOTIFY_CMD | while read DIRECTORY EVENT FILE do if [ $(pgrep rsync | wc -l) -le 0 ]; then $RSYNC_CMD fi done

[root@client ~]# chmod +x /opt/inotify.sh #赋予可执行权限 [root@client ~]# chmod +x /etc/rc.d/rc.local #启动脚本 [root@client ~]# echo "/opt/inotify.sh" >> /etc/rc.d/rc.local
[root@client ~]# cd /wh [root@client wh]# mkdir 123 [root@client wh]# mkdir abc
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/223649.html原文链接:https://javaforall.net
