linux rsync安装与使用

rsync

Usage: /etc/init.d/rsync {start|stop|reload|force-reload|restart|status}

rsync默认配置文件

# defaults file for rsync daemon mode

# start rsync in daemon mode from init.d script?
# only allowed values are "true", "false", and "inetd"
# Use "inetd" if you want to start the rsyncd from inetd,
# all this does is prevent the init.d script from printing a message
# about not starting rsyncd (you still need to modify inetd's config yourself).
RSYNC_ENABLE=false

# which file should be used as the configuration file for rsync.
# This file is used instead of the default /etc/rsyncd.conf
# Warning: This option has no effect if the daemon is accessed
# using a remote shell. When using a different file for
# rsync you might want to symlink /etc/rsyncd.conf to
# that file.
# RSYNC_CONFIG_FILE=

# what extra options to give rsync --daemon?
# that excludes the --daemon; that's always done in the init.d script
# Possibilities are:
# --address=123.45.67.89 (bind to a specific IP address)
# --port=8730 (bind to specified port; default 873)
RSYNC_OPTS=''

# run rsyncd at a nice level?
# the rsync daemon can impact performance due to much I/O and CPU usage,
# so you may want to run it at a nicer priority than the default priority.
# Allowed values are 0 - 19 inclusive; 10 is a reasonable value.
RSYNC_NICE=''

# run rsyncd with ionice?
# "ionice" does for IO load what "nice" does for CPU load.
# As rsync is often used for backups which aren't all that time-critical,
# reducing the rsync IO priority will benefit the rest of the system.
# See the manpage for ionice for allowed options.
# -c3 is recommended, this will run rsync IO at "idle" priority. Uncomment
# the next line to activate this.
# RSYNC_IONICE='-c3'

# Don't forget to create an appropriate config file,
# else the daemon will not start.

 http://www.linuxidc.com/Linux/2014-09/106574.htm

rsync -h

Usage: rsync [OPTION]... SRC [SRC]... DEST
or rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST
or rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST
or rsync [OPTION]... SRC [SRC]... rsync://[USER@]HOST[:PORT]/DEST
or rsync [OPTION]... [USER@]HOST:SRC [DEST]
or rsync [OPTION]... [USER@]HOST::SRC [DEST]
or rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]
The ':' usages connect via remote shell, while '::' & 'rsync://' usages connect
to an rsync daemon, and require SRC or DEST to start with a module name.

 -r, --recursive             recurse into directories

-v, --verbose increase verbosity
-q, --quiet suppress non-error messages
--no-motd suppress daemon-mode MOTD (see manpage caveat)
-c, --checksum skip based on checksum, not mod-time & size
-a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)

-u, --update skip files that are newer on the receiver
--inplace update destination files in-place (SEE MAN PAGE)
--append append data onto shorter files
--append-verify like --append, but with old data in file checksum

-z, --compress compress file data during the transfer
--compress-level=NUM explicitly set compression level
--skip-compress=LIST skip compressing files with a suffix in LIST

-I, --ignore-times don't skip files that match in size and mod-time
--size-only skip files that match in size
--modify-window=NUM compare mod-times with reduced accuracy

-H, --hard-links            preserve hard links

 -e, --rsh=COMMAND           specify the remote shell to use

rsync,它比scp更强大,支持“不覆盖”原目录

例子:rsync -avz --progress /root/client/   root@202.112.23.12:/home/work/      

        //将本机的/root/client/拷贝至远程的202.112.23.12:/home/work/目录,--progress可以查看拷贝的过程

例子:rsync -avzu --progress /root/client/   root@202.112.23.12:/home/work/      //u选项,指定不覆盖原目录内容

我的命令`

rsync -e 'ssh -p 2330'  -avzu -r  --progress  test  root@201202:/home/work

如果主机A删除了某个文件,想要B上也删除,要加一个参数 --delete.

--delete delete extraneous files from destination dirs
--delete-before receiver deletes before transfer, not during
--delete-during receiver deletes during transfer (default)
--delete-delay find deletions during, delete after
--delete-after receiver deletes after transfer, not during
--delete-excluded also delete excluded files from destination dirs

增加--delete参数会把原有目录下的文件删除以保持客户端和服务器端文件系统完全一致,慎用.

使用rsync同步的时候,指定ssh的端口号

rsync有两种常用的认证方式,一种为rsync-daemon方式,另外一种则是ssh。

在一些场合,使用rsync-daemon方式会比较缺乏灵活性,ssh方式则成为首选。但是今天实际操作的时候发现当远端服务器的ssh默认端口被修改后,rsync时找不到一个合适的方法来输入对方ssh服务端口号。

在查看官方文档后,找到一种方法,即使用-e参数。

-e参数的作用是可以使用户自由选择欲使用的shell程序来连接远端服务器,当然也可以设置成使用默认的ssh来连接,但是这样我们就可以加入ssh的参数了。

具体语句写法如下:

  1. rsync -e 'ssh -p 1234' username@hostname:SourceFile DestFile

其他参数完全按照rsync的规定格式加入即可。

上面语句中比较新鲜的地方就是使用了单引号,目的是为了使引号内的参数为引号内的命令所用。没有引号的话系统就会识别-p是给rsync的一个参数了。我的描述可能比较烂,详情可以参考rsync官方描述:

Command-line arguments are permitted in COMMAND provided that COMMAND is presented to rsync as a single argument. You must use spaces (not tabs or other whitespace) to separate the command and args from each other, and you can use single- and/or double-quotes to preserve spaces in an argument (but not backslashes). Note that doubling a single-quote inside a single-quoted string gives you a single-quote; likewise for double-quotes (though you need to pay attention to which quotes your shell is parsing and which quotes rsync is parsing).

转自:http://yynotes.net/rsync-with-ssh-without-default-port/

rsync同步文件有两种方式,一种是daemon的方式(rsync daemon)另一种方式是通过远程shell方式(rsync remote shell)。

两种方式的区别

daemon方式,这种方式通过TCP方式连接远程rsync daemon,需要使用配置文件,并启用daemon进程。

rsync [OPTION] user@host::src dest
rsync [OPTION] src user@host::dest

remote shell方式,这种方式不需要使用配置文件,也不需要daemon进程。

然后是文件的增量更新,主要解决的问题是现在有两台服务器A和B,要将A服务器上的test目录增量复制到B服务器上的test目录(所谓增量更新就是指B服务器原来已经有的文件不再传了,只传送那些A有的而B没有的),这样使得B服务器上的test文件夹保持与A同步

这就需要用到rsync这个命令,这个命令的用法跟scp是一样的

比如在A服务器下执行命令:rsync -r /home/test/ user@B:/home/test 即可实现增量更新

注意:这里有一个问题需要详细说明一下:

假如test目录下有1.txt,2.txt这两个文件

如果命令中源目录那里写成这样:/home/test/

那么rsync准备更新的文件列表就是

1.txt

2.txt

然后就会在B机器的/home/test目录下找这两个文件并做增量更新,这样能够满足需求

但如果源目录写成这样:/home/test

那么rsync准备增量更新的文件列表就会是这样:

test/1.txt

test/2.txt

然后在B机器的/home/test目录下寻找test/1.txt,发现没有test文件夹,于是又创建了test文件夹,所以这样执行的结果就是B机器的目录结构就会有这两个文件,而这不是希望看到的

/home/test/test/1.txt

/home/test/test/2.txt

所以要注意这个问题。下面两种写法是正确的:

rsync -r /home/test/ user@B:/home/test 

或者

rsync -r /home/test user@B:/home

具体的关于rsync的详细参数的设置这里就不说了,这里只把一种推荐的命令运行方式记录下来:

rsync -rtv /home/test/ user@B:/home/test

-t是指判断文件是否已有的时候只判断文件的时间戳和文件的大小,如果都一样就把这个文件跳过(这是一种不够严谨但足够快的方法)

-v是指输出一下执行的日志,其实可以加很多v,v越多,输出的日志越多。

参考:http://www.cnblogs.com/iforever/p/4488733.html

http://my.oschina.net/immk/blog/193926

原文地址:https://www.cnblogs.com/youxin/p/5074670.html