本文共 8057 字,大约阅读时间需要 26 分钟。
Remote Sync,远程同步,它是一个开源的快速增量备份工具,可以在不同主机之间镜像同步整个目录树。
支持增量备份、保持连接和权限,且采用优化的同步算法,传输前执行压缩,因此非常适用于异地备份、镜像服务器等应用 支持本地复制,或者与其他SSH、rsync主机同步 rsync 与scp FTP等工具备份的机制的优越性在于rsync 同步备份是先比较在拷贝变化过的数据,这样更节省资源,如有1T的数据只有1K的数据改变,则rsync 基本上备份只要同步1k 的数据而 scp 是个傻瓜式的拷贝,全部拷贝。1、ssh方式进行同步
2、C/S 方式,rsync 有服务器端daemon模块 和rsync 客户端
在远程同步任务中,负责发起rsync同步操作的客户机称为发起端,而负责相应来自客户机的rsync同步操作的服务器称为同步源。
在同步过程中,同步源负责提供文档的原始位置,发起端应对该位置有读取权限。 如下图: rsync是一款快速增量备份工具,支持:稍后从这三个方面来演示怎么使用rsync备份工具。
配置rsync源服务器大致分为三步:
准备两台虚拟机,一台作为同步源,一台用于客户机发起同步。
语法rsync [选项] 原始位置 目标位置常用选项-a: 归档模式,递归并保留对象属性,等同于-rlptgoD-v: 显示同步过程的详细(verbose)信息-z: 在传输文件时进行压缩(compress)-H: 保留硬连接文件-A: 保留ACL属性信息-p: 保留文件的权限标记-t: 保留文件的时间标记-g: 保留文件的属组标记-o: 保留文件的属主标记-delete: 删除目标位置有而原始位置没有的文件-checksum: 根据对象的校验和来决定是否跳过文件
同步源服务器:
[root@server1 ~]# rpm -q rsync #检查rsync是否安装rsync-3.0.9-18.el7.x86_64[root@server1 ~]# vi /etc/rsyncd.confuid = nobodygid = nobodyuse chroot = yes #禁锢在宿主目录中address = 192.168.158.10 #监听地址port 873 #端口号pid file = /var/run/rsyncd.pid #进程文件位置log file = /var/log/rsyncd.log #日志文件位置hosts allow = 192.168.158.0/24 #允许地址范围dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 #不再压缩这几种格式的文件[wwwroot]path = /var/www/html #同步的目录comment = www.tom.comread only = yes #只读auth users = backupersecrets file = /etc/rsyncd_users.db #用户密码存放位置
[root@server1 ~]# vi /etc/rsyncd_users.dbbackuper:123456
[root@server1 ~]# chmod 600 /etc/rsyncd_users.db
[root@server1 ~]# rsync --daemon[root@server1 ~]# netstat -anpt |grep rsynctcp 0 0 192.168.158.10:873 0.0.0.0:* LISTEN 7360/rsync
[root@server1 ~]# yum -y install httpd[root@server1 ~]# echo "this is test web" > /var/www/html/index.html
[root@client1 ~]# rsync -avz backuper@192.168.158.10::wwwroot /optPassword: receiving incremental file list./index.htmlsent 83 bytes received 172 bytes 102.00 bytes/sectotal size is 17 speedup is 0.07[root@client1 ~]# cat /opt/index.html this is test web[root@client1 ~]# rm -rf /opt/index.html [root@client1 ~]# cat /opt/index.html cat: /opt/index.html: 没有那个文件或目录[root@client1 ~]# rsync -avz rsync://backuper@192.168.158.10/wwwroot /optPassword: receiving incremental file list./index.htmlsent 83 bytes received 172 bytes 72.86 bytes/sectotal size is 17 speedup is 0.07[root@client1 ~]# cat /opt/index.html this is test web
[root@client1 ~]# cat /etc/server.pass 123456[root@client1 ~]# rsync -avz --delete --password-file=/etc/server.pass backuper@192.168.158.10::wwwroot /optreceiving incremental file listdeleting rh/./index.htmlsent 83 bytes received 172 bytes 510.00 bytes/sectotal size is 17 speedup is 0.07
定期同步存在一些不足之处,比如:
实时同步的优点
inotify介绍
[root@server1 ~]# vi /etc/rsyncd.conf read only = no
[root@server1 ~]# vim /etc/sysctl.conffs.inotify.max_queued_events = 16384 #监控事件的最大队列数fs.inotify.max_user_instances = 1024 #监控的最大实例数fs.inotify.max_user_watches = 1048576 #监控的每实例的最大文件数加载[root@server1 ~]# sysctl -pfs.inotify.max_queued_events = 16384fs.inotify.max_user_instances = 1024fs.inotify.max_user_watches = 1048576
重新启动rsync服务,注意:kill pid号和kill -9 pid号的区别:后者不会删除pid文件,导致服务起不来
[root@server1 ~]# cd /var/run[root@server1 run]# cat rsyncd.pid9514[root@server1 run]# kill 9514 #删除pid文件[root@server1 run]# ll | grep rsync.pid[root@server1 run]# rsync --daemon[root@server1 run]# ll | grep rsyncd.pid-rw-r--r--. 1 root root 6 11月 12 13:19 rsyncd.pid[root@server1 run]# cat rsyncd.pid9514[root@server1 run]# kill -9 9154 #不删除pid文件,导致启动不了[root@server1 run]# ll | grep rsyncd.pid-rw-r--r--. 1 root root 6 11月 12 13:19 rsyncd.pid[root@server1 run]# rsync --daemon[root@server1 run]# failed to create pid file /var/run/rsyncd.pid: File exists[root@server1 run]# rm -rf rsyncd.pid #删除pid文件再启动[root@server1 run]# rsync --daemon[root@server1 run]# netstat -anpt | grep rsynctcp 0 0 192.168.158.10:873 0.0.0.0:* LISTEN 9666/rsync
inotifywait:用于持续监控,实时输出结果
inotifywatch:用于短期监控,任务完成后再出结果
[root@client1 ~]# tar zxf inotify-tools-3.14.tar.gz [root@client1 ~]# cd inotify-tools-3.14/[root@client1 inotify-tools-3.14]# ./configure [root@client1 inotify-tools-3.14]# make && make install
[root@client1 inotify-tools-3.14]# mkdir -p /var/www/html [root@client1 inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html inotifywait -mrq -e modify,create,move,delete /var/www/html
[root@client1 ~]# cd /var/www/html/[root@client1 html]# touch a.txt[root@client1 html]# vi 1.txt[root@client1 html]# rm -rf 1.txt [root@client1 html]# mv a.txt /opt/[root@client1 html]#
[root@client1 inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html /var/www/html/ CREATE a.txt/var/www/html/ CREATE .1.txt.swp/var/www/html/ CREATE .1.txt.swx/var/www/html/ DELETE .1.txt.swx/var/www/html/ DELETE .1.txt.swp/var/www/html/ CREATE .1.txt.swp/var/www/html/ MODIFY .1.txt.swp/var/www/html/ MODIFY .1.txt.swp/var/www/html/ CREATE 1.txt/var/www/html/ MODIFY 1.txt/var/www/html/ MODIFY .1.txt.swp/var/www/html/ DELETE .1.txt.swp/var/www/html/ DELETE 1.txt/var/www/html/ MOVED_FROM a.txt
[root@client1 inotify-tools-3.14]# vi /opt/inotify.sh#!/bin/bashINOTIFY="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html"RSYNC="rsync -azH --delete --password-file=/etc/server.pass /var/www/html/ backuper@192。168.158.10::wwwroot/"$INOTIFY | while read DIRECTORY EVENT FILE #逐条读取监控记录do if [ $(pgrep rsync | wc -l) -le 0 ];then $RSYNC fidone[root@client1 inotify-tools-3.14]# chmod +x /opt/inotify.sh[root@client1 inotify-tools-3.14]# cd /opt/注:两边的同步目录权限都设置777[root@server1 run]# chmod 777 /var/www/html/[root@client1 html]# chmod 777 /var/www/html/运行脚本[root@client1 opt]# ./inotify.sh
[root@client1 html]# touch a.txt b.txt #客户机查看源端/var/www/html目录,新建的和原来就存在的文件都成功同步,且属主属组均为nobody用户[root@server1 html]# ll #同步源总用量 0-rw-r--r--. 1 nobody nobody 0 11月 12 19:30 a.txt-rw-r--r--. 1 nobody nobody 0 11月 12 19:30 b.txt
使用如下命令进行上行同步(上传)时,本地目录最后要加上/,否则会将目录同步到对方目标文件夹下,而不仅仅是同步本地目录下的文件
[root@client opt]# vi /opt/inotify.sh#!/bin/bashINOTIFY="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html"RSYNC="rsync -azH --delete --password-file=/etc/server.pass /var/www/html backuper@20.0.0.10::wwwroot"$INOTIFY | while read DIRECTORY EVENT FILE #逐条读取监控记录do if [ $(pgrep rsync | wc -l) -le 0 ];then $RSYNC fidone运行脚本[root@client opt]# ./inotify.sh
客户机写入数据[root@client html]# touch e.txtrsync上查看[root@client1 html]# touch f.txt[root@client1 html]# ll总用量 0-rw-r--r--. 1 root root 0 11月 12 19:30 a.txt-rw-r--r--. 1 root root 0 11月 12 19:30 b.txt-rw-r--r--. 1 root root 0 11月 12 19:35 dd.txt-rw-r--r--. 1 root root 0 11月 12 19:35 f.txt#整个目录同步到目标主机上[root@server html]# cd html/[root@server1 html]# ll总用量 0-rw-r--r--. 1 nobody nobody 0 11月 12 19:30 a.txt-rw-r--r--. 1 nobody nobody 0 11月 12 19:30 b.txt-rw-r--r--. 1 nobody nobody 0 11月 12 19:35 dd.txtdrwxrwxrwx. 2 nobody nobody 59 11月 12 19:35 html #整个目录同步到目标主机上
转载地址:http://yfdaf.baihongyu.com/