rsync命令

rsync命令

😄 Written by Zak Zhu

注意: rsync命令使用中, 如果源参数的末尾有斜线, 只会复制指定目录的内容, 而不复制目录本身, 没有斜线, 则会复制目录本身, 包括目录.
Examples:
rsync -r /mydata/data /bachups #会把目录data直接同步至/bakups目录中
rsync -r /mydata/data/ /backups #会把目录data/中的内容至/backups目录中

参考

rsync命令

rysnc -- a fast, versatile, remote and local file-copying tool

安装

前提: SRC和DEST端必须都安装rsync

yum install rsync -y

工作模式

其他选项:

# Options:
	-q, --quiet			# suppress non-error messages
	-c, --checksum
	-e ssh				# use ssh protocl to transfer
	-a, --archive		# archive mode; equals -rlptgoD 
	-z, --compress
	--stats				# give some file-transfer stats
	--progress			# show progress during transfer
	--password-file=FILE

Dry-run Mode

Please dry run first for test

rsync -nv SRC... DEST
# Options:
	-n, --dry-run		# perform a trail run with no changes mode
	-v, --verbose

0

1


Local Mode
rsync -avz --progress SRC... DEST

2


Remote Mode
# Pull
rsync -avz -e ssh --progress USER@HOST:SRC... DEST
# Push
rsync -avz -e ssh --progress SRC... USER@HOST:DEST

3


Daemon Mode

Port: 873/tcp

服务端配置

注意: 关闭该死的seinux !!!

  1. 安装超级守护进程xinetd

    yum install xinetd -y

  2. 为rsync提供配置文件

    man 5 rsyncd.conf

    # 查看rsyncd.conf配置里每个属性含义

    以及查看EXAMPLES部分了解基础配置例子

    vim /etc/rsyncd.conf

    定义一个全局配置和多个rsync共享配置

    # Global Settings

    uid = nobody

    gid = nobody

    use chroot = no # 是否禁锢用户家目录

    max connections = 10 # 最大连接数, 10已经非常大

    strict modes = yes # 是否完全检查

    pid file = /var/run/rsyncd.pid

    log file = /var/log/rsyncd.log

    # Directory to be synced

    [SYNCED_NAME] # 模块名映射成下面path

    path = /PATH/TO/SOME_DIR

    ignore errors = yes

    read only = yes

    # This parameter determines whether clients will be able to upload files or not. If “read only” is true then any attempted uploads will fail. If “read only” is false then uploads will be possible if file permissions on the daemon side allow them. The default is for all modules to be read only.

    write only = no

    # This parameter determines whether clients will be able to download files or not. If “write only” is true then any attempted downloads will fail. If “write only” is false then downloads will be possible if file permissions on the daemon side allow them. The default is for this parameter to be disabled.

    hosts allow = 192.168.0.0/24

    hosts deny = *

    list = false

    # This parameter determines if this module should be listed when the client asks for a listing of available modules. By setting this to false you can create hidden modules. The default is for modules to be listable.

    uid = USERNAME # 表示以哪个用户身份去获取文件, 最好不要用root

    gid = GROUPNAME # 表示以哪个属组身份去获取文件, 最好不要用root

    auth users = USERNAME # 允许的用户

    secrets file = /etc/rsyncd.passwd # 用户密码的存放位置

  3. 配置密码文件/etc/rsyncd.passwd

    vim /etc/rsyncd.passwd

    USERNAME:PASSWORD

    chmod 600 /etc/rsyncd.passwd

  4. 配置服务能够启动

    chkconfig rsync on

    chkconfig xinetd on

    service xinetd restart

客户端测试
# Pull
rsync -avz --progress USER@HOST::SRC... DEST
rsync -avz --progress rsync://USER@HOST/SRC... DEST
#SRC: rsyncd.conf里指定的共享模块名, 即配置文件里[SYNCED_NAME]

# Push
rsync -avz --progress SRC... USER@HOST::DEST
rsync -avz --progress SRC... rsync://USER@HOST/DEST
#DEST: rsyncd.conf里指定的共享模块名, 即配置文件里[SYNCED_NAME]
  1. 配置密码文件

    vim /etc/rsyncd.passwd

    PASSWORD

    chmod 600 /etc/rsyncd.passwd

  2. 指定密码文件rsync传输文件

    # E.G. pull
    rsync -avz --progress \
    --password-file=/etc/rsyncd.passwd \
    zak@192.168.0.117::mydata/test  /tmp
    

    4

😄 Oh Yeah!

posted @ 2019-10-07 23:36  ZakZhu  阅读(228)  评论(0编辑  收藏  举报