`

rsync 使用说明

阅读更多

我主要用rysnc同步文件, rsync是一款非常优秀的文件同步管理软件,它也支持多种操作系统平台,在Unix环境中,rsync有着卓绝的功绩。希望这篇文档能对一些朋友有所帮助。


1. Install

[url]http://www.samba.org/rsync/[/url]

shell> tar zxvf rsync-x.x.x.tar.gz
shell> cd rsync-x.x.x
shell> ./configure && make && make install

目前大部分 Unix/Linux 默认即安装了 rsync。



2. /etc/rsyncd.conf
 
shell> touch /etc/rsyncd.conf
shell> vi /etc/rsyncd.conf


Edit /etc/rsyncd.conf as below:
 
uid = nobody
gid = nobody
use chroot = no
max connections = 5
pid file = /var/run/rsync.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsync.log
 
[<dst-dir>]
path = <dst-dir-fullpath>
ignore errors
read only = no
list = yes
auth users = username
secrets file = /etc/rsyncd.secrets



3. /etc/rsync.secrets
 
shell> echo "jack:password" >> /etc/rsyncd.secrets
shell> chmod 600 /etc/rsyncd.secrets
 
*注:一定要把rsyncd.secrets的权限设为600,否则不能正常进行身份认证。
     包括--password-file指向的密码文件,也必须设成600权限。

     
     
4. Autorun

>>> Idea 1 - only linux <<<

shell> vi /etc/xinetd.d/rsync
 
set DISABLE to yes, the result looks like this:
service rsync
{
        disable         = no  <---------- change to yes
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/bin/rsync
        server_args     = --daemon
        log_on_failure  += USERID
}


>>> Idea 2 - on openSUSE <<<

shell> echo "/usr/local/bin/rsync --daemon" >> /etc/init.d/boot.local


>>> Idea 3 <<<
 
shell> echo "/usr/local/bin/rsync --daemon" >> /etc/rc.local



5. Run daemon

shell> rsync --daemon
 
*注一:在rsync的man手册的 CONNECTING TO AN RSYNC SERVER 处有提到:
It is also possible to use rsync without a remote shell as the transport. In this case you will connect to a remote  rsync server running on TCP port 873.
 
*注二:在rsync的man手册的 --port=PORT 选项解释中有提到:
This specifies an alternate TCP port number to use rather than the default port 873.



6. rsync 命令实例


6.1 显示目录内容

命令
------
a) rsync <dst-dir>
b) rsync -r <dst-dir>
c) rsync jack@192.168.0.1::<dst-dir>
d) rsync ssh_user@192.168.0.1:<dst-dir>

命令说明
---------
a) 显示<dst-dir>目录
内容 (第一层)
b) 递归显示<dst-dir>目录
内容
c) 显示远程主机<dst-dir>目录内容
   *注1:端口模式, 基于rsync用户的身份验证
   *注2:rsync server上的目录必须具有xx7的权限.
d) 查看
远程 主机<dst-dir>目录 内容
   *注1:remote shell模式, 通过ssh连接的基于系统本地用户的身份验证
   *注2:这里只使用了一个冒号(:),同时用户名是远
程主机 的ssh用户,密码也是ssh用户对应的密码。
   *注3:使用"<dst-dir>",则列出<dst-dir>文件夹本身的信息。若要列出<dst-dir>文件夹内容,应使用"<dst-dir>/"。
   
参数说明
---------
-r          对目录进行递归操作

   
6.2 本地目录之间同步

命令
------
a) rsync -av  --progress <src-dir>/ <dst-dir>             *** 注意(/) ***
b) rsync -av  --progress <src-dir>  <dst-dir>
c) rsync -avu --progress --delete <src-dir>/  <dst-dir>
d) 
rsync -av  --progress --temp-dir=/tmp <src-dir>/ <dst-dir>

命令说明
---------
a) 同步src-dir目录下所有文件到dst-dir目录下
b) 同步src-dir目录下所有文件到dst-dir/src-dir目录下
c) 对src-dir目录内容向dst-dir目录下进行差异更新,有增加/更新则添加替换,有减少则对其删减
d) 比a)多了
--temp-dir=/tmp,即指定/tmp为临时交换区,这样可以避免因目标目录空间不够引起的无法同步文件的错误。

参数说明
---------
-a          相当于 -rlptgoD 的集合
-u          等同于 --update,在目标文件比源文件新的情况下不更新
-v          显示同步的文件
--progress  显示文件同步时的百分比进度、传输速率
--delete    删除目标目录中多于源目录的文件


6.3 异地主机之间同步

命令
------
a) rsync -avz  --progress <src-dir> jack@192.168.0.1::<dst-dir>/

b) rsync -avz  --progress <src-dir> jack@192.168.0.1::<dst-dir>/ --password-file=/home/jack/rsync.jack

c) rsync -avuz  --progress --delete <src-dir> jack@192.168.0.1::<dst-dir>/ --password-file=/home/jack/rsync.jack

d) rsync -avz  --progress jack@192.168.0.1::<dst-dir>/<src-dir> <dst-dir>

命令说明
---------
a) 同步本地<src-dir>目录的内容到远程主机192.168.0.1的<dst-dir>目录下,jack是rsync数据库用户(参见3. /etc/rsync.secrets)
b) 通过自动读取用户密码而实现非交互登录文件同步

c) 较b)多了-u和--delete

d) 同步远程主机内容到本地目录


参数说明
---------
-z                等同于 --compress,对传输的文件压缩,这对节约网络带宽或在网络资源紧张的情况下非常有用
--password-file   引用192.168.0.1上rsync用户jack口令的本地文件,创建方法如下
                  shell> echo "jackpwd" >> /home/jack/
rsync.jack
                  shell> chown jack:wheel 
/home/jack/ rsync.jack
                  shell> chmod 600  /home/jack/ rsync.jack








分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics