学导网 >> it000 >> Oracle
用rsync简单配置同步目录
www.xuedao.net  2007-6-12 14:34:00 网络


    Unix下rsync工具软件可以实现Internet上两台机器间的目录同步。
   
    一般Redhat企业版系统安装好后会带有这个程序包,查看当前机器A的Linux系统是否rsync软件:
   
    机器A$whereis rsync
          rsync: /usr/bin/rsync /usr/share/man/man1/rsync.1.gz
   
    上面显示已经安装有了rsync
   
    如果没有,可以去网上下载rpm包,然后安装。
   
    使用前配置不需要密码的ssh连接
   
    在机器A一个普通用户web下生成密匙对
   
    机器A$ssh-keygen -t dsa
      Generating public/private dsa key pair.
      Enter file in which to save the key (/home/web/.ssh/id_dsa):
      Enter passphrase (empty for no passphrase): (enter passphrase ******)
      Enter same passphrase again: (enter passphrase ******)
      Your identification has been saved in /home/web/.ssh/id_dsa.
      Your public key has been saved in /home/web/.ssh/id_dsa.pub.
      The key fingerprint is:
      7e:5e:b2:f2:d4:54:58:6a:fa:6b:52:9c:da:a8:53:1b web@机器A_hostname
   
    把/home/web/.ssh/目录下生成的公钥id_dsa.pub文件用sftp传到远端Internet机器B的普通用户web2
   
    /home/web2/.ssh/目录下
   
    如果/home/web2/.ssh/目录下以前没有生成过authorized_keys文件,生成一个信任的authorized_keys文件
   
    机器B$mv id_dsa.pub authorized_keys
   
    如果/home/web2/.ssh/目录下以前有生成过authorized_keys文件,把id_dsa.pub添加到信任的authorized_keys文件里
   
    机器B$cat id_dsa.pub >> authorized_keys

    查看当前机器A的Linux系统是否keychain软件:
   
    机器A$whereis keychain
    keychain: /usr/bin/keychain /usr/share/man/man1/keychain.1.gz
   
    如果没有,要在机器A上安装keychain软件.
   
    切换到root用户,安装:
   
    机器A#rpm -i keychain-2.6.2-1.noarch.rpm
   
    切换到web2用户,在/home/web2/.bash_profile配置文件里面增加下面内容
   
    keychain id_dsa
    . ~/.keychain/$HOSTNAME-sh
   
    修改配置文件,重新登陆后,会提示要您输入密匙对信任连接的密码。
   
    正确输入建密匙对时的密码后,机器重启前,ssh都可以不需要密码登陆了:
   
    机器A$ssh web2@机器B
   
    机器B$
   
    说明:(Redhat机器重新启动后,再次登陆web用户的时候还需要输入建密匙对时的密码)
   
    最后在机器A上写类似下面的脚本rsync_web.sh
su - web -c "rsync --verbose --progress --stats --delete --recursive --times --perms --rsh=/usr/bin/ssh --links --exclude "*.log" /www/htdocs/web/new/* web2@机器B:/www/htdocs/web2/new >>/home/web2/log/rsync_`date `+%y%m%d``.log 2>&1"
    配合Linux的定时操作cron就可以实现Internet上两台机器间的目录同步了。
   
    配置不需要密码的ssh连接后,Internet网络间安全复制文件scp命令格式:
   
    从机器B拷贝文件/tmp/b.dmp.Z到机器A:
   
    机器A#su - web -c "/usr/bin/scp web2@机器B:/tmp/b.dmp.Z /tmp/b.dmp.Z"

    从机器A拷贝文件/tmp/a.dmp.Z到机器B:
   
    机器A#su - web -c "/usr/bin/scp /tmp/a.dmp.Z web2@机器B:/tmp/a.dmp.Z
   

 

评论】 【关闭
相关新闻
Google提供广告