3分钟快速搭建备份服务器之实时备份

1.环境准备

1.1服务端及客户端地址

server:192.168.56.32

client:192.168.56.33

2.服务端配置

2.1服务端配置

rpm -qa |grep rsync #查看是否存在rsync服务

1.服务端配置文件/etc/rsyncd.conf

##rsyncd.conf start##
uid = rsync
gid = rsync
use chroot = no
max connections = 300
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 192.168.56.0/24
auth users = rsync_backup
secrets file = /etc/rsync.password
[backup]
path = /backup/

2.创建用户授权及密码文件授权

[root@inotify ~]# id rsync
id: rsync: no such user
[root@inotify ~]# useradd -s /sbin/nologin -M rsync
[root@inotify ~]# id rsync
uid=1002(rsync) gid=1002(rsync) groups=1002(rsync)
[root@inotify ~]# mkdir /backup
[root@inotify ~]# chown -R rsync.rsync /backup
[root@inotify ~]# echo 'rsync_backup:123456' >/etc/rsync.password
[root@inotify ~]# chmod 600 /etc/rsync.password
[root@inotify ~]# ll /etc/rsync.password
-rw------- 1 root root 20 Apr 27 06:53 /etc/rsync.password
[root@inotify ~]# rsync --daemon
[root@inotify ~]# ps -ef|grep [r]sync
root 3737 1 0 06:55 ? 00:00:00 rsync --daemon
[root@inotify ~]# ll -d /backup/
drwxr-xr-x 2 rsync rsync 6 Apr 27 06:52 /backup/

2.客户端配置

 echo "123456" >/etc/rsync.password
 chomod 600 /etc/rsync.password

3.验证

rsync -avzP /etc rsync_backup@192.168.56.32::backup --password-file=/etc/rsync.password
[root@inotify ~]# ll /backup/
total 16
drwxr-xr-x 84 rsync rsync 8192 Apr 25 01:27 etc

4.实时监控工具介绍

4.1inotify简介

inotify是一个强大的异步文件系统事件的监控软件;通过其可以监控文件系统中的添加、删除、修改、移动等各种事件

在安装inotify-tools前先确认Linux内核是否达到了2.6.15,并且编译时开启CONFIG_INOTIFY选项,通过如下命令

[root@backup01 ~]# uname -r
3.10.0-327.el7.x86_64
[root@backup01 ~]# ls -l /proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 Apr 25 07:06 max_queued_events
-rw-r--r-- 1 root root 0 Apr 25 07:06 max_user_instances
-rw-r--r-- 1 root root 0 Apr 25 07:06 max_user_watches
##显示这三个文件则证明支持
关键参数说明:
在/proc/sys/fs/inotify目录下有三个文件,对inotify机制有一定的限制。
max_queued_events:设置inotify实例事件(event)列可容纳的事件数量。
max_user_instances:设置每个用户可运行的inotifywait或inotifywatch命令的进程数。
max_user_watches:设置inotifywait或inotifywatch命令可以监视的文件数量(单进程)

4.2在客户端安装部署inotify

[root@backup01 ~]#  yum install -y epel-release
[root@backup01 ~]# yum install -y inotify-tools
[root@backup01 ~]# rpm -qa |grep inotify
inotify-tools-3.14-8.el7.x86_64

 

一共安装了2个工具(命令),即inotifywait和inotifywatch
inotifywait:在被监控的文件或目录上等待特定文件系统事件(open、close、delete等)发生,执行后处于阻塞状态,适合在shell脚本中使用。
inotifywatch收集被监视的文件系统使用度统计数据,指文件系统事件发生的次数统计。

4.3 inotify常用参数详解

[root@nfs01 ~]# inotifywait --help

       -r|--recursive       Watch directories recursively.#递归查询目录

       -q|--quiet         Print less (only print events).#打印很少的信息,仅仅打印监控事件的信息

       -qq           Print nothing (not even events).#不打印,甚至不打印事件

       -m|--monitor     Keep listening for events forever.  Without

                            this option, inotifywait will exit after one

                            event is received.#始终保持事件监听状态

       --exclude <pattern>#排除文件或目录时,不区分大小写

                            Exclude all events on files matching the

                            extended regular expression <pattern>.

       --format <fmt>   Print using a specified printf-like format

                            string; read the man page for more details. #打印使用指定的输出类似格式字符串

       --timefmt <fmt> strftime-compatible format string for use with

                            %T in --format string.#指定时间输出的格式

       -e|--event <event1> [ -e|--event <event2> ... ]

              Listen for specific event(s).  If omitted, all events are

              listened for.#通过此参数可以指定需要监控的时间如下所示:

Events:

       access           file or directory contents were read#文件或目录被读取

       modify         file or directory contents were written#文件或目录内容被修改

       attrib            file or directory attributes changed#文件或目录属性被改变

       close_write  file or directory closed, after being opened in

                        writeable mode#文件或目录关闭,打开后可写模式

       close_nowrite     file or directory closed, after being opened in

                        read-only mode#文件或目录关闭,打开后只读模式

       close             file or directory closed, regardless of read/write mode#文件或目录被封闭,无论读/写模式

       open             file or directory opened#文件或目录被打开

       moved_to     file or directory moved to watched directory#文件或目录被移动至另外一个目录

       moved_from       file or directory moved from watched directory#文件或目录从已监视的目录移动

       move            file or directory moved to or from watched directory#文件或目录被移动另一个目录或从另一个目录移动至当前目了

       create           file or directory created within watched directory#文件或目录被创建在当前目录

       delete           file or directory deleted within watched directory#文件或目录被删除

       delete_self   file or directory was deleted#文件或目录已删除

       unmount              file system containing file or directory unmounted#文件系统被卸载


 

2.配置及使用

[root@nfs01 scripts]# cat /server/scripts/jiankong-bl.sh

#!/bin/bash
#Filename: jiankong-bl.sh
Path=/data
IP=192.168.56.33
/usr/bin/inotifywait -mrq  --format '%w%f' -e create,delete,close_write /data/|while read line

do
     cd $Path &&\

     rsync -az ./  --delete rsync_backup@$IP::nfsbackup --password-file=/etc/rsync.password
done

 

3.运行这个脚本

/bin/sh /server/scripts/jiankong-bl.sh &

4.检查运行

ps -ef |grep inotifywait

5.关闭inotifywait

killall inotifywait
pkill inotifywait

6.加入开机自启动

echo '/bin/sh /server/scripts/jiankong-bl.sh & ' >>/etc/rc.local

7.排查错误

sh -x /server/scripts/jiankong-bl.sh

 

 

 

 

0
如无特殊说明,文章均为本站原创,转载请注明出处

该文章由 发布

这货来去如风,什么鬼都没留下!!!
发表我的评论

Hi,请填写昵称和邮箱!

取消评论
代码 贴图 加粗 链接 删除线 签到