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. Linux装cudnn

    https://stackoverflow.com/questions/66977227/could-not-load-dynamic-library-libcudnn-so-8-when-runni ...

  2. 调度器30—调度相关结构体—p->flags

    一.PF_EXITING 1. 赋值路径 各驱动和内核机制中直接调用 SYSCALL_DEFINE1(exit, int, error_code) //exit.c do_exit(code); // ...

  3. OC基础 - iOS在枚举赋值时为何采用左移格式

    枚举值 1 - iOS 枚举成员在赋值时往往是如下模式:左移对齐 2 - 为何这般设计 ?其一提高了阅读性:其二便于计算,能够更好的表达枚举值的含义 1 #import "ViewContr ...

  4. NSQ(7)-nsq存在的问题

    nsq存在的缺陷 部署的难度 ​ nsq提供了一种消费者端进行服务发现的模型,所以无需告诉消费者去哪寻找对于的主题(Topic)在哪个nsqd实例上. ​ 然而,它并没有提供一种方案去解决一个生产者应 ...

  5. ERROR 1862 (HY000): Your password has expired. To log in you must change it using a .....

    navcat 登录本地失败 https://blog.csdn.net/fenniang16/article/details/81216602?spm=1001.2101.3001.6661.1&am ...

  6. 《Spring Boot从零开始学(视频教学版)》快速入门Spring Boot应用开发

    #好书推荐##好书奇遇季#<Spring Boot从零开始学(视频教学版)>,目前为止较好的一本Spring Boot入门书.京东当当天猫都有发售.本书配套示例代码.课件与教学视频.定价7 ...

  7. pgsql的round函数

    不知道是我菜还是咋地,感觉pg里面用round不是很爽啊,明明在其他库能运行的,字段类型卡得太死了吧 照说float8类型还是数值,怎么就报错呢,如下 错误:  函数 round(double pre ...

  8. 清空kafka全部数据

    1.停止机器上的kafka,停止业务系统 docker容器执行命令: docker stop 容器名称 2.删除kafka存储目录(server.properties文件log.dirs配置,默认为& ...

  9. body传参和query传参

    get请求只能传query参数,query参数都是拼接在请求地址上的.post可以传body和query两种形式的参数. get请求在url中传送的参数是有长度限制的,而post没有限制.get比po ...

  10. MySQL的架构与历史1.5MySQL的存储引擎

    1.5 MySQL的存储引擎 1.5.1 InnoDB存储引擎 InnoDB是 MySQL的默认事务型引,也是最重要.使用最广泛的存储引擎.它被设计用来处理大量的短期 (short-lived)事务, ...