VM Cleanup

mrjoli021

Member
Jul 20, 2017
133
2
18
47
Hello,

I currently have a bash script that I use to clean up the VM's in my system. The script checks for VM's older than X days and then deletes them. This has worked pretty well until a client marked one of their VM's as a saved message and I deleted it. Is there a way to clean up the VM's while keeping any VM's the clients mark as "To be saved"?
 

Amit Iyer

Member
Feb 6, 2018
60
11
8
29
You can just move the VM's Audio to another directory as soon as the client ask's you to save that and then just create a simple webpage for them to access it via new directory.
 

mrjoli021

Member
Jul 20, 2017
133
2
18
47
The problem is that I dont have control of that. The client from their phone/softclient presses the option to save the VM. As far as linux is concerned it does not know the client did this. I really dont know how to get which VM's are tagged as saved. If that is a possibility then I can just exclude those VM in my script.
 

Amit Iyer

Member
Feb 6, 2018
60
11
8
29
Ok, just from top of my head,

in v_voicemail_messages table create a new column called save or whatever you want, keep the default value as 0 and change this to 1 when the client / softclient presses the option to save.
I believe you can achieve this by modifying the index.lua located in /usr/share/freeswitch/scripts/app/voicemail/ directory so send the update query on the DB when the message is saved.
Once this is done, then setup a cronjob to copy that file to other directory so that you don't lose it.
i hope this helps.
 

mrjoli021

Member
Jul 20, 2017
133
2
18
47
This what I got.
update to the index.lua script
````
-- Save the message to voicemail messages
if (message_length ~= nil and tonumber(message_length) > 2) then
caller_id_name = string.gsub(caller_id_name, "'", "''");
local sql = {}
table.insert(sql, "INSERT INTO v_voicemail_messages ");
table.insert(sql, "(");
table.insert(sql, "voicemail_message_uuid, ");
table.insert(sql, "domain_uuid, ");
table.insert(sql, "voicemail_uuid, ");
table.insert(sql, "created_epoch, ");
table.insert(sql, "caller_id_name, ");
table.insert(sql, "caller_id_number, ");
table.insert(sql, "message_status, "); -- Add this line to set message status
if (storage_type == "base64") then
table.insert(sql, "message_base64, ");
end
table.insert(sql, "message_length ");
table.insert(sql, ") VALUES ( ");
table.insert(sql, ":voicemail_message_uuid, ");
table.insert(sql, ":domain_uuid, ");
table.insert(sql, ":voicemail_uuid, ");
table.insert(sql, ":start_epoch, ");
table.insert(sql, ":caller_id_name, ");
table.insert(sql, ":caller_id_number, ");
table.insert(sql, "'saved', "); -- Set status to saved
if (storage_type == "base64") then
table.insert(sql, ":message_base64, ");
end
table.insert(sql, ":message_length ");
table.insert(sql, ")");
sql = table.concat(sql, "\n");

````

cleanup_vm.sh
````

#!/bin/bash

# Voicemail cleanup script for FusionPBX
# This script deletes voicemails older than a specified number of days unless tagged as saved in the database.

# Set default directory where voicemails are stored
VM_DIR="/var/lib/freeswitch/storage/voicemail/default"

# Set default retention period in days (modifiable)
DAYS_TO_KEEP=${1:-14}

# Database connection details
DB_USER="fusionpbx" # Database user
DB_NAME="fusionpbx" # Database name
DB_HOST="localhost" # Database host (adjust if remote)

# Function to get saved voicemail files from the database
get_saved_voicemails() {
psql -U "$DB_USER" -d "$DB_NAME" -h "$DB_HOST" -t -c "
SELECT CONCAT('/default/', domain_name, '/', voicemail_id, '/msg_', voicemail_message_uuid, '.wav')
FROM v_voicemail_messages
JOIN v_voicemails ON v_voicemail_messages.voicemail_uuid = v_voicemails.voicemail_uuid
JOIN v_domains ON v_voicemails.domain_uuid = v_domains.domain_uuid
WHERE message_status = 'saved';"
}

# Get list of saved voicemails to exclude from deletion
saved_voicemails=($(get_saved_voicemails))

# Find and delete voicemails older than specified days if they are not saved
find "$VM_DIR" -type f -name "msg_*.wav" -mtime +$DAYS_TO_KEEP | while read -r vm_file; do
# Check if the voicemail is in the saved list
if [[ ! " ${saved_voicemails[*]} " =~ " ${vm_file#"$VM_DIR"} " ]]; then
echo "Deleting old voicemail: $vm_file"
rm -f "$vm_file"
else
echo "Skipping saved voicemail: $vm_file"
fi
done
````
 

whut

Member
Dec 23, 2022
228
22
18
the included maintenance script would be an easier modification since you can improve the voicemail section to not delete v_voicemail_messages.message_status = 'saved'. The files and the database records need to be maintained.



Bash:
if [ .$purge_voicemail = .true ]; then
    echo "delete voicemail older than $days_keep_voicemail days"
    if [ .$switch_package = .true ]; then
        echo ".";
        find /var/lib/freeswitch/storage/voicemail/default/*  -name 'msg_*.wav' -mtime +$days_keep_voicemail -exec rm {} \;
        find /var/lib/freeswitch/storage/voicemail/default/*  -name 'msg_*.mp3' -mtime +$days_keep_voicemail -exec rm {} \;
    else
        echo ".";
        find /usr/local/freeswitch/storage/voicemail/*  -name 'msg_*.wav' -mtime +$days_keep_voicemail -exec rm {} \;
        find /usr/local/freeswitch/storage/voicemail/*  -name 'msg_*.mp3' -mtime +$days_keep_voicemail -exec rm {} \;
    fi
    psql $db_name --port $db_port --host=$db_host --username=$db_username -c "delete from v_voicemail_messages WHERE to_timestamp(created_epoch) < NOW() - INTERVAL '$days_keep_voicemail days'"
else
    echo "not purging voicemails."
fi