Chinaunix首页 | 论坛 | 博客
  • 博客访问: 499061
  • 博文数量: 401
  • 博客积分: 244
  • 博客等级: 入伍新兵
  • 技术积分: 2215
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-04 10:02
文章分类

全部博文(401)

文章存档

2013年(37)

2012年(364)

分类: LINUX

2013-08-12 12:55:49

清除录音文件的脚本,在此博文中有介绍相关的数据结构http://blog.chinaunix.net/uid-27050514-id-3838897.html    考虑到有其它人员要操作,这个脚本增加了配置文件机制,以便让其它人员能更好的管理。脚本很简单,主是要增加了配置读取,每次取出符合条件的号码,再对每个号码对应的录音标识(seq)进行数据库操作及文件操作。主要提供一种思路,在运维过程中有很多类似的处理。

配置文件:

点击(此处)折叠或打开

  1. log=./log
  2. #源目录
  3. data=/home/wanggy/test_dir
  4. #备份目录
  5. topath=/home/wanggy/test_dir1
  6. #mv 0,del 1
  7. flag=0
  8. #间隔时间(s)
  9. interval=100
  10. #处理多少天前的数据
  11. days=10
  12. #db
  13. dbhost=192.168.1.14
  14. dbuser=wanggy
  15. dbpass=12345678
  16. dbbase=gzy_call

代码:

点击(此处)折叠或打开

  1. #!/bin/bash
  2. # File Name: mybk_record.sh
  3. # Author: wanggy
  4. # mail: wgy-email@163.com
  5. # Created Time: 2013年08月07日 星期三 12时27分30秒
  6. #########################################################################
  7. run_config()
  8. {
  9.     awk -F= '{if ($1 == "'$1'") print$2}' record.cfg
  10. }
  11. env()
  12. {
  13.     dbhost=`run_config dbhost`
  14.     dbbase=`run_config dbbase`
  15.     dbuser=`run_config dbuser`
  16.     dbpass=`run_config dbpass`
  17.     flag=`run_config flag`
  18.     data=`run_config data`
  19.     topath=`run_config topath`
  20.     log=`run_config log`
  21.     interval=`run_config interval`
  22.     days=`run_config days`
  23.     echo "获取到的环境变量..."
  24.     echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
  25.     echo "dbhost-->$dbhost"
  26.     echo "dbbase-->$dbbase"
  27.     echo "dbusre-->$dbuser"
  28.     echo "dbpass-->$dbpass"
  29.     echo "flag-->$flag"
  30.     echo "data-->$data"
  31.     echo "topath-->$topath"
  32.     echo "interval-->$interval"
  33.     echo "days-->$days"
  34.     echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
  35.     sleep 5
  36. }
  37. run_main()
  38. {
  39.     while :
  40.     do
  41.         run_record_main
  42.         sleep $interval
  43.     done
  44. }
  45. run_record_main()
  46. {
  47. echo "begin......"
  48. env
  49. #每次程序运行只执行一次(取大于二条记录录音的主叫号码)
  50. echo "#----------------------------------------------------------------------------------------------"
  51. echo "[select count(*) from(select SUBSTR(callno,1,INSTR(callno,'#')-1) as callno, count(*)"
  52. echo "from record where delflag =0 and begin_time < (now() - INTERVAL $days DAY) and imported = 1"
  53. echo "group by SUBSTR(callno ,1,INSTR(callno,'#')-1) having count(*) > 1)]"
  54. echo "#----------------------------------------------------------------------------------------------"
  55. mysql -u$dbuser -p$dbpass -D$dbbase <<EOF >record
  56. select SUBSTR(callno,1,INSTR(callno,'#')-1) as callno, count(*)
  57. from record where delflag =0 and begin_time < (now() - INTERVAL $days DAY) and imported = 1
  58. group by SUBSTR(callno ,1,INSTR(callno,'#')-1) having count(*) > 1;
  59. EOF
  60. sed -i '/callno/d' record
  61. line=`wc -l record |awk '{print$1}'`
  62. echo "此次共计$line号码需要处理......"
  63. sleep 5
  64. #对每个号码对应的录音文件进行操作,保留最新一条
  65. for ((i=1;i<=line;i++));do
  66.     telno=`awk -v c=$i 'NR==c {print$1}' record`
  67.     mysql -u$dbuser -p$dbpass -D$dbbase <<EOF >telno
  68.         select seq,path_relative from record where callno like '$telno%' order by seq desc;
  69. EOF
  70.     sed -i '/seq/d' telno
  71.     telno_record_line=`wc -l telno |awk '{print$1}'`
  72.     echo "`date "+%Y-%m-%d %H:%M:%S"` 正在处理$telno....."
  73.     echo "`date "+%Y-%m-%d %H:%M:%S"` 正在处理$telno....." >>$log/record.log
  74.     for ((j=2;j<=telno_record_line;j++));do
  75.         seq=`awk -v c=$j 'NR==c {print$1}' telno`
  76.         path=`awk -v c=$j 'NR==c {print$2}' telno`
  77.         mysql -u$dbuser -p$dbpass -D$dbbase <<EOF
  78.             update record set delflag=1 where seq='$seq';
  79. EOF
  80.     #判断是否成功
  81.     res=`echo $?`
  82.     if [ $res = 0 ];then
  83.         case $flag in
  84.             1)
  85.                 echo "`date "+%Y-%m-%d %H:%M:%S"` del $seq $data/$path"
  86.                 echo "`date "+%Y-%m-%d %H:%M:%S"` update record set delflag=1 where seq=$seq"
  87.                 echo "`date "+%Y-%m-%d %H:%M:%S"` del $seq $data/$path" >>$log/record.log
  88.                 echo "`date "+%Y-%m-%d %H:%M:%S"` update record set delflag=1 where seq=$seq" >>$log/record.log
  89.                 if [ -f $data/$path ];then
  90.                     rm -rf $data/$path
  91.                 else
  92.                     echo "$data/$path not exist,ignore!!!"
  93.                 fi
  94.                 ;;
  95.             0)
  96.                 echo "`date "+%Y-%m-%d %H:%M:%S"` mv $seq $data/$path->$topath/$path"
  97.                 echo "`date "+%Y-%m-%d %H:%M:%S"` update record set delflag=1 where seq=$seq"
  98.                 echo "`date "+%Y-%m-%d %H:%M:%S"` mv $seq $data/$path->$topath/$path" >>$log/record.log
  99.                 echo "`date "+%Y-%m-%d %H:%M:%S"` update record set delflag=1 where seq=$seq" >>$log/record.log
  100.                 m_path=`echo $path |awk '{print$1"/"$2"/"$3"/"$4}'`
  101.                 m_file=`echo $path |awk '{print$5}'`
  102.                 if [ -f $data/$path ];then
  103.                     mkdir -p $topath/$m_path
  104.                     mv $data/$path $topath/$m_path/$m_file
  105.                 else
  106.                     echo "$data/$path not exist,ignore!!!"
  107.                 fi
  108.                 ;;
  109.         esac
  110.     else
  111.         echo "$telno->$seq update err [$res]"
  112.         echo "$telno->$seq update err [$res]" >$log/record.log
  113.     fi
  114.     done
  115. done
  116. echo "end......"
  117. }
  118. main()
  119. {
  120.     #env
  121.     run_main
  122. }
  123. main
  124. exit 0


阅读(1131) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~