多台linux主机可以自动同步,rsync可以实现数据的备份,rsync可以和cront配合实现定时备份,但要实现数据的实时备份,rsync还不能实现,rsync需要结合inotify可以实现。rsync可以备份linux中的任何数据,这里用备份网站服务器为例来说明rsync功能。rsync要运行在内核2.6以上。
实验环境:3台rhel5.6虚机
实验过程:将一台linux web机作为源机器,即需要备份的机器,剩下的机器作为web目标机器,即要将生产线上源机器数据备份下来的机器
目标机配置:
[root@mubiao ~]# uname -r
2.6.18-238.el5 ###内核版本为2.6以上,支持rsync
[root@mubiao ~]# rpm -qa rsync
rsync-2.6.8-3.1 ###系统已经自动安装rsync软件包
[root@mubiao ~]# cat /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 ###系统默认值为yes,将yes改为no,即打开rsync服务
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
[root@mubiao ~]# service xinetd restart ###重启xinetd服务,使更改的rsync服务生效
[root@mubiao ~]# cat /etc/services |grep rsync ###查看rsync服务端口号 (873)
rsync873/tcp# rsync
rsync873/udp# rsync
airsync2175/tcp# Microsoft Desktop AirSync Protocol
airsync2175/udp# Microsoft Desktop AirSync Protocol
[root@mubiao ~]# lsof -i :873 ###查看rsync服务是否开启
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
xinetd 4080 root 5u IPv4 13922 TCP *:rsync (LISTEN)
[root@mubiao ~]# ls /etc/yum.repos.d/ ###配置yum源
haha.repo
[root@mubiao ~]# cat /etc/yum.repos.d/haha.repo
[h]
baseurl=file:///media/Server
gpgcheck=0
[root@mubiao ~]# yum install httpd -y
[root@mubiao ~]# cat /etc/rsyncd.conf ###新建rsyncd.conf文件并配置以下内容
uid=root
gid=root
use chroot=no
max connections=5
log file=/var/log/rsyncd.log
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
read only=no
list=yes
[web]
comment=web code dir
path=/var/www/html
[root@mubiao ~]# service xinetd restart
到这里,目标机器基本上配置完成,下面配置源主机:
源主机配置:
[root@yuan ~]# cat /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 ###系统默认值为yes,将yes改为no,即打开rsync服务
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
[root@yuan ~]# service xinetd restart ###重启xinetd服务,使更改的rsync服务生效
源主机的配置只需这一步即可,下面进行检测:
在进行检测(推送/拉取)时,要在源主机上操作,而不是在目标主机上操作。
1)在源主机上向目标主机做推送操作:
[root@yuan ~]# ls
anaconda-ks.cfg install.log install.log.syslog this is source_1 yuan_1.txt
[root@yuan ~]# rsync -aRHv yuan_1.txt 192.168.65.151::web
building file list ... done
yuan_1.txt
sent 91 bytes received 38 bytes 258.00 bytes/sec
total size is 0 speedup is 0.00
####在源主机上显示推送成功
[root@mubiao html]# pwd
/var/www/html
[root@mubiao html]# ls
index.html mubiao.txt yuan_1.txt
###在目标主机的/var/www/html网站目录下发现刚刚推送的文件yuan_1.txt,说明推送成功。
2)在源主机上向目标主机做拉取操作:
[root@yuan ~]# ls
anaconda-ks.cfg install.log install.log.syslog this is source_1 yuan_1.txt
[root@yuan ~]# rsync -aRHv 192.168.65.151::web/index.html ./
receiving file list ... done
index.html
sent 102 bytes received 130 bytes 464.00 bytes/sec
total size is 7 speedup is 0.03
[root@yuan ~]# ls
anaconda-ks.cfg index.html install.log install.log.syslog this is source_1 yuan_1.txt
####发现在源主机的当前目录上面获取到了目标机上的index.html文件,表明操作成功。
下面用第三台linux主机向目标机做推送或拉取实验:
[root@localhost ~]# ls
anaconda-ks.cfg Desktop install.log install.log.syslog ora.txt
[root@localhost ~]# rsync -aRHv 192.168.65.151::web/index.html ./
receiving file list ... done
index.html
sent 102 bytes received 130 bytes 464.00 bytes/sec
total size is 7 speedup is 0.03
[root@localhost ~]# ls
anaconda-ks.cfg Desktop index.html install.log install.log.syslog ora.txt
发现第三台linux机器也可以从目标机上拉取数据,即此时,任一台机器都可以从目标机上面拉取数据,由于目标机被模拟成线上web服务器,此时的配置对服务器很不安全,所以要对目标机的/etc/rsyncd.conf配置文件修改:
添加下面最后两行:
[root@mubiao ~]# cat /etc/rsyncd.conf
uid=root
gid=root
use chroot=no
max connections=5
log file=/var/log/rsyncd.log
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
read only=no
list=yes
[web]
comment=web code dir
path=/var/www/html
hosts allow=192.168.65.129
hosts deny=*
[root@mubiao ~]# service xinetd restart
下面再做测试:
用第三台linux主机做源主机(ip:192.168.65.134)
[root@localhost ~]# ls
anaconda-ks.cfg Desktop install.log install.log.syslog ora.txt
[root@localhost ~]# rsync -aRHv 192.168.65.151::web/index.html ./
@ERROR: access denied to web from unknown (192.168.65.134)
rsync error: error starting client-server protocol (code 5) at main.c(1296) [receiver=2.6.8]
[root@localhost ~]# ls
anaconda-ks.cfg Desktop install.log install.log.syslog ora.txt
####发现这台未授权的linux主机无法拉取目标机上的数据,即并不是每台机器都可以对目标机进行备份操作。
再用上面的第二台linux主机做源主机(ip:192.168.65.129)
[root@yuan ~]# ls
anaconda-ks.cfg install.log install.log.syslog nimei.txt this is source_1 yuan_1.txt
[root@yuan ~]# rsync -aRHv 192.168.65.151::web/index.html ./
receiving file list ... done
index.html
sent 102 bytes received 130 bytes 464.00 bytes/sec
total size is 7 speedup is 0.03
[root@yuan ~]# ls
anaconda-ks.cfg index.html install.log install.log.syslog nimei.txt this is source_1 yuan_1.txt
在源主机上(ip:192.168.65.129)表明拉取目标机数据index.html
[root@yuan ~]# rsync -aRHv nimei.txt 192.168.65.151::web
building file list ... done
nimei.txt
sent 90 bytes received 38 bytes 256.00 bytes/sec
total size is 0 speedup is 0.00
[root@mubiao ~]# cd /var/www/html/
[root@mubiao html]# ls
index.html mubiao.txt nimei.txt yuan_1.txt
在目标机上表明,从源主机(ip:192.168.65.129)向目标机推送的数据nimei.txt推送成功。
即表明,此时只有ip地址为192.168.65.129的主机可以备份目标机器。
到这里rsync的配置完成。
本文出自 “个人感受” 博客,谢绝转载!