根據字段狀態刪除指定目錄文件的shell腳本

           聲明:以下操作均爲在虛擬機上進行的,畢竟生產環境是不能夠亂來的,所以測試OK之後呢,再到線上執行腳本方可。

      要求:刪除/data/video/sports/shi/下面的視頻

      思路:

     1.首先有關部門已經將需要刪除的目錄,字段 statusCode改爲0,默認爲1

     2.根據statusCode的狀態查詢出要刪除的路徑

     3.刪除視頻文件完畢後,需要將statusCode的狀態改爲1 

好了,開整:

  現狀:

[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 |
+-----------+---------------------------------------------------------------------+------------+

      腳本思路:

      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 |
+------------+

     好了,腳本大概就是這樣子了。一些細節問題,今後繼續完善吧。

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章