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

简介:

rsync:是类unix系统下的数据镜像和快速增量备份的工具(remote sync).远程同步支持本地复制,或

       者与其他SSH、rsync主机同步,但不能做到实时同步.

Inotify:是监控文件系统操作,比如读取,写入和创建.Inotify反应灵敏,用法非常简单,并且比cron任务

       的繁忙轮询高效得多.

Unison:是一款跨windows/linux/MAC OS平台的文件同步工具,不仅支持本地对本地同步,也支持通过

        SSH、RSH和Socket等网络协议进行同步.更棒的是Unison支持双向同步操作,你既可以从A同步到

        B,也可以从B同步到A,这些都不需要额外的设定.

 

环境规划:

   IP              主机名

192.168.1.247      tong1

192.168.1.248      tong2

      

一.利用rsync+ssh工具对文件同步(用计划任务定时同步)

步骤:

(1)在文件服务端和文件镜像端都要ssh互信

(2)在文件服务端安装rsync软件

(3)在文件镜像端也要安装rsync软件


1.下载安装rsync软件和ssh互相信任

[root@tong1 ~]# hostname 
tong1
[root@tong1 ~]# cat /etc/hosts
192.168.1.247 tong1
192.168.1.248 tong2

[root@tong1 ~]# wget https://download.samba.org/pub/rsync/src/rsync-3.1.1.tar.gz

[root@tong1 ~]# tar xvf rsync-3.1.1.tar.gz

[root@tong1 ~]# cd rsync-3.1.1
[root@tong1 rsync-3.1.1]# ./configure --prefix=/usr/local/rsync-3.1.1

[root@tong1 rsync-3.1.1]# make && make install

[root@tong1 rsync-3.1.1]# echo $?
0

[root@tong1 ~]# 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:
79:cb:ca:73:40:9e:5c:2b:46:7f:67:b1:b2:35:8a:91 root@tong1
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|                 |
|        o..   .  |
|       =S+.o   o |
|        BoE.o *  |
|       . oo+ B . |
|       ...o o    |
|        oo       |
+-----------------+

[root@tong1 ~]# scp /root/.ssh/id_rsa.pub tong2:/root/.ssh/authorized 
root@tong2's password: 
id_rsa.pub                           100%  392     0.4KB/s   00:00    
[root@tong1 ~]#

 

tong2节点:

[root@tong2 ~]# 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:
79:cb:ca:73:40:9e:5c:2b:46:7f:67:b1:b2:35:8a:91 root@tong2
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|                 |
|        o..   .  |
|       =S+.o   o |
|        BoE.o *  |
|       . oo+ B . |
|       ...o o    |
|        oo       |
+-----------------+

[root@tong2 ~]# cd .ssh/

[root@tong2 .ssh]# cat id_rsa.pub >> authorized_keys 
[root@tong2 .ssh]# scp authorized_keys  192.168.1.247:/root/.ssh/

[root@tong2 .ssh]# ssh tong1 date
Wed May  6 11:28:19 CST 2015
[root@tong2 .ssh]# ssh tong2 date
Wed May  6 11:31:24 CST 2015
[root@tong2 .ssh]#


2.编辑配置文件和启动服务

tong1节点:

[root@tong1 .ssh]# cd /etc/
[root@tong1 etc]# vim rsyncd.conf 
uid=root             --rsync守护进程的用户
gid=root
use chroot=no        --不能跳到上级目录
max_connect=4        --允许4个客户端连接
strict modes=yes     --脚本模式
port=873             --监听端口

[backup]
path=/home/rsync             --服务根目录
comment=This is a test       --描述信息
ignore errors                --忽略IO错误
read only=yes                --只读
list=no                      --不能列出文件
auth users=hening            --认证用户名
secrets file=/etc/rsync.pas    --密钥文件
hosts allow=192.168.1.248      --允许哪个IP访问

pid file=/var/run/rsync.pid          --PID文件
lock file=/var/run/rsync.lock        
log file=/var/log/rsync.log          --日志文件

[root@tong1 etc]# vim rsync.pas      --密钥文件内容
hening:111111                        --用户名和密码(这个用户名不是系统用户名,是rsync客户端登陆时用的)

[root@tong1 etc]# chmod 600 /etc/rsync.pas 
[root@tong1 etc]# chown root:root /etc/rsync.pas

[root@tong1 etc]# vim rsync.motd     --登陆后的提示信息
Welcome to use the rsync services!

[root@tong1 etc]# mkdir /home/rsync/{1,2} -pv

[root@tong1 etc]# touch /home/rsync/1.txt

[root@tong1 etc]# /usr/local/rsync-3.1.1/bin/rsync --daemon

[root@tong1 etc]# netstat -antup |grep rsync
tcp        0      0 0.0.0.0:873      0.0.0.0:*        LISTEN      3352/rsync          
tcp        0      0 :::873           :::*             LISTEN      3352/rsync          
[root@tong1 etc]# vim /etc/rc.local 

/usr/local/rsync-3.1.1/bin/rsync --daemon

[root@tong1 etc]#


3.配置rsync客户端

tong2节点:

[root@tong2 ~]# mkdir /home/rsync

[root@tong2 ~]# vim /etc/rsync.pas

111111                 --只写密码就可以了

[root@tong2 ~]# chmod 600 /etc/rsync.pas 
[root@tong2 ~]# chown root:root /etc/rsync.pas 

[root@tong2 rsync]# /usr/local/rsync-3.1.1/bin/rsync -vzrtopg --progress --delete hening@192.168.1.247::backup /home/rsync --password-file=/etc/rsync.pas   --测试同步文件
receiving incremental file list
./

1/
2/

1.txt
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=2/4)

sent 48 bytes  received 155 bytes  135.33 bytes/sec
total size is 0  speedup is 0.00
[root@tong2 rsync]# ll /home/rsync/
total 8
drwxr-xr-x. 2 root root 4096 May  6 11:58 1
-rw-r--r--. 1 root root    0 May  6 13:08 1.txt
drwxr-xr-x. 2 root root 4096 May  6 11:58 2
[root@tong2 ~]# vim /usr/local/rsync-3.1.1/bin/rsync.sh    --将命令保存为脚本同步文件

#!/bin/bash
/usr/local/rsync-3.1.1/bin/rsync -vzrtopg --progress --delete hening@192.168.1.247::backup /home/rsync --password-file=/etc/rsync.pas

[root@tong2 ~]# crontab -e
*/1 * * * * /usr/local/rsync-3.1.1/bin/rsync.sh >/dev/null 2>&1   --每一分钟同步一次

[root@tong2 ~]#


rsync命令参数详解:

-v          表示显示verbose详细显示

-z          表示压缩文件

-r          表示递归

-t          保持原文件创建的时间

-o          保持原文件的属主

-g          保持原文件属组

-p          保持原文件的参数

-a          存档模式

--progress  详细信息进度情况

--delete    服务器端删除文件时,客户端也删除

--password-file    密码文件

hening             在服务端指定的用户,不是系统用户(auth users=hening)

192.168.1.247      服务端的IP地址

::backup           服务端配置文件的模块([backup])


二.rsync和Inotify文件实时同步(脚本在后台运行就行了)

步骤:

(1)在文件服务端安装rsync软件

(2)在文件镜像端安装rsync和Inotify软件

(3)在文件镜像端编辑脚本使文件同步


1.在tong1和tong2节点都下载安装rsync软件(安装如上)

2.在tong2节点下载安装inotify-tools工俱

tong2节点:

[root@tong2 ~]# wget http://ncu.dl.sourceforge.net/project/inotify-tools/inotify-tools/3.13/inotify-tools-3.13.tar.gz

[root@tong2 ~]# tar xvf inotify-tools-3.13.tar.gz 
[root@tong2 ~]# cd inotify-tools-3.13
[root@tong2 inotify-tools-3.13]# ./configure  --prefix=/usr/local/inotify-tools-3.13 && make && make install

[root@tong2 inotify-tools-3.13]# cd /usr/local/inotify-tools-3.13/bin/
[root@tong2 bin]# ll
total 80
-rwxr-xr-x. 1 root root 38598 May  6 13:29 inotifywait
-rwxr-xr-x. 1 root root 40369 May  6 13:29 inotifywatch
[root@tong2 bin]# vim inotify_rsync.sh 

#!/bin/bash

srcip=192.168.1.247
desip=192.168.1.248
descdir=/home/rsync
srcdir=/home/rsync

/usr/local/inotify-tools-3.13/bin/inotifywait -mr --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -e close_write,modify,delete,create,attrib $srcdir |
while read DATA TIME FILE DIR; do
   /usr/local/rsync-3.1.1/bin/rsync -vzrtopg --progress --delete /home/rsync  hening@192.168.1.248::backup --password-file=/etc/rsync.pas & > /dev/null 2>&1 && echo "
At ${TIME} on ${DATE},file was backed up via rsync" >> /var/log/rsync.log
done      

[root@tong2 bin]# ./inotify_rsync.sh  &       --后台运行脚本
[1] 23941
[root@tong2 bin]# 


3.测试文件是否同步

tong1节点:

[root@tong1 ~]# cd /home/rsync/
[root@tong1 rsync]# mkdir 1 2 3
[root@tong1 rsync]# touch a.txt b.txt c.xls
[root@tong1 rsync]# ll
total 12
drwxr-xr-x. 2 root root 4096 May  6 15:35 1
drwxr-xr-x. 2 root root 4096 May  6 15:35 2
drwxr-xr-x. 2 root root 4096 May  6 15:35 3
-rw-r--r--. 1 root root    0 May  6 15:35 a.txt
-rw-r--r--. 1 root root    0 May  6 15:35 b.txt
-rw-r--r--. 1 root root    0 May  6 15:35 c.xls
[root@tong1 rsync]# 


tong2节点:

[root@tong2 ~]# cd /home/rsync/          --如果两边文件不能同步就删除镜像服务器的文件

[root@tong2 rsync]# ll
total 12
drwxr-xr-x. 2 root root 4096 May  6 15:35 1
drwxr-xr-x. 2 root root 4096 May  6 15:35 2
drwxr-xr-x. 2 root root 4096 May  6 15:35 3
-rw-r--r--. 1 root root    0 May  6 15:35 a.txt
-rw-r--r--. 1 root root    0 May  6 15:35 b.txt
-rw-r--r--. 1 root root    0 May  6 15:35 c.xls
[root@tong2 rsync]# 


inotify参数详解:

inotifywait -mr --timefmt '%d/%m/%y %H:%M'  

-m     --一直保持监听

-r     --递归查看

-e close_write,modify,delete,create,attrib

-e     --是指监听创建,移动,删除,写入权限


三.unison+inotify文件双向同步,无法实现实时双向同步

1.下载安装ocaml,unison软件

tong1节点和tong2节点一样:

[root@tong1 ~]# wget http://caml.inria.fr/pub/distrib/ocaml-3.12/ocaml-3.12.1.tar.gz

[root@tong1 ~]# tar xvf ocaml-3.12.1.tar.gz

[root@tong1 ~]# cd ocaml-3.12.1

[root@tong1 ocaml-3.12.1]# ./configure 

[root@tong1 ocaml-3.12.1]# make world

[root@tong1 ocaml-3.12.1]# make opt

[root@tong1 ocaml-3.12.1]# make install

[root@tong1 ocaml-3.12.1]# cd ..

[root@tong1 ~]# wget http://www.seas.upenn.edu/~bcpierce/unison//download/releases/unison-2.40.63/unison-2.40.63.tar.gz

[root@tong1 ~]# tar xvf unison-2.40.63.tar.gz 
[root@tong1 ~]# cd unison-2.40.63
[root@tong1 unison-2.40.63]# make UISTYLE=text

[root@tong1 unison-2.40.63]# mkdir /root/bin

[root@tong1 unison-2.40.63]# make install

[root@tong1 unison-2.40.63]# cp -a  /root/bin/*  /usr/bin/


2.创建脚本对文件实时同步

tong1节点:

[root@tong1 unison-2.40.63]# cd /usr/local/inotify-tools-3.13/bin/
[root@tong1 bin]# vim inotify_unison.sh

#!/bin/bash
ip2=192.168.1.248
src2=/home/rsync
dst2=/home/rsync

/usr/local/inotify-tools-3.13/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -e close_write,modify,delete,create,attrib $src2 |
while read line; do
     /usr/bin/unison-2.40 -batch $src1 ssh://$ip2/$dst2 >> /var/log/rsync.log
done

[root@tong1 bin]#


tong2节点:

[root@tong2 unison-2.40.63]# cd /usr/local/inotify-tools-3.13/bin/
[root@tong2 bin]# vim inotify_unison.sh

#!/bin/bash
ip1=192.168.1.247
src1=/home/rsync
dst1=/home/rsync

/usr/local/inotify-tools-3.13/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -e close_write,modify,delete,create,attrib $src1 |
while read line; do
     /usr/bin/unison-2.40 -batch $src1 ssh://$ip1/$dst1 >> /var/log/rsync.log
done

[root@tong2 bin]#










本文转自 z597011036 51CTO博客,原文链接:http://blog.51cto.com/tongcheng/1341145,如需转载请自行联系原作者
目录
相关文章
|
4天前
|
Ubuntu 安全 Linux
《Linux 简易速速上手小册》第1章: Linux 系统基础(2024 最新版)
《Linux 简易速速上手小册》第1章: Linux 系统基础(2024 最新版)
36 1
|
11天前
|
资源调度 JavaScript 搜索推荐
Linux系统之部署envlinks极简个人导航页
【4月更文挑战第11天】Linux系统之部署envlinks极简个人导航页
52 2
|
14天前
|
缓存 Linux 测试技术
安装【银河麒麟V10】linux系统--并挂载镜像
安装【银河麒麟V10】linux系统--并挂载镜像
72 0
|
14天前
|
监控 Unix Linux
Linux操作系统调优相关工具(四)查看Network运行状态 和系统整体运行状态
Linux操作系统调优相关工具(四)查看Network运行状态 和系统整体运行状态
29 0
|
12天前
|
存储 算法 Linux
【实战项目】网络编程:在Linux环境下基于opencv和socket的人脸识别系统--C++实现
【实战项目】网络编程:在Linux环境下基于opencv和socket的人脸识别系统--C++实现
34 6
|
14天前
|
Linux 网络安全
linux免密登录报错 Bad owner or permissions on /etc/ssh/ssh_config.d/05-redhat.conf
linux免密登录报错 Bad owner or permissions on /etc/ssh/ssh_config.d/05-redhat.conf
23 1
|
2天前
|
资源调度 JavaScript Ubuntu
Linux系统之部署briefing视频聊天系统
【4月更文挑战第21天】Linux系统之部署briefing视频聊天系统
26 2
|
3天前
|
Linux Perl
Linux系统替换字符串常用命令
请注意,`sed`命令可以非常强大,可以根据不同的需求使用不同的选项和正则表达式来进行更复杂的字符串替换操作。
16 0
|
4天前
|
网络协议 安全 Linux
IDEA通过内网穿透实现固定公网地址远程SSH连接本地Linux服务器
IDEA通过内网穿透实现固定公网地址远程SSH连接本地Linux服务器
|
4天前
|
存储 网络协议 Linux
如何使用内网穿透工具实现远程SSH访问Deepin系统
如何使用内网穿透工具实现远程SSH访问Deepin系统