侧边栏壁纸
博主头像
silianpan博主等级

Feel no regret for this life.

  • 累计撰写 41 篇文章
  • 累计创建 22 个标签
  • 累计收到 5 条评论

inotify+rsync将服务器CentOS文件定时增量备份到另外CentOS

silianpan
2022-03-18 / 0 评论 / 0 点赞 / 1,277 阅读 / 3,340 字
温馨提示:
本文最后更新于 2022-03-18,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

背景

之前写了一篇关于从CentOS备份到Windows的文章:inotify+rsync将服务器CentOS文件定时增量备份到Windows,现在改为备份到CentOS

服务器配置

IP地址系统功能
192.168.1.100CentOS7.xrsync服务端
192.168.1.101CentOS7.xrsync客户端

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 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 $?
0

评论区