参考

rsync主站:https://rsync.samba.org/

rsync安装详解:http://sookk8.blog.51cto.com/455855/328076/


rsync 六种不同的工作模式

1、拷贝本地文件
rsync -a /home/ouyangjun/oa /tmp/oa

2、使用shell程序(如rsh、ssh)实现将本地机器的内容拷贝到远程机器
rsync -avz *.c foo:src

3、使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器
rsync -avz foo:src/bar /data

4、从远程rsync服务器中拷贝文件到本地机
rsync -av root@192.168.78.192::www /databack

5、从本地机器拷贝文件到远程rsync服务器中
rsync -av /databack root@192.168.78.192::www

6、列远程机的文件列表。这类似于rsync传输,不过只要在命令中省略掉本地机信息即可。
rsync -v rsync://192.168.78.192/www



配置rsync服务器

less /etc/xinetd.d/rsync

# default: off

# description: The rsync server is a good addition to an ftp server, as it \

# allows crc checksumming etc.

service rsync

{

    disable= no

    flags= IPv6

    socket_type     = stream

    wait            = no

    user            = root

    server          = /usr/bin/rsync

    server_args     = --daemon

    log_on_failure  += USERID

}

开机启动
#chkconfig rsync on


可以看到rysnc服务是关闭的(disable = yes),这里把它开启,把disable的值改为no


创建rsync服务器配置文件/etc/rsyncd.conf


vim /etc/rsyncd.conf

全局参数


uid = root    //运行守护进程的用户

gid = root    //运行守护进程的用户组

use chroot = no                 //不使用chroot

max connections = 4             // 最大连接数为4

strict modes =yes                //是否检查口令文件的权限

port = 873                      //默认端口873

path=/data/oa

read only=no

hosts allow=10.240.180.133

注释:下面这是安装完RSYNC服务后自动生成的文件
pid file = /var/run/rsyncd.pid      //pid文件的存放位置
lock file = /var/run/rsync.lock     //锁文件的存放位置
log file = /var/log/rsyncd.log      //日志记录文件的存放位置


模块参数
[oa]                   //这里是认证的模块名,在client端需要指定
path = /home/oa/        //需要做镜像的目录,不可缺少!
comment = OA backup       //这个模块的注释信息
ignore errors                //可以忽略一些无关的IO错误
read only = yes              // 只读
list = no                   //不允许列文件
auth users = ouyangjun    //认证的用户名,如果没有这行则表明是匿名,此用户与系统无关
secrets file = /etc/rsync.pas           //密码和用户名对比表,密码文件自己生成
hosts allow = 192.168.1.1,10.10.10.10      //允许主机
hosts deny = 0.0.0.0/0                   //禁止主机

[oa]
uid = nobody
gid = nobody
path=/var/oa
read only=no
hosts allow=192.169.0.1


配置rsync密码(在上边的配置文件中已经写好路径)

/etc/rsync.pas(名字随便写,只要和上边配置文件里的一致即可),格式(一行一个用户)
账号:密码
 vim /etc/rsync.pas
例子:
Hening:111111
权限:因为rsync.pas存储了rsync服务的用户名和密码,所以非常重要。要将rsync.pas设置为root拥有, 且权限为600。

cd /etc
sudo chown root.root rsync.pas
sudo chmod 600 rsync.pas


rsyncd.motd(配置欢迎信息,可有可无)
vim /etc/rsyncd.motd
rsyncd.motd记录了rsync服务的欢迎信息,你可以在其中输入任何文本信息,如:
Welcome to use the rsync services!


让配置生效
#service xinetd restart


//查看是否rsync启动

ps -ef | grep 'rsync'


//服务器端启动rsync

/usr/bin/rsync –daemon    //独立启动

/etc/rc.d/init.d/xinetd reload    //有xinetd超级进程启动


设置开机启动:

将/usr/local/rsync --daemon 2>>/var/log/startup_error.log其添加到 /etc/rc.d/rc.local中


如果服务器上装有防火墙,需在服务器中设置iptables将837端口开放。


查看已添加的iptables规则

iptables -L -n -v

iptables -L -n -v --line-number

开放指定端口

iptables -A INPUT -p tcp --dport 873 -j ACCEPT

iptables -I INPUT 10 -p tcp -m state --state NEW -m tcp --dport 873 -j ACCEPT

iptables -I INPUT 10 -p udp -m state --state NEW -m udp --dport 873 -j ACCEPT


为防重启将规则清除,我将规则直接加到规则配置文件里边了(/etc/sysconfig/iptables),如下:
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT


//防火墙重启

service iptables restart

chkconfig iptables on


//查看防火墙状态

/etc/init.d/iptables status


//删除input里需要为6的规则

iptables -D INPUT 6