Remote Sync ----- 远程同步,支持本地复制,或者与其余SSH 、rsync主机同步,功能相似于scp,可是要比scp丰富。 官方网站:http://rsync.samba.org
一、能够镜像保存整个目录树和文件系统。 二、能够很容易作到保持原来文件的权限、时间、软硬连接等等,无须特殊权限便可安装。 三、快速:第一次同步时 rsync 会复制所有内容,但在下一次只传输修改过的文件。rsync 在传输数据的过程当中能够实行压缩及解压缩操做,所以可使用更少的带宽。 四、安全:可使用scp、ssh等方式来传输文件,固然也能够经过直接的socket链接。 五、支持匿名传输,以方便进行网站镜像。
rsync 是系统内核自带的,rpm - q rsync查看包 ,无需额外安装.若是是最小化安装的话,使用 yum安装一下便可
1.修改rsyncd.conf配置文件html
[root@server ~]# vim /etc/rsyncd.conf #七、八、9行, uid = nobody gid = nobody use chroot = yes #11行, pid file = /var/run/rsyncd.pid #16行, dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 #追加端口号 port 873 #追加日志文件路径 log file = /var/log/rsyncd.log #追加受权访问地址段 hosts allow = 192.168.142.0/24 #添加共享模块 #模块名称 [wwwroot] #源共享目录路径 path = /var/www/html #网站说明 comment = www.bdqn.cn #是否只读 read only = yes #认证用户名 auth users = backuper #认证用户密码文件路径 secrets file = /etc/rsyncd_users.db
2.建立认证用户密码文件c++
[root@server ~]# vim /etc/rsyncd_users.db #写入认证用户名与密码 backuper:abc123
3.受权仅属主的最大权限web
[root@server ~]# chmod 600 /etc/rsyncd_users.db
4.安装HTTP的服务vim
[root@server ~]# yum install httpd -y
5.建立共享内容安全
[root@server ~]# cd /var/www/html [root@server html]# echo "this is test web" > index.html
6.开启服务bash
[root@server html]# rsync --daemon
7.查看服务状态服务器
[root@server html]# netstat -ntap | grep rsync tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 60268/rsync tcp6 0 0 :::873 :::* LISTEN 60268/rsync
8.关闭防火墙及安全功能ssh
[root@server html]# systemctl stop firewalld.service [root@server html]# setenforce 0
1.关闭防火墙及安全功能socket
[root@localhost ~]# systemctl stop firewalld.service [root@localhost ~]# setenforce 0
2.安装http服务tcp
[root@localhost ~]# yum install httpd -y
3.客户发起端配置方式
#配置源方式一,用户名@主机地址::共享模块名 [root@localhost ~]# rsync -avz backuper@192.168.142.153::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@localhost ~]# cat /var/www/html/index.html this is test web
#配置源方式二,rsync://用户名@主机地址/共享模块名 [root@localhost ~]# rsync -avz rsync://backuper@192.168.142.153/wwwroot /var/www/html Password: receiving incremental file list ./ index.html sent 83 bytes received 172 bytes 56.67 bytes/sec total size is 17 speedup is 0.07 #查看共享到的内容 [root@localhost ~]# cat /var/www/html/index.html this is test web
4.建立免交互密码文件
[root@localhost ~]# vim /etc/server.pass abc123 [root@localhost ~]# chmod 600 /etc/server.pass #免交互配置源方式 [root@localhost ~]# rsync -avz --delete --password-file=/etc/server.pass backuper@192.168.142.153::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@localhost ~]# cat /var/www/html/index.html this is test web
1.按期同步的不足: 执行备份的时间固定,延迟明细,实时性差; 当同步源长期不变化时,密集的按期任务是没必要要的 2.实时同步的优势: 一旦同步源出现变化,当即启用备份; 只要同步源不变化,则不执行备份
Inotify 是一个 Linux特性,它监控文件系统操做,好比读取、写入和建立。Inotify 反应灵敏,用法很是简单,而且比 cron 任务的繁忙轮询高效得多。 从版本 2.6.13 开始提供; 能够监控文件系统的变化状况,并做出通知响应; 辅助软件:inotify-tools
1.配置rsync源服务器,修改rsyncd.conf配置文件
[root@server ~]# vim /etc/rsyncd.conf #关闭只读 read only = no
2.调整客户端的inotify内核参数
[root@client ~]# vim /etc/sysctl.conf #监控队列大小 fs.inotify.max_queued_events = 16384 #最多监控实例数 fs.inotify.max_user_instances = 1024 #每一个实例最多监控文件数 fs.inotify.max_user_watches = 1048576
3.生效内核参数
[root@client ~]# sysctl -p fs.inotify.max_queued_events = 16384 fs.inotify.max_user_instances = 1024 fs.inotify.max_user_watches = 1048576
4.安装编译环境
[root@client ~]# yum install -y gcc gcc-c++ make
5.远程获取资源包
[root@client ~]# mount.cifs //192.168.142.1/inotify /mnt [root@sclient ~]# cd /mnt [root@client mnt]# ls inotify-tools-3.14.tar.gz
6.解压资源包
[root@client mnt]# tar zxvf inotify-tools-3.14.tar.gz -C /opt
7.配置inotify
[root@client mnt]# cd /opt/inotify-tools-3.14/ [root@client inotify-tools-3.14]# ./configure
8.编译安装
[root@client inotify-tools-3.14]# make && make install
9.安装inotify-tools辅助工具
[root@client inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html/ #-m表示持续进行监控,-r表示递归监控全部子对象,-q表示简化输出信息,-e表示要监控哪些时间类型
10.重开一个终端登陆,增删文件
#建立文件 [root@client html]# touch abc [root@client html]# ls abc index.html #删除文件 [root@client html]# rm -rf abc [root@client html]# ls index.html
11.返回监控端,验证同步效果
[root@client inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html/ /var/www/html/ CREATE abc #建立记录 /var/www/html/ DELETE abc #删除记录
12.经过inotifywait触发rsync同步操做脚本
vim inotify.sh #!/bin/bash INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/" RSYNCLCMD="rsyne -azH --delete --password-file=/etc/server.pass /var/www/htm1/ backuper@192.168.142.153::wwwroot/" $INOTIFY_CMD | while read DIRECTORY EVENT FILE #读取输出的监控记录 do if [ $(pgrep rsync | wc -l) -le 0 ] ; then #若rsync为执行,则当即启动 $RSYNC_CMD fi done
13.源端于客户端都须要html目录最高受权
[root@server www]# chmod 777 html/ [root@client www]# chmod 777 html/
14.执行脚本
[root@client opt]# source inotify.sh
15.重开终端,并切入共享目录
[root@client opt]# cd /var/www/html/
16.写入新的内容
[root@client html]# echo "this is my update" > test.txt
**1.回到源端查看同步数据包** [root@server html]# ls index.html test.txt
2.查看同步数据
[root@server html]# cat test.txt this is my update