【Linux】rsync用法

来源:互联网 发布:恋爱软件免费版 编辑:程序博客网 时间:2024/05/16 18:59

【Linux】rsync用法

rsync 最早是想要取代 rcp 这个指令的,因为 rsync 不但传输的速度快,而且他在传输时, 可以比对本地端与远端主机欲复制的档案内容,而仅复制两端有差异的档案而已,所以传输的时间就相对的降低很多! 此外, rsync 的传输方式至少可以透过三种方式来运作:

· 在本机上直接运作,用法就与 cp 几乎一模一样,例如:

rsync -av /etc /tmp (将 /etc/ 的资料备份到 /tmp/etc )

· 透过 rsh 或 ssh 的通道在 server / client 之间进行资料传输,例如:

rsync -av -e ssh user@rsh.server:/etc /tmp (将 rsh.server 的 /etc 备份到本地主机的 /tmp )

· 直接透过 rsync 提供的服务 (daemon) 来传输,此时 rsync 主机需要启动 873 port

1. 你必须要在 server 端启动 rsync , 看 /etc/xinetd.d/rsync 即可;
2. 你必须编辑 /etc/rsyncd.conf 设定档;
3. 你必须设定好 client 端连线的密码资料;
4. 在 client 端可以利用:rsync -av user@hostname::/dir/path /local/path

其实三种传输模式差异在于有没有冒号 (:) 而已,本地端传输不需要冒号,透过 ssh 或 rsh 时,就得要利用一个冒号 (:), 如果是透过 rsync daemon 的话,就得要两个冒号 (::) ,应该不难理解啦!因为本地端处理很简单, 而我们的系统本来就有提供 ssh 的服务,所以,底下鸟哥将直接介绍利用 rsync 透过 ssh 来备份的动作喔。 不过,在此之前咱们先来看看 rsync 的语法吧!

转自:http://linux.vbird.org/linux_server/0310telnetssh.php#rsync


1rsync语法及参数

[root@linux ~]# rsync [-avrlptgoD] [-e ssh] [user@host:/dir] [/local/path]

参数:

-v :观察模式,可以列出更多的资讯;

-q :与 -v 相反,安静模式,输出的资讯比较少;

-r :递回复制!可以针对"目录"来处理!很重要!

-u :仅更新 (update),不会覆盖目标的新档案;

-l :复制连结档的属性,而非连结的目标原始档案内容;

-p :复制时,连同属性 (permission) 也保存不变!

-g :保存原始档案的拥有群组;

-o :保存原始档案的拥有人;

-D :保存原始档案的装置属性 (device)

-t :保存原始档案的时间参数;

-I :忽略更新时间 (mtime) 的属性,档案比对上会比较快速;

-z :加上压缩的参数!

-e :使用的通道协定,例如使用 ssh 通道,则 -e ssh

-a :相当于 -rlptgoD ,所以这个 -a 是最常用的参数了!

更多说明请参考 man rsync 的解说!


2,使用rsync将远程37上的/etc目录备份到本地的  /tmp/bb目录下面!

[root@kk ~]# rsync -av -e ssh 192.168.254.37:/etc /tmp/bb

The authenticity of host '192.168.254.37 (192.168.254.37)' can't be established.

RSA key fingerprint is 67:aa:a3:19:b4:be:1f:64:e6:76:49:41:92:76:ef:7c.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '192.168.254.37' (RSA) to the list of known hosts.

root@192.168.254.37's password:

。。。。。。

sent 549296 bytes received 25779122 bytes 810105.17 bytes/sec

total size is 82609812 speedup is 3.14

说明:-a保持原文件的所有属性(这个参数是其他参数的组合);

-v 显示备份过程中的详细信息

-e 指定通道协议,-e sshssh协议连接远程的37机器

/tmp/bb 本地用来保存备份的目录


3,将本机的/etc目录备份到本机的/tmp/aa目录下

第一次:可以理解为全备!

[root@nod1 peers]# rsync -av /etc/ /tmp/aa

yum/yum-updatesd.conf

yum/pluginconf.d/

yum/pluginconf.d/security.conf

sent 89011933 bytes received 33008 bytes 6595921.56 bytes/sec

total size is 88889417 speedup is 1.00

第二次:仅作了差异备份!效率很高!

来给/etc/下添加一个zbb文件,再备份!

[root@nod1 peers]# rsync -av /etc/ /tmp/aa

building file list ... done

./

zbb //呵呵,这里只备份自上次以来有差异的文件!效率很高!!

sent 60946 bytes received 54 bytes 40666.67 bytes/sec

total size is 88889417 speedup is 1457.20

0 0