使用rsync同步数据

rsync介绍

Rsync(Remote Synchronize)是一个远程资料同步工具,可通过LAN/WAN快速同步多台主机,Rsync使用所为的“Rsync演算法”来使本地主机和远程主机之间达到同步,这个演算法并不是每次都整份传送,它只传送两台计算机之间所备份的资料不同的部分,因此速度相当快。

Rsync的优点如下:

  • 能更新整个目录树和文件系统;
  • 有选择性的保持源文件的符号链链、硬链接、文件属于、权限、设备以及时间等;
  • 安装无任何特殊权限要求;
  • 对于多个文件来说,内部流水线减少文件等待的延时;
  • 能用rsh、ssh 或直接端口做为传输端口;
  • 支持匿名rsync 同步文件,是理想的镜像工具;

rsync安装

rsync官方网站:https://rsync.samba.org 。

yum安装方式:
shell> yum install rsync

源码安装方式:
shell> tar zxvf rsync-3.1.2.tar.gz
shell> cd rsync-3.1.2
shell> ./configure --prefix=/usr
shell> make -j4 && make install

rsync配置

rsync的主要有以下三个配置文件:

  • rsyncd.conf:主配置文件
  • rsyncd.secrets:密码文件
  • rsyncd.motd:rysnc登录显示信息
shell> touch /etc/rsyncd.conf
shell> touch /etc/rsyncd.secrets
shell> chmod 600 /etc/rsyncd.secrets
shell> touch /etc/rsyncd.motd

shell> more /etc/rsyncd.conf
# This line is required by the /etc/init.d/rsyncd script
pid file = /var/run/rsyncd.pid
port = 873
address = 10.106.58.172
# 指定同步用户
uid = root
gid = root

use chroot = yes
read only = yes

#limit access to private LANs
hosts allow=10.106.58.0/255.255.255.0
hosts deny=*

max connections = 5
motd file = /etc/rsyncd.motd

#This will give you a separate log file
log file = /var/log/rsync.log

#This will log every file transferred - up to 85,000+ per user, per sync
#transfer logging = yes

log format = %t %a %m %f %b
syslog facility = local3
timeout = 300

# 指定同步模块信息
[test]
path = /test
list=yes
ignore errors
auth users = root
secrets file = /etc/rsyncd.secrets
comment = Test Rsync
exclude = data/

rsync密码文件用户名和密码可以随便定义,最好不要和系统帐户一致,同时要把密码文件权限设置为600。
shell> more /etc/rsyncd.secrets
root:root123

设定rsync登陆显示信息,可以不设置
shell> more /etc/rsyncd.motd
++++++++++++++++++++++++++++++++++++++++++++++
Welcome to use the rsync services!
++++++++++++++++++++++++++++++++++++++++++++++

启动rsync服务器

A、使用--daemon参数方式启动

shell> /usr/bin/rsync --daemon --config=/etc/rsyncd.conf

B、使用xinetd方式启动

shell> more /etc/xinetd.d/rsync
# default: off
# description: The rsync server is a good addition to an ftp server, as it \
#       allows crc checksumming etc.
service rsync
{
        disable = no
        flags           = IPv6
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/bin/rsync
        server_args     = --daemon
        log_on_failure  += USERID
}

重启xinetd服务
shell> /etc/init.d/xinetd restart

rsync用法及常用参数

  • -a,--archive:归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD
  • -v --verbose:详细模式输出
  • -r --recursive:对子目录以递归处理
  • -p --perms:保持文件原有权限
  • -o --owner:保持文件原有属主
  • -g --group:保持文件原有用户组
  • -t --times:保持文件原有时间
  • --delete:删除那些DST中存在而SRC中不存在的文件或目录
  • -z --compress:对文件在传输时进行压缩处理
  • --exclude=PATTERN:指定不需要传输的目录
  • --include=PATTERN:指定需要传输的目录
  • --exclude-from=FILE:排除FILE中指定模式的文件
  • --include-from=FILE:不排除FILE中指定模式匹配的文件
  • --progress:显示详细的传输进度情况
  • --password-file:指定密码文件,使用时无需交互式地输入验证密码

rsync使用测试

创建客户端密码文件
shell> more /etc/rsyncd.secrets
root123

列出rsync 服务器上的所提供的同步内容
shell> rsync --list-only  root@10.106.58.172::
++++++++++++++++++++++++++++++++++++++++++++++
Welcome to use the rsync services!
++++++++++++++++++++++++++++++++++++++++++++++

test               Test Rsync

shell> rsync --list-only --password-file=/etc/rsyncd.secrets root@10.106.58.172::test
++++++++++++++++++++++++++++++++++++++++++++++
Welcome to use the rsync services!
++++++++++++++++++++++++++++++++++++++++++++++

drwxr-xr-x        4096 2016/03/17 23:37:28 .
-rw-r--r--           0 2016/03/17 23:32:06 abs.cfg
-rw-r--r--      685426 2016/03/17 23:37:28 setuptools-19.4.zip
drwxr-xr-x        4096 2016/03/17 23:36:35 test

使用rsync同步rsync服务器指定数据测试
shell> rsync -avzP --delete --password-file=/etc/rsyncd.secrets root@10.106.58.172::test /tmp
++++++++++++++++++++++++++++++++++++++++++++++
Welcome to use the rsync services!
++++++++++++++++++++++++++++++++++++++++++++++

receiving incremental file list
deleting .ICE-unix/
deleting mysql.sock
./
abs.cfg
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=3/5)
setuptools-19.4.zip
      685426 100%   12.11MB/s    0:00:00 (xfer#2, to-check=2/5)
test/
test/Python-2.7.9.tgz
    16657930 100%   15.28MB/s    0:00:01 (xfer#3, to-check=0/5)

sent 95 bytes  received 17288397 bytes  2659768.00 bytes/sec
total size is 17343356  speedup is 1.00

本地rsync,类似cp,将/tmp目录下内容拷贝到/backup目录下
shell> rsync -azP /tmp/* /backup

类似scp命令,将backup下目录传输到174服务器/tmp目录下
shell> rsync -az /backup/ root@10.106.58.174:/tmp
root@10.106.58.174's password:

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29733787/viewspace-2059495/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29733787/viewspace-2059495/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值