主機:
192.168.40.105 test5
192.168.40.106 test6html
系統
centos 6.8c++
必備的軟件包
inotify-tools-3.14.tar.gz
http://pan.baidu.com/s/1nvSAs1F
unison-2.40.128.tar.gz
http://pan.baidu.com/s/1slyFWi5
ocaml-3.12.1.tar.gz
http://pan.baidu.com/s/1mipwIoCvim
首先實現雙機互信
host5
生成密鑰對
[root@test5 ~]# ssh-keygen -t rsa -P ''
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
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:
46:e0:39:fc:e6:d6:d0:6c:72:f6:3b:fc:0f:1e:34:5b root@test5
The key's randomart image is:
+--[ RSA 2048]----+
| . |
| o o |
| = . |
| + o |
| S * o E|
| + B . . + |
| o ... + |
| . oo o |
| .oo..|
+-----------------+
把公鑰複製到遠程主機
[root@test5 ~]# ssh-copy-id -i .ssh/id_rsa.pub 192.168.40.106
The authenticity of host '192.168.40.106 (192.168.40.106)' can't be established.
RSA key fingerprint is 52:69:48:c4:70:3c:9c:91:c1:c2:c5:2b:c2:29:30:49.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.40.106' (RSA) to the list of known hosts.
root@192.168.40.106's password:
Now try logging into the machine, with "ssh '192.168.40.106'", and check in:centos
.ssh/authorized_keysbash
to make sure we haven't added extra keys that you weren't expecting.
測試,看是否不用輸入密碼直接執行遠程命令
[root@test5 ~]# ssh 192.168.40.106 'hostname'
test6服務器
host6
一樣執行實現密鑰進行認證
[root@test6 ~]# ssh-keygen -t rsa -P ''
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
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:
54:b3:69:7d:94:33:6c:73:9a:e8:6f:b4:cf:e6:87:b3 root@test6
The key's randomart image is:
+--[ RSA 2048]----+
| o ... |
| . = .B .|
| . + .o.B |
| . . ..o |
| S . |
| . . |
| o o |
| *.o|
| .E*=|
+-----------------+
[root@test6 ~]# ssh-copy-id -i .ssh/id_rsa.pub 192.168.40.105
The authenticity of host '192.168.40.105 (192.168.40.105)' can't be established.
RSA key fingerprint is c4:72:2a:50:9e:b6:9d:4b:86:0e:4d:e6:c5:e8:50:26.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.40.105' (RSA) to the list of known hosts.
root@192.168.40.105's password:
Now try logging into the machine, with "ssh '192.168.40.105'", and check in:app
.ssh/authorized_keysdom
to make sure we haven't added extra keys that you weren't expecting.ssh
[root@test6 ~]# ssh 192.168.40.105 'hostname'
test5ide
保持兩主機時間一致,經過ntpdate命令同步一臺時間服務器
兩主機都執行
ntpdate NTP_SERVER
備註:Linux下支持inotify的內核最小爲2.6.13,能夠輸入命令:uname -a查看內核
test5上
環境準備
#!/bin/bash
SRC="/data/oops"
DES=192.168.40.106
yum install -y automake gcc gcc-c++ libtool 1> /dev/null
tar xf ocaml-3.12.1.tar.gz
cd ocaml-3.12.1
./configure 1> /dev/null
make world.opt 1> /dev/null
make install 1> /dev/null
yum install -y ctags-etags 1> /dev/null
cd ..
tar xf unison-2.40.128.tar.gz
cd unison-2.40.128
#表示:使用命令方式,加入線程支持
make UISTYLE=text THREADS=true 1> /dev/null
make install 1> /dev/null
cp unison /usr/local/bin/
scp unison ${DES}:/usr/local/bin/
mkdir ~/.unison
cat > ~/.unison/default.prf << EOF
root = ${SRC}
root = ssh://root@${DES}/${SRC}
batch = true
maxthreads = 300
owner = true
group = true
perms = -1
silent = true
fastcheck = false
sshargs = -C
xferbycopying = true
log = true
logfile = /tmp/unison.log
EOF
cd ..
tar xf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure --prefix=/usr/local/inotify 1> /dev/null
make 1> /dev/null
make install 1> /dev/null
cd ~
cat > unison.sh << EOF
#!/bin/bash
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f %e' -e modify,delete,create,attrib ${SRC} | while read file
do
unison
echo "\${file} was rsynced" >> /tmp/rsync.log 2>&1
done
EOF
bash unison.sh &
test6上
#!/bin/bash
SRC="/data/oops"
DES=192.168.40.105
yum install -y automake gcc gcc-c++ libtool 1> /dev/null
tar xf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure --prefix=/usr/local/inotify 1> /dev/null
make 1> /dev/null
make install 1> /dev/null
mkdir ~/.unison
cat > ~/.unison/default.prf << EOF
root = ${SRC}
root = ssh://root@${DES}/${SRC}
batch = true
maxthreads = 300
owner = true
group = true
perms = -1
silent = true
fastcheck = false
sshargs = -C
xferbycopying = true
log = true
logfile = /tmp/unison.log
EOF
cd ~
cat > unison.sh << EOF
#!/bin/bash
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f %e' -e modify,delete,create,attrib ${SRC} | while read file
do
unison
echo "\${file} was rsynced" >> /tmp/rsync.log 2>&1
done
EOF
bash unison.sh &
參考連接:
http://www.cnblogs.com/MYSQLZOUQI/p/5184642.html
http://openskill.cn/article/161
測試:
新增文件
[root@test6 ~]# cp install.log /data/oops/
Contacting server...
[root@test6 ~]# Connected [//test5//data/oops -> //test6//data/oops]
Looking for changes
Warning: No archive files were found for these roots, whose canonical names are:
/data/oops
//test5//data/oops
This can happen either
because this is the first time you have synchronized these roots,
or because you have upgraded Unison to a new version with a different
archive format.
Update detection may take a while on this run if the replicas are
large.
Unison will assume that the 'last synchronized state' of both replicas
was completely empty. This means that any files that are different
will be reported as conflicts, and any files that exist only on one
replica will be judged as new and propagated to the other replica.
If the two replicas are identical, then no changes will be reported.
If you see this message repeatedly, it may be because one of your machines
is getting its address from DHCP, which is causing its host name to change
between synchronizations. See the documentation for the UNISONLOCALHOSTNAME
environment variable for advice on how to correct this.
Donations to the Unison project are gratefully accepted:
http://www.cis.upenn.edu/~bcpierce/unison
Waiting for changes from server
Reconciling changes
file ----> install.log
local : file modified on 2017-02-10 at 20:19:30 size 16949 --rw-r--r-- user=0 group=0
test5 : absent
Propagating updates
UNISON 2.40.128 started propagating changes at 20:19:31.99 on 10 Feb 2017
[BGN] Copying install.log from /data/oops to //test5//data/oops
[END] Copying install.log
UNISON 2.40.128 finished propagating changes at 20:19:32.01 on 10 Feb 2017
Saving synchronizer state
Synchronization complete at 20:19:32 (1 item transferred, 0 skipped, 0 failed)
Contacting server...
Connected [//test5//data/oops -> //test6//data/oops]
Looking for changes
Waiting for changes from server
Reconciling changes
Nothing to do: replicas have not changed since last sync.
[root@test6 ~]#
[root@test6 ~]#
[root@test6 ~]# ls /data/oops/
install.log
[root@test5 ~]# Contacting server...
Connected [//test5//data/oops -> //test6//data/oops]
Looking for changes
Waiting for changes from server
Reconciling changes
Nothing to do: replicas have not changed since last sync.
Contacting server...
Connected [//test5//data/oops -> //test6//data/oops]
Looking for changes
Waiting for changes from server
Reconciling changes
Nothing to do: replicas have not changed since last sync.
Contacting server...
Connected [//test5//data/oops -> //test6//data/oops]
Looking for changes
Waiting for changes from server
Reconciling changes
Nothing to do: replicas have not changed since last sync.
Contacting server...
Connected [//test5//data/oops -> //test6//data/oops]
Looking for changes
Waiting for changes from server
Reconciling changes
Nothing to do: replicas have not changed since last sync.
Contacting server...
Connected [//test5//data/oops -> //test6//data/oops]
Looking for changes
Waiting for changes from server
Reconciling changes
Nothing to do: replicas have not changed since last sync.
Contacting server...
Connected [//test5//data/oops -> //test6//data/oops]
Looking for changes
Waiting for changes from server
Reconciling changes
Nothing to do: replicas have not changed since last sync.
Contacting server...
Connected [//test5//data/oops -> //test6//data/oops]
Looking for changes
Waiting for changes from server
Reconciling changes
Nothing to do: replicas have not changed since last sync.
[root@test5 ~]# ls /data/oops/
install.log
兩主機上都有了
取消顯示這些信息
在配置文件中添加
silent = true
[root@test5 ~]# vim .unison/default.prf
[root@test5 ~]# ps aux | grep unison
root 40104 0.0 0.0 106108 1148 pts/1 S 20:17 0:00 bash unison.sh
root 40106 0.0 0.0 106112 724 pts/1 S 20:17 0:00 bash unison.sh
root 40143 0.0 0.0 103320 844 pts/1 S+ 20:22 0:00 grep unison
[root@test5 ~]# kill 40104 40106
[root@test5 ~]# ps aux | grep unison
root 40146 0.0 0.0 103316 832 pts/1 S+ 20:23 0:00 grep unison
[root@test6 ~]# vim .unison/default.prf
[root@test6 ~]# ps aux | grep unison
root 32015 0.0 0.0 106108 1148 pts/1 S 20:17 0:00 bash unison.sh
root 32017 0.0 0.0 106112 720 pts/1 S 20:17 0:00 bash unison.sh
root 32071 0.0 0.0 103316 836 pts/1 S+ 20:22 0:00 grep unison
[root@test6 ~]# kill 32015 32017
[root@test6 ~]# ps aux | grep unison
root 32074 0.0 0.0 103316 836 pts/1 S+ 20:23 0:00 grep unison
再次啓動
兩主機都執行
bash unison.sh &
[root@test5 ~]# cp inotify-tools-3.14.tar.gz /data/oops/
[root@test5 ~]# ls /data/oops/
inotify-tools-3.14.tar.gz install.log
[root@test6 ~]# ls /data/oops/
inotify-tools-3.14.tar.gz install.log
刪除文件
[root@test6 ~]# rm -rf /data/oops/install.log
[root@test6 ~]# ls /data/oops/
inotify-tools-3.14.tar.gz
[root@test5 ~]# ls /data/oops/
inotify-tools-3.14.tar.gz
OK兩邊文件都保持了一致了