AnonSec Shell
Server IP : 85.193.89.191  /  Your IP : 3.22.71.109
Web Server : Apache
System : Linux 956367-cx40159.tmweb.ru 3.10.0-1160.105.1.el7.x86_64 #1 SMP Thu Dec 7 15:39:45 UTC 2023 x86_64
User : bitrix ( 600)
PHP Version : 8.1.27
Disable Function : NONE
MySQL : OFF  |  cURL : OFF  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : ON  |  Pkexec : ON
Directory :  /etc/ansible/library/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ HOME ]     

Current File : /etc/ansible/library/bx_db
#!/bin/bash
# create dump for database and upload it to local mysql
# src=          server with source of db ( ip address or unix socket )
# dst=          server with destination of db  
# db=           db anme
# login=        login for connection
# password=     password
# Carefully! Ansible already provides values in quotes

#set -e

BY_TABLE=1      # if set to 1 it is create dump per table
                # Longer but more reliable for large databases
REPLACE=0       # relace destionation database or not

DEFAULT_CONF=/root/.my.cnf # can set login and password from file
DEFAULT_CONF_USAGE=0
MYSQLCMD=mysql

LOGDIR=/opt/webdir/logs
[[ ! -d $LOGDIR ]] && mkdir -m 750 $LOGDIR

TMPDIR=/tmp
[[ -d /dev/shm ]] && TMPDIR=/dev/shm
TMPFILE=$TMPDIR/.bx_db

# test mandatory option and print error if not exists
test_variables() {
  [[ -z $db ]] && db=mysql
  if [[ ( -z $login ) && ( -z $password ) ]]; then
    if [[ -f $DEFAULT_CONF ]]; then
      DEFAULT_CONF_USAGE=1
    else
      # create config with empty root
      echo "# test mysql conf" > $DEFAULT_CONF
      echo "[mysqld]"  >> $DEFAULT_CONF
      echo "user=root" >> $DEFAULT_CONF
      echo "password=" >> $DEFAULT_CONF
      echo >> $DEFAULT_CONF
      chmod 640 $DEFAULT_CONF

      # test default
      mysql -e 'select 1;' 1>/dev/null 2>&1
      if [[ $? -gt 0 ]]; then
        echo "{\"changed\":false,\"failed\":true,\"msg\":\"option $var= is mandatory, you must define it or set in $DEFAULT_CONF file\"}"
        rm -f $DEFAULT_CONF
        exit 1
      fi
      DEFAULT_CONF_USAGE=1
    fi
  else
    login=root
    MYSQLCMD=$MYSQLCMD" --user=$login --password='$password'"
  fi
}

get_db_list() {
  # test if mysql running
  system_process=$(ps -ef | grep -w 'mysqld' | grep -vw grep | grep -vwc $$)
  if [[ $system_process -gt 0 ]]; then 

    $MYSQLCMD --skip-column-names -e "show databases;" | \
      grep -vw 'information_schema\|performance_schema\|mysql\|test' | \
      grep -v '#mysql' >$TMPFILE 2>&1
    if [[ $? -gt 0 ]]; then
      echo "{\"changed\":false,\"failed\":true,\"msg\":\"mysql cmd return error $(head $TMPFILE)\"}"
      rm -f $TMPFILE
      exit 1
    fi

    dbs_list=$(grep -vw "$db" $TMPFILE| awk '{printf "\"%s\",", $1}' | sed -e 's/,$//;')
  else
    dbs_list=""
  fi
    dbs_out="{\"ansible_facts\":{"
    dbs_out=$dbs_out"\"dbs_list\":["
    dbs_out=$dbs_out$dbs_list
    dbs_out=$dbs_out"]}}"
    echo $dbs_out
}


# get options from file
source ${1}

test_variables

get_db_list

Anon7 - 2022
AnonSec Team