CentOS下使用rsync实现定时复制功能

CentOS下使用rsync实现定时复制功能,第1张

一、下载、安装rsync
**** 正常安装RH的时候是带RSYNC的,运行文件放在/usr/bin/rsync
#tar zxvf rsync-2.6.9.tar.gz
#cd rsync-2.6.9
#./configure --prefix=/usr/local/rsync
#make
#make install
二、配置rsync server
1、启动RSYNC
#vi /etc/xinetd.d/rsync
  把disable原来的YES改成NO
service rsync
{
        disable = no
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/bin/rsync
        server_args     = --daemon
        log_on_failure  += USERID
}
随系统启动RSYNC
     #chkconfig rsync on

2、配置/etc/rsyncd.conf(需要手动生成)
rsyncd.conf的参数写在上边就是全局参数和写在模块里的就是模块参数
#vi /etc/rsyncd.conf
全局参数
uid = root                                  #运行RSYNC守护进程的用户
gid = root                                  #运行RSYNC守护进程的组
use chroot = no                 #不使用chroot
max connections = 4             # 最大连接数为4
strict modes =yes                #是否检查口令文件的权限
port = 873                      #默认端口873
          
模块参数
[backup]                   #这里是认证的模块名,在client端需要指定
path = /home/backup/        #需要做镜像的目录,不可缺少!
comment = This is a test       #这个模块的注释信息
ignore errors                #可以忽略一些无关的IO错误
read only = yes              # 只读
list = no                   #不允许列文件
auth users = hening             #认证的用户名,如果没有这行则表明是匿名,此用户与系统无关
secrets file = /etc/rsyncd.pw           #密码和用户名对比表,密码文件自己生成
hosts allow = 192.168.1.1,10.10.10.10      #允许主机
hosts deny = 0.0.0.0/0                   #禁止主机
#transfer logging = yes
注释:下面这些绿色文件是安装完RSYNC服务后自动生成的文件
pid file = /var/run/rsyncd.pid      #pid文件的存放位置
lock file = /var/run/rsyncd.lock     #锁文件的存放位置
log file = /var/log/rsyncd.log      #日志记录文件的存放位置

==============实例====================
uid = nobody
gid = nobody
use chroot = no
max connections = 4
stirict modes = yes
port = 873
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
[html]
path = /home/html/
comment = This is a html
ignore errors
read only = no
list = no
hosts allow = 192.168.152.155,10.10.10.10
hosts deny = 0.0.0.0/0
auth users = devilzy
secrets file =/etc/rsyncd.pw
[index]
path = /home/index/
comment = This is a index
ignore errors
read only = no
list = no
hosts allow = 192.168.152.155
hosts deny = 0.0.0.0/0
auth users = devilzy
secrets file =/etc/rsyncd.pw
==============实例====================
 
3、配置rsync密码(在上边的配置文件中已经写好路径) /etc/rsyncd.pw(名字随便写,只要和上边配置文件里的一致即可),格式(一行一个用户)
账号:密码
  #vi /etc/rsyncd.pw
例子:
Hening:111111
权限:因为rsyncd.pw存储了rsync服务的用户名和密码,所以非常重要。要将rsyncd.pw设置为root拥有, 且权限为600。
#cd /etc
#chown root.root rsyncd.pw
#chmod 600 rsyncd.pw
3.rsyncd.motd(配置欢迎信息,可有可无)
# vi /etc/rsyncd.motd
rsyncd.motd记录了rsync服务的欢迎信息,你可以在其中输入任何文本信息,如:
Welcome to use the rsync services!
4、让配置生效
#service xinetd restart
===============出现问题================================
1.xinetd: unrecognized service
执行如下 *** 作:
yum -y install xinetd
===============出现问题================================
三、启动rsync server
  RSYNC服务端启动的两种方法
1、启动rsync服务端(独立启动)
#/usr/bin/rsync --daemon
2、启动rsync服务端 (有xinetd超级进程启动)
# /etc/rc.d/init.d/xinetd reload
3.重启方法:kill -9 rsync

四:加入rc.local
在各种 *** 作系统中,rc文件存放位置不尽相同,可以修改使系统启动时把rsync --daemon加载进去。
#vi /etc/rc.local
加入一行/usr/bin/rsync --daemon
五.检查rsync
#netstat -a | grep rsync
   tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN
六、开启端口 iptables
iptables -A INPUT -p tcp -s ! 192.168.152.155 --dport 873 -j DROP
如此, 只有 11.22.33.44 这个 client IP 能进入这台 rsync server.


七.配置rsync client
1、设定密码
#vi /etc/rsyncd.pw
111111
修改权限
#cd /etc
#chown root.root rsyncd.pw
#chmod 600 rsyncd.pw
2、client连接SERVER
  从SERVER端取文件
/usr/bin/rsync -vzrtopg --progress --delete [email protected]::index /home/index --password-file=/etc/rsyncd.pw
/usr/bin/rsync -vzrtopg --progress --delete [email protected]::html /home/html --password-file=/etc/rsyncd.pw
向SERVER端上传文件
/usr/bin/rsync -vzrtopg --progress --password-file=/root/rsyncd.pw  /home/index [email protected]::index
/usr/bin/rsync -vzrtopg --progress --password-file=/root/rsyncd.pw  /home/html [email protected]::html
这个命令将把本地机器/home/backup目录下的所有文件(含子目录)全部备份到RSYNC SERVER(172.20.0.6)的backup模块的设定的备份目录下。
请注意如果路径结束后面带有"/",表示备份该目录下的东东,但不会创建该目录,如不带"/"则创建该目录。
RSYNC用法:
       rsync [OPTION]... [USER@]HOST::SRC  [DEST]              #从RSYNC SERVER备份文件到本地机器
    rsync [OPTION]... SRC [SRC]...      [USER@]HOST::DEST   #从本地机器备份文件到RSYNC SERVER
3、自动运行
1)vi /usr/local/rsync/time.sh     //制作脚本文件
把下边的内容复制进去
#!/bin/bash
/usr/bin/rsync -vzrtopg --progress --delete [email protected]::index /home/index --password-file=/etc/rsyncd.pw
/usr/bin/rsync -vzrtopg --progress --delete [email protected]::html /home/html --password-file=/etc/rsyncd.pw

#chmod +x /usr/local/rsync/time.sh
2) crontab -e
加入* * * * * /usr/local/rsync/time.sh        //每隔一分钟运行一次time.sh脚本文件
加入55 * * * * /usr/local/rsync/time.sh        //每到某个小时的55分钟就运行一次time.sh脚本文件(每隔一个小时运行一次)


命令介绍:-rvlHpogDtS
rsync命令参数
-v表示verbose详细显示
-z表示压缩
-r表示recursive递归
-t表示保持原文件创建时间
-o表示保持原文件属主
-p表示保持原文件的参数
-g表示保持原文件的所属组
-a存档模式
-P表示代替-partial和-progress两者的选项功能
-e ssh建立起加密的连接。
--partial阻止rsync在传输中断时删除已拷贝的部分(如果在拷贝文件的过程中,传输被中断,rsync的默认 *** 作是撤消前 *** 作,即从目标机上
删除已拷贝的部分文件。)
--progress是指显示出详细的进度情况
--delete是指如果服务器端删除了这一文件,那么客户端也相应把文件删除,保持真正的一致。
--exclude不包含/ins目录
--size-only 这个参数用在两个文件夹中的差别仅是源文件夹中有一些新文件,不存在重名且被修改过的文件,因为这种文件有可能会因为内容被修改可大小一样,而被略过。这个参数可以大大地提高同步的效率,因为它不需要检查同名文件的内容是否相同。
--password-file来指定密码文件,内容包含server端指定认证用户的密码。
这样就可以在脚本中使用而无需交互式地输入验证密码了,这里需要注意的是这份密码文件权限属性要设得只有属主可读。
[email protected]::backup
hening是指server端指定认证的用户
192.168.0.217是指服务器端的ip
::backup 表示服务器端需要同步的模块名称;
/home/quack/backup/$DATE是同步后的文件指存放在本机的目录地址。
/var/log/rsync.$DATE是同步后的日志文件存放在本机的目录地址。
注意
不放/  则目录名也包含mirror,放 / 则只有目录里面的东西mirror了



实例总结流程:
1.配置主控端
# vim /etc/rsyncd.conf
###################################
uid = nobody
gid = nobody
use chroot = no
max connections = 4
stirict modes = yes
port = 873
[html]
path = /home/html
comment = This is a html
ignore errors
read only = false
list = no
hosts allow = 192.168.152.*
hosts deny = 0.0.0.0/0
auth users = devilzy
secrets file =/etc/rsyncd.pw
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
[index]
path = /home/index
comment = This is a index
ignore errors
read only = false
list = no
hosts allow = 192.168.152.*
hosts deny = 0.0.0.0/0
auth users = devilzy
secrets file =/etc/rsyncd.pw
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
###################################
# vim /etc/rsyncd.pw
###################################
devilzy:123456
###################################
# cd /etc
# chown root.root rsyncd.pw
# chmod 600 rsyncd.pw
启动rsync server
# rsync --daemon
查看端口873是否打开
加入启动
# echo "rsync --daemon" >>/etc/rc.local
给/usr/local/test目录写权限
# chown -R nobody.nobody /usr/local/test
# chmod -R 770 /usr/local/test
主控配置完成
2.客户端配置
# vim /etc/rsyncd.pw
####################################
123456
####################################
# chown root.root /etc/rsyncd.pw
# chmod 600 /etc/rsyncd.pw
再使用命令直接更新到服务器数据文件就不需要密码
/usr/bin/rsync -vzrtopg --progress --password-file=/root/rsyncd.pw  /home/index [email protected]::index
/usr/bin/rsync -vzrtopg --progress --password-file=/root/rsyncd.pw  /home/html [email protected]::html
注意:
1.这里的index名字为主控conf配置里面的[index],一定要同名
2.devilzy为主控conf配置里面的devilzy,可以随意命名,不是系统用户



==================遇到的问题和解决办法========================================
1. @ERROR: chdir failed
rsync error: error starting client-server protocol (code 5) at main.c(1296) [receiver=2.6.8]
原因及解决办法:
SELinux;
setsebool -P rsync_disable_trans on
==================遇到的问题和解决办法========================================

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/2090092.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-07-22
下一篇 2022-07-22

发表评论

登录后才能评论

评论列表(0条)

保存