Rsync基础应用

客户端:向哪端同步就哪端就做客户端
服务端:被客户端同步就做服务端

一.客户端:idv-server    192.168.56.144
1.安装rsync,xinetd

yum install rsync xinetd

2.编辑rsync配置文件

vi /etc/xinetd.d/rsync
disable = no

3.创建认证密码文件

vi /etc/passwd.txt
123456

4.授权

chmod 600 /etc/passwd.txt

5.建立源文件夹&测试

mkdir -p /home/uicc/ceshi
rsync -avH --port=873 --progress --delete  /home/uicc/  jiamin@192.168.56.145::uicc --password-file=/etc/passwd.txt    
说明:使用145节点的rsync服务,将144节点的/home/uicc文件夹下的内容,同步到145节点rsync服务下的uicc模块(设置的目录也为/home/uicc/),此时在145节点的/home/uicc/ 目录下也出现了ceshi文件夹

6.实现实时同步

1)查看服务器内核是否支持inotify
  ll /proc/sys/fs/inotify   #列出文件目录,出现下面的内容,说明服务器内核支持inotify
  -rw-r--r-- 1 root root 0 Mar  7 02:17 max_queued_events
  -rw-r--r-- 1 root root 0 Mar  7 02:17 max_user_instances
  -rw-r--r-- 1 root root 0 Mar  7 02:17 max_user_watches
  备注:Linux下支持inotify的内核最小为2.6.13,可以输入命令:uname -a查看内核
2)修改inotify默认参数(inotify默认内核参数值太小)
  sysctl -w fs.inotify.max_queued_events="99999999"
  sysctl -w fs.inotify.max_user_watches="99999999"
  sysctl -w fs.inotify.max_user_instances="65535"
  vi /etc/sysctl.conf #添加以下代码
  fs.inotify.max_queued_events=99999999
  fs.inotify.max_user_watches=99999999
  fs.inotify.max_user_instances=65535
3)安装sersync
   下载:https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz
   解压:tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz
4)配置sersync
   vi confxml.xml
   修改的地方
   <localpath watch="/home/uicc">
       <remote ip="192.168.56.145" name="uicc"/>
   </localpath>
   <commonParams params="-artuz"/>
   <auth start="true" users="jiamin" passwordfile="/etc/passwd.txt"/>

二.服务端:idv-client  192.168.56.145
1.安装rsync,xinetd

yum install rsync xinetd

2.编辑rsync配置文件

vi /etc/xinetd.d/rsync
disable = no

3.创建编辑rsyncd.conf配置文件

  vi /etc/rsyncd.conf
    log file = /var/log/rsyncd.log #日志文件位置,启动rsync后自动产生这个文件,无需提前创建
    pidfile = /var/run/rsyncd.pid  #pid文件的存放位置
    lock file = /var/run/rsync.lock  #支持max connections参数的锁文件
    secrets file = /etc/rsync.pass  #用户认证配置文件,里面保存用户名称和密码,后面会创建这个文件
    motd file = /etc/rsyncd.Motd  #rsync启动时欢迎信息页面文件位置(文件内容自定义)
    [uicc] #自定义名称
    path = /home/uicc/ #rsync被同步到的服务端数据目录路径
    comment = uicc #模块名称与[home_www.osyunwei.com]自定义名称相同
    uid = root #设置rsync运行权限为root
    gid = root #设置rsync运行权限为root
    port=873  #默认端口
    use chroot = no #默认为true,修改为no,增加对目录文件软连接的备份
    read only = no  #设置rsync服务端文件为读写权限
    list = no #不显示rsync服务端资源列表
    max connections = 200 #最大连接数
    timeout = 600  #设置超时时间
    auth users = jiamin #执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
    hosts allow = 192.168.56.144  #允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
    hosts deny = 192.168.56.254 #禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开

4.创建目录

mkdir /home/uicc/

5.创建编辑用户认证文件

vi /etc/rsync.pass
jiamin:123456

6.设置文件权限

chmod 600 /etc/rsyncd.conf
chmod 600 /etc/rsync.pass

7.启动rsync

/etc/init.d/xinetd start  #启动
service xinetd stop   #停止
service xinetd restart  #重新启动

三.Java调用

package com.luckmin.test.demo;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import com.github.fracpete.rsync4j.RSync;
import com.github.fracpete.processoutput4j.core.StreamingProcessOutputType;
import com.github.fracpete.processoutput4j.core.StreamingProcessOwner;
import com.github.fracpete.processoutput4j.output.StreamingProcessOutput;

public class RsyncTest {

	public static class Output implements StreamingProcessOwner {
		RSync rsync = null;

		public Output(RSync rsync) {
			this.rsync = rsync;
		}

		public StreamingProcessOutputType getOutputType() {
			return StreamingProcessOutputType.BOTH;
		}

		public void processOutput(String line, boolean stdout) {
			System.out.println((stdout ? "[OUT] " : "[ERR] ") + line);
			if(line.indexOf("%")!=-1){ 
			  String pbar = line.substring(16,21).trim();
			  System.out.println(pbar); 
			}
		}
	}

	public static void main(String[] args) throws Exception {
		RSync rsync = new RSync();
		List<String> sourceList = new ArrayList<String>();
		sourceList.add("D:\\rsync\\test1.zip");
		sourceList.add("D:\\rsync\\test2.zip");
		sourceList.add("D:\\rsync\\test3.zip");
		rsync.sources(sourceList);
		rsync.destination("rsync://jiamin@192.168.56.145:873/uicc");
		rsync.passwordFile("D:\\passwd.txt");
		rsync.archive(true);
		rsync.recursive(true);
		rsync.verbose(true);
		rsync.progress(true);
		rsync.partial(true);
		rsync.compress(true);
		rsync.delete(true);
		rsync.humanReadable(true);
		rsync.outputCommandline(true);
		StreamingProcessOutput output = new StreamingProcessOutput(new Output(rsync));
		output.monitor(rsync.builder());
		TimeUnit.SECONDS.sleep(5);
	}

}

 

转载于:https://my.oschina.net/u/4042972/blog/3053761

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值