2017-03-29 23:03:24 +02:00
#!/bin/sh
2017-03-29 23:08:53 +02:00
#settings
2018-12-11 09:20:09 +01:00
export PGPASSWORD="zzz"
2017-03-29 23:03:24 +02:00
db_host=127.0.0.1
db_port=5432
2024-02-08 19:15:42 +01:00
db_name=fusionpbx
2023-06-14 19:07:45 +02:00
db_username=fusionpbx
2017-03-29 23:08:53 +02:00
switch_package=true # true or false
2017-03-29 23:03:24 +02:00
2021-09-18 20:39:13 +02:00
purge_voicemail=false
purge_call_recordings=false
purge_cdrs=false
purge_fax=false
purge_switch_logs=true
2021-09-18 21:17:31 +02:00
purge_php_sessions=true
purge_database_transactions=true
2023-10-04 00:15:41 +02:00
purge_device_logs=false
2024-04-20 18:27:24 +02:00
purge_event_guard_logs=false
purge_user_logs=false
2022-04-16 06:19:02 +02:00
purge_email_queue=false
purge_fax_queue=true
2021-09-18 20:39:13 +02:00
days_keep_voicemail=90
days_keep_call_recordings=90
2023-01-27 18:35:04 +01:00
days_keep_cdrs=730
2021-09-18 20:39:13 +02:00
days_keep_fax=90
days_keep_switch_logs=7
2021-09-18 21:17:31 +02:00
days_keep_php_sessions=8
days_keep_database_transactions=30
2023-10-04 00:15:41 +02:00
days_keep_device_logs=180
2024-04-20 18:27:24 +02:00
days_keep_event_guard_logs=180
days_keep_user_logs=180
2022-04-16 06:19:02 +02:00
days_keep_email_queue=30
days_keep_fax_queue=30
2021-09-18 21:17:31 +02:00
2017-03-29 23:08:53 +02:00
#set the date
2017-03-29 23:03:24 +02:00
now=$(date +%Y-%m-%d)
2017-03-29 23:08:53 +02:00
#make sure the directory exists
2021-09-18 20:39:13 +02:00
if [ -e /var/backups/fusionpbx/postgresql ]; then
echo "postgres backup directory exists"
else
mkdir -p /var/backups/fusionpbx/postgresql
fi
2017-03-29 23:03:24 +02:00
#show message to the console
echo "Maintenance Started"
2021-09-18 20:39:13 +02:00
if [ .$purge_switch_logs = .true ]; then
2024-08-31 00:51:14 +02:00
echo "delete freeswitch logs older $days_keep_switch_logs days"
2021-09-18 20:39:13 +02:00
if [ .$switch_package = .true ]; then
find /var/log/freeswitch/freeswitch.log.* -mtime +$days_keep_switch_logs -exec rm {} \;
else
find /usr/local/freeswitch/log/freeswitch.log.* -mtime +$days_keep_switch_logs -exec rm {} \;
fi
2017-03-29 23:08:53 +02:00
else
2021-09-18 20:39:13 +02:00
echo "not purging Freeswitch logs"
2017-03-29 23:08:53 +02:00
fi
2017-03-29 23:03:24 +02:00
2021-09-18 21:29:01 +02:00
if [ .$purge_fax = .true ]; then
2024-08-31 00:51:14 +02:00
echo "delete fax file storage older than $days_keep_fax days"
2021-09-18 20:39:13 +02:00
if [ .$switch_package = .true ]; then
echo ".";
find /var/lib/freeswitch/storage/fax/* -name '*.tif' -mtime +$days_keep_fax -exec rm {} \;
find /var/lib/freeswitch/storage/fax/* -name '*.pdf' -mtime +$days_keep_fax -exec rm {} \;
else
echo ".";
find /usr/local/freeswitch/storage/fax/* -name '*.tif' -mtime +$days_keep_fax -exec rm {} \;
find /usr/local/freeswitch/storage/fax/* -name '*.pdf' -mtime +$days_keep_fax -exec rm {} \;
fi
#delete from the database
2023-06-14 19:23:43 +02:00
psql $db_name --port $db_port --host=$db_host --username=$db_username -c "delete from v_fax_files WHERE fax_date < NOW() - INTERVAL '$days_keep_fax days'"
psql $db_name --port $db_port --host=$db_host --username=$db_username -c "delete from v_fax_logs WHERE fax_date < NOW() - INTERVAL '$days_keep_fax days'"
2017-03-29 23:08:53 +02:00
else
2021-09-18 20:39:13 +02:00
echo "not purging Faxes"
2017-03-29 23:08:53 +02:00
fi
2017-03-29 23:03:24 +02:00
2021-09-18 20:39:13 +02:00
if [ .$purge_call_recordings = .true ]; then
2024-08-31 00:51:14 +02:00
echo "delete call recordings older than $days_keep_call_recordings days"
2021-09-18 20:39:13 +02:00
if [ .$switch_package = .true ]; then
find /var/lib/freeswitch/recordings/*/archive/* -name '*.wav' -mtime +$days_keep_call_recordings -exec rm {} \;
find /var/lib/freeswitch/recordings/*/archive/* -name '*.mp3' -mtime +$days_keep_call_recordings -exec rm {} \;
2024-05-03 20:45:28 +02:00
#remove empty folders
find /var/lib/freeswitch/recordings/*/archive/* -empty -type d -delete
2021-09-18 20:39:13 +02:00
else
find /usr/local/freeswitch/recordings/*/archive/* -name '*.wav' -mtime +$days_keep_call_recordings -exec rm {} \;
find /usr/local/freeswitch/recordings/*/archive/* -name '*.mp3' -mtime +$days_keep_call_recordings -exec rm {} \;
2024-05-03 20:45:28 +02:00
#remove empty folders
find /usr/local/freeswitch/recordings/*/archive/* -empty -type d -delete
2021-09-18 20:39:13 +02:00
fi
2023-04-18 23:48:47 +02:00
#Call recordings table uses a view. The data is from v_xml_cdr table. Changed in FusionPBX 5.0.7 and higher. The following line is useful to older versions.
2023-06-14 19:23:43 +02:00
#psql $db_name --port $db_port --host=$db_host --username=$db_username -c "delete from v_call_recordings WHERE call_recording_date < NOW() - INTERVAL '90 days'"
2017-08-18 19:21:06 +02:00
else
2021-09-18 20:39:13 +02:00
echo "not purging Recordings."
2017-08-18 19:21:06 +02:00
fi
2018-08-30 21:19:38 +02:00
2021-09-18 20:39:13 +02:00
if [ .$purge_voicemail = .true ]; then
2024-08-31 00:51:14 +02:00
echo "delete voicemail older than $days_keep_voicemail days"
2021-09-18 20:39:13 +02:00
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
2023-06-14 19:23:43 +02:00
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'"
2017-03-29 23:08:53 +02:00
else
2021-09-18 20:39:13 +02:00
echo "not purging voicemails."
2017-03-29 23:08:53 +02:00
fi
2021-04-26 19:57:23 +02:00
2021-09-18 20:39:13 +02:00
if [ .$purge_cdrs = .true ]; then
2024-08-31 00:51:14 +02:00
echo "delete call detail records older $days_keep_cdrs days"
2023-06-14 19:23:43 +02:00
psql $db_name --port $db_port --host=$db_host --username=$db_username -c "delete from v_xml_cdr WHERE start_stamp < NOW() - INTERVAL '$days_keep_cdrs days'"
2024-04-20 18:27:24 +02:00
#call detail record - call flow
2024-05-09 16:29:41 +02:00
psql $db_name --port $db_port --host=$db_host --username=$db_username -c "delete from v_xml_cdr_flow WHERE insert_date < NOW() - INTERVAL '$days_keep_cdrs days'"
2024-04-20 18:27:24 +02:00
#call detail record - json
2024-05-09 16:29:41 +02:00
psql $db_name --port $db_port --host=$db_host --username=$db_username -c "delete from v_xml_cdr_json WHERE start_stamp < NOW() - INTERVAL '$days_keep_cdrs days'"
2024-04-20 18:27:24 +02:00
#call detail record - call logs
2024-08-31 00:51:14 +02:00
psql $db_name --port $db_port --host=$db_host --username=$db_username -c "delete from v_xml_cdr_logs WHERE insert_date < NOW() - INTERVAL '$days_keep_cdrs days'"
2021-09-18 20:39:13 +02:00
else
echo "not purging CDRs."
fi
2021-04-26 19:57:23 +02:00
2024-08-31 00:51:14 +02:00
echo "delete php sessions older than $days_keep_php_sessions days"
2021-09-18 21:17:31 +02:00
if [ .$purge_php_sessions = .true ]; then
2021-09-18 21:27:05 +02:00
find /var/lib/php/sessions/* -name 'sess_*' -mtime +$days_keep_php_sessions -exec rm {} \;
2021-09-18 21:17:31 +02:00
else
echo "not purging PHP Sessions."
fi
2024-08-31 00:51:14 +02:00
echo "delete database_transactions older $days_keep_database_transactions days"
2021-09-18 21:17:31 +02:00
if [ .$purge_database_transactions = .true ]; then
2023-06-14 19:23:43 +02:00
psql $db_name --port $db_port --host=$db_host --username=$db_username -c "delete from v_database_transactions where transaction_date < NOW() - INTERVAL '$days_keep_database_transactions days'"
2021-09-18 21:17:31 +02:00
else
echo "not purging database_transactions."
fi
2024-08-31 00:51:14 +02:00
echo "delete device_logs older $days_keep_device_logs days"
2023-10-04 00:15:41 +02:00
if [ .$purge_device_logs = .true ]; then
2024-05-09 16:29:41 +02:00
psql $db_name --port $db_port --host=$db_host --username=$db_username -c "delete from v_device_logs where timestamp < NOW() - INTERVAL '$days_keep_device_logs days'"
2023-10-04 00:15:41 +02:00
else
echo "not purging device_logs."
fi
2024-08-31 00:51:14 +02:00
echo "delete event_guard_logs older $days_keep_event_guard_logs days"
2024-04-20 18:27:24 +02:00
if [ .$purge_event_guard_logs = .true ]; then
2024-08-31 00:51:14 +02:00
psql $db_name --port $db_port --host=$db_host --username=$db_username -c "delete from v_event_guard_logs where log_date < NOW() - INTERVAL '$days_keep_event_guard_logs days'"
2024-04-20 18:27:24 +02:00
else
echo "not purging event_guard_logs."
fi
2024-08-31 00:51:14 +02:00
echo "delete user_logs older $days_keep_user_logs days"
if [ .$purge_user_logs = .true ]; then
2024-05-09 16:29:41 +02:00
psql $db_name --port $db_port --host=$db_host --username=$db_username -c "delete from v_user_logs where timestamp < NOW() - INTERVAL '$days_keep_user_logs days'"
2024-04-20 18:27:24 +02:00
else
echo "not purging user_logs."
fi
2024-08-31 00:51:14 +02:00
echo "delete email_queue older $days_keep_email_queue days"
2022-04-16 06:19:02 +02:00
if [ .$purge_email_queue = .true ]; then
2023-06-14 19:23:43 +02:00
psql $db_name --port $db_port --host=$db_host --username=$db_username -c "delete from v_email_queue where email_status = 'sent' and email_date < NOW() - INTERVAL '$days_keep_email_queue days'"
2022-04-16 06:19:02 +02:00
else
echo "not purging email_queue."
fi
2024-08-31 00:51:14 +02:00
echo "delete fax_queue older $days_keep_fax_queue days"
2022-04-16 06:19:02 +02:00
if [ .$purge_fax_queue = .true ]; then
2023-06-14 19:23:43 +02:00
psql $db_name --port $db_port --host=$db_host --username=$db_username -c "delete from v_fax_queue where fax_status = 'sent' and fax_date < NOW() - INTERVAL '$days_keep_fax_queue days'"
2022-04-16 06:19:02 +02:00
else
echo "not purging fax_queue."
fi
2017-04-11 04:06:57 +02:00
#completed message
echo "Maintenance Completed";