一、测试

服务器 IP-addr         status   工具安装       系统版本    操作目录

客服端192.168.1.161 client   inotify-tools    centos7    /server/backup/

服务器192.168.1.150 server      rsync        centos7    /server/backup/ 

、配置

1、服务器(server)配置

(服务器IP):192.168.1.150

(1) 安装 rsync(备份服务器只安装 rsync,需要启动)

可以运行命令 rpm -aq rsync 或者 rsync -v 进行查看,如下信息说明已经安装了 rsync

[root@localhost yang]# rpm -qa rsync

rsync-3.1.2-6.el7_6.1.x86_64

(2)如果没有默认安装,建议运行如下命令

yum install rsync

在自己安装的 rsync 目录下,创建 rsyncd.conf 文件 ,并写入如下的信息

[root@localhost ~]# vim /etc/rsyncd.conf

uid = root

gid = root

use chroot = no

max connections = 10

strict modes = yes

hosts allow = 192.168.1.161

port = 873

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsync.lock

log file = /var/log/rsyncd.log

[inotify]

path = /server/backup/

ignore errors

read only = no

write only = no

hosts allow = 192.168.1.161

hosts deny = *

list = false

uid = root

gid = root

auth users = root

secrets file = /etc/rsyncd/rsync.password 

(3)建立用户与密码认证文件

使用 root 用户进行操作,自定义密码为 root@mzla2019 (此密码并非登录密码,只是一个 rsync 与 client 服务器进行交互的凭证,可自定义)

创建 rsync.password 文件,并写入【用户名:密码】,记住此处的密码!

[root@localhost rsyncd]# echo "root:root@mzla2019"> /etc/rsyncd/rsync.password

需要给密码文件 600 权限

[root@localhost rsyncd]# chmod 600 /etc/rsyncd/rsync.password

(4) 启动 rsync

以守护进程方式启动 rsync 服务器

rsync --daemon

 [root@localhost rsyncd]rsync --daemon

 [root@localhost rsyncd]# netstat -nltp |grep rsync 也可以查看是否启动 

2 (client)配置

客户端IP:192.168.1.161

(1) 安装 rsync

(2)建立访问服务端的密码认证文件

创建了目录/etc/rsyncd/

 [root@localhost rsyncd]echo "root@mzla2019" >/etc/rsyncd/rsync.password   

#其中 `root@mzla2019可以自己设置密码,rsync.password 名字也可以自己设置

设置权限为只读

 [root@localhost rsyncd]# chmod 600 rsync.password 

(3)安装 inotify-tools

查看当前系统是否支持 inotify

 [root@localhost etc]# ll /proc/sys/fs/inotify/

总用量 0

-rw-r--r--. 1 root root 0 12月  5 20:12 max_queued_events

-rw-r--r--. 1 root root 0 12月  5 20:12 max_user_instances

-rw-r--r--. 1 root root 0 12月  5 20:12 max_user_watches

依次执行如下命令

[root@localhost rsyncd]# wget

http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

[root@localhost rsyncd]# tar zxvf inotify-tools-3.14.tar.gz  

[root@localhost rsyncd]#cd inotify-tools-3.14  

[root@localhos inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify  

[root@localhos inotify-tools-3.14]# make  

[root@localhos inotify-tools-3.14]# make install

(4) 创建 rsync 复制脚本

此项功能主要是将 client 端的目录 /server/backup/ 里的内容,如果修改了(无论是添加、修改、删除文件)能够通过 inotify 监控到,并通过 rsync 实时的同步给 server 的 /server/backup/ 

创建 inotify.sh 脚本文件

[root@localhost server]# vim inotify.sh

#!/bin/bash

host=192.168.1.150

src=/server/backup/

des=inotify

password=/etc/rsyncd/rsync.password

user=root

inotify=/usr/local/inotify

 

${inotify}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \

| while read files

do

rsync -avzP --delete  --timeout=100 --password-file=${password} $src $user@$host::$des

echo "${files} was rsynced" >>/tmp/rsync.log 2>&1

done

保存文件后,为 .sh 脚本增加 764 权限

[root@localhost server]# chmod 764 inotify.sh

运行脚本

[root@localhost server]# sh inotify.sh &

[1] 1925

echo "setsid /server/inotify.sh &" >> /etc/rc.local 

3 功能测试

192.168.1.161

[root@localhost backup]# touch {1..10}.txt

[root@localhost backup]# ls

10.txt  1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt

  

查看192.168.1.150服务器

[root@localhost ~]# cd /server/backup/

[root@localhost backup]# ls

10.txt  1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt


©著作权归作者所有:来自51CTO博客作者mob604756e834f7的原创作品,如需转载,请注明出处,否则将追究法律责任

更多相关文章

  1. Android TCP Socket通信客户端/服务器端Demo(附APP源码)
  2. mongoDB入门系列之配置解释及错误汇总
  3. 苹果笔记本macBookAir2020 -M1密码--重置
  4. Playbook分发Nginx配置文件
  5. 实战--Playbook批量修改密码
  6. 实战--Playbook批量部署zabbix-agent
  7. 实战--Playbook批量更改服务器主机名
  8. 使用无服务器式的 Jenkins X:探索Prow,Jenkins X Pipeline Oper
  9. MySQL数据库root账户密码忘记了怎么办?

随机推荐

  1. awk域分隔符问题
  2. Linux下如何备份恢复和查看硬盘MBR与分区
  3. 008_Linux驱动之_IO口的配置
  4. 将JSON文件中的所有时间戳转换为bash(Ubu
  5. Linux时区设置和时间同步-基于CentOS 6(最
  6. VMware Workstation虚拟机平台安装Linux-
  7. 小记——inotify文件监控
  8. Linux技巧:如何利用Putty连接Linux主机
  9. Linux--常用命令--last
  10. 配置 limits.conf 限制 Linux 用户登录数