Move Call Recording to NFS

Status
Not open for further replies.

gunemalli

New Member
Dec 18, 2018
24
3
3
This script is based off of "Convert yesterdays wavs to mp3" thread in this same section.

The reason behind doing this way rather than changing the recording DIR to NFS share is that there's a probability the NFS share might go down. And when you have Lawyers and Financial people as your customers, you and even them can be in deep trouble if there are missing recordings.

This gives you a buffer to restore the NFS, move the recordings without much of hassle.

What this script does:
* Check if there are call recordings
* If there are, use rsync to copy the files to NFS share folder.
* Checks if the NFS folder has the correct DIR structure and if not creates them.
* Sets correct folder/file permissions.
* Updates the recording DB with the new path.

There maybe more efficient and elegant ways to do this and if someone can improve on this, that would be awesome.

Step 1:
Mount the NFS DIR to /var/lib/freeswitch/storage/NFS/

Step 2:
Add the following code to file, make it executable and run it via crontab

Code:
0 2 * * * /usr/share/freeswitch/custom/nfstransfer.sh > /dev/null

Bash:
#!/bin/sh
YESTDAY=`date -d "yesterday" +%d`
for DIRECTORY in /var/lib/freeswitch/recordings/*/; do
    DIRYESTERDAY=/var/lib/freeswitch/recordings/`basename "$DIRECTORY"`/archive/`date -d "yesterday" +%Y/%b/%d`
    DIRNFS=/var/lib/freeswitch/storage/NFS/`basename "$DIRECTORY"`/archive/`date -d "yesterday" +%Y/%b/%d`
    if [ -d "$DIRYESTERDAY" ]; then
        if [ -d "$DIRNFS" ]; then
            rsync -ar --no-owner --no-group $DIRYESTERDAY $DIRNFS
            psql --host=127.0.0.2 --username=fusionpbx -t -c "UPDATE v_call_recordings SET call_recording_path = REPLACE(call_recording_path,'recordings','storage/NFS') WHERE call_recording_path = '$DIRYESTERDAY'"
            if [ $YESTDAY = "01" ]; then
                chown -R www-data:www-data /var/lib/freeswitch/storage/NFS/`basename "$DIRECTORY"`/archive/`date -d "yesterday" +%Y/%b`
            else
                chown -R www-data:www-data $DIRNFS
            fi
        else
            mkdir -p $DIRNFS
            rsync -ar --no-owner --no-group $DIRYESTERDAY $DIRNFS
            psql --host=127.0.0.2 --username=fusionpbx -t -c "UPDATE v_call_recordings SET call_recording_path = REPLACE(call_recording_path,'recordings','storage/NFS') WHERE call_recording_path = '$DIRYESTERDAY'"
            if [ $YESTDAY = "01" ]; then
                chown -R www-data:www-data /var/lib/freeswitch/storage/NFS/`basename "$DIRECTORY"`/archive/`date -d "yesterday" +%Y/%b`
            else
                chown -R www-data:www-data $DIRNFS
            fi
        fi
    fi
done

Step 3:
Modify the /etc/cron.daily/fusionpbx-housekeeping.sh file so that call recordings are deleted every 2 or 3 days time from the main disk.

Step 4:
Either from the NFS Server or from the FusionPBX server setup a cronjob delete call recordings.
Ex:
/etc/cron.daily/recording_del

Bash:
#!/bin/bash
#delete mp3 and wav recordings older then 90 days

find /path/to/NFS/*/archive/ -type f \( -name \*.mp3 -o -name \*.wav \) -mtime +90 -exec rm {} \;
 
  • Like
Reactions: KonradSC

gunemalli

New Member
Dec 18, 2018
24
3
3
As the above script only moves yesterday's recordings, you can use the following commands to copy all the recordings to NFS.

Code:
rsync -arvh --no-owner --no-group /var/lib/freeswitch/recordings/ /var/lib/freeswitch/storage/NFS/
psql --host=127.0.0.2 --username=fusionpbx -t -c "UPDATE v_call_recordings SET call_recording_path = REPLACE(call_recording_path,'recordings','storage/NFS')"
 

agree

Member
Aug 26, 2018
135
24
18
both of the above options are missing moving the recordings to the NFS location right after the call has finished. I wrote a bash script that gets executed right after a call hangs up through setting an api_hangup_hook in the dialplan

add this to the user_record dialplan right before the uuid_record
XML:
<action application="set" data="api_hangup_hook=sched_api +4 none bg_system bash /home/move_recordings.sh ${record_path}/${record_name}" inline="true"/>

it schedules the API to execute the bash script 4 seconds after hangup

Bash:
#!/bin/sh
if [ $# -eq 0 ]; then
    exit 1
fi
export PGPASSWORD="xxxxxxxxxxxxxxxxxxxxxxxxx"
db_host=127.0.0.1
db_port=5432
recording=$1
recording_path=`dirname $1`
recording_filename=`basename $1`
nfs_dir="/path/to/nfs"


if [ -f $recording ]; then
    if [ -d $nfs_dir ]; then
        destination_dir=$nfs_dir/$recording_path/
        mkdir -p $destination_dir
        mv $recording $destination_dir
        chown -R www-data:www-data $nfs_dir/
        sql="update v_call_recordings set call_recording_path = '$destination_dir' where call_recording_name = '$recording_filename';"
        sql="$sql update v_xml_cdr set record_path = '$destination_dir' where record_name = '$recording_filename';"
        psql --host=127.0.0.1 --username=fusionpbx -c "$sql"
    fi
fi

The Freeswitch API passes the recordings name and path to the script as an argument the script checks if the NFS directory exists and moves the recording to it and updates the database for the recording's new path.
 
Last edited:
Status
Not open for further replies.