unison做文件双向同步
Posted surplus
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unison做文件双向同步相关的知识,希望对你有一定的参考价值。
- [root@nginx ~]# cd /usr/src/
- [root@nginx src]# ll
- total 16
- drwxr-xr-x 2 root root 4096 Jan 26 2010 debug
- drwxr-xr-x 2 root root 4096 Jan 26 2010 kernels
- [root@nginx src]# wget http://rsync.samba.org/ftp/rsync/src/rsync-3.0.9.tar.gz
- [root@nginx src]# tar zxvf rsync-3.0.9.tar.gz
- [root@nginx src]# cd rsync-3.0.9
- [root@nginx rsync-3.0.9]# ./configure --prefix=/usr/local/rsync
- [root@nginx rsync-3.0.9]# make
- [root@nginx rsync-3.0.9]# make install
- [root@nginx rsync-3.0.9]# cd /usr/local/rsync/
- [root@nginx rsync]# echo "rsync-pwd" >/usr/local/rsync/rsync.passwd
- [root@nginx rsync]# chmod 600 rsync.passwd
- password file must not be other-accessible
- continuing without password file
- [root@nginx rsync]# cd /usr/src/
- [root@nginx src]# wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
- [root@nginx src]# tar zxvf inotify-tools-3.14.tar.gz
- [root@nginx src]# cd inotify-tools-3.14
- [root@nginx inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify
- [root@nginx inotify-tools-3.14]# make
- [root@nginx inotify-tools-3.14]# make install
- #!/bin/bash
- host=192.168.10.221
- src=/tmp/
- des=web
- user=root
- /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 files
- do
- /usr/bin/rsync -vzrtopg --delete --progress --password-file=/usr/local/rsync/rsync.passwd $src $user@$host::$des
- echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
- done
- [root@nginx tmp]# chmod 764 rsync.sh
- [root@nginx tmp]# sh /tmp/rsync.sh &
- rsync: failed to connect to 192.168.10.221: Connection refused (111)
- rsync error: error in socket IO (code 10) at clientserver.c(107) [sender=2.6.8]
- [root@nginx tmp]# echo "/tmp/rsync.sh" >> /etc/rc.local
二、备份服务器(client,我这里为nginx-backup)
1、安装rsync(备份服务器只安装rsync)
- [root@nginx-backup ~]# cd /usr/src/
- [root@nginx-backup src]# ll
- total 16
- drwxr-xr-x 2 root root 4096 Jan 26 2010 debug
- drwxr-xr-x 2 root root 4096 Jan 26 2010 kernels
- [root@nginx-backup src]# wget http://rsync.samba.org/ftp/rsync/src/rsync-3.0.9.tar.gz
- [root@nginx-backup src]# tar zxvf rsync-3.0.9.tar.gz
- [root@nginx-backup src]# cd rsync-3.0.9
- [root@nginx-backup rsync-3.0.9]# ./configure --prefix=/usr/local/rsync
- [root@nginx-backup rsync-3.0.9]# make
- [root@nginx-backup rsync-3.0.9]# make install
- [root@nginx-backup rsync-3.0.9]# echo "webuser:rsync-pwd" > /usr/local/rsync/rsync.passwd
- [root@nginx-backup rsync]# chmod 600 rsync.passwd
- uid = root
- gid = root
- use chroot = no
- max connections = 10
- strict modes = yes
- pid file = /var/run/rsyncd.pid
- lock file = /var/run/rsync.lock
- log file = /var/log/rsyncd.log
- [web]
- path = /tmp/
- comment = web file
- ignore errors
- read only = no
- write only = no
- hosts allow = 192.168.10.220
- hosts deny = *
- list = false
- uid = root
- gid = root
- auth users = root
- secrets file = /usr/local/rsync/rsync.passwd
- [root@nginx-backup rsync]# /usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/rsync.conf
- /usr/local/rsync/bin/rsync: error while loading shared libraries: libiconv.so.2: cannot open shared object file: No such file or directory,
- [root@nginx-backup rsync]# whereis libiconv.so.2
- libiconv.so: /usr/local/lib/libiconv.so.2 /usr/local/lib/libiconv.so
- [root@nginx-backup rsync]# echo "/usr/local/lib/" >> /etc/ld.so.conf
- [root@nginx-backup rsync]# ldconfig
- [root@nginx-backup rsync]# echo "/usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/rsync.conf" >> /etc/rc.local
系统:centos 5.4
ocaml 3.09.3
unison 2.40.63
A主机10.10.11.85 B主机10.10.11.92
只需要在其中一台主机安装unison server端即可,这里以A主机为例
1.ocaml下载
wget http://caml.inria.fr/pub/distrib/ocaml-3.09/ocaml-3.09.3.tar.gz
2.unison下载最新稳定版
wget http://www.seas.upenn.edu/~bcpierce/unison//download/releases/stable/unison-2.40.63.tar.gz
3.Ocaml安装脚本如下:
tar -zxf ocaml-3.09.3.tar.gz
cd ocaml-3.09.3
./configure
make world opt
make install
cd ..
4.unison安装脚本如下:
tar -zxf unison-2.40.63.tar.gz
cd unison-2.40.63
make UISTYLE=text
make install
cp unison /usr/local/bin
scp unison 10.10.11.92:/usr/local/bin/
5.配置双机ssh信任
A主机:
ssh-keygen -t rsa
cd .ssh/
scp id_rsa.pub 10.10.11.92:/root/
B主机--10.10.11.92:
cat id_rsa.pub >>~/.ssh/authorized_keys
B主机:
ssh-keygen -t rsa
cd .ssh/
scp id_rsa.pub 10.10.11.85:/root/
A主机--10.10.11.85:
cat id_rsa.pub >>~/.ssh/authorized_keys
6.通过配置文件来使用unison
使用root安装unison后,配置文件默认生成在/root/.unison/default.prf,可以手动写一个配置文件,运行unison时只需指定此配置文件即可。
下面以同步两个不同主机的/mnt目录为例的配置信息:
只在A主机中配置:
# more /root/.unison/unison_test.prf
root = /mnt
root = ssh://root@10.10.11.92//mnt
#force =/mnt
path = mnt
ignore = Path tmp
#prefer = ssh://root@10.10.11.92//mnt
batch = true
maxthreads = 180
#repeat = 1
#retry = 3
owner = true
group = true
perms = -1
fastcheck=false
rsync =false
#debug=verbose
sshargs = -C
xferbycopying = true
confirmbigdel = false
log = true
logfile = /root/.unison/unison_test.log
7.在A主机上做计划任务,进行每两分钟自动同步
*/2 * * * * /usr/local/bin unison unison_test.prf >/dev/null 2>&1 &
注各参数详解:
——root表示需要同步的目录
——force表示使用unison单项同步功能,注释掉以便启用双向同步.
——ignore = Path表示同步/mnt目录时不同步tmp。
——batch = true,表示全自动模式,接受缺省动作
——fastcheck true,表示同步时使用文件的创建时间来比较两地文件,如果这个选项为false,unison则将比较两地文件的内容.建议设置为true
——log = true
——logfile则指定了同时将输出写入log文件。
但是同步的时候文件同步不过去,最后把crontab -e里面的命令修改了下
*/1 * * * * /usr/local/bin unison -batch /home/ssh://root@172.16.2.110//home/
以上是关于unison做文件双向同步的主要内容,如果未能解决你的问题,请参考以下文章