聲明:如下操做均爲在虛擬機上進行的,畢竟生產環境是不可以亂來的,因此測試OK以後呢,再到線上執行腳本方可。mysql
要求:刪除/data/video/sports/shi/下面的視頻sql
思路:bash
1.首先有關部門已經將須要刪除的目錄,字段 statusCode改成0,默認爲1ide
2.根據statusCode的狀態查詢出要刪除的路徑測試
3.刪除視頻文件完畢後,須要將statusCode的狀態改成1 spa
好了,開整:視頻
現狀:ip
[root@localhost shi]# ll total 0 -rw-r--r-- 1 root root 0 May 4 09:38 hc2012051620.mp4 -rw-r--r-- 1 root root 0 May 4 09:38 hc2012051621.mp4 -rw-r--r-- 1 root root 0 May 4 09:38 hc2012051622.mp4 -rw-r--r-- 1 root root 0 May 4 09:38 hc2012051623.mp4
[root@localhost shi]# mysql -uroot -proot -e "use sne;select ContentID,PlayAddress,statusCode from del_video_file;" +-----------+---------------------------------------------------------------------+------------+ | ContentID | PlayAddress | statusCode | +-----------+---------------------------------------------------------------------+------------ | 128704 | http://xxx:17533/gdtv/sports/shi/hc2012051620.mp4 | 0 | | 128705 | http://xxx:17533/gdtv/sports/shi/hc2012051621.mp4 | 0 | | 128706 | http://xxx:17553/gdtv/sports/shi/hc2012051622.mp4 | 0 | | 128707 | http://xxx:17553/gdtv/sports/shi/hc2012051623.mp4 | 0 | +-----------+---------------------------------------------------------------------+------------+
腳本思路:rem
1.登陸mysql查詢statusCode爲0的記錄,而且用awk取到所須要的路徑虛擬機
#!/bin/bash #author:ley #聲明路徑的變量,由於sql中的路徑並非絕對路徑 datadir='/data/video'
result=`mysql -uroot -proot -e "use sne; select ContentID,PlayAddress from del_video_file where statusCode=0;" |awk 'BEGIN{FS="17553/"} {print $2}'`
2.用for循環依次刪除視頻文件
for i in $result do rm -rf $datadir/$i echo "remove the file is ok" done
3.根據statusCode=0查詢出對應的ContentID
ContentID= `mysql -uroot -proot -e "use sne; select ContentID,PlayAddress from del_video_file where statusCode=0;"|awk '{print $1}'`
4.再用for循環依次更新statusCode=1
for id in $ContentID do update=`mysql -uroot -proot -e "use sne; update del_video_file set statusCode=1 where ContentID=$id;"` if [ $? == 0 ] then echo "update is ok" else echo "update is error" fi done
5.查看statusCode的狀態已經爲1了
[root@localhost script]# mysql -uroot -proot -e "use sne;select statusCode from del_video_file;" +------------+ | statusCode | +------------+ | 1 | | 1 | | 1 | | 1 | +------------+
好了,腳本大概就是這樣子了。一些細節問題,從此繼續完善吧。