八周二次课(12月5日)
10.28 rsync工具介绍
rsync -av /etc/passwd /tmp/12.txt
rsync -av /etc/passwd root@192.168.11.131:/tmp/12.txt
10.29/10.30 rsync常用选项
rsync常用选项
-a 包含-rtplgoD
-r 同步目录时要加上,类似cp时的-r选项
-v 同步时显示一些信息,让我们知道同步的过程
-l 保留软连接
-L 加上该选项后,同步软链接时会把源文件给同步
-p 保持文件的权限属性
-o 保持文件的属主
-g 保持文件的属组
-D 保持设备文件信息
-t 保持文件的时间属性
–delete 删除DEST中SRC没有的文件
–exclude 过滤指定文件,如–exclude “logs”会把文件名包含logs的文件或者目录过滤掉,不同步
-P 显示同步过程,比如速率,比-v更加详细
-u 加上该选项后,如果DEST中的文件比SRC新,则不同步
-z 传输时压缩
10.31 rsync通过ssh同步
二、Rsync编译安装
说明:当然你也可以使用“yum -y install rsync”命令进行安装,直接进入Rsync server 配置环节。
1.检查本机是否存在
[root@kry132 ~]# rpm -qa rsync
rsync-3.0.6-12.el6.x86_64
2.卸载
[root@kry132 ~]# yum remove rsync
说明:需要安装最新的版本就需要卸载老版本。
3、下载rsync软件
[root@kry132 ~]# cd /usr/local/src/
[root@kry132 src]# wget https://download.samba.org/pub/rsync/rsync-3.1.2.tar.gz
4.解压包
[root@kry132 src]# tar zxvf rsync-3.1.2.tar.gz
5.创建程序安装目录,并编译安装
[root@kry132 src]# cd rsync-3.1.2
[root@kry132 rsync-3.1.2]# mkdir /usr/local/rsync
[root@kry132 rsync-3.1.2]# ./configure –prefix=/usr/local/rsync
[root@kry132 rsync-3.1.2]# make && make install
6.配置环境变量
[root@kry132 ~]# vim /etc/profile.d/rsync.sh
#!/bin/sh
export PATH=$PATH:/usr/local/srync/bin
7.建立软连接
[root@kry132 ~]# ln -s /usr/local/rsync/bin/rsync /usr/bin/rsync
8.查看当前版本
[root@kry132 ~]# rsync –version
rsync version 3.1.2 protocol version 31
三、Rsync server配置
1.编辑配置文件
[root@kry132 ~]# vim /etc/ rsyncd.conf
uid = rsync
gid = rsync
port = 873
use chroot = on
max connections = 200
timeout = 120
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
[bakup]
path = /data/bakup
read only = false
list = false
hosts allow = 192.168.0.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_bakup
secrets file = /etc/rsyncd.password
2.创建密码文件
[root@kry132 ~]# vim /etc/rsyncd.password
rsync_bakup:4297f44b13955235245b2497399d7a93
说明:定义格式 用户:密码
3.修改密码文件权限
[root@kry132 ~]# chmod 600 /etc/rsyncd.password
4.创建Rsync server服务启动的系统账户
[root@kry132 ~]# useradd rsync -M -s /bin/nologin
5.创建数据存放目录
[root@kry132 ~]# mkdir -p /data/bakup
6.修改目录权限
[root@kry132 ~]# chown rsync.rsync /data/bakup
7.启动服务
[root@kry132 ~]# rsync –daemon
说明:如果配置文件路径不是/etc/rsyncd.conf,启动是需要使用“–config” 参数指定配置文件。
8.查看服务是否启动
[root@kry132 ~]# ps axu |grep rsync
root 9198 0.0 0.0 107652 672 ? Ss 19:41 0:00 rsync –daemon –config /etc/rsync/rsyncd.conf
root 9205 0.0 0.0 103308 848 pts/0 S+ 19:44 0:00 grep rsync
[root@kry132 ~]# netstat -nlp |grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 9198/rsync
tcp 0 0 :::873 :::* LISTEN 9198/rsync
四、客户端配置
1.创建密码文件
[root@Kry103 ~]# vim /etc/rsyncd.password
4297f44b13955235245b2497399d7a93
2.修改文件权限
[root@Kry103 ~]# chmod 600 /etc/rsyncd.password
3.安装客户端
[root@Kry103 ~]# yum -y install rsync
说明:如果客户端存在,跳过该步骤。
五、测试
1.客户端上推送数据
[root@Kry103 ~]# ll test.txt
-rw-r–r–. 1 kry kry 13 Mar 24 15:55 test.txt
[root@Kry103 ~]# rsync -avzP test.txt rsync_bakup@192.168.0.132::bakup –password-file=/etc/rsyncd.password
sending incremental file list
test.txt
11 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1)
sent 91 bytes received 27 bytes 236.00 bytes/sec
total size is 11 speedup is 0.09
2.服务端查看文件是否推送成功
[root@kry132 bakup]# ll
total 4
-rw-r–r–. 1 rsync rsync 13 Mar 24 15:55 test.txt
3.从服务端拉取数据
[root@Kry103 ~]# ll /tmp/
total 0
[root@Kry103 ~]# rsync -avzP rsync_bakup@192.168.0.132::bakup /tmp –password-file=/etc/rsyncd.password
receiving incremental file list
./
test.txt
13 100% 12.70kB/s 0:00:00 (xfer#1, to-check=7/17)
sent 64 bytes received 419 bytes 966.00 bytes/sec
total size is 13981 speedup is 28.95
4.查看拉取的文件
[root@Kry103 ~]# ll /tmp/
total 4
-rw-r–r–. 1 kry kry 13 Mar 24 15:55 test.txt
说明:文件的属主组跟推送时保持一直。