1. 程式人生 > 程式設計 >inotify+rsync將伺服器CentOS檔案定時增量備份到另外CentOS

inotify+rsync將伺服器CentOS檔案定時增量備份到另外CentOS

背景

之前寫了一篇關於從CentOS備份到Windows的文章:inotify+rsync將伺服器CentOS檔案定時增量備份到Windows,現在改為備份到CentOS

伺服器配置

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工具監測檔案增加、刪除和修改,同時同步到備份伺服器windows

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 [email protected]::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檔案定時增量備份到另外CentOS