VPS侦探论坛

 找回密码
 注册
查看: 9044|回复: 1

来这里也很久了,今天发一个用rsync两台服务器同步镜像及备份,可以实时更新!【亲测】

[复制链接]
发表于 2011-10-9 22:29:52 | 显示全部楼层 |阅读模式

来这里也很久了,今天发一个用rsync两台服务器同步镜像及备份,可以实时更新!【本人亲测留下的笔记】注:本安装教程在CentOS5.5 X86 上通过测试
服务器:
A ip:    184.82.228.22         http://dzserver.189k.net   主服务器
B ip:    184.82.253.30         http://dz.189k.net/        备份服务器

================================================================================
检测是否安装软件
#rpm -qa |grep rsync 卸载rsync(系统一般会默认安装rsync)
#rpm -e rsync-2.6.8-3.1
================================================================================
安装gcc编译器 (有的x86上没有安装.)
#yum -y install gcc
================================================================================
rsync安装及配置
#wget -c http://rsync.samba.org/ftp/rsync/src/rsync-3.0.7.tar.gz
#tar zxvf rsync-3.0.7.tar.gz
#cd rsync-3.0.7
#./configure --prefix=/usr/local/rsync
#make$make install
参数说明:--prefix 指定rsync要安装的目录.
所有主服务器和备份服务器都需要安装rsync
================================================================================
重要部分主服务器上配置rsync:
#vi /ect/rsyncd.conf
写入以下内容
===============================================================================
uid = root
gid = root
use chroot = no                       # 不使用chroot
max connections = 4                   # 最大连接数为4
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[wwwroot]                             # 这里是认证的模块名,在client端需要指定
path = /home/wwwroot/                 # 需要做镜像的目录
ignore errors                         # 可以忽略一些无关的IO错误
read only = true                      # 只读
list = false                          # 不允许列文件
hosts allow = 184.82.253.30           # 只允许备份服务器IP连接,支持184.82.253.30/55
hosts deny = *                        # 禁止连接IP
auth users = shimenren                # 认证的用户名,如果没有这行,则表明是匿名
secrets file = /etc/backshimenren.pas # 认证文件名,密码文件
================================================================================
建立密码文件
#vi /ect/backshimenren.pas
写入
=================================================================================
shimenren:1234
     格式为:用户名:密码
=================================================================================
设置backshimenren.pas 文件权限
#chown root.root backshimenren.pas
#chmod 600 backshimenren.pas
至止rsync 主服务器设定完成。
=================================================================================
启动主服务器rsync
守护进程方式启动rsync:
#rsync --daemon

#/usr/local/rsync/bin/rsync --daemon
=================================================================================
验证是否启动
#netstat -na|grep 873
查看rsync 进程
#ps -ef | grep rsync   
进入备份服务器
#/usr/local/rsync/bin/rsync -vzrtopg --delete --exclude "dz.189k.net/config/" --exclude "dz.189k.net/uc_server/data/config.inc.*/" --progress shimenren@184.82.228.22::wwwroot /home/wwwroot/
#/usr/local/rsync/bin/rsync -vuzrtopg --progress shimenren@113.107.101.119::wwwroot /home/wwwroot/ --password-file=/etc/backshimenren.pawdss
#/usr/local/rsync/bin/rsync -vuzrtopg --progress shimenren@113.107.101.119::nginx /usr/local/nginx/conf --password-file=/etc/backshimenren.pawdss
上面这个命令行中--vzrtopg里的v是代表Verbose(详细);z是代表Zip(压缩);r是代表Recursive(递归);Topg都是保
持文件原有属性,如属主、时间的参数;--progress是指显示出详细的进度情况;--delete是指如果服务器端删除了这一文件,那么客户端也相
应把文件删除,保持真正的一致;--exclude logs/是表示不对/wwwroot/logs目录下的文件进行备份。同样-exclude conf/ssl.*/是表示不对
/www/conf/ssl.*/目录下的文件进行备份。shimenren@184.82.228.22::wwwroot是表示该命令是对服务器184.82.228.22中的wwwroot模块进行备份,
其中shimenren表示使用shimenren用户来对该模块进行备份。
--password-file=/etc/rsync.pass来指定密码文件,这样就可以在脚本中使用而无须交互式地输入验证密码了。
这里需要注意的是,这份密码文件权限属性要设得只有执行这个命令的当前用户可读!
提示输入密码则说明配置成功了!否则请查检文件或防火墙。
================================================================================

评分

参与人数 1威望 +10 收起 理由
licess + 10

查看全部评分

美国VPS推荐: 遨游主机LinodeLOCVPS主机云搬瓦工80VPSVultr美国VPS主机中国VPS推荐: 阿里云腾讯云。LNMP付费服务(代装/问题排查)QQ 503228080
 楼主| 发表于 2011-10-9 22:30:17 | 显示全部楼层


接下面,一篇还放不下!!!!
=================================================================================

rsync --daemon加入到/etc/rc.d/rc.local文件中设置为开机自动启动
=================================================================================
配置rsync备份服务器
这里我们只需要一个密码文件就可以了
#vi /ect/backshimenren.pawdss
         #1234   之前主服务器设置的密码
#cd  /ect
#chmod 600 backshimenren.pas
=================================================================================
测试指令是否成功
#/usr/local/rsync/bin/rsync -vzrtopg --delete --exclude "logs/" --exclude "conf/ssl.*/" --progress shimenren@184.82.228.22::wwwroot /home/wwwroot/ --password-file=/etc/backserver.pas

如果出现传输目录文件的画面,即表示测试成功。
===================================================================================
置入工作任务,假设每天凌晨5点开始备份:
先建立备份Script文件:
bk.sh
==========================================================================================================
"#! /bin/Bash"
RSYNC="/usr/local/rsync/bin/rsync"
para="-vzrtopg --delete"
pwdfile="--password-file=/etc/backserver.pas"
user="shimenren"
server="184.82.228.22" #主服务器IP
block="wwwroot"        #主服务器认证的模块名
$RSYNC $para --exclude "logs/" --exclude "conf/ssl.*/" --progress $user@$server:block /home/wwwroot/

保存后放入 bk.sh
==============================================================================================================
执行crontab:
#crontab -u root -e
0 5 * * * /root/bk.sh
保存
重启
#/etc/init.d/crond restart
注:这里执行备份的时间可以自行设置,设置成一分钟,嘿嘿就差不多是同步镜像备份了!
===============================================================================================================
至此主服务器和备份服务器安装全部完成

以上教程有本人亲测,并记下的笔记,尊重本人的劳动成果,转载请注明!如果记录有错欢迎指正,我是测试成功了!

军哥介绍的VPS新架了一个博客http://www.vick.cc  速度还不错,里面也有同步镜像的文章,还有一篇mysql 同步镜像的。A--B相互以递增备份



[ 本帖最后由 shli 于 2011-10-9 22:31 编辑 ]
Linux下Nginx+MySQL+PHP自动安装工具:https://lnmp.org
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|VPS侦探 ( 鲁ICP备16040043号-1 )

GMT+8, 2024-9-21 11:14 , Processed in 0.026409 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表