美文网首页
shell 利用二位数组备份多个实例多个库表

shell 利用二位数组备份多个实例多个库表

作者: W朱珠W | 来源:发表于2021-04-22 11:22 被阅读0次

    shell 脚本如下


    #!/bin/bash

    #需要备份的数据库及表

    conn1=(ISUser IS@saic0! 10.133.103.179 4230 crs sht01)

    conn1=(xyx_super_brain Pass1234$ 10.129.24.163 4088 sh_smcv_maxus_test_drive t_test_drive)

    conn2=(maxusclub ClubSaicMaxus_1 10.129.24.218 4016 maxusclub mcb_car_owners)

    conn3=(carMall MaxusProd2019_ 10.129.41.51 4741 maxus maxus_order)

    conn4=(carMall MaxusProd2019_ 10.129.41.51 4741 maxus maxus_user)

    conn5=(maxus_member MaxusProd2019_ 10.129.24.155 4596 sh_smcv_maxus_owner_remind tt_maintenance_info)

    conn6=(c2b_sso SSOSaicPub2018_ 10.129.24.145 4930 maxus_c2b_mall sysuser_account)

    conn7=(scoreuser Score2017user 10.129.24.222 4109 score_db u_user)

    conn8=(wxuser Wx_user_2019 10.129.24.228 4709 maxus_c2b_auth t_cust)

    conn9=(wxuser Wx_user_2019 10.129.24.228 4709 maxus_c2b_auth u_user)

    conn10=(maxus_c2b_pre PreC2B521 10.133.103.166 4777 c2b_communal t_sms_ip)

    conn11=(ISUser IS@saic0! 10.133.103.179 4230 crs met01)

    conn12=(ISUser IS@saic0! 10.133.103.179 4230 crs cat86)

    conn13=(ISUser IS@saic0! 10.133.103.179 4230 crs sht01)

    conn14=(is-prod-db816 IS@saic0! 10.133.103.180 4828 datawarehouse-db dw_crm_customers)

    #每次的备份以日期创建文件夹存放,同时删除过期备份

    #此处已保留1周的备份数据

    TIME=`date -d "now" +'%Y-%m-%d %H:%M:%S'`

    DATE=`date -d "now" +%Y%m%d`

    ODATE=`date -d "-1 day" +%Y%m%d`

    array=(conn1 conn2 conn3 conn4 conn5 conn6 conn7 conn8 conn9 conn10 conn11 conn12 conn13 conn14)

    for i in ${array[@]};do

      eval user=\${${i}[0]}

      eval pwd=\${${i}[1]}

      eval ip=\${${i}[2]}

      eval port=\${${i}[3]}

      eval db=\${${i}[4]}

      eval table=\${${i}[5]}

        for element in ${db};do

            newdir=/data/mysql/dbbackup/$db/$DATE

            olddir=/data/mysql/dbbackup/$db/$ODATE

            #删除过期备份数据

            if [ -d $olddir ];then

              rm -rf $olddir

            fi

            #创建新备份目录

            if [ ! -d $newdir ];then

              mkdir -p $newdir

            else

              echo $newdir exist >> ${newdir}/${table}.log

            fi

            #备份表

            echo "--------------$TIME start backup -------------------" >> $newdir/${table}.log

            /data/mysql/base/5.7.19/bin/mysqldump  --single-transaction --set-gtid-purged=OFF -u${user} -p${pwd} -h${ip} -P ${port}  ${db}  ${table} | gzip > $newdir/${table}.sql.gz

            if [ $? -eq 0 ];then

              echo " $TIME table ${table} backup complete ok!" >> $newdir/${table}.log

            else

              echo " $TIME table ${table} backup failed " >> $newdir/${table}.log

            fi

            echo "-------------$TIME backup is end ------------------" >> $newdir/${table}.log

        done

      sleep 60   

    done

    exit


    相关文章

      网友评论

          本文标题:shell 利用二位数组备份多个实例多个库表

          本文链接:https://www.haomeiwen.com/subject/qwpxrltx.html