今天接到有关部门反映,存储的空间不够了。需要删除一些视频文件来腾出空间。由于刚接手工作没多久,上任写的是python脚本。无奈,个人python水平还是入门。所以只能另写个shell脚本来完成工作了。
声明:以下操作均为在虚拟机上进行的,毕竟生产环境是不能够乱来的,所以测试OK之后呢,再到线上执行脚本方可。
要求:删除/data/video/sports/shi/下面的视频
思路:
1.首先有关部门已经将需要删除的目录,字段 statusCode改为0,默认为1
2.根据statusCode的状态查询出要删除的路径
3.删除视频文件完毕后,需要将statusCode的状态改为1
好了,开整:
现状:
[[email protected] 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
[[email protected] 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了
[[email protected] script]# mysql -uroot -proot -e "use sne;select statusCode from del_video_file;" +------------+ | statusCode | +------------+ | 1 | | 1 | | 1 | | 1 | +------------+
好了,脚本大概就是这样子了。一些细节问题,今后继续完善吧。
时间: 2024-11-06 14:00:27