两台linux
服务器实现文件同步
实现主服务器上的
/home/yy/test
目录的文件和备份服务器上的/home/uu/test
目录实时同步
主服务器配置
1.下载rsync
mkdir /home/yy
cd /home/yy
wget https://download.samba.org/pub/rsync/src/rsync-3.1.2.tar.gz
tar -zxvf rsync-3.1.2.tar.gz
2.安装gcc
yum -y install gcc
3.安装rsync
cd rsync-3.1.2/
./configure
make
make install
5.设置免密同步
使用SSH免密操作,生成公钥和私钥
ssh-keygen -t rsa -P ""
把生成的公钥发送给备份主机
ssh-copy-id root@192.168.107.131
4.安装inotify-tools
cd ..
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar zxvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14/
./configure
make & make install
检查
/usr/local/bin/
目录下是否新增两个文件
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PR6Q4Kpg-1666170145738)(C:\Users\yy\AppData\Roaming\Typora\typora-user-images\image-20221019154902769.png)]
5.编写inotify.sh
脚本文件
cd /home/yy
touch inotify.sh
vim inotify.sh
/usr/local/bin/inotifywait -mrq -e modify,delete,create,attrib,move /home/yy/test/ |while read events
do
rsync -av --delete /home/yy/test/ root@192.168.107.131:/home/uu/test/
echo "`date +%F\ %T`出现事件$events" >> /var/log/rsync.log 2>&1
done
6.添加可执行权限并执行
给
inotify.sh
脚本文件添加可执行权限
chmod +x inotify.sh
./inotify.sh &
备份服务器配置
1.下载rsync
cd /home/yy
wget https://download.samba.org/pub/rsync/src/rsync-3.1.2.tar.gz
tar -zxvf rsync-3.1.2.tar.gz
2.安装gcc
yum -y install gcc
3.安装rsync
./configure
make
make install
4.启动rsyncd
服务
systemctl start rsyncd
监听端口号
netstat -tnlp | grep rsync