linux两台服务器之间文件做同步

文章目录

概要

实现两台 Linux 服务器之间的文件同步,确保两台服务器上的文件保持一致。

一.使用到的包

1.使用到的包

1.unison-2.32.52-1.el6.rf.x86_64.rpm

2.inotify-tools-3.13-1.el6.rf.x86_64.rpm

2.下载地址:

bash 复制代码
https://download.csdn.net/download/yini123321/13054010

3.安装包

bash 复制代码
1.unison安装
rpm -ivh unison-2.32.52-1.el6.rf.x86_64.rpm

2.inotify
rpm -ivh inotify-tools-3.13-1.el6.rf.x86_64.rpm

4.验证安装是否成功
1.unison(任意一种都可以)

bash 复制代码
1.which unison

2.unison -version

3.rpm -qa | grep unison

2.inotify(任意一种都可以)

bash 复制代码
1.rpm -qa | grep inotify-tools

2.which inotifywait

3.inotifywait --version

二.服务器做免密登录

1.在服务器上执行如下命令(如果是以普通用户做免密切换到普通用户执行)

bash 复制代码
ssh-keygen -t rsa
bash 复制代码
一直回车
bash 复制代码
cd /home/test/.ssh

> 在以上目录下会生成两个文件
id_rsa
id_rsa.pub

2.需要登录的服务器

bash 复制代码
1.切换到普通用户
cd /home/test/.ssh
bash 复制代码
2.写入
cat id_rsa.pub >authorized_keys

3.测试登录

bash 复制代码
ssh 127.0.0.1(这你服务器的IP)

三. 同步脚本

1.创建shell脚本(rsync.sh

bash 复制代码
#!/bin/bash
IP=目标IP
src=/data
dst=/data
/usr/bin/inotifywait -mrq -e create,delete,modify,move,attrib,close_write $src | while read line; do /usr/bin/unison  -batch ${src} ssh://${IP}/${dst}
echo -n "$line " >> /var/log/inotify/inotify$(date +%u).log
echo `date | cut -d " " -f1-4` >> /var/log/inotify/inotify$(date +%u).log
done

2.授权

bash 复制代码
chmod -R 755 rsync.sh

四.定时任务执行

bash 复制代码
1.编辑定时任务
crontab -e
* * * * * /home/test/rsync.sh > /dev/null 2>&1 &
2.查看定时任务
crontab -l

* * * * * /home/test/rsync.sh > /dev/null 2>&1 &

小结

` 以上只是做了源同步到目标服务器,同样的目标同步到源,可以在目标服务器上做同样的操作即可,

相关推荐
莱茶荼菜17 分钟前
Ubuntu 20.04 系统库管理详细教程
linux·运维·ubuntu
迅为电子1 小时前
嵌入式Linux新手入门:北京迅为3568开发板驱动开发第二章helloworld 驱动实验
linux·运维·驱动开发
JanelSirry2 小时前
Redis服务器的的内存是多大
服务器·redis·github
稚麟2 小时前
GitHub Copilot CLI: How to get started
github·copilot
nono牛4 小时前
MTK平台详解`adb devices`输出的序列号组成
android·linux·adb·智能手机
逛逛GitHub4 小时前
老外吹爆的国产开源 AI 大模型,登顶了开源热榜第 1。
github
kyle~4 小时前
机器视觉---Intel RealSense SDK 2.0 开发流程
运维·c++·windows·深度相机·intel realsense
catoop5 小时前
Linux 自动清理临时文件配置
linux·服务器
Xの哲學5 小时前
Linux eMMC子系统深度解析:从硬件协议到内核实现
linux·网络·算法·架构·边缘计算
数智大号5 小时前
超云发布R2425存储服务器:以全栈自研引领国产存储新方向
运维·服务器