Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- ##1.3 -- added mysql insert and modified some variables to this end
- ## This script takes the file name from a directory, (filename ex: OUT1269-20100924-133348-1285353228.2274385.wav OR 20100924-124504-1285350287.2270497.wav) checks
- ## 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.
- ## logging is done to /var/log/asterisk/movewav/MONTH/logfile-DAY
- ##
- ## USAGE: (in cron) 59 23 * * * root cd /var/spool/asterisk/monitor && /usr/local/bin/movewav.sh
- ## some local variables
- set +x
- PROGNAME="`basename $0`"
- VERSION="1.4"
- BASE="/var/spool/asterisk/monitor/webdir"
- logpath="/var/log/asterisk/movewav/$(date +%m)"
- logfile="$logpath/logfile-$(date +%d)"
- ## create log directory
- [[ ! -d "$logpath" ]] && mkdir -p "$logpath"
- ## create logfile entry
- exec >> "$logfile" 2>&1
- uniqueid=$2
- ## do it
- file=$1
- shift
- ## check for gravity
- [[ ! -f $file ]] && { echo "darn, something went wrong! File does not exist!" >&2; exit 1; }
- ## capture and preserve original filename
- origfile=$file
- ## does the file match our known patterns?
- [[ ! "$file" =~ [0-9]{10}\.[0-9]+\.wav ]] && { echo "Sorry, $file does not match known patterns" >&2; exit 1; }
- #set -- ${file//-/ }
- ## remove epoch from the file name
- epoch=$(sed -r 's@.*-([[:digit:]]{10})\.[[:digit:]]+\.wav@\1@' <<< "$file")
- epoch2date() {
- local ep form
- ep=$1
- shift
- form=$1
- shift
- case $form in
- year) date --date "$[$(date '+%s')-${ep}] seconds ago" '+%Y' ;;
- month) date --date "$[$(date '+%s')-${ep}] seconds ago" '+%m' ;;
- day) date --date "$[$(date '+%s')-${ep}] seconds ago" '+%d' ;;
- hour) date --date "$[$(date '+%s')-${ep}] seconds ago" '+%H' ;;
- minute) date --date "$[$(date '+%s')-${ep}] seconds ago" '+%M' ;;
- second) date --date "$[$(date '+%s')-${ep}] seconds ago" '+%S' ;;
- esac
- # date --date "$[$(date '+%s')-${ep}] seconds ago" '+%Y-%m-%d %H:%M:%S'
- }
- # epoch2date $epoch >>> $date
- ## set variables for directory structure and mysql import
- year="$(epoch2date $epoch year)"
- month="$(epoch2date $epoch month)"
- day="$(epoch2date $epoch day)"
- hour="$(epoch2date $epoch hour)"
- minute="$(epoch2date $epoch minute)"
- second="$(epoch2date $epoch second)"
- timestamp="$year-$month-$day $hour:$minute:$second"
- ## create path variables; copy the file and delete the old one
- datepath="$year/$month/$day/$hour/"
- umask 0002
- newpath="$BASE/$datepath"
- mkdir -p -v "$newpath"
- chmod g=rw "$file"
- cp -v --preserve=timestamps "$file" "$newpath"
- rm -f "$file" && echo "Removed $file from $PWD"
- ## Connect to and import data to MySQL DB
- 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