现在的位置: 首页 > 综合 > 正文

rsync实例用法

2013年08月31日 ⁄ 综合 ⁄ 共 3705字 ⁄ 字号 评论关闭
vi /etc/xinetd.d/rsync
disable = yes
改成
disable = no

rsyncd.conf 是rsyncd的config文件

vi /etc/rsyncd.conf

#uid = nobody
#gid = nobody
use chroot = yes
max connections = 4
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log

    [compshop]
    path = /home/sites/compshop
    auth users = compshop
    uid = compshop
    gid = compshop
    secrets file = /etc/rsyncd.secrets
    read only = no

    [datafeed]
    path = /home/sites/datafeed
    auth users = datafeed
    uid = datafeed
    gid = datafeed
    secrets file = /etc/rsyncd.secrets
    read only = no

    [smtemplates]
    path = /home/sites/smtemplates
    auth users = smtemplates
    uid = smtemplates
    gid = smtemplates
    secrets file = /etc/rsyncd.secrets
    read only = no

    [smarterv2]
    path = /home/sites/smarterv2
    auth users = smarterv2
    uid = smarterv2
    gid = smarterv2
    secrets file = /etc/rsyncd.secrets
    read only = no

rsyncd.secrets是rsyncd的密码文件,里面是写用户名和密码,就是linux的用户名和密码

vi /etc/rsyncd.secrets

compshop:any
datafeed:any
smtemplates:any
smarterv2:any

以上是服务器端的配置,开了这个服务以后,端口是873

以下是客服端的配置文件,是在另外一台电脑上的。文件名是可以自己改的。

vi /home/sites/sites_rsyncd

#!/bin/bash
rsync -tvzrp --progress --password-file=/home/sites/rsyncd.secrets --delete --exclude

/home/sites/compshop/logs compshop@192.168.168.241::compshop  /home/sites/compshop/
rsync -tvzrp --progress --password-file=/home/sites/rsyncd.secrets --delete --exclude

/home/sites/datafeed/logs datafeed@192.168.168.241::datafeed  /home/sites/datafeed/
rsync -tvzrp --progress --password-file=/home/sites/rsyncd.secrets --delete --exclude

/home/sites/smtemplates/logs smtemplates@192.168.168.241::smtemplates  /home/sites/smtemplates/
rsync -tvzrp --progress --password-file=/home/sites/rsyncd.secrets --delete --exclude

/home/sites/smarterv2/logs smarterv2@192.168.168.241::smarterv2  /home/sites/smarterv2/

chmod 744 /home/sites/sites_rsyncd

vi /home/sites/rsyncd.secrets

any

chmod 600 /home/sites/rsyncd.secrets

三、rsync命令的用法
  在配置完rsync服务器后,就可以从客户端发出rsync命令来实现各种同步的操

作。rsync有很
多功能选项,下面就对介绍一下常用的选项:
  rsync的命令格式可以为:
  1. rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST
  2. rsync [OPTION]... [USER@]HOST:SRC DEST
  3. rsync [OPTION]... SRC [SRC]... DEST
  4. rsync [OPTION]... [USER@]HOST::SRC [DEST]
  5. rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST
  6. rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]
  rsync有六种不同的工作模式:
  1. 拷贝本地文件;当SRC和DES路径信息都不包含有单个冒号":"分隔符时就启动这

种工作模式。
  2.使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器

。当DST
路径地址包含单个冒号":"分隔符时启动该模式。
  3.使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器

。当SRC
地址路径包含单个冒号":"分隔符时启动该模式。
  4. 从远程rsync服务器中拷贝文件到本地机。当SRC路径信息包含"::"分隔符时启动

该模式。
  5. 从本地机器拷贝文件到远程rsync服务器中。当DST路径信息包含"::"分隔符时启

动该模式。
  6. 列远程机的文件列表。这类似于rsync传输,不过只要在命令中省略掉本地机信

息即可。
  下面以实例来说明:
  # rsync -vazu -progress  terry@192.168.100.21:/terry/  /home
  v详细提示
  a以archive模式操作,复制目录、符号连接
  z压缩
  u只进行更新,防止本地新文件被重写,注意两者机器的时钟的同时
  -progress指显示
  以上命令是保持客户机192.168.100.90上的/home/terry目录和rsync服务器上的terry目

录同
步。该命令执行同步之前会要求你输入terry账号的密码,这个账号是我们前面

在rsyncd.secrets
文件中定义的。如果想将这条命令写到一个脚本中,然后定时执行它的话,可以使

用--password-file
选项,具体命令如下:
  # rsync -vazu -progress --password-file=/etc/rsync.secret
  terry@192.168.100.21:/terry/  /home
  要使用--password-file选项,就得先建立一个存放密码的文件,这里指定

为/etc/rsync.secret。
其内容很简单,如下:
  terry:12345
  同样要修改文件属性如下:
  # chmod 600 /etc/rsyncd.secrets
  四、利用rsync保持Linux服务器间的文件同步实例
  现在假设有两台Linux服务器A(192.168.100.21)和B(192.168.100.90),服务器A中的
/home/terry和服务器B中的/home/terry这两个目录需要保持同步,也就是当服务器A中文

件发生
改变后,服务器B中的文件也要对应去改变。
  我们按上面的方法,在服务器A上安装rsync,并将其配置为一台rsync服务器,并

将/home/terry
目录配置成rsync共享出的目录。然后在服务器B上安装rsync,因为B只做客户端,所以

无需配置。
然后在服务器B,建立以下脚本:
  #!/bin/bash
  /usr/loca/rsync/bin/rsync -vazu -progress  --delete
  --password-file=/etc/rsync.secret terry@192.168.100.21:/terry/  /home
  将这个脚本保存为AtoB.sh,并加上可执行属性:
  # chmod 755 /root/AtoB.sh
  然后,通过crontab设定,让这个脚本每30分钟运行一次。执行命令:
  # crontab -e
  输入以下一行:
  0,30 * * * *  /root/AtoB.sh
  保存退出,这样服务器B每个小时的0分和30分时都会自动运行一

次AtoB.sh,AtoB.sh是负责
保持服务器B和服务器A同步的。这样就保证了服务器A的所有更新在30钟后,服务器B

也一样取
得了和服务器A一样的最新的资料。


 

抱歉!评论已关闭.