Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.23 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. ##1.3 -- added mysql insert and modified some variables to this end
  3. ## This script takes the file name from a directory, (filename ex:  OUT1269-20100924-133348-1285353228.2274385.wav OR 20100924-124504-1285350287.2270497.wav) checks
  4. ## the first part and figures out if it's ex 1 or ex 2 and then creates directories based on date (year/month/day/hour). It then moves the file to it's specific dir.
  5. ## logging is done to /var/log/asterisk/movewav/MONTH/logfile-DAY
  6. ##
  7. ## USAGE: (in cron) 59 23 * * *  root cd /var/spool/asterisk/monitor && /usr/local/bin/movewav.sh
  8.  
  9.  
  10. ## some local variables
  11.  
  12. set +x
  13. PROGNAME="`basename $0`"
  14. VERSION="1.2"
  15. BASE="/var/spool/asterisk/monitor/webdir"
  16. logpath="/var/log/asterisk/movewav/`date +%m`"
  17. logfile="$logpath/logfile-`date +%d`"
  18.  
  19.  
  20. ## create log directory
  21. [[ ! -d "$logpath" ]] && mkdir -p "$logpath"
  22.  
  23. ## create logfile entry
  24. exec > "$logfile" 2>&1
  25.  
  26. ## do it
  27. file=$1
  28. shift
  29.  
  30. ## capture and preserve original filename
  31. origfile=$file
  32.  
  33. ## does the file match our known patterns?
  34. [[ ! "$file" =~ (^20[0-9]{6}-[0-9]{6}|^[[:alnum:]]+-20[0-9]{6}-[0-9]{6}) ]] && { echo "Sorry, $file does not match known patterns" >&2; exit 1; }
  35. set -- ${file//-/ }
  36.  
  37. [[ ! -f $file ]] && { echo "darn, something went wrong!" >&2; exit 1; }
  38.  
  39. a="20110223-182724-1298507244.35252.wav"; sed -r 's@.*-([[:digit:]]+\.[[:digit:]]+)\.wav@\1@' <<< $a
  40.  
  41. epoch2date() { EPOCH="$1"; date --date "$[$(date '+%s')-${EPOCH}] seconds ago" '+%Y-%m-%d %H:%M:%S'; }; epoch2date 1298507244
  42.  
  43.        
  44.  
  45.         ##  set variables for directory structure and mysql import
  46. year=${date:0:4}
  47. month=${date:4:2}
  48. day=${date:6:2}
  49. hour=${time:0:2}
  50. minute=${time:2:2}
  51. second=${time:4:2}
  52. timestamp="$year-$month-$day $hour:$minute:$second"
  53.  
  54.  
  55.   ## create path variables; copy the file and delete the old one
  56. datepath="$year/$month/$day/$hour/"
  57. umask 0002
  58. newpath="$BASE/$datepath"
  59. mkdir -p -v "$newpath"
  60. chmod g=rw "$file"
  61. cp -v --preserve=timestamps "$file" "$newpath"
  62. rm -f "$file" && echo "Removed $file from $PWD"
  63.  
  64. ## Connect to and import data to MySQL DB
  65. mysql -urecadmin -ppassword -Drecordings -e "insert into recdata(uniqueid,timestamp,datepath,filename) VALUES('$uniqueid','$timestamp','$datepath','$origfile');"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement