使用rsync+inotify实现目录实时同步
环境
OS |
IP |
HOSTNAME |
centos7 |
192.168.220.138 |
server |
centos7 |
192.168.220.139 |
client |
rsync安装
[root@server ~]# yum install rsync -y
启动服务
[root@server ~]# systemctl start rsyncd
设置开机自启
[root@server ~]# systemctl enable rsyncd
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.
端口873
[root@server ~]# netstat -anutp | grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 91525/rsync
tcp6 0 0 :::873 :::* LISTEN 91525/rsync
常用选项
rsync命令的基本用法:
格式:rsync 【选项】 源文件 目标文件
常见的选项:
-a,--archive(存档) 归档模式,表示以递归的方式传输文件,并且保持文件属性,等同于加了参数-rlptgoD
-r,–recursive 对子目录以递归模式处理
-l,--links 表示拷贝链接文件
-p , --perms 表示保持文件原有权限
-t , --times 表示保持文件原有时间
-g , --group 表示保持文件原有属用户组
-o , --owner 表示保持文件原有属主
-D , --devices 表示块设备文件信息
-z , --compress 表示压缩传输
-H 表示硬连接文件
-A 保留ACL属性信息
-P 显示传输进度
-u, --update 仅仅进行更新,也就是跳过所有已经存在于目标位置,并且文件时间晚于要备份的文件。(不覆盖更新的文件)
--port=PORT 指定其他的rsync服务端口 873
--delete 删除那些目标位置有而原始位置没有的文件
--password-file=FILE 从FILE中得到密码
--bwlimit=KBPS 限制I/O带宽,Kbytes /second
--filter “- 文件名”需要过滤的文件
--exclude= :需要过滤的文件
-v 显示同步过程的详细信息
备份数据
备份192.168.220.138的/etc/目录到本地backup目录下,目录不存在会自动创建
[root@client ~]# rsync -azP [email protected]:/etc/ /backup
[email protected]'s password:
receiving incremental file list
created directory /backup
./
.pwd.lock
0 100% 0.00kB/s 0:00:00 (xfr#1, ir-chk=1194/1196)
.updated
163 100% 159.18kB/s 0:00:00 (xfr#2, ir-chk=1193/1196)
DIR_COLORS
5,090 100% 4.85MB/s 0:00:00 (xfr#3, ir-chk=1192/1196)
DIR_COLORS.256color
5,725 100% 5.46MB/s 0:00:00 (xfr#4, ir-chk=1191/1196)
......
配置rsync服务器备份目录,不使用系统用户
[root@server ~]# vim /etc/rsyncd.conf
# /etc/rsyncd: configuration file for rsync daemon mode
# See rsyncd.conf man page for more options.
# configuration example:
#全局参数案例
# uid = nobody #运行进程的身份
# gid = nobody #进程组
# use chroot = yes #chroot锁定家目录
# max connections = 4 #最大连接数
# pid file = /var/run/rsyncd.pid #存放进程ID
# exclude = lost+found/
# transfer logging = yes #使rsync服务器使用ftp格式的文件来记录下载和上载操作在自己单独的日志中
# timeout = 900 #超时时间
# ignore nonreadable = yes #指定 rysnc 服务器完全忽略那些用户没有访问权限的文件。
# dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 #用来指定那些在传输之前不进行压缩处理的文件。
#模块参数
# [ftp]
# path = /home/ftp
# comment = ftp export area
添加
[test] #模块名称
path = /etc/ 备份路径
comment = core 描述
read only = yes 以只读的方式提供备份
list = yes 允许查看模块信息
auth users = backuser 指定备份用户,和系统用户无关
secrets file = /root/rsync.passwd 指定存放用户密码文件
密码文件
[root@server ~]# vim /root/rsync.passwd
backuser:000000 #用户名,密码
[root@server ~]# chmod 600 /root/rsync.passwd
重启服务
[root@server ~]# systemctl restart rsyncd
rsync [选项] 用户@备份服务器地址::模块名 目录
[root@client ~]# rsync -azP [email protected]::test /backup
Password:
created directory /backup
./
.pwd.lock
0 100% 0.00kB/s 0:00:00 (xfr#1, ir-chk=1194/1196)
.updated
163 100% 159.18kB/s 0:00:00 (xfr#2, ir-chk=1193/1196)
DIR_COLORS
5,090 100% 4.85MB/s 0:00:00 (xfr#3, ir-chk=1192/1196)
DIR_COLORS.256color
5,725 100% 5.46MB/s 0:00:00 (xfr#4, ir-chk=1191/1196)
rsync+inotify实现实时同步
[root@server ~]# yum install -y epel-release
[root@server ~]# yum install inotify-tools -y
[root@server ~]# inotifywait -h
inotifywait 3.14
Wait for a particular event on a file or set of files.
Usage: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ]
常用参数
-e 用来指定要监控哪些事件
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
-m 表示持续监控
-r 表示递归整个目录
-q 表示简化输出信息
[root@server ~]# inotifywait -rmq -e create,delete,move,modify /root/
#打开另一个终端
[root@server ~]# mkdir /root/test.txt
[root@server ~]# cp /etc/passwd /root/
#观察另一端变化
[root@server ~]# inotifywait -rmq -e create,delete,move,modify /root/
/root/ CREATE,ISDIR test.txt
/root/ CREATE passwd
/root/ MODIFY passwd
rsync+inotify实现目录同步备份
[root@server ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:GP1U0Q7kEdEKS1dZUMJ+7xdVapFuGLOzcDOthL7I//c root@server
The key's randomart image is:
+---[RSA 2048]----+
| .BO+*o|
| . oo=.B..|
| . ...=.% o.|
| o o+ X O o|
| . S..+ O .o|
| . o ..|
| . . . ..|
| o . . o|
| ..... .E|
+----[SHA256]-----+
[root@server ~]# ssh-copy-id 192.168.220.139
[root@server ~]# cat inotify.sh
#!/bin/bash
#================================================================
# Copyright (C) 2019 Sangfor Ltd. All rights reserved.
#
# 文件名称:inotify.sh
# 创 建 者:fly
# 创建日期:2019年07月22日
# 描 述:backup files
#
#================================================================
SRC=/etc
[email protected]:/etc-backup
inotifywait -rmq -e create,delete,move,modify $SRC
do
rsync -ahqzt --delete $SRC $HOST
done
[root@server ~]# chmod +x inotify.sh
#检查下语法
[root@server ~]# bash -s inotify.sh
[root@server ~]# ./inotify.sh &
[root@server ~]# touch /etc/test.txt
#client查看
[root@client ~]# ls /etc-backup/etc/test.txt
/etc-backup/etc/test.txt
#测试目录同步
[root@server ~]# rm -rf /etc/test.txt
[root@client ~]# ls /etc-backup/etc/test.txt
ls: 无法访问/etc-backup/etc/test.txt: 没有那个文件或目录