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.2"
- 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
- ## do it
- file=$1
- shift
- ## capture and preserve original filename
- origfile=$file
- ## does the file match our known patterns?
- [[ ! "$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; }
- set -- ${file//-/ }
- [[ ! -f $file ]] && { echo "darn, something went wrong!" >&2; exit 1; }
- a="20110223-182724-1298507244.35252.wav"; sed -r 's@.*-([[:digit:]]+\.[[:digit:]]+)\.wav@\1@' <<< $a
- epoch2date() { EPOCH="$1"; date --date "$[$(date '+%s')-${EPOCH}] seconds ago" '+%Y-%m-%d %H:%M:%S'; }; epoch2date 1298507244
- ## set variables for directory structure and mysql import
- year=${date:0:4}
- month=${date:4:2}
- day=${date:6:2}
- hour=${time:0:2}
- minute=${time:2:2}
- second=${time:4:2}
- 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