sersync服务器同步 #单向同步
1. 配置同步目标服务器rsync
在目标服务器上配置如下:
vi /etc/rsyncd.conf
uid=root
gid=root
max connections=36000
use chroot=no
log file=/var/log/rsyncd.log
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
[rsync]
path=/opt/rsync
comment = test files
ignore errors = yes
read only = no
hosts allow = 192.168.0.107/24 #从哪台服务器备份
hosts deny = *
mkdir /opt/rsync #同步目录
rsync --daemon #后台运行rsync
2. 在主服务器上安装配置sresync
tar zxf sersync2.5_32bit_binary_stable_final.tar.gz
cd GNU-Linux-x86/
vi confxml.xml, 修改如下行
<sersync>
<localpath watch="/opt/rsync">
#本地同步目录
<remote ip="192.168.0.104" name="rsync"/> #目标服务器列表和同步模块名
<remote ip="192.168.0.x" name="rsync"/> #可以设置多个
</localpath>
mkdir /opt/rsync #同步目录
./sersync2 -r -d #后台实时同步,-r同步执行所有,-d后台执行
测试:
目标服务器/opt/rsync会随着主服务器/opt/rsync的变动而变动
######################################################################
csync2 服务器同步 #双向同步,不适合同步大文件,适合电子商务
PC1:192.168.0.107
PC2:192.168.0.108
在两台上同样操作
yum install gcc gcc-c++ libgpg-error libgpg-error-devel libgcrypt libgcrypt-devel -y
tar zxf libtasn1-2.1.tar.gz #这个可以用yum装,要装libtasn1.x86_64
libtasn1-devel.x86_64
cd libtasn1-2.1
./configure && make && make install
tar zxf sqlite-2.8.17.tar.gz
cd sqlite-2.8.17
./configure && make && make install
tar zxf librsync-0.9.7.tar.gz
cd librsync-0.9.7
./configure && make && make install
tar jxf gnutls-2.6.6.tar.bz2
cd gnutls-2.6.6
./configure && make && make install
echo "/usr/local/lib" >> /etc/ld.so.conf
ldconfig
tar zxf inotify-tools-3.13.tar.gz
inotify-tools-3.13
./configure && make && make install
yum install byacc flex -y
tar zxf csync2-1.34.tar.gz
cd csync2-1.34
./configure && make && make install
make cert
csync2 -k /etc/csync2.key
scp /etc/csync2.* 192.168.0.108:/etc/ #只在107做,拷贝到108,108不用make cert,两台主机要有一样的key
文件配置,两台机子操作一样
yum install xinetd -y
vi /etc/xinetd.d/csync2 #模板/etc/xinetd.d/下的
service csync2
{
disable = no
protocol = tcp
port=30865
flags = REUSE
socket_type = stream
wait = no
user = root
group = root
server = /usr/local/sbin/csync2 #csync2服务路径
server_args = -i
}
vi /etc/services
csync2 30865/tcp
vi /etc/csync2.cfg
group mygroup
{
host host server7.example.com desktop73.example.com; (每台主机完整的主机名)
#host host4@host4-eth2;
#key /etc/csync2.key;
include /opt/rsync; #需要同步的目录,可以多个
#include %homedir%/bob;
#exclude %homedir%/bob/temp;
exclude *~ .*; #排除以 “.”开头的文件
#
#action
#{
#pattern /etc/apache/httpd.conf;
#pattern /etc/apache/sites-available/*;
#exec "/usr/sbin/apache2ctl graceful";
#logfile "/var/log/csync2_action.log";
#do-local;
#}
#
#backup-directory /var/csync2; #防错备份目录,根据自己的需求设置,每台主机上都要有
#backup-generations 3;
#
auto younger; #同步以最新的文件为标准更新
}
#
# prefix homedir
#{
#on host[12]: /export/users;
#on *: /home;
#}
vi /etc/hosts #两台机子都要有解析(自己和对方)
/etc/init.d/xinetd start
csync2 -vvv -T 测试csync配置是否正确,在两台机子都做
没有错误后,测试
cd /opt/rsync
cp /boot/* .
csync2 -xv 执行同步命令
查看是否同步
若要自动同步,写脚本
vi /usr/local/sbin/csync2.sh
#!/bin/bash
src=/opt/rsync
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' \
--exclude "\.swp$" \
-e close_write,modify,delete,create,attrib \
${src} \
| while read file
do
csync2 -x >/dev/null 2>&1
#csync2 -xv
#echo "${src} was csynced....."
done
chmod +x csync2.sh
csync2.sh & (在每台主机上执行 )
若出现问题,尝试ldconfig /usr/local/bin
ldconfig是一个动态链接库管理命令,装了新的动态库,让动态链接库为系统所共享