---
title: "NFS搭建以及rsync+inotify实时备份"
author: "Clayz"
date: "2013-02-28T12:40:39Z"
tags: []
categories: ["操作系统"]
slug: "nfs-rsync-inotify"
draft: false
lastmod: "2013-02-28T12:40:39Z"
id: 1817701
url: "https://clayz.iteye.com/blog/1817701"
---

**搭建NFS**

假设有2台Server，分别是Server1和Server2。现在要在Server1上安装NFS，在Server2上挂载该目录。

 

1\. 在两台Server上都安装：

yum install portmap nfs-utils nfs-utils-lib

 

2\. 编辑Server1文件/etc/exports: 

/var/www/commentator/images 192.168.10.1(rw,sync,no\_root\_squash)

 

3\. 在Server1上启动NFS：

service rpcbind start

service nfs start

 

4\. 在Server2上挂载该目录

mount -t nfs 192.168.10.1:/var/www/commentator/images /mnt/images

 

**设置rsync+inotify实时备份**

现在要将Server1上的NFS目录备份到Server3上。

 

1\. 在Server1和Server3上安装rsync和inotify。

yum install rsync inotify-tools

 

2\. 配置SSH，让Server1可以远程访问Server3。

 

3\. 在Server1上创建同步脚本rsync\_images\_backup.sh。

 

```
#!/bin/sh
source=/var/www/commentator/images/profile
destination=root@192.168.10.3:/var/local/commentator/backup/images

inotifywait -mrq -e modify,delete,create,attrib $source | while read D E F
    do
        /usr/bin/rsync -ahqzt --delete $source $destination
    done
```

 

 

4\. 运行该脚本。

nohup ./rsync\_images\_backup.sh &gt;/dev/null 2&gt;&1 &

 

配置结束，在Server1上添加一个文件，在Server2上查看挂载目录里的文件是否更新，在Server3上查看文件是否自动备份。