现将以前整理的笔记开源:
=============
rsync
部分
=============
一、
rsync
简述
rsync(remote rynchronization)
多功能的可实现增量的本地或远程数据镜像同步备份的工具,适用于
unix/linux/windows
多种平台
可使本地主机不同分区或目录间及本地和远程主机间的数据快速同步镜像,远程备份等策略。
相比传统的
cp,scp
它具有增量拷贝的功能。
二、工作方式
²
本地数据传输
²
通过
rcp,ssh
等通道传输
²
以守护进程的方式传输数据。
1.
本地传输:
rsync [option] src
源
dest
目的
2.
通过远程shell进行数据传输(拉取或推送):
rsync option user@host:src dest
rsync option src user@host:dest
拉取实例:
rsync -vzrtopg --progress -e 'ssh -p 22' wyan@10.0.0.141:/opt /tmp
-
vzrtopg
相当于
-avz
保持数据一致,同步文件和目录属性不变。
--progress
显示同步的过程,可用-
P
替换。
-e 'ssh -p 22'
表示通过
ssh
的通道传输数据
,-p 22
可省略。
推送实例:
rsync -vzrtop --progress -e 'ssh -p 22' /tmp wyan@10.0.0.141:/opt
3.
守护进程的方式数据传输
相当于在服务端配置
rsync
的守护进程,客户端拉取或推送
拉取:
1
)
rsync [option...] [user@]HOST::SRC.. [DEST]
2)rsync [option..] rsync://[user@]HOST[:PORT]/SRC... [DEST]
推送:
1)rsync [option...] SRC... [user@]HOST::DEST
2)rsync [option...] SRC... rsync://[user@]HOST[:PORT]/DEST
注:
第一种方法均为双冒号连接SRC或DEST,这里指的不再是路径而是服务器端守护进程配置的模块名称。
rsync
命令常用同步参数选项:
- rsync [option] src dest
- -v --verbose 详细模式输出,传输时的进度等信息
- -z --compress 传输时进行压缩以提高传输效率,--compress-level=NUM可按级别压缩
- -r --recursive 对子目录以递归模式传输
- -t --times 保持文件时间信息
- -o --owner 保持文件属主信息
- -p --perms 保持文件权限
- -g --group 保持文件属组
- -P --progress 显示同步过程的进度
- -a --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,同-rtopgDl
- -e -rsh=command 使用的信道协议,指定替代rsh的shell程序,如ssh
- --exclude=PATTERN 指定排除不需要传输的文件模式
- -D --devices 保持设备文件信息
- -l --links 保持软链接
- -avz –progress 等同于-vzrtopgDl --progress
- 脚本中可去掉-v 与--progress
三、实例
测试环境
centos5.6 32 /SN121_test_db01:192.168.40.93 /SN121_test_db01:192.168.40.94
1.
rsync
本地传输
- [root@SN121_test_db01 ~]# rpm -qa rsync
- rsync-2.6.8-3.1
- [root@SN121_test_db01 ~]# rsync -avz -P /etc /data/test
- ……
- etc/yum/pluginconf.d/fastestmirror.conf
- 143 100% 1.72kB/s 0:00:00 (xfer#902, to-check=0/1621)
- sent 5341041 bytes received 24178 bytes 631202.24 bytes/sec
- total size is 52737816 speedup is 9.83
- [root@SN121_test_db01 ~]# ll /data/test
- 总计 4
- drwxr-xr-x 85 root root 4096 08-17 04:02 etc
- [root@SN121_test_db01 ~]# rsync -avz -P /etc /data/test/
- building file list ...
- 1621 files to consider
- sent 46384 bytes received 20 bytes 92808.00 bytes/sec
- total size is 52737816 speedup is 1136.49
注:这里拷贝的目的目录加/与不加/是一样的,拷贝到目的目录下,因为rsync是增量复制,源目录没有更新,当第二遍拷时没有更新的内容将不再拷贝.
- [root@SN121_test_db01 ~]# rsync -avz -P /etc/ /data/test/
- ……
- yum/pluginconf.d/fastestmirror.conf
- 143 100% 0.29kB/s 0:00:00 (xfer#902, to-check=0/1621)
- sent 5340782 bytes received 24178 bytes 975447.27 bytes/sec
- total size is 52737816 speedup is 9.83
- 注:源目录加/则表示拷贝目录内的所有内容。倘若远程同步时需注意拷贝目录加/与不加/的情况.
- [root@SN121_test_db01 ~]# ll /data/test/
- Display all 206 possibilities? (y or n)
- acpi/ etc/ lftp.conf pcmcia/ scsi_id.config
- adjtime exports libaudit.conf php.d/ securetty
2.
远程shell模式
1)
通过远程
shell
模式将本地目录传输至远端
- [root@SN121_test_db01 ~]# rsync -avz --progress /tmp/test4 192.168.40.94:/tmp
- root@192.168.40.94's password:
- building file list ...
- 2 files to consider
- test4/
- test4/00aa.txt
- 41 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/2)
- [root@SN121_test_db02 ~]# ls -lrt /tmp/
- total 16
- drwxr-xr-x 2 root root 4096 Aug 7 15:44 test4
- drwxrwxr-x 3 admp admp 4096 Aug 10 10:49 ext3grep-0.10.1
- ……
默认情况下rsync使用secure shell(ssh)作为传输机制。同下借助ssh通道传输
2)
借助
ssh
通道在不同主机间远程传输,可结合
ssh-key
密钥认证登录方式
- [root@SN121_test_db01 ~]# rsync -avz -P /tmp/rsync-test -e 'ssh -p 22' 192.168.40.94:/tmp/aa 远端tmp下自动创建aa目录
- root@192.168.40.94's password:
- building file list ...
- 4 files to consider
- created directory /tmp/aa
- rsync-test/
- rsync-test/bak
- 12457 100% 0.00kB/s 0:00:00 (xfer#1, to-check=2/4)
- rsync-test/bak-cmd
- 3355 100% 3.20MB/s 0:00:00 (xfer#2, to-check=1/4)
- rsync-test/bak-dev
- 9639 100% 9.19MB/s 0:00:00 (xfer#3, to-check=0/4)
- sent 14082 bytes received 92 bytes 5669.60 bytes/sec
- total size is 25451 speedup is 1.80
本地目录推送到远端通过
ssh
通道机制,这里注意目录后加
/
与不加
/
有区别。
- [root@SN121_test_db01 ~]# rsync -avz -P -e 'ssh -p 22' root@192.168.40.94:/tmp/aa.txt /tmp/test4/
- root@192.168.40.94's password:
- receiving file list ...
- 1 file to consider
- aa.txt
- 1115 100% 1.06MB/s 0:00:00 (xfer#1, to-check=0/1)
- sent 42 bytes received 645 bytes 152.67 bytes/sec
- total size is 1115 speedup is 1.62
通过
ssh
通道机制将远程目录抓取到本地,
’ssh –p 22’
可默认写做
ssh
或不加
-e ‘ssh’
,因为默认就是
ssh
通道
3.
借助
ssh key
密钥实现数据免登陆验证加密传输(生产常用)
rsync+ssh key
建立密钥认证:
- [root@SN121_test_db01 ~]# useradd rsyncuser
- [root@SN121_test_db01 ~]# echo "123456" |passwd --stdin rsyncuser
- [rsyncuser@SN121_test_db01 ~]$ ssh-keygen -t dsa
- Generating public/private dsa key pair.
- Enter file in which to save the key (/home/rsyncuser/.ssh/id_dsa):
- Created directory '/home/rsyncuser/.ssh'.
- Enter passphrase (empty for no passphrase):
- Enter same passphrase again:
- Your identification has been saved in /home/rsyncuser/.ssh/id_dsa.
- Your public key has been saved in /home/rsyncuser/.ssh/id_dsa.pub.
- The key fingerprint is:
- ed:a6:05:51:04:11:b2:5e:31:cf:54:63:b4:04:77:6c rsyncuser@SN121_test_db01
- [rsyncuser@SN121_test_db01 ~]$ ll -a .ssh/
- 总计 16
- drwx------ 2 rsyncuser rsyncuser 4096 08-17 17:08 .
- drwx------ 4 rsyncuser rsyncuser 4096 08-17 17:08 ..
- -rw------- 1 rsyncuser rsyncuser 672 08-17 17:08 id_dsa
- -rw-r--r-- 1 rsyncuser rsyncuser 615 08-17 17:08 id_dsa.pub
- [rsyncuser@SN121_test_db01 ~]$ ssh-copy-id -i .ssh/id_dsa.pub rsyncuser@192.168.40.94
- 15
- The authenticity of host '192.168.40.94 (192.168.40.94)' can't be established.
- RSA key fingerprint is cf:b9:ea:cc:3a:02:2c:8e:05:4a:3e:4d:22:4f:7f:d6.
- Are you sure you want to continue connecting (yes/no)? yes
- Warning: Permanently added '192.168.40.94' (RSA) to the list of known hosts.
- rsyncuser@192.168.40.94's password:
- Now try logging into the machine, with "ssh 'rsyncuser@192.168.40.94'", and check in:
- .ssh/authorized_keys
- to make sure we haven't added extra keys that you weren't expecting.
- [rsyncuser@SN121_test_db02 ~]$ ll -a .ssh/
- total 12
- drwx------ 2 rsyncuser rsyncuser 4096 Aug 17 17:10 .
- drwx------ 4 rsyncuser rsyncuser 4096 Aug 17 17:10 ..
- -rw------- 1 rsyncuser rsyncuser 615 Aug 17 17:10 authorized_keys
远程同步:
前提开放rsyncuser的使用rsync命令的权限,visudo
- rsyncuser ALL=(root) NOPASSWD: /usr/bin/rsync, /usr/bin/scp, /bin/cp
- [rsyncuser@SN121_test_db01 ~]$ sudo –l
- User rsyncuser may run the following commands on this host:
- (root) NOPASSWD: /usr/bin/rsync, (root) /usr/bin/scp, (root) /bin/cp
- [rsyncuser@SN121_test_db01 ~]$/usr/bin/rsync -avz -P /tmp/dd rsyncuser@192.168.40.94:/tmp 推送操作
- building file list ...
- 1 file to consider
- dd
- 0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1)
- sent 84 bytes received 42 bytes 252.00 bytes/sec
- total size is 0 speedup is 0.00
- [rsyncuser@SN121_test_db01 ~]/usr/bin/rsync -avz -P rsyncuser@192.168.40.94:/tmp/ee /tmp 拉取操作
- receiving file list ...
- 1 file to consider
- ee
- 12 100% 11.72kB/s 0:00:00 (xfer#1, to-check=0/1)
- sent 42 bytes received 140 bytes 364.00 bytes/sec
- total size is 12 speedup is 0.07
注意:普通用户的目录权限问题.
4.
以守护进程方式传输数据
A-server
备份服务器
10.0.0.178
为服务端
B,C-server
节点服务器
x.179
,
180
1)
服务端配置文件
/etc/rsyncd.conf
- uid = root
- gid = root rsync以什么用户来读取目录
- use chroot = no
- max connection = 200
- timeout = 300
- pid file = /var/run/rsyncd.pid
- lock file = /var/run/rsyncd.lock
- log file = /var/log/rsyncd.log 不设,rsync会使用syslog输出相关日志信息
- [wyan] 定义同步模块名称
- path = /wyan/ 抓取与备份的路径
- ignore errors 忽略I/O错误
- read only = false 表示可以上传数据
- list = false 是否允许客户端可以查看可用模块列表,默认为可以。
- hosts allow = 10.0.0.0/24
- hosts deny = 0.0.0.0/32
- auth users = rsync_backup 指定以空格或逗号分隔的多个同步虚拟用户可以使用哪些模块,用户不需要在本地系统中存在,默认所有用户无密码的访问(anonymous rsync)
- secrets file = /etc/rsync.password指定用户名和密码存放的文件,格式:用户名:密码
- #exclude = a c b/2 排除的文件或目录,相对路径
说明:
模块中的参数项可以拿到全局配置中使用。
更多参数可
man rsyncd.conf
查看
如果配置中的
path = /wyan/
路径不存在则需创建且属主组为配置文件中指定的用户
.
3
)启动服务
rsync --daemon
以守护进程方式启动
默认
873
端口
lsof -i tcp:873
根据端口反查服务
加到
rc.local
开机启动
pkill rsync rsync --daemon
先杀掉再重启。
4)echo "rsync_backup:wyan" >/etc/rsync.password
配置密码且文件需
600
权限
----------------------------
客户端配置:
- echo "wyan" >/etc/rsync.password 仅配密码不需要帐号。
- chomd 600 /etc/rsync.password
执行推送本地文件到备份机服务端
- rsync -avz -P /tmp/wyan rsync_backup@10.0.0.178::wyan/ --password-file=/etc/rsync.password 双冒号后面是模块名
- 或者
- rsync -av -P /tmp/wyan rsync://rsync_backup@10.0.0.178:/wyan/ --password-file=/etc/rsync.password 直接接路径方式
注:推送时注意如加
--delete
参数,本地目录里有什么远端目录里就有什么,必须完全同步一致,如果本地目录里没有的,推送时远端目录里也会被删除,确保数据一致。这样可能会导致远端丢数据的风险。
执行抓取,路径对调下即可
,
如抓取备份机上
wyan
目录下的
hosts
文件
rsync -avz -P rsync://rsync_backup@10.0.0.178:/wyan/hosts /tmp/ --password-file=/etc/rsync.password
注:拉取时加
--delete
参数同推送风险一样,拉取时远端有什么本地就有什么,远端没有的,本地有也会被删除,实现完全同步。
############################################
多个目录的
rsync:
可设置多模块
[blog]
path = /blog/
[wyan]
path = /wyan/
其它参数可放置于全局位置对两个模块都有效。
转载于:https://blog.51cto.com/iceeggplant/702944