Linux学习–rsync+inotify实现自动同步
rsync
remote synchronization(远程同步)
rsync 的最大特点是会检查发送方和接收方已有的文件,仅传输有变动的部分(默认规则是文件大小或修改时间有变动),默认端口:873
服务端
一、安装
[root@localhost ~]# yum install rsync*
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
a 10 kB/s | 2.7 kB 00:00
b 509 kB/s | 3.2 kB 00:00
Package rsync-3.1.3-4.el8.x86_64 is already installed.
Dependencies resolved.
===============================================================================================================
Package Arch Version Repository Size
===============================================================================================================
Installing:
rsync-daemon noarch 3.1.3-4.el8 a 42 k
.......
Complete!
二、配置文件:/etc/rsyncd.conf
[root@localhost ~]# cat /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
# max connections = 4
# pid file = /var/run/rsyncd.pid
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
# [ftp]
# path = /home/ftp
# comment = ftp export area
配置示例
1 log file = /var/log/rsyncd.log
2 pid file = /var/log/rsyncd.pid
3 lock file = /var/log/rsyncd.lock
4 secrets file = /etc/rsyncd.pas
5 motd file = /etc/rsyncd.motd
6 read only = no
7 hosts allow = 192.168.31.11
8 list = yes
9 uid = nobody
10 gid = nobody
11 use chroot = no
12 max connections = 4
13 [linux_bak]
14 path = /testrsync
15 comment = data backup
16 auth users = rsyncuser
lock file :进行资源配置或者数据共享时候进行进程锁定
pid:存放rsync进程的PID
secrets:存放密码 ,建议权限600
motd file :欢迎条幅,可以自定义其中的内容
host allow :允许哪些IP连接
服务端配置好,客户端不需要配置东西
三、配置账号密码文件
如示例中的账号密码文件是secrets file = /etc/rsyncd.pas,就在/etc/rsyncd.pas中配置用于rsync连接的账号密码,并将该文件的权限设置为600
[root@bogon log]# cat /etc/rsyncd.pas
rsyncuser:123456
[root@bogon log]# chmod 600 /etc/rsyncd.pas
四、创建共享文件夹
在模块中会配置共享文件夹,如果共享文件夹不存在需要将其创建出来,示例中配置的共享文件夹为path = /testrsync,并给予该文件夹足够的权限,这里我给的权限是777
mkdir /testrsync
chmod 7777 /testrsync
客户端
一、配置用于rsync连接的密码文件
[root@localhost ~]# vim /etc/1.pas
[root@localhost ~]# cat /etc/1.pas
123456
[root@localhost ~]# chmod 600 /etc/1.pas
推(客户端数据同步至服务端) | 参数 | rsync -avh --delete | –password-file=/etc/rsyncd.pas | /tmp/* | rsyncuser@192.168.31.10::linux_bak |
---|---|---|---|---|---|
作用 | 用的密码是哪个文件里的密码 | 源目录 | 账号@服务器IP::共享模块名称 | ||
拉(把服务端的数据同步至客户端 | 参数 | rsync -avh --delete | –password-file=/etc/rsyncd.pas | rsyncuser@192.168.31.10::linux_bak | /tmp/* |
-v: 详细模式输出。
-z: 传输时进行压缩以提高传输效率
-a: 归档模式,表示以递归方式传输文件,并保持文件属性。等于-rtopgDl
-h: 将文件大小转为我们习惯的M、K等为单位的大小
-e: 指定通信协议。如ssh
-P: 显示同步的过程及传输时的进度等信息(大P)
--delete: 让目标目录SRC和源目录数据DST一致,即无差异数据同步
--daemon: daemon表示以守护进程的方式启动rsync服务。
二、客户端推送数据测试
[root@localhost ~]# rsync -avH --delete --password-file=/etc/1.pas /tmp/* rsyncuser@192.168.31.10::linux_bak
sending incremental file list
anaconda.log
dbus.log
。。。。。
sent 7,514 bytes received 224 bytes 15,476.00 bytes/sec
total size is 6,174 speedup is 0.80
三、在服务端验证
[root@bogon log]# ll /testrsync/
total 20
-rw-r--r--. 1 nobody nobody 2765 Oct 26 01:43 anaconda.log
-rw-r--r--. 1 nobody nobody 2286 Oct 26 01:42 dbus.log
-rw-r--r--. 1 nobody nobody 0 Oct 26 01:42 dnf.librepo.log
。。。。
源代码安装inotify
inotify安装在客户端上配合脚本使用
./configure ; make ;make install
#./configure --perfix=/usr/local/inotify
configure过程中看有没有报错
内库文件 lib event
二进制文件放在/usr/local/bin
头文件 /usr/include
内库文件/usr/lib64
到源代码路径下INSTALL 、README可以查看各种安装方法
一、安装 gcc gcc-c++
[root@localhost inotify-tools-3.14]# yum install -y gcc gcc-c++
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
...
二、编译安装三部曲
./configure ;make; make install完成后可在/opt/下看到相关的文件目录
[root@localhost inotify-tools-3.14]# ./configure --prefix=/opt/
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
......
[root@localhost inotify-tools-3.14]# make
make all-recursive
make[1]: Entering directory '/inotify-tools-3.14'
Making all in libinotifytools
make[2]: Entering directory '/inotify-tools-3.14/libinotifytools'
Making all in src
......
[root@localhost inotify-tools-3.14]# make install
Making install in libinotifytools
make[1]: Entering directory '/inotify-tools-3.14/libinotifytools'
Making install in src
make[2]: Entering directory '/inotify-tools-3.14/libinotifytools/src'
Making install in inotifytools
......
[root@localhost opt]# ll
total 0
drwxr-xr-x. 2 root root 45 Nov 2 11:05 bin
drwxr-xr-x. 3 root root 26 Nov 2 11:05 include
drwxr-xr-x. 2 root root 143 Nov 2 11:05 lib
drwxr-xr-x. 4 root root 28 Nov 2 11:05 share
三、上传脚本并运行,自动同步测试
脚本内容
#!/bin/bash
/opt/bin/inotifywait -mrq -e create,move,delete,modify /tmp/test | while read D E F ; do
#rsync -avH --password-file=/etc/1.pas /tmp/test/* rsyncuser@192.168.116.1::linux_bak
rsync -avH --password-file=/etc/1.pas /tmp/test rsyncuser@192.168.31.10::linux_bak
done
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bSmxhHFe-1635841017677)(C:\Users\ThinkPad\AppData\Roaming\Typora\typora-user-images\image-20211102161229103.png)]
四、在服务端查看是否同步成功
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZfIULoID-1635841017679)(C:\Users\ThinkPad\AppData\Roaming\Typora\typora-user-images\image-20211102161310332.png)]
将程序后台运行可以在脚本后面加&,也可以使用nohup 后面加脚本来实现