LINUX中的rsync

来源:互联网 发布:c语言字符串最大长度 编辑:程序博客网 时间:2024/05/16 23:01

rsync是类unix系统下的数据镜像备份工具,从软件的命名上就可以看出来了——remote sync。它的特性如下:

  可以镜像保存整个目录树和文件系统。

  可以很容易做到保持原来文件的权限、时间、软硬链接等等。

  无须特殊权限即可安装。

  优化的流程,文件传输效率高。

  可以使用rcp、ssh等方式来传输文件,当然也可以通过直接的socket连接。

  支持匿名传输,以方便进行网站镜象。

  软件下载

  rysnc的主页地址为:http://rsync.samba.org/

  目前最新版本为3.0.7。可以选择从原始网站下载:http://rsync.samba.org/ftp/rsync/。

[编辑本段]
安装

  Ubuntu安装: sudo apt-get install rsync

  RedHat: yum install rsync

  编译安装

  rsync的编译安装非常简单,只需要以下简单的几步:

  [root@www rsync-2.4.6]# ./configure

  [root@www rsync-2.4.6]# make

  [root@www rsync-2.4.6]# make install

  但是需要注意的是必须在服务器A和B上都安装rsync,其中A服务器上是以服务器模式运行rsync,而B上则以客户端方式运行rsync。这样在web服务器A上运行rsync守护进程,在B上定时运行客户程序来备份web服务器A上需要备份的内容。

[编辑本段]
rsyncd.conf

  rsync服务器的配置文件是rsyncd.conf.

  以下是一个rsyncd.conf的样本:

  # Distributed under the terms ofthe GNU General Public License v2

  # Minimal configuration file for rsyncdaemon

  # See rsync(1) and rsyncd.conf(5) man pagesfor help

  # This line is required by the/etc/init.d/rsyncd script

  pid file = /var/run/rsyncd.pid

  port = 873

  uid = root

  gid = root

  use chroot = yes

  read only = yes

  max connections = 5

  #This will give you a separate log file

  #log file = /var/log/rsync.log

  log format = %t %a %m %f %b

  syslog facility = local3

  timeout = 300

  [test]

  path = /home/nemo

  list=yes

  ignore errors

  auth users = root, nemo

  secrets file = /etc/rsyncd/rsyncd.secrets

  comment = linuxsir home

  exclude = tmp/

  各个参数具体含义参见man rsyncd.conf

[编辑本段]
服务器端启动:

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

  可能需要root权限运行.

  /etc/rsyncd/rsyncd.conf 是你刚才编辑的rsyncd.conf的位置.

  也可以在/etc/rc.d/rc.local里加入让系统自动启动等.

[编辑本段]
客户端同步:

  rsync -参数 用户名@同步服务器的IP::rsyncd.conf中那个方括号里的内容 本地存放路径 如:

  rsync -avzP nemo@192.168.10.1::nemo /backup

  说明:

  -a 参数,相当于-rlptgoD,-r 是递归 -l 是链接文件,意思是拷贝链接文件;-p 表示保持文件原有权限;-t 保持文件原有时间;-g 保持文件原有用户组;-o 保持文件原有属主;-D 相当于块设备文件;

  -z 传输时压缩;

  -P 传输进度;

  -v 传输时的进度等信息,和-P有点关系,自己试试。可以看文档;