• 实用工具使用


    wget使用

    wget下载带递归时出现错误,顶层不让你递归!

    wget url //下载该网页将其保存为index.html文件
    wget -i index.html -F -B url //解析index.html文件将其的链接加上url进行下载
    

    wget下载src_link网站中所有的rpm格式的文件

    wget -r -c -np -nd $src_link --accept=rpm  --directory-prefix=$dirname
    

    rsync+rsync同步和网站实时镜像

    本文链接:https://blog.csdn.net/Z_xiao_feng/article/details/90270467

    2.rsync+rsync同步

    采用rsync+SSH的远程同步时,使用起来是最简单的,但是目标用户也被允许SSH登录到远程主机的Shell环境。在某些情况下,企业会希望只提供需要认证的同步目录资源,但并不希望提供远程登录,这时候就可以采用rsync+rsync同步。
    沿用练习一,需要完成的配置任务如下:
    1)在rsync源端将/usr/src 目录发布为同步资源:共享名为tools,仅允许用户 ruser 以密码 pwd123 访问
    2)在rsync操作端测试 rsync+rsync下行同步
    使用两台RHEL6虚拟机,其中一台为rsync同步提供源目录(192.168.4.5),另外一台作为rsync同步操作的发起端(192.168.4.205)
    在这里插入图片描述

    img

    实现此案例需要按照如下步骤进行。
    步骤一: 配置rsync服务端,发布tools同步资源
    1 建立同步账号文件

    [root@svr5 ~]# vim  /etc/rsyncd_users.db
    ruser:pwd123  									//用户名:密码,每行一个用户
    othername:123456
    
    [root@svr5 ~]# chmod  600  /etc/rsyncd_users.db  	//严格权限,否则同步会失败
    

    2 建立 /etc/rsyncd.conf 共享设置

    [root@svr5 ~]# vim  /etc/rsyncd.conf
    [tools]  											//定义共享名
        path = /usr/src  								//被共享的目录位置
        comment = Rsync Share Test  					//同步资源说明
        read only = yes  								//只读
        dont compress = *.gz *.bz2 *.tgz *.zip  		//同步时不再压缩的文档类型
        auth users = ruser  							//允许谁访问
        secrets file = /etc/rsyncd_users.db  			//指定账号文件的路径
    

    在上述配置文件中,若不添加最后两行认证配置,则默认以匿名方式提供。
    3 启用 rsync --daemon 服务端

    **[root@svr5 ~]# du -sh /usr/src/  					//确认待发布的同步目录
    163M    /usr/src/
    [root@svr5 ~]# yum  -y  install  xinetd
    [root@svr5 ~]# chkconfig  rsync  on  				//打开rsync服务开关
    [root@svr5 ~]# chkconfig  xinetd  on
    [root@svr5 ~]# service  xinetd  restart  			//通过xinetd启动**
    

    步骤二: rsync + rsync下行同步测试
    1 查看及列表同步资源
    查看远程主机提供了哪些同步资源:

    [root@pc205 ~]# rsync 192.168.4.5::
    tools           Rsync Share Test 					//共享名、共享说明
    列出指定同步资源下的文档:
    [root@pc205 ~]# rsync  ruser@192.168.4.6::tools  	//浏览共享
    Password:  										//验证ruser用户的口令
    drwxr-xr-x        4096 2009/10/01 22:58:39 debug
    drwxr-xr-x        4096 2009/10/01 22:58:39 kernels
    .. ..
    

    2)rsync下行同步

    [root@pc205 ~]# rsync -avz  ruser@192.168.4.6::tools/  /root/mysrc/ 
    													//下行同步,删除多余文件
    Password:  										//验证密码pwd123
    .. .. 
    sent 271848 bytes  received 37119880 bytes  598267.65 bytes/sec
    total size is 130075707  speedup is 3.48
    
    [root@pc205 ~]# du -sh /root/mysrc/  				//确认同步结果
    163M    /root/mysrc/
    

    3.网站实时镜像

    公司的网站服务器有两个镜像站点,分别放在北京和上海的IDC机房。现在要求利用rsync同步机制实现“服务器A–>服务器B”的实时镜像同步。
    需要完成的配置任务如下:
    1 双方的目录均为 /var/www/html/
    2 以 svr5 为同步发起方,配置 inotify+rsync 同步操作
    3 以 pc205 为同步目标,基于SSH方式进行验证
    使用两台RHEL6虚拟机,其中一台作为服务器A(192.168.4.5),另外一台作为服务器B(192.168.4.205),两台主机都安装httpd网站软件
    在这里插入图片描述

    img

    安装并启用inotify-tools工具,就可以在同步发起端实现对指定目录的监控,一旦出现更改、增加文件等操作,立即触发相应的命令操作(本例中即上行同步)。根据监控结果触发同步操作,其中用到了一部分Shell控制语句,最好建立专用脚本来实现,本例中只需理解脚本的用途即可。
    实现此案例需要按照如下步骤进行。
    步骤一:准备网页环境
    1 在svr5上,启用httpd网站服务、部署测试网页

    [root@svr5 ~]# yum -y install httpd
    .. ..
    [root@svr5 ~]# service httpd restart
    停止 httpd:                                               [确定]
    正在启动 httpd:                                           [确定]
    [root@svr5 ~]# chkconfig httpd on
    [root@svr5 ~]# echo "Welcome to Tarena" > /var/www/html/index.html
    [root@svr5 ~]# elinks -dump http://192.168.4.5  	//访问测试网页
       Welcome to Tarena
    

    2 在pc205上,启用httpd网站服务,先不用部署网页

    [root@pc205 ~]# yum -y install httpd
    .. ..
    [root@pc205 ~]# service httpd restart
    停止 httpd:                                               [确定]
    正在启动 httpd:                                           [确定]
    [root@pc205 ~]# chkconfig httpd on
    
    [root@pc205 ~]# ls /var/www/html/*  			//网页目录为空
    ls: 无法访问/var/www/html/*: 没有那个文件或目录
    

    步骤二:配置、启用实时同步脚本

    1 在svr5上,安装inotify-tools工具包

    [root@svr5 ~]# tar  xf  inotify-tools-3.13.tar.gz
    [root@svr5 ~]# cd  inotify-tools-3.13
    [root@svr5 inotify-tools-3.13]# ./configure
    .. ..
    [root@svr5 ~]# make  &&  make  install
    

    2)创建并部署SSH公钥,实现免密码验证

    [root@svr5 ~]# ssh-keygen  								//创建密钥对
    Generating public/private rsa key pair. 
    Enter file in which to save the key (/root/.ssh/id_rsa):  		//回车
    Enter passphrase (empty for no passphrase):  					//回车
    Enter same passphrase again:  									//回车
    Your identification has been saved in /root/.ssh/id_rsa.
    Your public key has been saved in /root/.ssh/id_rsa.pub.
    .. ..
    
    [root@svr5 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.4.205
      														//上传公钥
    root@192.168.4.205's password:  							//验证对方密码
    Now try logging into the machine, with "ssh 'root@192.168.4.205'", and check in:
    
      .ssh/authorized_keys
    
    to make sure we haven't added extra keys that you weren't expecting.
    
    [root@svr5 ~]# ssh root@192.168.4.205  					//验证免密码登录
    Last login: Thu Dec 24 00:53:00 2015 from 192.168.4.5
    [root@pc205 ~]# exit 										//返回客户机
    logout
    Connection to 192.168.4.205 closed.
    

    3 建立inotify实时同步脚本文件
    为了方便脚本的移植使用,在脚本中定义了两个变量:TARGET_DIR用来指定监控的目标文件夹,而RSYNC_CMD用来指定需要触发的同步操作。注意给脚本添加x执行权限,实际使用时根据需要变更这两个变量的值即可

    [root@svr5 ~]# vim  /root/isync.sh  				//新建脚本
    #!/bin/bash
    TARGET_DIR="/var/www/html"   						#//指定监控目录
    RSYNC_CMD="rsync  -az  --delete  /var/www/html/  192.168.4.205:/var/www/html/"
        												#//指定同步操作
    inotifywait  -mrq  -e  modify,move,create,delete,attrib  /opt | while read  -n5  X 
    do
        $RSYNC_CMD
    done  &
    
    [root@svr5 ~]# chmod  +x  /root/isync.sh  			//添加执行权限
    

    4 启动实时同步脚本程序
    此脚本一旦运行后,会一直在后台运行;如果有必要,可以将此脚本添加为开机自启动任务。

    [root@svr5 ~]# /root/isync.sh  					//执行脚本
    [root@svr5 ~]#
    

    步骤三:测试实时同步效果

    1 在svr5上向/var/www/html/目录下添加一个文件

    [root@svr5 ~]# touch /var/www/html/a.html
    [root@svr5 ~]# ls -lh /var/www/html/*.html
    -rw-r–r--. 1 root root 0 12月 17 09:02 /var/www/html/a.html
    -rw-r–r--. 1 root root 18 12月 17 08:37 /var/www/html/index.html
    

    2 在pc205上观察/var/www/html目录下的变化

    [root@pc205 ~]# ls -lh /var/www/html/*.html
    -rw-r--r--. 1 root root  0 12月 17 09:02 /var/www/html/a.html
    -rw-r--r--. 1 root root 18 12月 17 08:37 /var/www/html/index.html
    [root@pc205 ~]# 
    

    3 在svr5上删除刚添加的文件a.html

    [root@svr5 ~]# rm -rf /var/www/html/a.html 
    [root@svr5 ~]# ls -lh /var/www/html/*.htm
    -rw-r--r--. 1 root root 18 12月 17 08:37 /var/www/html/index.html
    

    4 在pc205上再次观察/var/www/html目录下的变化

    [root@pc205 ~]# ls -lh /var/www/html/*.html
    -rw-r--r--. 1 root root 18 12月 17 08:37 /var/www/html/index.html
    [root@pc205 ~]# 
    
  • 相关阅读:
    CI框架源码研读(整体架构)
    PHP微信公众号后台开发(Yii2实现)
    PHP后台支付的开发:微信支付和支付宝支付
    linux安装netcat 运行udp服务器
    运行swoole_server方法
    bind (ERROR 502): bind(0.0.0.0:9501) failed. Error: Address already in use [98] (端口被占用)
    随机生成红包算法
    获取分类算法
    PHP中的关系判断和注释
    php中自运算++ 或-- 的总结
  • 原文地址:https://www.cnblogs.com/DXGG-Bond/p/13288015.html
Copyright © 2020-2023  润新知