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

背景

以前寫了一篇關於從CentOS備份到Windows的文章:inotify+rsync將服務器CentOS文件定時增量備份到Windows,如今改成備份到CentOSphp

服務器配置

IP地址 系統 功能
192.168.1.100 CentOS7.x rsync服務端
192.168.1.101 CentOS7.x rsync客戶端

rsync服務端

1. 安裝rsync

yum install -y rsync
複製代碼

2. 配置/etc/rsyncd.conf

transfer logging = yes
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
uid = nobody
gid = nobody
use chroot = no
ignore errors
read only = no

[web1]
path = /root/test
auth users = myuser
secrets file = /etc/rsyncd.secrets
host allow = *
hosts deny = *
list = false
複製代碼

3. 新建/etc/rsyncd.secrets

echo "myuser:mypassword" > /etc/rsyncd.secrets
chmod +x /etc/rsyncd.secrets
複製代碼

4. 啓動服務

systemctl restart rsyncd
複製代碼

rsync客戶端

1. 安裝rsync

yum install rsync -y
複製代碼

2. 新建/etc/rsync.passwd,內容以下,注意客戶端rsync只須要密碼

mypassword
複製代碼

3. 更改權限

chmod 600 /etc/rsync.passwd
複製代碼

4. 安裝inotify

inotify-tools工具監測文件增長、刪除和修改,同時同步到備份服務器windowsweb

yum install inotify-tools -y
複製代碼

5. 啓動腳本inotify_start.sh

#!/bin/bash
host=192.168.1.101
src=/home/backup
des=web1
user=myuser
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 myuser@192.168.1.101::web1
複製代碼

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文件定時增量備份到另外CentOSwindows

相關文章
相關標籤/搜索