现将以前整理的笔记开源:

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