sync详解

sync概述:

rsync是一个提供快速增量文件传输的开源工具。rsync在GNU通用公共许可证下免费提供,目前由Wayne Davison维护。传输前进行压缩,适合做备份使用。

命令格式

rsync [-参数] 源目录 目的目录

-r 递归模式,包含目录及子目录中的所有文件

-l(小写L) 对于符号链接文件仍然复制为符号链接文件

-v 显示同步过程中的详细(verbose)信息

-z 在传输文件时进行压缩(compress)

-a 归档模式,保留文件的权限,属性等信息,等同于组合选项"-rlptgoD"

-p 保留文件的权限标记

-t 保留文件的时间标记

-g 保留文件的属组标记(仅超级用户使用)

-o 保留文件的属主标记(仅超级用户使用)

-H 保留硬链接文件

-A 保留ACL属性信息

-D 保留设备文件及其他特殊文件

-S(--sparce) 处理稀疏文件时使用

--delete 删除目录位置有,而原始位置没有的文件。

--checksum 更具校验和(而不是文件大小,修改时间)来决定是否跳过文件.

--password-flie= 指定密码文件

本地传输文件 目录后如果没有加/(/test/)是代表传输整个目录 如果带/(/text)是传输目录下所有文件

systemctl  stop  firewalld
[root@shell-test ~]# rsync -a ./ /opt/
[root@shell-test ~]# cd /opt/
[root@shell-test opt]# ll
total 16
-rw------- 1 root root 1244 Feb 20 01:50 anaconda-ks.cfg
-rw-r--r-- 1 root root 3671 Feb 20 03:20 instalization.sh
drwxr-xr-x 4 root root 92 Aug 1 10:42 script
-rw-r--r-- 1 root root 100 Jul 1 05:24 test.sh
-rw-r--r-- 1 root root 16 Aug 10 11:31 test.txt
[root@shell-test opt]# rsync -a /opt /opt/
[root@shell-test opt]# cd /opt/
[root@shell-test opt]# ll
total 4
dr-xr-x--- 6 root root 198 Aug 10 22:19 opt
drwxr-xr-x 4 root root 92 Aug 1 10:42 script
-rw-r--r-- 1 root root 16 Aug 10 11:31 test.txt

配置rsync同步服务器

rsync+ssh同步数据

systemctl  stop  firewalld
setenforce 0

服务端配置

修改配置文件rsync.conf

uid = nobody
gid = nobody
use chroot = yes #禁锢在源目录
address = 192.168.23.104 #监听地址
port 873 #监听端口(UDP/TCP 873端口)
log file = /var/run/rsyncd.log #日志文件位置
pid file = /var/run/rsyncd.pid #存放进程pid文件的位置
hosts allow = 192.168.23.0/24 #允许访问的客户机
[wwwroot] #模块名称
path = /var/www/html #源目录的实际路径
comment = Document Root of web server #描述信息
read only = yes #设置为只读
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 #同步时不再压缩的文件类型
auth users = backuper #授权用户,多个账户用空格分隔
secrets file = /etc/rsyncd_users.db #设置存放账户信息的数据文件

创建账户

vim /etc/rsyncd_users.db
#rsync 的认证用户和密码。不用创建同名的系统用户
backuper:abc123
chomod 600 rsync_users.db

启动服务

rsync --daemon
ss -tnap|grep rsync
[root@shell-test opt]# ss -natp |grep rsync
LISTEN 0 5 192.168.211.30:873 *:* users:(("rsync",pid=5540,fd=3))

客户端验证

rsync -azv backuper@192.168.211.30::wwwroot #IP地址后面是双冒号之后跟共享模块名称
将指定文件目录文件同步,注意客户端要有相对应的目录
[root@ansible-1 ~]# rsync -azv backuper@192.168.211.30::wwwroot /var/www/html
Password:
receiving incremental file list
sent 20 bytes received 34 bytes 15.43 bytes/sec
total size is 0 speedup is 0.00

免交互定时同步数据

#创建文件,将密码写入文件中
echo "abc123" > /etc/server.pass
#设置权限,防止密码文件被其他用户篡改
chmod 600 /etc/server.pass
crontab -e
*/1 * * * /usr/bin/rsync -az --delete --password-file=/etc/server.pass backuper@192.168.211.30::wwwroot /var/www/html
#每天1:30 j进行备份,将源服务器的wwwroot模块下文件备份到本地的 /opt/目录下
#--deleete 删除源服务器没有,而本地有的文件
#--password-file= 指定保存rsync 用户backuper密码的文件,以实现免交互
[root@host103 opt]# systemctl restart crond
[root@host103 opt]# systemctl enable crond

rsync + inotify 机制实现实时同步

将inotify机制与rsync工具相结合,可以实现触发式备份(实时同步),即只要原始位置的文档发生变化,则立即启动增量备份操作,否则处于静默等待状态。这样,就避免了按固定周期备份时存在的延迟性、周期过密等问题。

[root@host104 ~]# vim /etc/rsyncd.conf
........
#uid和gid 修改问目录/var/www/html 属组和属主
uid = root
gid = root
#关闭只读,上行同步需要可以写。
read only = no
...... #关闭rsync 服务
kill $(cat /var/run/rsyncd.pid)
rm -rf /var/run/rsyncd.pid #启动rsync服务
rsync --daemon
netstat -natp | grep rsync
tcp 0 0 192.168.211.30:873 0.0.0.0:* LISTEN 83003/rsync #修改目录权限
chmod 777 /var/www/html/

发起内核参数调整

在Linux内核中,默认的inotify机制提供了三个调控参数∶max_queue_events(监控事件队列,默认值为16384)、max_user_instances(最多监控实例数,默认值为128)、max_user_watches(每个实例最多监控文件数,默认值为8192)。当要监控的目录、文件数量较多或者变化较频繁时,建议加大这三个参数的值。
cat /proc/sys/fs/inotify/max_queued_events
16384
cat /proc/sys/fs/inotify/max_user_instances
128
cat /proc/sys/fs/inotify/max_user_watches
8192 vim /etc/sysctl.conf
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576 sysctl -p

发起端需要安装 inotify-tools

用 inotify 机制还需要安装inotify-tools,以便提供 inotifywait、inotifywatch 辅助工具程序,用来监控、汇总改动情况。

inotifywait∶可监控modify(修改)、create(创建)、move(移动)、delete(删除)、attrib(属性更改)等各种事件,一有变动立即输出结果。

inotifywatch∶可用来收集文件系统变动情况,并在运行结束后输出汇总的变化情况

cd /opt/
tar xf inotify-tools-3.14.tar.gz -C /opt/ #wget  http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
cd /opt/inotify-tools-3.14/
./configure
make && make install

测试执行结果

inotifywait -mrq -e modify,create,move,delete /var/www/html
再开一个终端 [root@ansible-1 ~]# cd /var/www/html/
You have new mail in /var/spool/mail/root
[root@ansible-1 html]# ll
total 0
-rw-r--r-- 1 root root 0 Aug 11 22:24 index1.html
[root@ansible-1 html]# touch a.txt
[root@ansible-1 html]# touch b.txt
[root@ansible-1 html]# mv a.txt /opt
[root@ansible-1 html]# echo "abcd" b.txt
abcd b.txt
You have new mail in /var/spool/mail/root
[root@ansible-1 html]# ll
total 0
-rw-r--r-- 1 root root 0 Aug 15 01:39 b.txt
-rw-r--r-- 1 root root 0 Aug 11 22:24 index1.html

客户端脚本编写

[root@host103 opt]# vim /opt/inotify.sh
Path=/var/www/html/
backup_Server=192.168.211.30
inotifywait -mrq --format '%w%f' -e create,close_write,delete /var/www/html/ | while read line
do
if [ -f $line ];then
rsync -az $line --delete backuper@$backup_Server::wwwroot --password-file=/etc/server.pass
else
cd $Path &&\
rsync -az --delete backuper@$backup_Server::wwwroot --password-file=/etc/server.pass
fi
done
[root@host103 opt]# chmod +x /opt/inotify.sh
[root@host103 opt]# chmod 777 /var/www/html/
[root@host103 opt]# chmod +x /etc/rc.d/rc.local
[root@host103 opt]# echo '/opt/inotify.sh' >> /etc/rc.d/rc.local

创建文件进行测试如果同步成功则可以使用了

sync同步工具使用的更多相关文章

  1. 文件同步工具BT Sync介绍和使用说明

    BT Sync介绍 BT 下载,相信大伙儿都知道的.今儿个要介绍的 BT Sync,跟 BT 下载一样,都是 BitTorrent 公司发明滴玩意儿,都是采用 P2P 协议来进行传输. 简而言之,BT ...

  2. Microsoft SyncToy 文件同步工具

    Microsoft SyncToy SyncToy 是由 微软 推出的一款免费的文件夹同步工具.虽然名字中有一个 Toy,但是大家可千万不要误以为它的功能弱爆了.实际上,我感觉这款软件还真是摆脱了微软 ...

  3. MySQL表结构同步工具 mysql-schema-sync

    mysql-schema-sync 是一款使用go开发的.跨平台的.绿色无依赖的 MySQL 表结构自动同步工具.用于将线上(其他环境)数据库结构变化同步到测试(本地)环境! 可以解决多人开发,每人都 ...

  4. Linux下同步工具inotify+rsync使用详解

    1. rsync 1.1 什么是rsync rsync是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.它使用所谓的“Rsync演算法”来使本地和远程两个主机之间的文件达到同步,这 ...

  5. C#同步SQL Server数据库中的数据--数据库同步工具[同步新数据]

    C#同步SQL Server数据库中的数据 1. 先写个sql处理类: using System; using System.Collections.Generic; using System.Dat ...

  6. 并发是个什么鬼之同步工具类CountDownLatch

    扯淡 写这篇文章,我先酝酿一下,实不相瞒,脱离底层太久了,更确切的情况是,真没曾认真研究过.就目前来说,很多框架包括工具类已经把实现封装的很深,你只需轻轻的调用一下API,便不费半点力气.以至于大家会 ...

  7. rsync - 远程同步工具

    一直没有对这个命令太有深入的理解 简介 rsync 即 remote sync,一个远程与本地文件同步工具.rsync 使用的算法能够最小化所需复制的数据,因为它只移动那些修改了的文件. rsync ...

  8. Lsyncd - 实时文件同步工具(精译)

    原文: http://axkibe.github.io/lsyncd/ 描述 Lsyncd监视本地目录树事件监视器接口(inotify或fsevents).它聚集并组合事件几秒钟,然后生成一个(或多个 ...

  9. 非常好用的sersync同步工具

    作者:邓聪聪 常用同步工具sync的进阶软件 服务端的配置: uid = rsync gid = rsync port = use chroot = on max connections = time ...

  10. [cmd] rsync - 远程同步工具

    简介 rsync 即 remote sync,一个远程与本地文件同步工具.rsync 使用的算法能够最小化所需复制的数据,因为它只移动那些修改了的文件. rsync 是一个非常灵活的同步工具,它也是一 ...

随机推荐

  1. 提交docker镜像到远程仓库

    生成镜像 Docker build 镜像 编辑Dockerfile文件 新建Dockerfile文件,将如下构建脚本复制进去 # Build for ansible envirament FROM c ...

  2. 1 wine-stable + 2 brew install mono

    一. 通过wine官网找到安装方法 1  brew tap homebrew/cask-versions2  brew install --cask --no-quarantine (selected ...

  3. zabbix5.2+mysql+ubuntu20.4

    服务端 0.初始化机器 1.mysql安装 # apt-get install mysql-server # apt update 根据提示一步一步确认,要求输入的密码是创建管理员的密码 2.安装za ...

  4. vim多视窗操作

    命令行模式下输入:sp 1.txt 打开新的水平分屏视窗来编辑1.txt 命令行模式下输入:vsp 2.txt 打开新的垂直分屏视窗来编辑2.txt 普通模式下Ctrl+w s 将当前窗口分割成两个水 ...

  5. VSCode搭建Go语言环境

    一.安装go 1. 获取go安装包   https://golang.org/dl/ 2. 本地安装(省略) 3. 配置和环境变量 GO111MODULE 是否支持gomod GOROOT go安装的 ...

  6. 吴恩达老师机器学习课程chapter07——聚类

    吴恩达老师机器学习课程chapter07--聚类 本文是非计算机专业新手的自学笔记,高手勿喷. 本文仅作速查备忘之用,对应吴恩达(AndrewNg)老师的机器学期课程第十三章. 缺少重要推演,只能作为 ...

  7. 看K线学炒股(8.9)

    今天是食品饮料类题材大涨的一天,相应板块涨了6个多点,看着真是诱人.我以前关注的两只股票:海天味业和三全食品,今天都大涨.三全食品接近涨停.这种票容易选出来但也很难拿住.比如前些天买入了,结果8.5的 ...

  8. SpringBoot 配置内部tomcat https双向验证

    1.在application.properties或者application.yml配置文件中加入 server: port: 8443 ssl: key-store: classpath:xxxx. ...

  9. feign的工作原理

    1.开发微服务时,我们会在微服务的主程序入口添加EnableFignClient注解开启对Feign Client扫描加载处理,根据FignClient接口规范,定义接口并加上FignClient注解 ...

  10. js - class 操作

    js - class 操作 // 添加 function addClass(dom, classNameString = '') { if (!dom.className.length) dom.cl ...