rsync+inotify实现nfs实时备份


一,rsync服务端配置
1,1配置rsyncd配置文件

[root@Rsync ~]# cat /etc/rsyncd.conf 
#rsync server
uid = rsync
gid = rsync
use chroot = no
max connections = 2000
timeout = 600
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
lof file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 10.0.0.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
###################################################
[backup]
comment = bakcup by linuxea
path = /backup

1,2创建rsync用户并启动

[root@Rsync ~]# useradd rsync -M
[root@Rsync ~]# rsync --daemon
[root@Rsync ~]# ps -ef |grep rsync|grep -v grep
root       2094      1  0 03:44 ?        00:00:00 rsync --daemon

1,3创建备份文件目录并授权

[root@Rsync ~]# mkdir /backup
[root@Rsync ~]# chown -R rsync /backup
[root@Rsync ~]# ls -ld /backup/
drwxr-xr-x 2 rsync root 4096 Dec 19 03:51 /backup/
[root@Rsync ~]#

1,4创建连接密码文件

[root@Rsync ~]# echo "rsync_backup:linuxea" >/etc/rsync.password
[root@Rsync ~]# cat /etc/rsync.password
rsync_backup:linuxea

1,5开机启动

[root@Rsync ~]# echo "rsync --daemon" >>/etc/rc.local

二,配置NFS-server,在NFS-server上配置inotify-tools+rsync客户端
1,配置NFS-server
1,1安装nfs

[root@Rsync ~]# yum install nfs-utils rpcbind -y

1,2修改配置文件

[root@Rsync ~]# vim /etc/exports 
/data   10.0.0.0/24(rw,sync,all_sqiash)

1,3创建挂载目录

[root@Rsync ~]# mkdir /data

1,4修改属性

[root@Rsync ~]# chown -R nfsnobody /data/
[root@Rsync ~]# /etc/init.d/rpcbind start
Starting rpcbind:                                          [  OK  ]
[root@Rsync ~]# /etc/init.d/nfs start
                                                           [FAILED]
Starting NFS quotas:                                       [  OK  ]
Starting NFS mountd:                                       [  OK  ]
Starting NFS daemon:                                       [  OK  ]
Starting RPC idmapd:                                       [  OK  ]

1,5开机挂载

[root@Rsync ~]# vim /etc/rc.local
/etc/init.d/rpcbind start
/etc/init.d/nfs start
/bin/sh /rsync/scripts/rsync-inotify.sh &

2,配置rsync+inotify-tools
安装,inotify-tools和nfs-server跑在同一个服务器上的

[root@rsync-nfs src]# wget http://nchc.dl.sourceforge.net/project/inotify-tools/inotify-tools/3.13/inotify-tools-3.13.tar.gz

2,1查看是否支持事件监控

[root@rsync-nfs src]# ls -l /proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 Dec 24 06:18 max_queued_events
-rw-r--r-- 1 root root 0 Dec 24 06:18 max_user_instances
-rw-r--r-- 1 root root 0 Dec 24 06:18 max_user_watches

2,2开始编译安装inotify

[root@rsync-nfs src]# tar xf inotify-tools-3.13.tar.gz
[root@rsync-nfs src]# cd inotify-tools-3.13
[root@rsync-nfs inotify-tools-3.13]# ./configure --prefix=/usr/local/inotify-tools-3.14
[root@rsync-nfs inotify-tools-3.13]# echo $?
0
[root@rsync-nfs inotify-tools-3.13]# make && make install

2,3为了方便软件升级,做一个软连接

[root@rsync-nfs inotify-tools-3.13]# ln -s /usr/local/inotify-tools-3.14/ /usr/local/inotify-tools
[root@rsync-nfs inotify-tools-3.13]# ls -l /usr/local/inotify-tools
lrwxrwxrwx 1 root root 30 Dec 24 06:23 /usr/local/inotify-tools -> /usr/local/inotify-tools-3.14/
[root@rsync-nfs inotify-tools-3.13]# 

2,4其他介绍
2个工具inotifywait inotifywatch

[root@rsync-nfs bin]# pwd
/usr/local/inotify-tools/bin
[root@rsync-nfs bin]# ll
total 80
-rwxr-xr-x 1 root root 38614 Dec 24 06:22 inotifywait
-rwxr-xr-x 1 root root 40377 Dec 24 06:22 inotifywatch
[root@rsync-nfs bin]# 

inotifywait:在被监控的文件或目录上等待特定的文件系统事件发生,执行后处于阻塞状态
inotifywatch:收集被监控的文件系统使用统计数据,指文件系统事件发生的次数统计
2,5创建脚本目录文件
[root@rsync-nfs scripts]# mkdir /rsync/scripts/ -p && cd /rsync/scripts/
2,6推送data脚本

[root@rsync-nfs scripts]# vim rsync-inotify.sh
#!/bin/bash
inotify=/usr/local/inotify-tools/bin/inotifywait 
$inotify -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create,close_write,delete /data \
|while read file
do
        cd / &&
        rsync -az ./data --delete rsync_backup@10.0.0.55::backup \
        --password-file=/etc/rsync.password
done

2,7后台运行

[root@rsync-nfs scripts]# /bin/sh /rsync/scripts/rsync-inotify.sh &
[1] 11888
[root@rsync-nfs scripts]# 

三,web1和web2需要挂载/data目录,和配置rsync每天备份/var/www/html/以及其他的文件到rsync备份服务器上
1,web-nfs配置
1,1安装

yum install nfs-utils rpcbind -y
[root@NFS-WEB1 ~]# /etc/init.d/rpcbind start
Starting rpcbind:                                          [  OK  ]
[root@NFS-WEB1 ~]# chkconfig rpcbind on
[root@NFS-WEB1 backup]# showmount -e 10.0.0.30
Export list for 10.0.0.30:
/data 10.0.0.0/24

1,2挂载

[root@NFS-WEB1 backup]# mount -t nfs 10.0.0.30:/data /mnt
[root@NFS-WEB1 backup]# df -h
Filesystem       Size  Used Avail Use% Mounted on
/dev/sda2         18G  2.5G   15G  15% /
tmpfs            238M     0  238M   0% /dev/shm
/dev/sda1        283M   28M  240M  11% /boot
10.0.0.30:/data   18G  2.5G   15G  15% /mnt
[root@NFS-WEB1 backup]# 

1,3开机挂载

[root@NFS-WEB1 backup]# vim /etc/rc.local 
/etc/init.d/rpcbind start
mount -t nfs 10.0.0.30:/data /mnt

2,web-rsync配置
2,1创建推送密码文件

[root@NFS-WEB1 /]# echo "linuxea" >/etc/rsync.password
[root@NFS-WEB1 /]# chmod 600 /etc/rsync.password

2,2创建脚本文件

[root@NFS-WEB1 /]# mkdir /rsync/scripts/ && cd /rsync/scripts/
[root@NFS-WEB1  scripts]# ls

rsync.sh
2,3推送文件脚本

[root@NFS-WEB1  scripts]# cat rsync.sh 
#!/bin/sh
path=/backup
dir="`ifconfig eth1|awk -F '[ :]+' 'NR==2 {print $4}'`_$(date +%F-%T)"
mkdir $path/$dir -p &&\
/bin/cp -pr /var/www/html/  $path/web-$dir &&\
/bin/cp /etc/rc.local $path/$dir/rc.local-$(date +%F-%T) &&\
rsync -az $path/ rsync_backup@10.0.0.55::backup/ --password-file=/etc/rsync.password

2,4计划任务
计划任务:
每天晚上1点备份

[root@NFS-WEB1 ]# crontab -e
*/5 * * * *  /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1
00 01 * * * /bin/sh /rsync/scripts/rsync.sh >/dev/null 2>&1

1 分享

您可以选择一种方式赞助本站

支付宝扫码赞助

支付宝扫码赞助

日期: 2015-12-25分类: rsync,NFS

标签: rsync, nfs

发表评论