bash shell寫的圖片下載備份定時任務

1 環境

linux centos 7.x

2 背景說明

圖片,文件的存儲使用的是私有云,但是運維弄了一個多月後,告訴我說由於某些原因他們無法對數據進行備份,讓自己想辦法處理。
想到的辦法是 從mysql 讀取使用的圖片文件數據,然後下載到服務器進行存儲。
mysql 中存儲的是圖片的地址或者文件名,兩種情況都有。

所以最好的辦法是 將mysql 存儲的圖片字段進行處理,獲取文件名, 將文件名於路徑拼接,獲取圖片下載地址。

備份策略: 每4小時增量更新一次。

代碼說明

獲取文件所在的路徑,並進入該路徑。(個人在寫shell, python 時很喜歡這麼寫,程序執行不容易出現因路徑不對問題)

DIR=$(readlink -f "$(dirname "${BASH_SOURCE[0]}")")
cd $DIR

執行mysql 語句,獲取一個表中的兩個字段的值,並將返回結果賦值給contract_result (含表頭)

contract_result=`/work/install/mysql-5.7.28/bin/mysql -h$host -P$port -u$mysql_u -p$mysql_p car_subsidy -e "${contract_sql}"`

判斷以上語句是否成功執行

if [ $? = 0 ] 

去掉數據庫返回結果中的表頭,並將數據存儲到一個臨時文件中。

echo "$contract_result"|tail -n +2 > ${f_temp}

循環讀取臨時文件的數據,

while read line;do
        ((all_count++))
        echo "$all_count"
        arr=(${line})
        #user_id=${arr[0]}
        pic=${arr[1]}
        pic=`echo ${pic}`
        pic=${pic##http*\/}
        if [[ $imgprefix* == $pic ]]
        then
            img_url=${pic}
        else 
            img_url=${imgprefix}${pic}
        fi
        #echo $pic
        #flag=$(`cat $file_contract | grep "${pic}" | wc -l`)
        flag=` grep "${pic}" ${file_out} | wc -l`
        if [ $flag -lt 1 ]
        then
           wget -P ${imgpath} ${img_url}
           ((new_count++))
           echo "${line}" >>  ${file_out}
        fi
    done < ${f_temp}

1 獲取數據數組

arr=(${line})

2 去掉 http到 / 之前的字符串

pic=${pic##http*\/}

例如: http:/123455/abcd/xyz.jpg
處理後: xyz.jpg

3 判斷字符串包含關係

if [[ $imgprefix* == $pic ]]

pic 是否包含 imgprefix, 即pic 是否以 imgprefix 開頭。

4 下載圖片到 目錄 imgpath。

 wget -P ${imgpath} ${img_url}

5 判斷 file_out 文件中是否有 pic 字段。

` grep "${pic}" ${file_out} | wc -l`

定時任務

crontab -e
0 */4 * * *  cd /data/images && sh start.sh

3 完整代碼

start.sh

DIR=$(readlink -f "$(dirname "${BASH_SOURCE[0]}")")
cd $DIR

host=localhost            ####### change  ######
imgpath=img/contract/     ######## change ########
imgprefix=http://obs.example.com.cn/
mysql_u=user
mysql_p=password
port=3306
#d=$(date +%Y-%m-%d %H:%M:%S)
d=$(date +%F#%T)
########### t_purchase_contract  ######################
file_out=recorder ############ change #############
f_temp=${file_out}"_temp" ############ change #############
mysql_table=table            ############ change #############
mysql_field=contract             ############ change #############
contract_sql="select user_id,${mysql_field} from  ${mysql_table}"
contract_result=`/work/install/mysql-5.7.28/bin/mysql -h$host -P$port -u$mysql_u -p$mysql_p car_subsidy -e "${contract_sql}"`
# 判斷是否連接成功
new_count=0
all_count=0
img_url=
if [ $? = 0 ] 
then 
    echo "$contract_result"|tail -n +2 > ${f_temp}
    while read line;do
        ((all_count++))
        echo "$all_count"
        arr=(${line})
        #user_id=${arr[0]}
        pic=${arr[1]}
        pic=`echo ${pic}`
        pic=${pic##http*\/}
        if [[ $imgprefix* == $pic ]]
        then
            img_url=${pic}
        else 
            img_url=${imgprefix}${pic}
        fi
        #echo $pic
        #flag=$(`cat $file_contract | grep "${pic}" | wc -l`)
        flag=` grep "${pic}" ${file_out} | wc -l`
        if [ $flag -lt 1 ]
        then
           wget -P ${imgpath} ${img_url}
           ((new_count++))
           echo "${line}" >>  ${file_out}
        fi
    done < ${f_temp}
    echo "${mysql_table} ${mysql_field} finished at ${d},new pics: ${new_count}, all pics: ${all_count}">>log
else
    echo "${mysql_table} ${mysql_field} finished at ${d}, with errors" >> log
fi
rm -rf ${f_temp}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章