网络环境
CENTOS6.4 IP:193.168.120.69 备份机器
CENTOS6.4 IP:193.168.120.91 主服务器
1、备份机器上面需要安装rsync,并配置好虚拟用户密码及同步的目录等,如下:
先安装rsync,系统一般都已经装好,没有安装的话先安装
[root@MSLINUX ~]# sh /shell/rsync.sh
#!bin/bash
a=`rpm -aq rsync`
if
[ "$a" = "" ]; then
yum install rsync-* -y
fi
建立rsync用户,并配置虚拟用户vbak及其密码和用来备份的目录等,如下脚本
[root@cos64 rsync]# sh 1.sh
#!/bin/bash
egrep "^rsync" /etc/passwd >& /dev/null
if [ $? -ne 0 ]
then
useradd rsync -s /sbin/nologin -M
fi
if [ ! -d /webs ]
then
mkdir -p /webs
else
mv /webs /websold
mkdir -p /webs
fi
chown rsync.rsync /webs
echo "vbak:vpwdssss" >/etc/rsync.password
chmod 600 /etc/rsync.password
编辑rsync的配置文件
cat /etc/rsyncd.conf
root@cos64 rsync]# cat /etc/rsyncd.conf
uid = rsync
gid = rsync
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[backup]
path = /webs
ignore errors
read only = false
list = false
hosts allow = 193.168.120.0/24
hosts deny = 0.0.0.0/32
auth users = vbak
secrets file = /etc/rsync.password
启动服务:rsync --daemon
2、主服务器的配置
将虚拟用户的密码写入密码文件:
echo "vpwdssss" >/etc/rsync.password
chmod 600 /etc/rsync.password
测试:
rsync -avz /etc/passwd [email protected]::backup --password-file=/etc/rsync.password
此时备份机器上面的/webs目录应该有指定的备份文件了
配置实时同步
安装inotify
先检查是否支持
[root@centos64 webs]# ll /proc/sys/fs/inotify/
总用量 0
-rw-r--r-- 1 root root 0 7月 23 16:34 max_queued_events
-rw-r--r-- 1 root root 0 7月 23 16:34 max_user_instances
-rw-r--r-- 1 root root 0 7月 23 16:34 max_user_watches
有以上三个文件说明支持
下载安装:inotify-tools-3.14.tar.gz
tar -zxvf inotify-tools-3.14.tar.gz -C /tmp/
cd /tmp/inotify-tools-3.14/
./configure --prefix=/usr/local/inotify-3.14
make && make install
安装完成以后编写同步的脚本
[root@centos64 rsync]# cat start.sh
#!/bin/bash
bakhost=193.168.120.69
src=/webs/
dst=backup
user=vbak
rsync_passfile=/etc/rsync.password
inotify_home=/usr/local/inotify-3.14
if [ ! -e "$src" ] || [ ! -e "${rsync_passfile}" ] || [ ! -e "${inotify_home}/bin/inotifywait" ] || [ ! -e "/usr/bin/rsync" ];
then
echo "please check inotify"
exit 9
fi
${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src \
| while read file
do
# rsync -avzP --delete --timeout=100 --password-file=${rsync_passfile} $src $user@$host01::$dst >/dev/null 2>&1
cd $src && rsync -aruz -R --delete ./ --timeout=100 $user@$bakhost::$dst --password-file=${rsync_passfile} >/dev/null 2>&1
done
exit 0
放到后台执行:
sh start.sh &
此时再修改主服务器的/webs目录,同样能写入到备份机器的/webs目录当中,实现实时的同步备份