背景
公司現在有一個需求,需要將服務器CentOS的文件定時增量到Windows服務器,Windows服務器連接了存儲服務器磁盤陣列,空間比較大。基於這樣的需求,我們採用inotify+rsync增量備份的解決方案。
服務器配置
IP地址 | 系統 |
---|---|
192.168.1.100 | CentOS7.x |
192.168.1.101 | Windows Server 2012 r2 |
Windows
1. 安裝cwRsyncServer
- 雙擊安裝
- 設置服務名和密碼
注意:這裏的服務器名和密碼用於後面配置項目中,默認用戶名:SvcCWRSYNC,密碼設置爲admin123
- 配置文件
use chroot = false
strict modes = false
hosts allow = *
log file = rsyncd.log
uid = 0 # 需要配置此項,不然連接報錯
gid = 0 # 需要配置此項,不然連接報錯
# Module definitions
# Remember cygwin naming conventions : c:\work becomes /cygwin/c/work
#
#[test]
#path = /cygdrive/c/work
#read only = false
#transfer logging = yes
[rsyncdata]
path = /cygdrive/e/cdbid-pro1.0-backup/57
read only = false # 只讀屬性爲false
list = no
hosts allow = *
auth users = SvcCWRSYNC # 對應配置用戶名
secrets file = /cygdrive/e/cdbid-pro1.0-backup/rsync.passwd
- 新建rsync.passwd文件,填寫如下
SvcCWRSYNC:admin123
- 添加目錄cdbid-pro1.0-backup目錄訪問權限,如果沒有SvcCWRSYNC用戶,點擊高級添加
- 啓動服務
Linux
1. 安裝rsync
yum install rsync -y
2. 新建/etc/rsync.passwd,內容如下,注意客戶端rsync只需要密碼
admin123
3. 更改權限
chmod 600 /etc/rsync.passwd
4. 安裝inotify
inotify-tools工具監測文件增加、刪除和修改,同時同步到備份服務器windows
yum install inotify-tools -y
5. 啓動腳本inotify_start.sh
#!/bin/bash
host=192.168.1.101
src=/home
des=rsyncdata
user=SvcCWRSYNC
inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src | while read files
do
/usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/rsync.passwd $src $user@$host::$des
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
6. 測試
# 測試命令
/usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/rsync.passwd /root/test [email protected]::rsyncdata
7. 後臺運行啓動腳本
inotify_start.sh &