shell 腳本如下
#!/bin/bash
#需要備份的數(shù)據(jù)庫及表
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)
#每次的備份以日期創(chuàng)建文件夾存放,同時刪除過期備份
#此處已保留1周的備份數(shù)據(jù)
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
? ? ? ? #刪除過期備份數(shù)據(jù)
? ? ? ? if [ -d $olddir ];then
? ? ? ? ? rm -rf $olddir
? ? ? ? fi
? ? ? ? #創(chuàng)建新備份目錄
? ? ? ? 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