1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# sudo apt-get install rsync #ubuntu系统默认已安装
# sudo cp/usr/share/doc/rsync/examples/rsyncd.conf /etc/
# sudo vi /etc/rsyncd.conf
# 以下是全局配置
log
file
=
/var/log/rsyncd
.log
pid
file
=
/var/run/rsyncd
.pid
lock
file
=
/var/lock/rsyncd
#以下是模块选项配置
[home]
#模块名,在源服务器指定这个名字
comment =
sync
rsync
/home
#描述信息
path =
/home/rsync
#备份目录
use chroot=no
#不使用chroot,不用root权限
read
only = no
#设置本地备份目录为读写权限
uid=root
gid=root
max connections=10
#客户端最大连接数
auth
users
=
rsync
#指定数据同步用户
secrets
file
=
/etc/rsyncd
.pass
#指定数据同步用户信息文件
hosts allow=192.168.18.0
/24
#允许连接的客户端
ignore errors =
yes
#忽略出现I/O错误
timeout = 600
|
1
2
3
4
5
6
7
8
9
|
# sudo vi /etc/rsyncd.pass
rsync
:123456
#格式是用户名:密码
# sudo chmod 600 /etc/rsyncd.pass #属主要有权限读这个文件,否则会报没权限
# sudo /etc/init.d/rsync start #如果启动报如下错误,则根据提示打开/etc/default/rsync文件,将RSYNC_ENABLE=false该为RSYNC_ENABLE=true,再重启即可。
*
rsync
daemon not enabled
in
/etc/default/rsync
, not starting...
#,查看是否启动,有rsync监听端口说明正常:
# sudo netstat -antp |grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 29605
/rsync
|
1
2
3
4
5
|
# sudo apt-get install rsync inodify-tools
#rsync不用配置,因为我们只使用rsync命令,紧接着创建认证文件
# sudo vi /etc/rsyncd.pass
123456
#只写密码
# sudo chmod 600/etc/rsyncd.pass
|
1
2
3
4
5
6
|
# rsync -avzP --password-file=/etc/rsyncd.pass --delete /home/rsync rsync@192.168.18.20::home #home即是目标服务器上rsync里面的模块名
sending incremental
file
list
rsync
/a
0 100% 0.00kB
/s
0:00:00 (xfer
#1, to-check=5/7)
sent 354 bytes received 126 bytes 960.00 bytes
/sec
total size is 0 speedup is 0.00
|
1
2
3
4
5
6
7
8
9
|
# vi inotify_rsync.sh
#!/bin/bash
SRC=
'/home/rsync'
DST=
'rsync@192.168.18.20::home'
/usr/bin/inotifywait
-mrq --timefmt
'%y-%m-%d %H:%M'
--
format
'%T %w %f %e'
-e create,delete,move,modify $SRC|
while
read
files
do
rsync
-avzP --password-
file
=
/etc/rsyncd
.pass --delete $SRC $DST
echo
"$files was rsynced."
>>
/tmp/rsync
.log
done
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# bash -x inotify_rsync.sh
+ SRC=
/home/rsync/
#目录结尾一定要加个/,否则会再目标服务器再创建个目录
+DST=
rsync
@192.168.18.213::home
+
read
files
+
/usr/bin/inotifywait
-mrq --timefmt
'%y-%m-%d %H:%M'
--
format
'%T %w%f %e'
-e create,delete,move,modify,attrib
/home/loongtao
+
rsync
-avzP --password-
file
=
/etc/rsyncd
.pass --delete
/home/rsync
rsync
@@192.168.18.213::home
sending incremental
file
list
rsync
/
rsync
/test
.txt
0 100% 0.00kB
/s
0:00:00 (xfer
#1, to-check=2/11)
sent 349 bytes received 32 bytes 762.00 bytes
/sec
total size is 9380 speedup is 24.62
+
echo
'15-04-24 13:33/home/rsync/test.txt CREATE was rsynced.'
+
read
files
+
rsync
-avzP--password-
file
=
/etc/rsyncd
.pass --delete
/home/rsync
rsync
@192.168.18.213::home
sending incremental
file
list
sent 310 bytes received 10 bytes 640.00 bytes
/sec
total size is 9380 speedup is 29.31
+
echo
'15-04-24 13:33/home/rsync/test.txt ATTRIB was rsynced.'
+
read
files
|
1
2
|
# chmod +x inotify_rsync.sh
# ./inotify_rsync.sh &
|