Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2814156
  • 博文数量: 587
  • 博客积分: 6356
  • 博客等级: 准将
  • 技术积分: 6410
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-23 10:54
个人简介

器量大者,福泽必厚

文章分类

全部博文(587)

文章存档

2019年(3)

2018年(1)

2017年(29)

2016年(39)

2015年(66)

2014年(117)

2013年(136)

2012年(58)

2011年(34)

2010年(50)

2009年(38)

2008年(16)

分类: LINUX

2010-03-01 15:36:57

用rsync 来mirror  web 站点
说明:nodelb1.domain.com (机器名) ip地址为:172.17.61.126
     nodelb2.domain.com (机器名) ip地址为:172.170.61.127
     两台机器的os为:rhel4 up4
两台机器都安装了rhel4 up4 操作系统和rsync软件
1:启动rsync服务, 该服务依附于xinetd服务。
可以通过chkconfig  --list | grep rsync来查看rsync是否开机启动。
[root@nodelb1 files]# chkconfig --list | grep rsync
        rsync:  on
如上表示rsync为开机启动的服务!
如果不然:
则:[root@nodelb1 xinetd.d]# vi rsync
# default: off
# description: The rsync server is a good addition to an ftp server, as it \
#       allows crc checksumming etc.
service rsync
{
        disable = yes
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/bin/rsync
        server_args     = --daemon
        log_on_failure  += USERID
}
将disable =  yes改为no
重启xinetd服务
[root@nodelb1 xinetd.d]# service xinetd restart
Stopping xinetd:                                           [  OK  ]
Starting xinetd:                                           [  OK  ]
[root@nodelb1 xinetd.d]# netstat -antl | grep 873
tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN     
rsync服务在873端口监听,如果上面有输出,则表示rsync服务在运行!
在nodelb1.domain.com和nodelb2.domain.com同时进行上面的设置!
2:说明,我的apache服务器安装目录为/usr/local/httpd2.2  ,发布目录为:/usr/local/httpd2.2/htdocs (nodelb1.domain.com和nodelb2.domain.com安装目录一样)
apache的安装省略!
2:在nodelb1.domain.com上创建用户:
 groupadd backupuser
 useradd -g backupuser backupuser
 passwd backupuser
输入密码即可
3:在nodelb2.domain.com上进行测试,现在nodelb1.domain.com上的/usr/local/httpd2.2/htdocs目录下
mkdir hello
echo "hello">index.html
上面是在nodelb1.domain.com上的准备工作,
然后在nodelb2.domain.com上运行:

[root@nodelb2 ~]# rsync -avz -e ssh backupuser@nodelb1.domain.com:/usr/local/httpd2.2/htdocs/   /usr/local/httpd2.2/htdocs/

ssh: node1: Temporary failure in name resolution

显然是因为nodelb1.domain.com没有被解析所致:

解决方法:在nodelb1.domain.com和nodelb2.domain.com同时进行设置:


[root@nodelb1 httpd2.2]# cat /etc/hosts
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1       cp3.domain.com localhost.localdomain    localhost
172.17.61.124   server1.domain.com
172.17.61.125   db1.domain.com
172.17.61.126   nodelb1.domain.com  ##红色字体为需要添加到部分!
172.17.61.127   nodelb2.domain.com


[root@nodelb2 ~]# ping nodelb1.domain.com

PING nodelb1 (172.17.61.126) 56(84) bytes of data.

64 bytes from nodelb1 (172.17.61.126): icmp_seq=0 ttl=64 time=10.2 ms

64 bytes from nodelb1 (172.17.61.126): icmp_seq=1 ttl=64 time=0.213 ms

 

--- nodelb1 ping statistics ---

2 packets transmitted, 2 received, 0% packet loss, time 1000ms

rtt min/avg/max/mdev = 0.213/5.223/10.234/5.011 ms, pipe 2

[root@nodelb2 ~]# rsync -avz -e ssh backupuser@nodelb1:/usr/local/httpd2.2/htdocs/   /usr/local/httpd2.2/htdocs/

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

RSA key fingerprint is 46:75:f2:73:c7:72:7d:61:c8:05:e7:37:42:8a:30:b4.

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

Warning: Permanently added 'nodelb1,172.17.61.126' (RSA) to the list of known hosts.

backupuser@nodelb1's password:

receiving file list ... done

./

hello/

hello/index.html

 

sent 36 bytes  received 157 bytes  22.71 bytes/sec

total size is 59  speedup is 0.31

显然已经rsync可以使用了。

4:Create The Keys On nodelb2.domain.com

[root@nodelb2 ~]# mkdir /root/rsync
[root@nodelb2 ~]# ssh-keygen -t dsa -b 1024 -f /root/rsync/mirror-rsync-key
Generating public/private dsa key pair.
Enter passphrase (empty for no passphrase)
<-- ENTER
Enter same passphrase again: <-- ENTER
Your identification has been saved in /root/rsync/mirror-rsync-key.
Your public key has been saved in /root/rsync/mirror-rsync-key.pub.
The key fingerprint is:
68:1e:9c:12:f1:f5:7f:53:d5:1d:d0:f2:dd:c2:88:f3 root@server2.example.com
The key's randomart image is:

去/root/rsync目录查看如下:

[root@nodelb2 rsync]# pwd
/root/rsync
[root@nodelb2 rsync]# ll
total 8
-rw-------  1 root root 668 Mar  1 14:27 mirror-rsync-key
-rw-r--r--  1 root root 602 Mar  1 14:27 mirror-rsync-key.pub
[root@nodelb2 rsync]#

5:接着copy public key到nodelb1.domain.com

在nodelb2.domain.com上执行:

scp /root/rsync/mirror-rsync-key.pub backup

在nodelb1上执行:

su - backupuser

mkdir ~/.ssh
chmod 700 ~/.ssh
mv ~/mirror-rsync-key.pub ~/.ssh/
cd ~/.ssh
mv mirror-rsync-key.pub  authorized_keys
chmod 600 authorized_keys

将command="/home/someuser/rsync/checkrsync",from="server2.example.com",no-port-forwarding,no-X11-forwarding,no-pty 添加到/home/backupuser/.ssh/authorized_keys的开头:

添加后如下:

 

[backupuser@nodelb1 .ssh]$ cat authorized_keys
command="/home/backupuser/rsync/checkrsync",from="nodelb2.domain.com",no-port-forwarding,no-X11-forwarding,no-pty ssh-dss AAAAB3NzaC1kc3MAAACBANbtTYPmd6OUf99tdWFyURm/IaOe7wBC4GuEoRZUA0DgY5xSEnLPt204JYtTXeENDGOS1DPiGGoC1SoWPFVdPzxdoc9tUqyzFudaC/b/RQfKkIAo/indR/hWETS1RCa7/j0dzd+ti43TxN5JIkaruX6JyYVC+BSMLhSO/pCWFQy1AAAAFQDr60fBphjskBOvgdmzhP0xTvrNOQAAAIBUKbAuLgtLo2ots124UOOkZzJnD6Z97XYP/9pH5rryFf1oBb0TWRhX/ytQGWJvSPYo/OA+tOfZViv26Vq2MPR3eW20uYDbCQZPyo0K3W1CGCG8VGE5oAUjVBBHlbDJylWvuWBxW92BeiP7xZCLmncErTduu803R+9YZAbIQ/LCEgAAAIAEkO7g2Sn46JQoai9waKWJfgxMOD2SF8IDFoTX+goJ+tRGZjLeJCGE1DD2OwTZIQ1vWh71iY1CRbIlYPFqbgtie0qD0+XJAVLbpRs6Hb88YxRSH5sLF02/RfLA3LI0iXx/w/Ay3oTDPbw23HQD56Ks2T585+JaCoOxA9GqUIpFtw== root@nodelb2


红色字体为添加部分,from必须为 FQDN 名,ip地址不正确! 
在nodelb1上进行如下操作:
mkdir ~/rsync
vi ~/rsync/checkrsync   ###Now we create the script ~/rsync/checkrsync that rejects all commands except rsync.
 

#!/bin/sh

case "$SSH_ORIGINAL_COMMAND" in
        *\&*)
                echo "Rejected"
                ;;
        *\(*)
                echo "Rejected"
                ;;
        *\{*)
                echo "Rejected"
                ;;
        *\;*)
                echo "Rejected"
                ;;
        *\<*)
                echo "Rejected"
                ;;
        *\`*)
                echo "Rejected"
                ;;
        rsync\ --server*)
                $SSH_ORIGINAL_COMMAND
                ;;
        *)
                echo "Rejected"
                ;;
esac


修改权限:

chmod 700 ~/rsync/checkrsync

6:测试:在nodelb2.domain.com上运行如下:

[root@nodelb2 rsync]# rsync -avz --delete --exclude=**/stats  --exclude=**/error --exclude=**/files/pictures -e "ssh -i /root/rsync/mirror-rsync-key" /usr/local/httpd2.2/htdocs/

说明:(The --delete option means that files that have been deleted on server1.example.com should also be deleted on server2.example.com. The --exclude option means that these files/directories should not be mirrored; e.g. --exclude=**/error means "do not mirror /usr/local/httpd2.2/htdocs/error". You can use multiple

 --exclude options.(当然nodelb1.domain.com上的/usr/local/httpd2.2/htdocs目录下必须有stats,error,files/pictures目录才可以测出效果来)

可以在nodelb2上创建cron job来定是运行rsync!
留为笔记!
阅读(1438) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~