阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Linux系统文件同步rsync+ssh+inotify+unison实现

480次阅读
没有评论

共计 8161 个字符,预计需要花费 21 分钟才能阅读完成。

1. 用 rsync+ssh 软件做文件定时备份,增量备份,但不是做到实时备份,所以单独使用效果不是很好.
服务器 A(ip:2.2.2.135):
[root@CentOS ~]# ifconfig  eth0      — 查看 IP 地址
eth0      Link encap:Ethernet  HWaddr 08:00:27:56:AA:AA
inet addr:2.2.2.135  Bcast:2.255.255.255  Mask:255.0.0.0
inet6 addr: fe80::a00:27ff:fe56:aaaa/64 Scope:Link
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:13072 errors:0 dropped:0 overruns:0 frame:0
TX packets:328 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1009592 (985.9 KiB)  TX bytes:31733 (30.9 KiB)
[root@centos ~]# vim /etc/hosts    – 添加以下行
2.2.2.145 centos1
2.2.2.135 centos
[root@centos ~]# yum install rsync    – 安装 rsync 软件,记住两台电脑必须全部安装
[root@centos ~]# ssh-keygen  -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
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:
12:c6:7b:6a:12:4a:15:c8:8b:0f:90:54:02:fa:08:27 root@centos
The key’s randomart image is:
+–[RSA 2048]—-+
|+=.o.            |
|+ +  o          |
|E…. +          |
|+=.. . o        |
|.oo . o S        |
| … . +        |
|  . . o          |
|    o          |
|                |
+—————–+
[root@centos ~]# scp ~/.ssh/id_rsa.pub  2.2.2.145:/root/.ssh/authorized_keys  – 将公钥放到别一台服务器上
[root@centos ~]# ssh 2.2.2.145    – 登陆不用密码了
Last login: Tue Dec 17 09:46:05 2013 from centos
[root@centos1 ~]#

服务器 B(ip:2.2.2.145):
[root@centos1 ~]$ ifconfig  eth0    – 查看 IP 地址
eth0      Link encap:Ethernet  HWaddr 08:00:27:56:AA:AA
inet addr:2.2.2.145  Bcast:2.2.2.255  Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe56:aaaa/64 Scope:Link
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:14005 errors:0 dropped:0 overruns:0 frame:0
TX packets:503 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1081748 (1.0 MiB)  TX bytes:46832 (45.7 KiB)
[root@centos1 ~]$ vim /etc/hosts    – 添加以下两行
2.2.2.135  centos
2.2.2.145  centos1
[root@centos1 ~]$ ping 2.2.2.135 -c 1      – 是否相通
PING 2.2.2.135 (2.2.2.135) 56(84) bytes of data.
64 bytes from 2.2.2.135: icmp_seq=1 ttl=64 time=0.438 ms
— 2.2.2.135 ping statistics —
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.438/0.438/0.438/0.000 ms
[root@centos ~]# yum install rsync
PING 2.2.2.135 (2.2.2.135) 56(84) bytes of data.
64 bytes from 2.2.2.135: icmp_seq=1 ttl=64 time=0.438 ms
— 2.2.2.135 ping statistics —
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.438/0.438/0.438/0.000 ms
[root@centos ~]# yum install rsync    – 安装软件
[root@centos1 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
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:
4b:74:44:ca:38:50:17:25:3a:1e:b0:ac:a4:7d:f3:be root@centos1
The key’s randomart image is:
+–[RSA 2048]—-+
|    o.. +++      |
|  . + = +      |
|  . o * + .      |
| + . . = .      |
|. o o . S        |
|  . o . .      |
|      . .        |
|    .          |
|      E.        |
+—————–+
[root@centos1 ~]# scp ~/.ssh/id_rsa.pub  2.2.2.135:/root/.ssh/authorized_keys
[root@centos1 ~]$ rsync  2.2.2.135:/home/*.txt /home/    – 远程复制文件,- r 是目录
[root@centos1 ~]$ ll /home/*.txt        – 复制成功
-rw-r–r– 1 root root 36 Dec 16 14:40 /home/1.txt
-rw-r–r– 1 root root 12 Dec 16 14:34 /home/2.txt
-rw-r–r– 1 root root  0 Dec 16 15:48 /home/3.txt
[root@centos1 ~]$ crontab  -e
* 24 * * *  /usr/bin/rsync 2.2.2.135:/home/*.txt /home/    – 每天 24 点备份

2.rsync+ssh+inotify 做文件实时备份工作 (inotify 文件系统监控)
服务器 A(ip:2.2.2.135): 备份文件

[root@centos1 samba]$ tar xvf inotify-tools-3.14.tar.gz
[root@centos1 samba]$ cd inotify-tools-3.14
[root@centos1 inotify-tools-3.14]$ ./configure  –prefix=/usr/local/inotify && make && make install    – 安装软件
[root@centos inotify-tools-3.14]# cd /usr/local/inotify/
[root@centos inotify]# ll
total 16
drwxr-xr-x. 2 root root 4096 Dec 16 16:08 bin
drwxr-xr-x. 3 root root 4096 Dec 16 16:08 include
drwxr-xr-x. 2 root root 4096 Dec 16 16:08 lib
drwxr-xr-x. 4 root root 4096 Dec 16 16:08 share
[root@centos inotify]# cd bin/
[root@centos bin]# ll
total 72
-rwxr-xr-x. 1 root root 36447 Dec 16 16:08 inotifywait
-rwxr-xr-x. 1 root root 34961 Dec 16 16:08 inotifywatch
[root@centos bin]# vim start.sh
#!/bin/sh
SRC=/home/
DST=/home/
IP=2.2.2.145
/usr/local/inotify/bin/inotifywait -mrq -e modify,delete,create,attrib ${SRC} | while read status
do
/usr/bin/rsync  -auvg  –delete  $DST  $IP:$SRC  &&    – 在后台执行
if (echo “$?” == “0”);then
echo `date + %D`:back ok >> /home/1.log  – 在 date 前面有反单引号
else
echo `date +%D`:error >> /home/1.log
done
[root@centos1 bin]# chmod o+x start.sh
[root@centos1 bin]# ./start.sh  &
[2] 2915
[root@centos1 bin]# ll /home/1
total 8
-rw-r–r– 1 root root 36 Dec 16 14:40 1.txt
-rw-r–r– 1 root root 12 Dec 16 14:34 2.txt
-rw-r–r– 1 root root  0 Dec 17 10:58 3.txt
[root@centos1 bin]# mkdir /home/1/{1,2,3}
[root@centos1 bin]# ll /home/1
total 20
drwxr-xr-x 2 root root 4096 Dec 17 11:31 1
-rw-r–r– 1 root root  36 Dec 16 14:40 1.txt
drwxr-xr-x 2 root root 4096 Dec 17 11:31 2
-rw-r–r– 1 root root  12 Dec 16 14:34 2.txt
drwxr-xr-x 2 root root 4096 Dec 17 11:31 3
-rw-r–r– 1 root root    0 Dec 17 10:58 3.txt
[root@centos1 bin]# cat /home/1.log    – 日志有记录,成功了
:back ok
:back ok
:back ok
:back ok
[root@centos1 bin]#    – 去 2.2.2.145 看看文件有没有同步

服务器 B(ip:2.2.2.145):用于读文件
[root@centos1 1]# ifconfig  eth0
eth0      Link encap:Ethernet  HWaddr 08:00:27:56:AA:AA
inet addr:2.2.2.145  Bcast:2.2.2.255  Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe56:aaaa/64 Scope:Link
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:38855 errors:0 dropped:0 overruns:0 frame:0
TX packets:12226 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:3390394 (3.2 MiB)  TX bytes:1557639 (1.4 MiB)
[root@centos1 1]# ll /home/1    – 服务器 B 可以看到了
total 20
drwxr-xr-x 2 root root 4096 Dec 17 11:31 1
-rw-r–r– 1 root root  36 Dec 16 14:40 1.txt
drwxr-xr-x 2 root root 4096 Dec 17 11:31 2
-rw-r–r– 1 root root  12 Dec 16 14:34 2.txt
drwxr-xr-x 2 root root 4096 Dec 17 11:31 3
-rw-r–r– 1 root root    0 Dec 17 10:58 3.txt

3.unison+ssh 文件双向同步, 无法实现实时双向同步
1) 安装 unison 软件
[root@centos1 samba]# tar xvf ocaml-3.12.1.tar.gz
[root@centos1 samba]# cd ocaml-3.12.1
[root@centos1 ocaml-4.01.0]# ./configure  && make && make install
[root@centos1 ocaml-4.01.0]# cd ..
[root@centos1 samba]# tar xvf unison-2.40.63.tar.gz
[root@centos1 samba]# cd unison-2.40.63
[root@centos1 unison-2.40.63]# make UISTYLE=text
[root@centos1 unison-2.40.63]# mkdir /root/bin
[root@centos1 unison-2.40.63]# make install
[root@centos1 unison-2.40.63]# cp /root/bin/unison /usr/bin

2)ssh 登陆不用密(在服务器 1 登陆服 2 不用密码)
[root@centos home]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
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:
e3:70:96:80:20:8b:e8:a9:6f:7a:00:64:5f:8b:86:90 root@centos
The key’s randomart image is:
+–[RSA 2048]—-+
|…              |
|E= . o          |
|B o + o          |
|o..+ . . .      |
|.o.  . S        |
|o      = .      |
|..      .        |
| .o              |
|.+.              |
+—————–+
[root@centos home]# scp ~/.ssh/id_rsa.pub  2.2.2.145:/root/.ssh/authorized_keys
root@2.2.2.145’s password:
id_rsa.pub                                                                              100%  393    0.4KB/s  00:00
[root@centos home]# ssh 2.2.2.145  – 成功, 不用输入密码
Last login: Tue Dec 17 13:25:15 2013 from centos
[root@centos1 ~]#

3)服务器 2 登陆服务器 1 不用密码
[root@centos1 unison-2.40.63]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
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:
be:43:3a:24:4c:83:fe:bd:22:e2:ed:c2:89:7e:ab:c9 root@centos1
The key’s randomart image is:
+–[RSA 2048]—-+
|                |
|                |
|  .            |
|  . o            |
| . o .  S        |
|  . o …        |
|o .. + o.        |
|+++.o + ..      |
|oE==…o..      |
+—————–+
[root@centos1 unison-2.40.63]# scp ~/.ssh/id_rsa.pub 2.2.2.135:/root/.ssh/authorized_keys
[root@centos1 unison-2.40.63]# ssh 2.2.2.135
Last login: Tue Dec 17 13:26:06 2013 from centos1
[root@centos ~]#

4)服务器 1,2 文件同步
[root@centos1 bin]# unison /home ssh://root@2.2.2.145:/home    – 文件同步
[root@centos1 bin]# unison /home ssh://root@2.2.2.145:/home –path  目录  – 只同步指定的目录

Rsync 的详细介绍:请点这里
Rsync 的下载地址:请点这里

推荐阅读:

利用 inotifywait 监控主机文件和目录 http://www.linuxidc.com/Linux/2013-03/81075.htm

利用 inotify+rsync 实现 Linux 文件批量更新 http://www.linuxidc.com/Linux/2012-01/52132.htm

inotify-tools+rsync 实时同步文件安装和配置 http://www.linuxidc.com/Linux/2012-06/63624.htm

rsync 同步完整配置 http://www.linuxidc.com/Linux/2013-06/85781.htm

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-20发表,共计8161字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中

星哥玩云

星哥玩云
星哥玩云
分享互联网知识
用户数
4
文章数
19351
评论数
4
阅读量
7995929
文章搜索
热门文章
星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛 NAS-6:抖音视频同步工具,视频下载自动下载保存 前言 各位玩 NAS 的朋友好,我是星哥!...
星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛 NAS-3:安装飞牛 NAS 后的很有必要的操作 前言 如果你已经有了飞牛 NAS 系统,之前...
我把用了20年的360安全卫士卸载了

我把用了20年的360安全卫士卸载了

我把用了 20 年的 360 安全卫士卸载了 是的,正如标题你看到的。 原因 偷摸安装自家的软件 莫名其妙安装...
再见zabbix!轻量级自建服务器监控神器在Linux 的完整部署指南

再见zabbix!轻量级自建服务器监控神器在Linux 的完整部署指南

再见 zabbix!轻量级自建服务器监控神器在 Linux 的完整部署指南 在日常运维中,服务器监控是绕不开的...
飞牛NAS中安装Navidrome音乐文件中文标签乱码问题解决、安装FntermX终端

飞牛NAS中安装Navidrome音乐文件中文标签乱码问题解决、安装FntermX终端

飞牛 NAS 中安装 Navidrome 音乐文件中文标签乱码问题解决、安装 FntermX 终端 问题背景 ...
阿里云CDN
阿里云CDN-提高用户访问的响应速度和成功率
随机文章
星哥带你玩飞牛NAS-12:开源笔记的进化之路,效率玩家的新选择

星哥带你玩飞牛NAS-12:开源笔记的进化之路,效率玩家的新选择

星哥带你玩飞牛 NAS-12:开源笔记的进化之路,效率玩家的新选择 前言 如何高效管理知识与笔记,已经成为技术...
每年0.99刀,拿下你的第一个顶级域名,详细注册使用

每年0.99刀,拿下你的第一个顶级域名,详细注册使用

每年 0.99 刀,拿下你的第一个顶级域名,详细注册使用 前言 作为长期折腾云服务、域名建站的老玩家,星哥一直...
飞牛NAS中安装Navidrome音乐文件中文标签乱码问题解决、安装FntermX终端

飞牛NAS中安装Navidrome音乐文件中文标签乱码问题解决、安装FntermX终端

飞牛 NAS 中安装 Navidrome 音乐文件中文标签乱码问题解决、安装 FntermX 终端 问题背景 ...
三大开源投屏神器横评:QtScrcpy、scrcpy、escrcpy 谁才是跨平台控制 Android 的最优解?

三大开源投屏神器横评:QtScrcpy、scrcpy、escrcpy 谁才是跨平台控制 Android 的最优解?

  三大开源投屏神器横评:QtScrcpy、scrcpy、escrcpy 谁才是跨平台控制 Andr...
自己手撸一个AI智能体—跟创业大佬对话

自己手撸一个AI智能体—跟创业大佬对话

自己手撸一个 AI 智能体 — 跟创业大佬对话 前言 智能体(Agent)已经成为创业者和技术人绕...

免费图片视频管理工具让灵感库告别混乱

一言一句话
-「
手气不错
开源MoneyPrinterTurbo 利用AI大模型,一键生成高清短视频!

开源MoneyPrinterTurbo 利用AI大模型,一键生成高清短视频!

  开源 MoneyPrinterTurbo 利用 AI 大模型,一键生成高清短视频! 在短视频内容...
300元就能买到的”小钢炮”?惠普7L四盘位小主机解析

300元就能买到的”小钢炮”?惠普7L四盘位小主机解析

  300 元就能买到的 ” 小钢炮 ”?惠普 7L 四盘位小主机解析 最近...
如何安装2026年最强个人助理ClawdBot、完整安装教程

如何安装2026年最强个人助理ClawdBot、完整安装教程

如何安装 2026 年最强个人助理 ClawdBot、完整安装教程 一、前言 学不完,根本学不完!近期,一款名...
免费无广告!这款跨平台AI RSS阅读器,拯救你的信息焦虑

免费无广告!这款跨平台AI RSS阅读器,拯救你的信息焦虑

  免费无广告!这款跨平台 AI RSS 阅读器,拯救你的信息焦虑 在算法推荐主导信息流的时代,我们...
星哥带你玩飞牛NAS硬件02:某鱼6张左右就可拿下5盘位的飞牛圣体NAS

星哥带你玩飞牛NAS硬件02:某鱼6张左右就可拿下5盘位的飞牛圣体NAS

星哥带你玩飞牛 NAS 硬件 02:某鱼 6 张左右就可拿下 5 盘位的飞牛圣体 NAS 前言 大家好,我是星...