#!/bin/bash database="scopserv" tenant="default" dump_cdr=false dump_acd=false dump_cel=false show_help=false date_from="" date_to="" if [ $# -eq 0 ]; then show_help=true fi for arg in "$@"; do case $arg in --help) show_help=true ;; --tenant=*) tenant="${arg#*=}" ;; --database=*) database="${arg#*=}" ;; --cdr) dump_cdr=true ;; --acd) dump_acd=true ;; --cel) dump_cel=true ;; --all) dump_cdr=true dump_acd=true dump_cel=true ;; --from=*) date_from="${arg#*=}" ;; --to=*) date_to="${arg#*=}" ;; esac done if [ "$show_help" = true ]; then echo "Usage: scopserv_report_dump.sh [OPTIONS]" echo "Options:" echo " --tenant=TENANT Set the tenant name (default: 'default')" echo " --database=DATABASE Set the database name (default: 'scopserv')" echo " --cdr Dump CDR table" echo " --acd Dump ACD (queue_log) table" echo " --cel Dump CEL table" echo " --all Dump all tables (CDR, ACD, CEL)" echo " --from=YYYY-MM-DD Set start date for data filtering" echo " --to=YYYY-MM-DD Set end date for data filtering" echo " --help Display this help and exit" echo "" echo "Examples:" echo " Dump all tables for the default tenant:" echo " ./scopserv_report_dump.sh --all" echo "" echo " Dump CDR, ACD, and CEL tables for tenant 'default' from 2021-01-01 to 2021-01-31:" echo " ./scopserv_report_dump.sh --tenant=default --acd --cel --cdr --from=2021-01-01 --to=2021-01-31" echo "" echo " Dump ACD table for tenant 'example' from 2022-05-01:" echo " ./scopserv_report_dump.sh --tenant=example --acd --from=2022-05-01" exit 0 fi if [ -z "$tenant" ]; then echo "Tenant is required" exit 1 fi dump_table() { table=$1 condition=$2 date_column=$3 if [ -n "$date_from" ]; then condition="$condition AND $date_column >= '$date_from'" fi if [ -n "$date_to" ]; then condition="$condition AND $date_column <= '$date_to'" fi echo "Dumping to ${tenant}_${table}.sql ..." mysql ${database} -e "DROP TABLE IF EXISTS ${table}_temp;" mysql ${database} -e "CREATE TABLE ${table}_temp AS SELECT * FROM ${table} WHERE $condition;" mysqldump ${database} ${table}_temp > ${tenant}_${table}.sql mysql ${database} -e "DROP TABLE ${table}_temp;" } [ "$dump_cdr" = true ] && dump_table "cdr" "tenant='${tenant}'" "calldate" [ "$dump_acd" = true ] && dump_table "queue_log" "queuename LIKE '${tenant}-%'" "time" [ "$dump_cel" = true ] && dump_table "cel" "context LIKE '${tenant}-%'" "FROM_UNIXTIME(eventtime)" echo "Dump completed."