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. [vsCode]Visual Studio Code环境变量不同步

    很奇怪的事情,在设置好Path环境变量后,powershell正常了, 但是vscode不正常 解决方法竟然是: 确认{在PowerShell运行正常}后,关闭{vsCode},进入{管理员模式的Po ...

  2. satpy 处理卫星 FY4A 数据

    读取数据并画图 import os import glob from datetime import datetime, timedelta from satpy.scene import Scene ...

  3. Salesforce Connect 连接两个不同的Org(实际设置方法)

    利用Salesforce的标准功能:Salesforce Connect,可以轻松的将两个组织(Org)连接起来.实现Object的共享(包括参照和编辑). 要求: ①两个组织必须是开发者Edtion ...

  4. 二.navicate

    navicat -创建 -新建查询 -转储sql文件命令: 转储当前目录所有的文件与数据:mysqldump -u root db4 > db4.sql -p 转储当前目录到表结构没有数据:my ...

  5. Treetop Lights使用条款与免责协议

    Treetop Lights (以下简称"我们")在此特别提醒您务必认真阅读.充分理解本<使用条款与免责协议>(以下简称"本协议")中各条款并选择是 ...

  6. 备份还原ESXi 中的虚拟机快照

    了解 ESXi 中的虚拟机快照 (1015180) Click here to view full document Symptoms 免责声明:本文是 Understanding VM snapsh ...

  7. x264码率控制

    1. x264 1.1 preset 的参数主要调节编码速度和质量的平衡,有ultrafast.superfast.veryfast.faster.fast.medium.slow.slower.ve ...

  8. 智能风控:python金融风险管理与评分卡建模

    Ch1 互联网金融的申请过程,通常由用户从移动端发起,首次贷款用户会经理申请.四要素验证.授信与额度利率定价.多层审批.用户提款等多个环节:对于还款后再次贷款的复贷客户,平台通常会基于更好的信用评估结 ...

  9. PCB Layout之EMMC_Flash走线总结@@@

    PCB Layout之EMMC_Flash走线总结 1,数据线DATA[0-7]走线要(基本)等长(含芯片内部线长),线要短,线间距控制3W原则,参考面要完整(参考面下面最好不要走其它高速信号线),阻 ...

  10. centos8.5安装kvm及kvm虚拟机的端口映射问题

    1.安装KVM   grep -Ei 'vmx|svm' /proc/cpuinfo|more  #查看硬件是否支持虚拟化 yum install -y virt-* libvirt qemu-img ...