Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.83 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.4"
  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. uniqueid=$2
  26. ## do it
  27. file=$1
  28.  
  29. shift
  30.  
  31. ## check for gravity
  32. [[ ! -f $file ]] && { echo "darn, something went wrong! File does not exist!" >&2; exit 1; }
  33.  
  34. ## capture and preserve original filename
  35. origfile=$file
  36.  
  37. ## does the file match our known patterns?
  38. [[ ! "$file" =~  [0-9]{10}\.[0-9]+\.wav ]] && { echo "Sorry, $file does not match known patterns" >&2; exit 1; }
  39.  
  40. #set -- ${file//-/ }
  41. ## remove epoch from the file name
  42. epoch=$(sed -r 's@.*-([[:digit:]]{10})\.[[:digit:]]+\.wav@\1@' <<< "$file")
  43.  
  44. epoch2date() {
  45.     local ep form
  46.     ep=$1
  47.     shift
  48.     form=$1
  49.     shift
  50.     case $form in
  51.         year)   date --date "$[$(date '+%s')-${ep}] seconds ago" '+%Y' ;;
  52.         month)  date --date "$[$(date '+%s')-${ep}] seconds ago" '+%m' ;;
  53.         day)    date --date "$[$(date '+%s')-${ep}] seconds ago" '+%d' ;;
  54.         hour)   date --date "$[$(date '+%s')-${ep}] seconds ago" '+%H' ;;
  55.         minute) date --date "$[$(date '+%s')-${ep}] seconds ago" '+%M' ;;
  56.         second) date --date "$[$(date '+%s')-${ep}] seconds ago" '+%S' ;;
  57.     esac
  58. #   date --date "$[$(date '+%s')-${ep}] seconds ago" '+%Y-%m-%d %H:%M:%S'
  59. }
  60. # epoch2date $epoch >>> $date
  61.  
  62.        
  63.  
  64.         ##  set variables for directory structure and mysql import
  65. year="$(epoch2date $epoch year)"
  66. month="$(epoch2date $epoch month)"
  67.  
  68. day="$(epoch2date $epoch day)"
  69. hour="$(epoch2date $epoch hour)"
  70.  
  71. minute="$(epoch2date $epoch minute)"
  72. second="$(epoch2date $epoch second)"
  73.  
  74. timestamp="$year-$month-$day $hour:$minute:$second"
  75.  
  76.  
  77.   ## create path variables; copy the file and delete the old one
  78. datepath="$year/$month/$day/$hour/"
  79. umask 0002
  80. newpath="$BASE/$datepath"
  81. mkdir -p -v "$newpath"
  82. chmod g=rw "$file"
  83. cp -v --preserve=timestamps "$file" "$newpath"
  84. rm -f "$file" && echo "Removed $file from $PWD"
  85.  
  86. ## Connect to and import data to MySQL DB
  87. 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