|
安装常用工具:
1
| yum install tree nmap sysstat dos2unix lrzsz -y
|
设置两台机器主机名
1
2
| hostname inotify
hostname rsync
|
这里测试,就直接hostname修改了,要使永久生效,还需要修改
/etc/network/sysconfig和/etc/hosts文件
rsync有三种模式:
1.单个主机的本地数据传输(此事类似于cp命令)
例如:rsync -avz /etc/hosts /home/
2.借助rpc.ssh等通道来传输数据(scp)
例如:rsync -avz -e 'ssh -p52113' /etc/hosts root@192.168.80.111:~
3.以守护进程(socket)的方式传输数据
这里我们用第三种,前两种就是一条命令
rsync 编辑rsync配置文件,默认不存在,需手动创建
[iyunv@rsync ~]# cat /etc/rsyncd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| .conf
uid = rsync
gid = rsync
use chroot = no
max chonections = 200
time out = 300
pid file = /var/run/rsync.pid
lock file = /var/run/rsync.locl
log file = /var/log/rsync.log
ignore errors
read only = false
list = false
hosts allow = 192.168.80.0/24
hosts deny = 0.0.0.0/32
auth user = rsync_backup
secrets file = /etc/rsync.password
[gong]
path = /data
|
创建rsync用户
1
| [iyunv@rsync ~]# useradd rsync -s /sbin/nologin -M
|
创建同步的目录
为目录授权拥有者和组
1
| [iyunv@rsync ~]# chown rsync.rsync /data -R
|
为验证文件/etc/rsync.password添加账户密码
1
| [iyunv@rsync ~]# cat /etc/rsync.password
|
rsync_backup:gong
此验证文件需要600权限,不然报错
1
| [iyunv@rsync ~]# chmod 600 /etc/rsync.password
|
启动rsync进程
1
| [iyunv@rsync ~]# rsync --daemon
|
到这里,以守护进程(socket)的方式的rsync就配置好了
把远端数据拉到本地(pull)
1
2
| rsync -avz rsync_backup@192.168.80.131::gong /data --password-file=/etc/rsync.password
rsync -avz rsync://rsync_backup@192.168.80.111/gong /data --password-file=/etc/rsync.password
|
把本地数据推到远端(push)
1
2
| rsync -avz /data rsync_backup@192.168.80.131::gong --password-file=/etc/rsync.password
rsync -avz /data rsync://rsync_backup@192.168.80.111/gong --password-file=/etc/rsync.password
|
在rsync端/data目录创建一个文件测试
1
2
3
| [iyunv@rsync data]# touch aa
[iyunv@rsync data]# ls
aa
|
在inotify端通过拉的命令看能否把数据拉过来
首先创建一个包含密码的文件,只包含密码即可,权限600
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| [iyunv@inotify data]# cat /etc/rsync.password
gong
[iyunv@inotify data]# chmod 600 /etc/rsync.password
[iyunv@inotify ~]# mkdir /data
[iyunv@inotify ~]# rsync -avz rsync_backup@192.168.80.132::gong /data --password-file=/etc/rsync.password
receiving incremental file list
./
aa
sent 48 bytes received 120 bytes 336.00 bytes/sec
total size is 0 speedup is 0.00
[iyunv@inotify ~]# cd /data
[iyunv@inotify data]# ls
aa
|
传输完成,可见数据成功同步到了本地
inotofy
接下配配置inotify,
inotify的作用主要是监控本地目录文件的变化,然后通过脚本中rsync命令把本地数据推送到远端命令完成
1
2
3
4
5
| [iyunv@inotify tools]# tar xf inotify-tools-3.14.tar.gz
[iyunv@inotify tools]# cd inotify-tools-3.14
[iyunv@inotify inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify-tools-3.14
[iyunv@inotify inotify-tools-3.14]# make && make install
[iyunv@inotify inotify-tools-3.14]# ln -s /usr/local/inotify-tools-3.14/ /usr/local/inotify
|
好了inotify就安装完成了
接下来用inofity监控/data这个目录(-e可以指定所监控的事件create创建,delete删除,attrib文件或目录属性被修改等)
1
2
| [iyunv@inotify inotify-tools-3.14]# /
usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create /data
|
我重新打开另外一个终端,在inotify这台机器上的/data目录下创建文件
1
2
3
| [iyunv@inotify inotify-tools-3.14]# /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create /data
30/03/15 06:59 /data/bb
30/03/15 06:59 /data/cc
|
可以很清楚的看到inotify监控到了创建的bb和cc
接下来编写脚本让监控到的数据能够自动推到rsync上
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| [iyunv@inotify scripts]# cat inotify.sh
#!/bin/bash
host1=192.168.80.132
src=/data/
dst=gong
user=rsync_backup
rsync_passfile=/etc/rsync.password
inotify_home=/usr/local/inotify
if [ ! -e "$src" ] || [ ! -e "$rsync_passfile" ] || [ ! -e "${inotify_home}/bin/inotifywait" ] || [ ! -e "/usr/bin/rsync" ]; then
echo "error"
exit 9
fi
${inotify_home}/bin/inotifywait -mrq --timefmt '%d-%m-%y %H:%M' --format '%T %w%f' -e close_write,create,delete,attrib $src | \
while read line
do
cd $src && rsync -az --delete $src --timeout=100 $user@$host1::$dst --password-file=${rsync_passfile} >/dev/null 2>&1
done
|
执行脚本让在后台执行
1
2
| [iyunv@inotify scripts]# sh inotify.sh &
[1] 4615
|
创建100个文件
1
2
3
4
5
6
7
8
| [iyunv@inotify data]# touch {1..100}
[iyunv@inotify data]# ls
1 14 2 25 30 36 41 47 52 58 63 69 74 8 85 90 96 cc
10 15 20 26 31 37 42 48 53 59 64 7 75 80 86 91 97
100 16 21 27 32 38 43 49 54 6 65 70 76 81 87 92 98
11 17 22 28 33 39 44 5 55 60 66 71 77 82 88 93 99
12 18 23 29 34 4 45 50 56 61 67 72 78 83 89 94 aa
13 19 24 3 35 40 46 51 57 62 68 73 79 84 9 95 bb
|
在rsync端查看
1
2
3
4
5
6
7
| [iyunv@rsync data]# ls
1 14 2 25 30 36 41 47 52 58 63 69 74 8 85 90 96 cc
10 15 20 26 31 37 42 48 53 59 64 7 75 80 86 91 97
100 16 21 27 32 38 43 49 54 6 65 70 76 81 87 92 98
11 17 22 28 33 39 44 5 55 60 66 71 77 82 88 93 99
12 18 23 29 34 4 45 50 56 61 67 72 78 83 89 94 aa
13 19 24 3 35 40 46 51 57 62 68 73 79 84 9 95 bb
|
删除
1
2
| [iyunv@inotify data]# mkdir {a..f}
[iyunv@inotify data]# rm -f *
|
rsync端查看
1
2
| [iyunv@rsync data]# ls
[iyunv@rsync data]#
|
至此,inotify+rsync的数据同步就完成了
|
|
|