现在的位置: 首页 > 综合 > 正文

数据同步的利器 – rsync

2017年12月15日 ⁄ 综合 ⁄ 共 1389字 ⁄ 字号 评论关闭

如果我们想在两台机器之间同步数据,怎么办呢?

可以用rsync,remote synchronize。rsync可以增量备份数据,在数据传输的时候只会传输不一样的地方,它利用ssh来传输文件,是一款开源的软件。

rsync分为客户端和服务器。

一般linux机器都会运行一个rsync的daemon,你可以

ps aux | grep rsync

来看是否启动了rsync daemon,如果显示

/bin/rsync --daemon --port=873 --ipv4 --config=/etc/rsyncd.conf

那么恭喜你,此机器已经启动了rsync服务器。剩下的就是用rsync客户端进行数据同步了。


下边是数据下载,从服务器下载到本地:

rsync -avzP  --delete  --password-file=rsyncd.secrets   zhc@192.168.0.131:/home/hongchangfirst /home


把两个数据源换一个方向,就成了上传,记住是从左到有传输就对了。

rsync -avzP  --delete  --password-file=rsyncd.secrets  /home/hongchangfirst  zhc@192.168.0.131:/home


如果想忽略掉其下的build文件夹,可以用--exclude

rsync -avzP  --delete  --exclude 'build' --password-file=rsyncd.secrets  /home/hongchangfirst  zhc@192.168.0.131:/home


上边的参数-a代表了以archive模式操作、复制目录、符号连接等,相当于-rlptgoD,如果想忽略掉link文件,可以把l去掉。


如果想实时监听变化,可以使用sersync,这个是利用操作系统提供的notify机制来监听目录中发生的变化,如增加、删除、修改文件或目录的名字。

先看看操作系统是否支持

ll /proc/sys/fs/inotify

出现

-rw-r--r-- 1 root root 0 Mar 7 09:19 max_queued_events
-rw-r--r-- 1 root root 0 Mar 7 09:19 max_user_instances
-rw-r--r-- 1 root root 0 Mar 7 09:19 max_user_watches

查看系统默认参数

sysctl -a | grep max_queued_events
结果是:fs.inotify.max_queued_events = 16384

sysctl -a | grep max_user

fs.inotify.max_user_watches = 8192

fs.inotify.max_user_instances = 128


max_queued_events代表inotify队列最大长度。

max_user_watches代表最大监听的目录和文件数。

max_user_instances代表每个用户创建inotify实例最大值。

好了,就到这里吧。

原文:http://blog.csdn.net/hongchangfirst/article/details/37905621

作者:hongchangfirst

hongchangfirst的主页:http://blog.csdn.net/hongchangfirst

抱歉!评论已关闭.