rsync英文名remote synchronization,可使本地和远程两台主机之间的数据快速复制同步镜像 远程备份的功能,rsync在拷贝时候会先比较源数据跟目标数据,不一致才复制,实现增量拷贝
监听于873/tcp 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| rsync有许多选项:
-n: 在不确定命令是否能按意愿执行时,务必要事先测试;-n可以完成此功能;
-v: --verbose,详细输出模式
-q: --quiet,静默模式 尽可能输出少
-c: --checksum,开启校验功能,强制对文件传输进行校验
-r: --recursive,递归复制;
-a: --archives,归档,保留文件的原有属性
-p: --perms 保留文件的权限
-t: --times 保留文件的时间戳
-l: --links 保留文件的符号链接
-g: --group 保留文件的属组
-o: --owner 保留文件的属主
-D: --devices 保留设备文件
-e ssh: 表示使用ssh协议作承载
-z: 对文件压缩后传输
--progress:显示进度条
--stats: 显示如何执行压缩和传输
|
复制前先测试:
1
2
3
4
5
6
| [iyunv@martin ~]# ls
anaconda-ks.cfg d1 d2 install.log install.log.syslog
[iyunv@martin ~]# rsync install.log d2 -n
[iyunv@martin ~]# rsync install.log.a d2 -n
rsync: link_stat "/root/install.log.a" failed: No such file or directory (2)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6]
|
注意:rsync命令使用中,如果源参数的末尾有斜线,就会复制指定目录的内容,而不复制目录本身;没有斜线,则会复制目录本身;目标参数末尾的斜线没有作用 源数据后面没有/测试 1
2
3
4
5
6
7
8
9
10
11
12
| [iyunv@martin ~]# rsync -rv d1 d2
sending incremental file list
d1/a
d1/a.bin.sql
d1/a.sql
d1/all.sql
d1/kk/a.sql
sent 658784 bytes received 109 bytes 1317786.00 bytes/sec
total size is 658357 speedup is 1.00
[iyunv@martin ~]# ls d2/
d1
|
源数据后面有/ 测试:
1
2
3
4
5
6
7
8
9
10
11
12
13
| [iyunv@martin ~]# rsync -rv d1/ d2
sending incremental file list
a
a.bin.sql
a.sql
all.sql
kk/
kk/a.sql
sent 658777 bytes received 111 bytes 1317776.00 bytes/sec
total size is 658357 speedup is 1.00
[iyunv@martin ~]# ls d2
a a.bin.sql a.sql all.sql d1 kk
|
基于ssh远程推送:
1
2
3
4
| [iyunv@martin d2]# rsync -e "ssh -p6789" -r ../d1 root@sherry:~/ --progress
[iyunv@sherry ~]# ls
a a.sql anaconda-ks.cfg d1 install.log install.log.syslog
|
基于ssh远程拉取:
1
2
3
4
5
6
7
8
9
10
11
| [iyunv@martin ~]# rsync -e "ssh -p6789" -rv root@sherry:~/d3 . --progress
root@sherry's password:
receiving incremental file list
d3/
d3/a.sql
1868 100% 1.78MB/s 0:00:00 (xfer#1, to-check=0/2)
sent 34 bytes received 1972 bytes 802.40 bytes/sec
total size is 1868 speedup is 0.93
[iyunv@martin ~]# ls
anaconda-ks.cfg d1 d2 d3 install.log install.log.syslog
|
安装: 服务器:sherry 客户端:martin 1
| [iyunv@sherry ~]# yum install xinetd rsync
|
配置文件:
1
2
| 配置文件为/etc/rsyncd.conf,获取帮助的方式:man rsyncd.conf
定义一个全局配置和多个rsync共享配置
|
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
26
27
28
29
| [iyunv@sherry ~]# vim /etc/rsyncd.conf
# Global Settings
uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log
# Directory to be synced
[synced_name]
path = /path/to/some_dir #共享目录
ignore errors = yes #同步时是否忽略错误
read only = no #yes不能push 只能下载
write only = no #yes不能pull 只能上传
hosts allow = white_list_ip/net #白名单
hosts deny = * #黑名单
说明:
1、二者都不出现时,默认为允许访问;
2、只出现hosts allow: 定义白名单;但没有被匹配到的主机由默认规则处理,即为允许;
3、只出现hosts deny: 定义黑名单;出现在名单中的都被拒绝;
4、二者同时出现:先检查hosts allow,如果匹配就allow,否则,检查hosts deny,如果匹配则拒绝;如二者均无匹配,则由默认规则处理,即为允许;
list = false #列出目录
uid = root #不用root
gid = root
auth users = username #用户认证
secrets file = /etc/rsyncd.passwd #文件格式(明文):username:password文件权限要设置为600;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| [iyunv@sherry rsync]# cat /etc/rsyncd.conf
# Global Settings
uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log
[mysql]
path =/mydata/backup/rsync-mysql
ignore errors = yes
read only = no
write only = no
hosts allow = 192.168.1.0/24
hosts deny = *
list = false
uid = sherry
gid = sherry
auth users = tom
secrets file = /etc/rsyncd.passwd
|
1
2
3
| [iyunv@sherry rsync-mysql]# pwd
/mydata/backup/rsync-mysql
[iyunv@sherry backup]# chown sherry.sherry rsync-mysql/ #用setfacl 数据一致性时候会报错
|
添加账号密码:
1
2
3
| [iyunv@sherry rsync]# cat /etc/rsyncd.passwd
tom:222222
[iyunv@sherry rsync]# chmod 600 /etc/rsyncd.passwd
|
启动端口:
1
2
3
4
5
| [iyunv@sherry rsync]# chkconfig xinetd on
[iyunv@sherry rsync]# service xinetd restart
[iyunv@sherry rsync]# chkconfig rsync on
[iyunv@sherry rsync]# ss -lntup|grep 873
tcp LISTEN 0 64 :::873 :::* users:(("xinetd",121111,5))
|
测试: push 手动输入密码: 1
2
3
4
5
6
7
8
9
10
| #client
[iyunv@martin mysql]# touch a
[iyunv@martin mysql]# ls
a
[iyunv@martin mysql]# rsync a tom@sherry::mysql
Password:
#server
[iyunv@sherry rsync-mysql]# ll
total 0
-rw-r--r-- 1 sherry sherry 0 May 28 10:01 a
|
push 用文件代替密码:
1
2
3
4
5
6
7
8
9
10
11
| #client
[iyunv@martin ~]# vim /etc/rsyncd.passwd
222222
[iyunv@martin ~]# chmod 600 /etc/rsyncd.passwd
[iyunv@martin mysql]# touch b
[iyunv@martin mysql]# rsync --password-file=/etc/rsyncd.passwd b tom@sherry::mysql
#server
[iyunv@sherry rsync-mysql]# ll
total 0
-rw-r--r-- 1 sherry sherry 0 May 28 10:01 a
-rw-r--r-- 1 sherry sherry 0 May 28 10:03 b
|
拉取:
1
2
3
4
| [iyunv@martin mysql]# rm -fr *
[iyunv@martin mysql]# rsync --password-file=/etc/rsyncd.passwd tom@sherry::mysql/* .
[iyunv@martin mysql]# ls
a b
|
增量上传:(上传服务器端没有的文件)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| #client
[iyunv@martin mysql]# rm -f *
[iyunv@martin mysql]# ls
[iyunv@martin mysql]# touch a b
[iyunv@martin mysql]# ls
a b
[iyunv@martin mysql]# rsync --password-file=/etc/rsyncd.passwd -az ./ tom@sherry::mysql
#server
[iyunv@sherry rsync-mysql]# rm -f *
[iyunv@sherry rsync-mysql]# ls
a b
#client
[iyunv@martin mysql]# rm -f b
[iyunv@martin mysql]# touch c
[iyunv@martin mysql]# ls
a c
[iyunv@martin mysql]# rsync --password-file=/etc/rsyncd.passwd -az ./ tom@sherry::mysql
#server
[iyunv@sherry rsync-mysql]# ls
a b c
|
同步上传:(数据一致性)
1
2
3
4
5
6
7
8
9
10
11
12
13
| #client
[iyunv@martin mysql]# touch d
[iyunv@martin mysql]# ls
a c d
#server
[iyunv@sherry rsync-mysql]# ls
a b c
#client
[iyunv@martin mysql]# rsync --password-file=/etc/rsyncd.passwd -az --delete ./ tom@sherry::mysql
#server
[iyunv@sherry rsync-mysql]# ls
a c d
|
同步下载:(数据一致性)
1
2
3
4
5
6
7
8
9
10
11
| #server
[iyunv@sherry rsync-mysql]# rm -f c
[iyunv@sherry rsync-mysql]# touch g
[iyunv@sherry rsync-mysql]# ls
a d g
#client
[iyunv@martin mysql]# ls
a c d
[iyunv@martin mysql]# rsync --password-file=/etc/rsyncd.passwd -az --delete tom@sherry::mysql ./
[iyunv@martin mysql]# ls
a d g
|
linux内核从2.6.13起开始支持inotify,通过inotify可以监控文件系统中添加 删除修改移动等各种事件。inotify是一种事件机制,它为应用程序文件系统提供实时响应事件的机制,相比cron的轮询机制相比,inotify资源消耗更低。
下载地址:
1
| http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
|
安装:
1
2
3
| [iyunv@martin inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify-tools-3.14
[iyunv@martin inotify-tools-3.14]# make && make install
[iyunv@martin local]# ln -sv /usr/local/inotify-tools-3.14/ /usr/local/inotify-tools
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| [iyunv@martin bin]# cd /usr/local/inotify-tools/bin
[iyunv@martin bin]# ls
inotifywait inotifywatch
#inotifywait 在被监控的文件或目录上等待特定文件系统事件(open,close,delete等)发生,执行后处于阻塞状态,适合在shell脚本中使用
#inotifywatch 收集被监控的文件系统使用度统计数据,指文件系统事件发生的次数统计
inotifywait
-r|--recursive 递归查询目录
q |--quiet 安静模式,尽量输出少
-m |--monitor 始终保持事件监听状态
--excludei 排除文件或者目录时,不区分大小写
--timefmt 指定时间输出的格式
--format 指定命令执行时的信息输出格式,%T为前面的时间格式
-e 事件
|
监控:(一般监控这三项即可 删除delete,创建create,修改close_write)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| [iyunv@martin bin]# ./inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e delete,create,close_write /backup/mysql/
#另起窗口 在/backup/mysql/ 下创建文件
[iyunv@martin mysql]# touch c
[iyunv@martin bin]# ./inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e delete,create,close_write /backup/mysql/
28/05/16 12:04 /backup/mysql/c
28/05/16 12:04 /backup/mysql/c #触发两次 create close_write
#delete
[iyunv@martin mysql]# rm -f a
[iyunv@martin bin]# ./inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e delete,create,close_write /backup/mysql/
28/05/16 12:04 /backup/mysql/c
28/05/16 12:04 /backup/mysql/c
28/05/16 12:06 /backup/mysql/a
|
1
2
3
4
5
6
7
8
9
10
11
| [iyunv@martin scripts]# vim inotify.sh
#!/bin/bash
inotify=/usr/local/inotify-tools/bin/inotifywait
src=/backup/mysql/
$inotify -mrq --format '%w%f' -e create,close_write,delete $src \
|while read file
do
cd $src &&
rsync -az $src --delete tom@sherry::mysql \
--password-file=/etc/rsyncd.passwd >/dev/null
done
|
1
2
3
4
5
6
7
| [iyunv@martin scripts]# chmod +x inotify.sh
[iyunv@martin scripts]# ./inotify.sh &
[iyunv@martin scripts]# jobs
[1]+ Running ./inotify.sh &
[iyunv@martin scripts]# kill %1
[iyunv@martin scripts]# jobs
[1]+ 已终止 ./inotify.sh
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| #server
[iyunv@martin mysql]# /scripts/inotify.sh &
[iyunv@martin mysql]# pwd
/backup/mysql
[iyunv@martin mysql]# ls
c d g kk ll
#client
[iyunv@sherry rsync-mysql]# ls
c d g kk ll
#server服务器创建文件
[iyunv@martin mysql]# touch pp
[iyunv@martin mysql]# ls
c d g kk ll pp
#client
[iyunv@sherry rsync-mysql]# ls
c d g kk ll pp #上传完成
#server
[iyunv@martin mysql]# rm -f c d
#cilent
[iyunv@martin mysql]# ls
g kk ll pp #可删除
|
加入开机启动
1
| echo '/bin/sh /scripts/inotify.sh &' >> /etc/rc.local
|
1
2
3
| [iyunv@martin mysql]# ps -ef |grep inotify.sh
root 126119 1 0 12:21 ? 00:00:00 /bin/bash /scripts/inotify.sh
root 126121 126119 0 12:21 ? 00:00:00 /bin/bash /scripts/inotify.sh
|
|