• 工作中实用的Shell脚本实例


    转自:https://www.jianshu.com/p/093d712c777d

    1. 扫描主机端口状态
    #!/bin/bash
    HOST=$1
    PORT="22 80 8080 3306"
    for PORT in $PORT; do
        if echo &>/dev/null > /dev/tcp/$HOST/$PORT; then
            echo "$PORT open"
        else
            echo "$PORT close"
        fi
    done
    2. 代码上线脚本
    #!/bin/bash
    # code online
    # author: ren
    PROJT_DIR=/usr/local/nginx/html
    OLD_DIR=/usr/local/nginx/html/web1
    PROJT=web1
    BACKUP_DIR=/data/backup
    DATA_CHMOD=www
    DATE=`date +%F`
    NEW_DIR=/data/web1
    # 关闭nginx
    function stop_nginx() {
      /usr/bin/systemctl stop nginx
      if [ $? -eq 0 ];then
         echo "nginx is stopd"
      else
         echo "nginx is not stop please check..."
         exit 1
      fi
    }
    
    #2 备份原有数据
    function backup_data() {
      if [ -d $BACKUP_DIR/$DATE'-'$PROJT ];then
        echo "DIR $BACKUP_DIR/$DATE'-'$PROJT is exist"
        exit 2
      else
        mv $OLD_DIR $BACKUP_DIR/$DATE'-'$PROJT
      fi
    }
    
    # 3移动新的代码 项目目录  注:此代码目录需手动上传解压 
    function new_code() {
      if [ -d $NEW_DIR ];then
         mv $NEW_DIR $PROJT_DIR
      else
         echo "NEW_DIR is not exist"
         exit 3
      fi
    }
    
    # 4 修改权限
    function chmod_news() {
      chown -R $DATA_CHMOD.$DATA_CHMOD $OLD_DIR
    }
    
    # 5 启动服务
    function start_nginx() {
      /usr/bin/systemctl start nginx
      if [ $? -eq 0 ];then
        echo "nginx start ok"
      else
        echo "ngin is not start,please check..."
      fi
    }
    
    stop_nginx
    backup_data
    new_code
    chmod_news
    start_nginx
    3. 检测MySQL主从复制是否异常
    #!/bin/bash
    user="root"
    password="123456"
    mycmd="mysql -u$user -p$password -h 192.168.1.88"
    
    function chkdb() {
    list=($($mycmd -e "show slave status G"|egrep "Running|Behind"|awk -F: '{print $2}'))
    if [ ${list[0]} = "Yes" -a ${list[1]} = "Yes" -a ${list[2]} -lt 120 ]
    then echo "Mysql slave is ok"
    else echo "Mysql slave replation is filed"
    fi
    }
    
    function main() {
    while true
    do chkdb
       sleep 3
    done
    }
    main
    4. MySQL数据库备份脚本(mysqldump)
    #!/bin/bash
    #删除15天以前备份
    
    source /etc/profile           #加载系统环境变量
    source ~/.bash_profile    #加载用户环境变量
    set -o nounset             #引用未初始化变量时退出
    #set -o errexit             #执行shell命令遇到错误时退出
    
    user="root"
    password="123456"
    host="localhost"
    port="3306"
    #需备份的数据库,数组
    db=("test")
    #备份时加锁方式,
    #MyISAM为锁表--lock-all-tables,
    #InnoDB为锁行--single-transaction
    lock="--single-transaction"
    mysql_path="/usr/local/mysql"
    backup_path="${mysql_path}/backup"
    date=$(date +%Y-%m-%d_%H-%M-%S)
    day=15
    backup_log="${mysql_path}/backup.log"
    
    #建立备份目录
    if [ ! -e $backup_path ];then
        mkdir -p $backup_path
    fi
    
    #删除以前备份
    find $backup_path -type f -mtime +$day -exec rm -rf {} ; > /dev/null 2>&1
    
    echo "开始备份数据库:${db[*]}"
    
    #备份并压缩
    backup_sql(){
        dbname=$1
        backup_name="${dbname}_${date}.sql"
        #-R备份存储过程,函数,触发器
        mysqldump -h $host -P $port -u $user -p$password $lock --default-character-set=utf8 --flush-logs -R $dbname > $backup_path/$backup_name    
        if [[ $? == 0 ]];then
            cd $backup_path
            tar zcpvf $backup_name.tar.gz $backup_name
            size=$(du $backup_name.tar.gz -sh | awk '{print $1}')
            rm -rf $backup_name
            echo "$date 备份 $dbname($size) 成功 "
        else
            cd $backup_path
            rm -rf $backup_name
            echo "$date 备份 $dbname 失败 "
        fi
    }
    
    #循环备份
    length=${#db[@]}
    for (( i = 0; i < $length; i++ )); do
            backup_sql ${db[$i]} >> $backup_log 2>&1
    done
    
    echo "备份结束,结果查看 $backup_log"
    du $backup_path/*$date* -sh | awk '{print "文件:" $2 ",大小:" $1}'
     
     
     
     
  • 相关阅读:
    Android 开源框架 ( 十二 ) 图片加载框架---Fresco
    Android 开源框架 ( 十一 ) 图片加载框架---Glide
    Android 开源框架 ( 十 ) 图片加载框架---Picasso
    Android 开源框架 ( 九 ) 图片加载框架---ImageLoader
    Android 开源框架 ( 八 ) 注解框架---ButterKnife
    Android 开源框架 ( 七 ) 事件总线---EventBus
    Android 开源框架 ( 六 ) Volley --- Google的轻量级网络通信框架
    Android 开源框架 ( 五 ) xUtils --- Android 里的 ORM IOC聚合型框架
    Android 开源框架 ( 四 ) Afinal --- Android 里的 ORM IOC聚合型框架
    Android 开源框架 ( 三 ) 基于OkHttp进一步封装的OkHttpUtils介绍
  • 原文地址:https://www.cnblogs.com/vzhangxk/p/15267618.html
Copyright © 2020-2023  润新知