公司如今有一個需求,須要將服務器CentOS的文件定時增量到Windows服務器,Windows服務器鏈接了存儲服務器磁盤陣列,空間比較大。基於這樣的需求,咱們採用inotify+rsync增量備份的解決方案。php
IP地址 | 系統 |
---|---|
192.168.1.100 | CentOS7.x |
192.168.1.101 | Windows Server 2012 r2 |
注意:這裏的服務器名和密碼用於後面配置項目中,默認用戶名: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
複製代碼
SvcCWRSYNC:admin123
複製代碼
yum install rsync -y
複製代碼
admin123
複製代碼
chmod 600 /etc/rsync.passwd
複製代碼
inotify-tools工具監測文件增長、刪除和修改,同時同步到備份服務器windowsbash
yum install inotify-tools -y
複製代碼
#!/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
複製代碼
# 測試命令
/usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/rsync.passwd /root/test SvcCWRSYNC@192.168.1.101::rsyncdata
複製代碼
inotify_start.sh &
複製代碼
[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
複製代碼
#!/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 $?
複製代碼