rsync+inotify自动备份服务器的配置

rsync+inotify自动备份服务器的配置

1 备份服务器IP : 192.168.1.106

  客户端IP     : 192.168.1.107

2 . 

#yum -y install rsync xinetd gcc gcc-c++

安装这两个服务,同时也将C/C++编译器也安上(如果事先没安的话,避免我们在编译二进制文件时找不到编译器的情况)

3 .在服务器端(192.168.1.106)的/etc下创建一个rsyncd.conf

#service iptables stop 关闭防火墙,与selinux

#vi /etc/selinux/config   将SELINUX=disabled

#vi /etc/rsyncd.conf

uid = nobody

gid = nobody

max connections = 200

timeout = 600

use chroot = no

hosts allow = *

pid file=/var/run/rsyncd.pid

#syslog facility = local7

log file=/var/log/rsyncd.log

#rsync con**

#The 'standard' things

[backup]

path = /backup   //在服务器的根下面创建一个目录用来备份客户端的文件

ignore errors    //忽略一些错误

read only = no

list = no

auth users = test  //认证用户

secrets file = /etc/rsyncd.secrets 认证用户的用户名与密码存放位置


4 . 在服务器端(192.168.1.106下面编译/etc/xinetd.d/rsync

#vi /etc/xinetd.d/rsync

service rsync

{

        disable = no     //将原来的YES改为NO就OK了

        flags           = IPv6

        socket_type     = stream

        wait            = no

        user            = root

        server          = /usr/bin/rsync

        server_args     = --daemon

        log_on_failure  += USERID

}

5 . 在服务器端创建建认证的用户与密码文件

#vi /etc/rsyncd.secrets

test:test

:wq

保存退出

6 . 启动rsync服务

#service xinetd restart

#/usr/bin/rsync --daemon --config=/etc/rsyncd.conf

7 . 注意权限的问题引起客户端无法连接的原因

#chmod 777 /backup

#chmod 600 /etc/rsyncd.secrets

密码文件必须要改为600的权限,不然客端连接的时候会报错

 

客户端的配置(192.168.1.107)

#yum install gcc gcc-c++ rsync xinetd

#service xinetd restart

#service iptables stop

#netstat -tnl |grep 873

1 . vi /etc/rsyncd.secrets

testpasswd

:wq

保存退出

2 . 修改权限

#chmod 600 /etc/rsyncd.secrets

3 . 在客户端

#rsync -vzrtopg --password-file=/etc/rsyncd.secrets /aaa [email protected]::backup

其中aaa是本地要备份到服务器上面的目录



这样的是用于手动同步的,命令也是很麻烦的可以将其写到一个shell角本通过crontab调用进行自动同步


但还有一种效率更高的方法就是下面我们要说的rsync+inotify的相结合的一种有效方式

http://inotify-tools.sourceforge.net下载相应的安装包

# tar -zxvf inotify-tools-3.14.tar.gz

#cd inotify-tools-3.14

#./configure && make && make install


安装完成后写一个shell角本

#vi rsync.sh

#!/bin/bash

src=/aaa

des=backup

ip=192.168.1.106

/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -e modify,delete,create,attrib $src | while read file

        do

                rsync -vzrtopg --delete --progress $src test@$ip::$des --password-file=/etc/rsyncd.secrets &&

                echo "$src was rsynced"

        done

:wq


#nohup sh  /root/rsync.sh  &  注意中间的空格

这样就可以保证当你修改客户端的/aaa里面的文件时会立即向服务器端进行更新,达到数据的快速一致


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章