#! /bin/bash # Author: Mindaugas Mardosas # Company: Kolmisoft # Year: 2013 # About: This script collects data required by programmers to solve bugs. # # Arguments: # $1 - How many days back from now calls table is needed? # $2 - Ticket number . /usr/src/m2/framework/bash_functions.sh . /usr/src/m2/framework/settings.sh #------VARIABLES------------- VERSION="1.1.0" HOW_MANY_DAYS_CALLS_ARE_NEEDED="$1" TICKER_NUMBER="$2" NO_SCREEN="$3" # Option to be tolerant on running without screen GREEN='\033[0;32m' NC='\033[0m' EXCLUDED_TABLES=( calls call_logs call_details calls_old bl_dst_scoring bl_src_scoring bl_ip_scoring aggregates time_periods server_loadstats ) # Skip first two arguments shift shift #Add exluded tables from passed command line arguments for arg in "$@" do if [ "$arg" != "screen" ] || [ "$arg" != "SCREEN" ]; then EXCLUDED_TABLES+=($arg) fi done if [ "$HOW_MANY_DAYS_CALLS_ARE_NEEDED" == "" ] || [ "$TICKER_NUMBER" == "" ]; then report "Invalid command line arguments" 1 echo -e "\n1 parameter: number of days calls history is needed. If you will specify 0 here - no dump will be made\n2 parameter - TRAC ticket number or other fraze which will be used as file name." exit 1 fi if [ "$NO_SCREEN" != "NO_SCREEN" ]; then # require to be running from screen from now on if ! k_we_are_inside_screen; then report "You have to run this script from 'screen' program. To do so - just run command 'screen' and launch the script again as usual" 1 exit 1 fi fi UPLOAD_SERVER="backup.kolmisoft.com" #UPLOAD_PORT="6666" #UPLOAD_PATH="/home/support/DB" #----- FUNCTIONS ------------ dump_db_without_calls() { # Author: Mindaugas Mardosas # Company: Kolmisoft # Year: 2013 # About: This function mysqldump --single-transaction -h "$DB_HOST" -u "$DB_USERNAME" --password="$DB_PASSWORD" "$DB_NAME" $IGNORED_TABLES_STRING > /home/.tmp_dir_for_m2_data_gathering/m2_db_without_calls.sql if [ "$?" == "0" ]; then clear_email_data "m2_db_without_calls.sql" report "Created DB backup: /home/.tmp_dir_for_m2_data_gathering/m2_db_without_calls.sql" 3 else report "Failed to create DB backup: /home/.tmp_dir_for_m2_data_gathering/m2_db_without_calls.sql. Exiting script." 1 cleanup_mess exit 1 fi } dump_calls_table() { # Author: Mindaugas Mardosas # Company: Kolmisoft # Year: 2013 # About: This function dumps call table only for specified time period local days_to_dump_from_now="$1" current_date=`date +%Y-%m-%-d` date_from=`date -d "$current_date -$days_to_dump_from_now days" +%Y-%m-%d` mysqldump --single-transaction -h "$DB_HOST" -u "$DB_USERNAME" --password=$DB_PASSWORD "$DB_NAME" calls --where="calldate between '$date_from 00:00:00' and '$current_date 00:00:00'" > /home/.tmp_dir_for_m2_data_gathering/m2_db_calls_only.sql if [ "$?" == "0" ]; then clear_email_data "m2_db_calls_only.sql" report "Created DB backup: /home/.tmp_dir_for_m2_data_gathering/m2_db_calls_only.sql" 3 else report "Failed to create DB backup: /home/.tmp_dir_for_m2_data_gathering/m2_db_calls_only.sql. Exiting script." 1 cleanup_mess exit 1 fi } compress_data() { # Author: Mindaugas Mardosas # Company: Kolmisoft # Year: 2013 # About: This function compress all data in order it would be sent to Kolmisoft server. cd /home/.tmp_dir_for_m2_data_gathering if [ -f "m2_db_calls_only.sql" ]; then tar czf $TICKER_NUMBER.tar.gz m2_db_calls_only.sql m2_db_without_calls.sql else # compressing DB without calls tar czf $TICKER_NUMBER.tar.gz m2_db_without_calls.sql fi if [ "$?" == "0" ]; then report "Successfully compressed the database to /home/.tmp_dir_for_m2_data_gathering/$TICKER_NUMBER.tar.gz" 3 else report "Failed to compress database. Exiting script" 1 cleanup_mess exit 1 fi } cleanup_mess() { # Author: Mindaugas Mardosas # Company: Kolmisoft # Year: 2013 # About: This function cleans all temporary data created during data gathering rm -rf /home/.tmp_dir_for_m2_data_gathering report "Cleaned out temporary dir at /home/.tmp_dir_for_m2_data_gathering" 3 } upload_archive() { # Author: Mindaugas Mardosas # Company: Kolmisoft- # Year: 2013 # About: This function uploads collected data to Kolmisoft backup server report "Ready to upload? Press enter" 3 read report "After successful connection to sftp paste these commands in sftp shell:" 3 echo "-----------------------------------------------------------------------" echo -e " ${GREEN}cd DB${NC}" echo -e " ${GREEN}put /home/.tmp_dir_for_m2_data_gathering/"$TICKER_NUMBER".tar.gz${NC}" echo -e " ${GREEN}exit${NC}" echo "-----------------------------------------------------------------------" if sftp support@"$UPLOAD_SERVER"; then report "Data archive was Successfully uploaded" 3 else report "Failed to upload data archive" 1 fi } # prevents email sending clear_email_data(){ ADDRESSES="UPDATE addresses SET email = id;" USERS="UPDATE users SET recordings_email = id, main_email = id, noc_email = id, billing_email = id, rates_email = id;" EMAIL_SENDING="UPDATE conflines SET value = 0 WHERE name = 'Email_Sending_Enabled';" SMTP="UPDATE conflines SET value = '' WHERE name = 'Email_Smtp_Server';" PORT="UPDATE conflines SET value = 25 WHERE name = 'Email_port';" LOGIN="UPDATE conflines SET value = '' WHERE name = 'Email_Login';" PASSWORD="UPDATE conflines SET value = '' WHERE name = 'Email_Password';" BATCH="UPDATE conflines SET value = 50 WHERE name = 'Email_Batch_Size';" FROM="UPDATE conflines SET value = '' WHERE name = 'Email_from';" printf "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s" "$ADDRESSES" "$USERS" "$EMAIL_SENDING" "$SMTP" "$PORT" "$LOGIN" "$PASSWORD" "$BATCH" "$FROM" >> /home/.tmp_dir_for_m2_data_gathering/$1 } #--------MAIN ------------- mysql_connect_data_v2 > /dev/null # Getting MySQL connect data #Build ignored tables argument string to mysqldump IGNORED_TABLES_STRING='' for ignored_table in "${EXCLUDED_TABLES[@]}" do IGNORED_TABLES_STRING+=" --ignore-table=${DB_NAME}.${ignored_table}" done mkdir -p /home/.tmp_dir_for_m2_data_gathering dump_db_without_calls if [ "$HOW_MANY_DAYS_CALLS_ARE_NEEDED" != "0" ]; then dump_calls_table "$HOW_MANY_DAYS_CALLS_ARE_NEEDED" fi compress_data upload_archive #-------------------------- cleanup_mess