inotify+rsync將服務器CentOS文件定時增量備份到Windows

背景

公司如今有一個需求,須要將服務器CentOS的文件定時增量到Windows服務器,Windows服務器鏈接了存儲服務器磁盤陣列,空間比較大。基於這樣的需求,咱們採用inotify+rsync增量備份的解決方案。php

服務器配置

IP地址 系統
192.168.1.100 CentOS7.x
192.168.1.101 Windows Server 2012 r2

Windows

1. 安裝cwRsyncServer

注意:這裏的服務器名和密碼用於後面配置項目中,默認用戶名:SvcCWRSYNC,密碼設置爲admin123windows

  • 配置文件
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工具監測文件增長、刪除和修改,同時同步到備份服務器windowsbash

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 SvcCWRSYNC@192.168.1.101::rsyncdata
複製代碼

7. 後臺運行啓動腳本

inotify_start.sh &
複製代碼

服務自啓動腳本

1. CentOS7

  • 將inotifyd.service文件拷貝到/usr/lib/systemd/system/目錄下
  • 啓動服務:systemctl start inotifyd.service
  • 自動啓動:systemctl enable inotfyd.service
  • 修改後執行:systemctl daemon-reload
[Unit]
Description=inotify rsync script
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=root
Group=root
PIDFile=/var/run/inotify.pid
ExecStart=/usr/bin/nohup /root/inotify_start.sh > /root/inotify.log 2>&1 &
ExecReload=
ExecStop=/usr/bin/ps -ef | /usr/bin/grep inotify | /usr/bin/grep -v grep | /usr/bin/awk '{print $2}' | /usr/bin/xargs /usr/bin/kill -9
PrivateTmp=true

[Install]
WantedBy=multi-user.target
複製代碼

2. CentOS6

  • 將inotifyd拷貝到/etc/init.d/目錄下
  • 啓動服務:/etc/init.d/inotifyd start
  • 自動啓動:chkconfig inotifyd on
#!/bin/sh
# chkconfig: - 91 35
# description: inotifyd

# Source function library.
if [ -f /etc/init.d/functions ] ; then
  . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
  . /etc/rc.d/init.d/functions
else
  exit 1
fi

# Avoid using root's TMPDIR
unset TMPDIR

# Source networking configuration.
. /etc/sysconfig/network

EXEC_FILE="/root/inotify_start.sh"
LOCK_FILE="/var/lock/subsys/inotifyd"
OPTIONS="> /root/inotify.log 2>&1 &"
RETVAL=0

start() {
     echo -n $"Starting $(basename $EXEC_FILE) services: "
     # daemon $EXEC_FILE $OPTIONS
     daemon $EXEC_FILE $OPTIONS
     RETVAL=$?
     echo

     [ $RETVAL -eq 0 ] && touch $LOCK_FILE || \
        RETVAL=1
     return $RETVAL
}    

stop() {
     echo -n $"Shutting down $(basename $EXEC_FILE) services: "
     # killproc $(basename $EXEC_FILE)
     ps -ef | grep inotify | grep -v grep | awk '{print $2}' | xargs kill -9
     RETVAL=$?
     echo

     [ $RETVAL -eq 0 ] && rm -f $LOCK_FILE
     return $RETVAL
}

restart() {
     stop
     start
}    

reload() {
     stop
     start
}

rhstatus() {
     status $(basename $EXEC_FILE)
     RETVAL=$?

     if [ $RETVAL -ne 0 ] ; then
          return $RETVAL
     fi
}    


# Allow status as non-root.
if [ "$1" = status ]; then
       rhstatus
       exit $?
fi

# Check that we can write to it... so non-root users stop here
# [ -w $CONF_FLIE ] || exit 4

case "$1" in
  start)
       start
     ;;
  stop)
       stop
     ;;
  restart)
       restart
     ;;
  reload)
       reload
     ;;
  status)
       rhstatus
     ;;
  condrestart)
       [ -f $LOCK_FILE ] && restart || :
     ;;
  *)
     echo $"Usage: $0 {start|stop|restart|reload|status|condrestart}"
     exit 2
esac

exit $?
複製代碼

轉載請註明:溜爸 » inotify+rsync將服務器CentOS文件定時增量備份到Windows服務器

相關文章
相關標籤/搜索