声明:以下操作均为在虚拟机上进行的,毕竟生产环境是不能够乱来的,所以测试OK之后呢,再到线上执行脚本方可。
要求:删除/data/video/sports/shi/下面的视频
思路:
1.首先有关部门已经将需要删除的目录,字段statusCode改为0,默认为1
2.根据statusCode的状态查询出要删除的路径
3.删除视频文件完毕后,需要将statusCode的状态改为1
好了,开整:
现状:
[root@localhostshi]#lltotal0-rw-r--r--1rootroot0May409:38hc2012051620.mp4-rw-r--r--1rootroot0May409:38hc2012051621.mp4-rw-r--r--1rootroot0May409:38hc2012051622.mp4-rw-r--r--1rootroot0May409:38hc2012051623.mp4
[root@localhostshi]#mysql-uroot-proot-e"usesne;selectContentID,PlayAddress,statusCodefromdel_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"usesne;selectContentID,PlayAddressfromdel_video_filewherestatusCode=0;"|awk'BEGIN{FS="17553/"}{print$2}'`
2.用for循环依次删除视频文件
foriin$resultrm-rf$datadir/$iecho"removethefileisok"done
3.根据statusCode=0查询出对应的ContentID
ContentID=`mysql-uroot-proot-e"usesne;selectContentID,PlayAddressfromdel_video_filewherestatusCode=0;"|awk'{print$1}'`
4.再用for循环依次更新statusCode=1
foridin$ContentIDdoupdate=`mysql-uroot-proot-e"usesne;updatedel_video_filesetstatusCode=1whereContentID=$id;"`if[$?==0]thenecho"updateisok"elseecho"updateiserror"done
5.查看statusCode的状态已经为1了
[root@localhostscript]#mysql-uroot-proot-e"usesne;selectstatusCodefromdel_video_file;"+------------+|statusCode|+------------+|1||1||1||1|+------------+
好了,脚本大概就是这样子了。一些细节问题,今后继续完善吧。
大多数人想要改造这个世界,但却罕有人想改造自己。