db_func.sh 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  1. is_db_valid() {
  2. config="$V_USERS/$user/db.conf"
  3. check_db=$(grep "DB='$database'" $config)
  4. # Checking result
  5. if [ -z "$check_db" ]; then
  6. echo "Error: db not added"
  7. log_event 'debug' "$E_DB_NOTEXIST $V_EVENT"
  8. exit $E_DB_NOTEXIST
  9. fi
  10. }
  11. is_db_new() {
  12. # Parsing domain values
  13. check_db=$(grep -F "DB='$database'" $V_USERS/$user/db.conf)
  14. # Checking result
  15. if [ ! -z "$check_db" ]; then
  16. echo "Error: db exist"
  17. log_event 'debug' "$E_DB_EXIST $V_EVENT"
  18. exit $E_DB_EXIST
  19. fi
  20. }
  21. # Shell list for single database
  22. db_shell_single_list() {
  23. # Reading file line by line
  24. line=$(grep "DB='$database'" $conf)
  25. # Parsing key=value
  26. for key in $line; do
  27. eval ${key%%=*}=${key#*=}
  28. done
  29. # Print result line
  30. for field in $fields; do
  31. eval key="$field"
  32. echo "${field//$/}: $key "
  33. done
  34. }
  35. # Json single list
  36. db_json_single_list() {
  37. i=1
  38. # Define words number
  39. last_word=$(echo "$fields" | wc -w)
  40. # Reading file line by line
  41. line=$(grep "DB='$database'" $conf)
  42. # Print top bracket
  43. echo '{'
  44. # Parsing key=value
  45. for key in $line; do
  46. eval ${key%%=*}=${key#*=}
  47. done
  48. # Starting output loop
  49. for field in $fields; do
  50. # Parsing key=value
  51. eval value=$field
  52. # Checking first field
  53. if [ "$i" -eq 1 ]; then
  54. echo -e "\t\"$value\": {"
  55. else
  56. if [ "$last_word" -eq "$i" ]; then
  57. echo -e "\t\t\"${field//$/}\": \"${value//,/, }\""
  58. else
  59. echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
  60. fi
  61. fi
  62. # Updating iterator
  63. i=$(( i + 1))
  64. done
  65. # If there was any output
  66. if [ -n "$value" ]; then
  67. echo -e "\t}"
  68. fi
  69. # Printing bottom json bracket
  70. echo -e "}"
  71. }
  72. # Shell list for single database host
  73. dbhost_shell_single_list() {
  74. # Reading file line by line
  75. line=$(grep "HOST='$host'" $conf)
  76. # Parsing key=value
  77. for key in $line; do
  78. eval ${key%%=*}=${key#*=}
  79. done
  80. # Print result line
  81. for field in $fields; do
  82. eval key="$field"
  83. echo "${field//$/}: $key"
  84. done
  85. }
  86. # Json list for single db host
  87. dbhost_json_single_list() {
  88. # Definigng variables
  89. i=1 # iterator
  90. # Define words number
  91. last_word=$(echo "$fields" | wc -w)
  92. # Reading file line by line
  93. line=$(grep "HOST='$host'" $conf)
  94. # Print top bracket
  95. echo '{'
  96. # Parsing key=value
  97. for key in $line; do
  98. eval ${key%%=*}=${key#*=}
  99. done
  100. # Starting output loop
  101. for field in $fields; do
  102. # Parsing key=value
  103. eval value=$field
  104. # Checking first field
  105. if [ "$i" -eq 1 ]; then
  106. echo -e "\t\"$value\": {"
  107. else
  108. if [ "$last_word" -eq "$i" ]; then
  109. echo -e "\t\t\"${field//$/}\": \"${value//,/, }\""
  110. else
  111. echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
  112. fi
  113. fi
  114. # Updating iterator
  115. i=$(( i + 1))
  116. done
  117. # If there was any output
  118. if [ -n "$value" ]; then
  119. echo -e "\t}"
  120. fi
  121. # Printing bottom json bracket
  122. echo -e "}"
  123. }
  124. # Checking database host existance
  125. is_db_host_valid() {
  126. config="$V_DB/$type.conf"
  127. check_db=$(grep "HOST='$host'" $config)
  128. # Checking result
  129. if [ -z "$check_db" ]; then
  130. echo "Error: host not added"
  131. log_event 'debug' "$E_DBHOST_NOTEXIST $V_EVENT"
  132. exit $E_DBHOST_NOTEXIST
  133. fi
  134. }
  135. get_next_db_host() {
  136. # Defining vars
  137. config="$V_DB/$type.conf"
  138. host="empty"
  139. host_str=$(grep "ACTIVE='yes'" $config)
  140. # Checking rows count
  141. check_row=$(echo "$host_str"|wc -l)
  142. # Checking empty result
  143. if [ 0 -eq "$check_row" ]; then
  144. echo "$host"
  145. exit
  146. fi
  147. # Checking one host
  148. if [ 1 -eq "$check_row" ]; then
  149. for key in $host_str; do
  150. eval ${key%%=*}="${key#*=}"
  151. done
  152. users=$(echo -e "${U_SYS_USERS//,/\n}"|wc -l)
  153. if [ "$MAX_DB" -gt "$U_DB_BASES" ] && [ $MAX_USERS -gt "$users" ];then
  154. host=$HOST
  155. fi
  156. echo "$host"
  157. exit
  158. fi
  159. # Defining balancing function
  160. weight_balance() {
  161. ow='100' # old_weght
  162. IFS=$'\n'
  163. for db in $host_str; do
  164. for key in $(echo $db|sed -e "s/' /'\n/g"); do
  165. eval ${key%%=*}="${key#*=}"
  166. done
  167. weight=$(echo "$U_DB_BASES * 100 / $MAX_DB"|bc)
  168. users=$(echo -e "${U_SYS_USERS//,/\n}"|wc -l)
  169. if [ "$ow" -gt "$weight" ] && [ $MAX_USERS -gt "$users" ]; then
  170. host="$HOST"
  171. ow="$weight"
  172. fi
  173. done
  174. }
  175. # Defining random balancing function
  176. random_balance() {
  177. # Parsing host pool
  178. HOST_LIST=''
  179. IFS=$'\n'
  180. for db in $host_str; do
  181. for key in $(echo $db|sed -e "s/' /'\n/g"); do
  182. eval ${key%%=*}="${key#*=}"
  183. done
  184. users=$(echo -e "${U_SYS_USERS//,/\n}"|wc -l)
  185. if [ "$MAX_DB" -gt "$U_DB_BASES" ] && [ $MAX_USERS -gt "$users" ]
  186. then
  187. HOST_LIST="$HOST_LIST$HOST "
  188. fi
  189. done
  190. # Checking one host
  191. if [ 2 -eq $(echo -e "${HOST_LIST// /\n}"|wc -l) ]; then
  192. host="${HOST_LIST// /\n}"# should test with disabled host
  193. else
  194. # Selecting all hosts
  195. HOSTS=($(echo -e "${HOST_LIST// /\n}"))
  196. num=${#HOSTS[*]}
  197. host="${HOSTS[$((RANDOM%num))]}"
  198. fi
  199. }
  200. # Defining first balancing function
  201. first_balance() {
  202. # Parsing host pool
  203. IFS=$'\n'
  204. for db in $host_str; do
  205. for key in $(echo $db|sed -e "s/' /'\n/g"); do
  206. eval ${key%%=*}="${key#*=}"
  207. done
  208. users=$(echo -e "${U_SYS_USERS//,/\n}"|wc -l)
  209. if [ "$MAX_DB" -gt "$U_DB_BASES" ] && [ $MAX_USERS -gt "$users" ]
  210. then
  211. host="$HOST"
  212. break
  213. fi
  214. done
  215. }
  216. # Parsing domain values
  217. db_balance=$(grep "DB_BALANCE='" $V_CONF/vesta.conf|cut -f 2 -d \')
  218. case $db_balance in
  219. weight) weight_balance "$config" ;;
  220. random) random_balance "$config" ;;
  221. first) first_balance "$config" ;;
  222. *) random_balance "$config" ;;
  223. esac
  224. echo "$host"
  225. }
  226. increase_db_value() {
  227. # Defining vars
  228. conf="$V_DB/$type.conf"
  229. host_str=$(grep "HOST='$host'" $conf)
  230. for key in $host_str; do
  231. eval ${key%%=*}=${key#*=}
  232. done
  233. # Increasing db_bases usage value
  234. U_DB_BASES=$((U_DB_BASES + 1))
  235. # Adding user to SYS_USERS pool
  236. if [ -z "$U_SYS_USERS" ]; then
  237. U_SYS_USERS="$user"
  238. else
  239. check_users=$(echo $U_SYS_USERS|sed -e "s/,/\n/g"|grep -w "$user")
  240. if [ -z "$check_users" ]; then
  241. U_SYS_USERS="$U_SYS_USERS,$user"
  242. fi
  243. fi
  244. # Concatenating db string
  245. case $type in
  246. mysql) new_str="HOST='$HOST' USER='$USER' PASSWORD='$PASSWORD'";
  247. new_str="$new_str PORT='$PORT' MAX_USERS='$MAX_USERS'";
  248. new_str="$new_str MAX_DB='$MAX_DB' U_SYS_USERS='$U_SYS_USERS'";
  249. new_str="$new_str U_DB_BASES='$U_DB_BASES' ACTIVE='$ACTIVE'";
  250. new_str="$new_str DATE='$DATE'";;
  251. pgsql) new_str="HOST='$HOST' USER='$USER' PASSWORD='$PASSWORD'";
  252. new_str="$new_str PORT='$PORT' TPL='$TPL'";
  253. new_str="$new_str MAX_USERS='$MAX_USERS' MAX_DB='$MAX_DB'";
  254. new_str="$new_str U_SYS_USERS='$U_SYS_USERS'";
  255. new_str="$new_str U_DB_BASES='$U_DB_BASES' ACTIVE='$ACTIVE'";
  256. new_str="$new_str DATE='$DATE'";;
  257. esac
  258. # Changing config
  259. sed -i "s/$host_str/$new_str/g" $conf
  260. }
  261. decrease_db_value() {
  262. # Defining vars
  263. conf="$V_DB/$type.conf"
  264. host_str=$(grep "HOST='$host'" $conf)
  265. for key in $host_str; do
  266. eval ${key%%=*}=${key#*=}
  267. done
  268. # Decreasing db_bases usage value
  269. U_DB_BASES=$((U_DB_BASES - 1))
  270. # Checking user databases on that host
  271. udb=$(grep "TYPE='$type'" $V_USERS/$user/db.conf|grep "HOST='$host'"|wc -l)
  272. if [ 2 -gt "$udb" ]; then
  273. U_SYS_USERS=$(echo "$U_SYS_USERS" | sed -e "s/,/\n/g" |\
  274. sed -e "/^$user$/d" | sed -e :a -e '$!N;s/\n/,/;ta')
  275. fi
  276. # Concatenating db string
  277. case $type in
  278. mysql) new_str="HOST='$HOST' USER='$USER' PASSWORD='$PASSWORD'";
  279. new_str="$new_str PORT='$PORT' MAX_USERS='$MAX_USERS'";
  280. new_str="$new_str MAX_DB='$MAX_DB' U_SYS_USERS='$U_SYS_USERS'";
  281. new_str="$new_str U_DB_BASES='$U_DB_BASES' ACTIVE='$ACTIVE'";
  282. new_str="$new_str DATE='$DATE'";;
  283. pgsql) new_str="HOST='$HOST' USER='$USER' PASSWORD='$PASSWORD'";
  284. new_str="$new_str PORT='$PORT' TPL='$TPL'";
  285. new_str="$new_str MAX_USERS='$MAX_USERS' MAX_DB='$MAX_DB'";
  286. new_str="$new_str U_SYS_USERS='$U_SYS_USERS'";
  287. new_str="$new_str U_DB_BASES='$U_DB_BASES' ACTIVE='$ACTIVE'";
  288. new_str="$new_str DATE='$DATE'";;
  289. esac
  290. # Changing config
  291. sed -i "s/$host_str/$new_str/g" $conf
  292. }
  293. create_db_mysql() {
  294. # Defining vars
  295. host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
  296. for key in $host_str; do
  297. eval ${key%%=*}=${key#*=}
  298. done
  299. sql="mysql -h $HOST -u $USER -p$PASSWORD -P$PORT -e"
  300. # Checking empty vars
  301. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
  302. echo "Error: config is broken"
  303. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  304. exit $E_PARSE_ERROR
  305. fi
  306. # Checking connection
  307. $sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
  308. if [ '0' -ne "$code" ]; then
  309. echo "Error: Connect failed"
  310. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  311. exit $E_DBHOST_UNAVAILABLE
  312. fi
  313. # Adding database & checking result
  314. $sql "CREATE DATABASE $database" >/dev/null 2>&1;code="$?"
  315. if [ '0' -ne "$code" ]; then
  316. echo "Error: Connect failed"
  317. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  318. exit $E_DBHOST_UNAVAILABLE
  319. fi
  320. # Adding user with password (% will give access to db from any ip)
  321. $sql "GRANT ALL ON $database.* TO '$db_user'@'%' \
  322. IDENTIFIED BY '$db_password'"
  323. # Adding grant for localhost (% doesn't do that )
  324. if [ "$host" = 'localhost' ]; then
  325. $sql "GRANT ALL ON $database.* TO '$db_user'@'localhost' \
  326. IDENTIFIED BY '$db_password'"
  327. fi
  328. # Flushing priveleges
  329. $sql "FLUSH PRIVILEGES"
  330. }
  331. create_db_pgsql() {
  332. # Defining vars
  333. host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
  334. for key in $host_str; do
  335. eval ${key%%=*}=${key#*=}
  336. done
  337. export PGPASSWORD="$PASSWORD"
  338. sql="psql -h $HOST -U $USER -d $TPL -p $PORT -c"
  339. # Checking empty vars
  340. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
  341. echo "Error: config is broken"
  342. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  343. exit $E_PARSE_ERROR
  344. fi
  345. # Checking connection
  346. $sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
  347. if [ '0' -ne "$code" ]; then
  348. echo "Error: Connect failed"
  349. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  350. exit $E_DBHOST_UNAVAILABLE
  351. fi
  352. # Adding database & checking result
  353. $sql "CREATE DATABASE $database" >/dev/null 2>&1;code="$?"
  354. if [ '0' -ne "$code" ]; then
  355. echo "Error: Connect failed"
  356. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  357. exit $E_DBHOST_UNAVAILABLE
  358. fi
  359. $sql "CREATE ROLE $db_user WITH LOGIN PASSWORD '$db_password'"
  360. $sql "GRANT ALL PRIVILEGES ON DATABASE $database TO $db_user"
  361. export PGPASSWORD='pgsql'
  362. }
  363. is_db_host_new() {
  364. if [ -e "$V_DB/$type.conf" ]; then
  365. check_host=$(grep "HOST='$host'" $V_DB/$type.conf)
  366. if [ ! -z "$check_host" ]; then
  367. echo "Error: db host exist"
  368. log_event 'debug' "$E_DBHOST_EXIST $V_EVENT"
  369. exit $E_DBHOST_EXIST
  370. fi
  371. fi
  372. }
  373. is_mysql_host_alive() {
  374. # Checking connection
  375. sql="mysql -h $host -u $db_user -p$db_password -P$port -e"
  376. $sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
  377. if [ '0' -ne "$code" ]; then
  378. echo "Error: Connect failed"
  379. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  380. exit $E_DBHOST_UNAVAILABLE
  381. fi
  382. }
  383. is_pgsql_host_alive() {
  384. # Checking connection
  385. export PGPASSWORD="$db_password"
  386. sql="psql -h $host -U $db_user -d $template -p $port -c"
  387. $sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
  388. if [ '0' -ne "$code" ]; then
  389. echo "Error: Connect failed"
  390. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  391. exit $E_DBHOST_UNAVAILABLE
  392. fi
  393. }
  394. is_db_suspended() {
  395. config="$V_USERS/$user/db.conf"
  396. check_db=$(grep "DB='$database'" $config|grep "SUSPEND='yes'")
  397. # Checking result
  398. if [ ! -z "$check_db" ]; then
  399. echo "Error: db suspended"
  400. log_event 'debug' "$E_DB_SUSPENDED $V_EVENT"
  401. exit $E_DB_SUSPENDED
  402. fi
  403. }
  404. is_db_unsuspended() {
  405. config="$V_USERS/$user/db.conf"
  406. check_db=$(grep "DB='$database'" $config|grep "SUSPEND='yes'")
  407. # Checking result
  408. if [ -z "$check_db" ]; then
  409. echo "Error: db unsuspended"
  410. log_event 'debug' "$E_DB_UNSUSPENDED $V_EVENT"
  411. exit $E_DB_UNSUSPENDED
  412. fi
  413. }
  414. is_db_user_valid() {
  415. config="$V_USERS/$user/db.conf"
  416. check_db=$(grep "DB='$database'" $config|grep "USER='$db_user'")
  417. # Checking result
  418. if [ -z "$check_db" ]; then
  419. echo "Error: dbuser not exist"
  420. log_event 'debug' "$E_DBUSER_NOTEXIST $V_EVENT"
  421. exit $E_DBUSER_NOTEXIST
  422. fi
  423. }
  424. change_db_mysql_password() {
  425. # Defining vars
  426. host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
  427. for key in $host_str; do
  428. eval ${key%%=*}=${key#*=}
  429. done
  430. sql="mysql -h $HOST -u $USER -p$PASSWORD -P$PORT -e"
  431. # Checking empty vars
  432. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
  433. echo "Error: config is broken"
  434. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  435. exit $E_PARSE_ERROR
  436. fi
  437. # Checking connection
  438. $sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
  439. if [ '0' -ne "$code" ]; then
  440. echo "Error: Connect failed"
  441. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  442. exit $E_DBHOST_UNAVAILABLE
  443. fi
  444. # Changing user password
  445. $sql "GRANT ALL ON $database.* TO '$db_user'@'%' \
  446. IDENTIFIED BY '$db_password'"
  447. $sql "GRANT ALL ON $database.* TO '$db_user'@'localhost' \
  448. IDENTIFIED BY '$db_password'"
  449. #$sql "SET PASSWORD FOR '$db_user'@'%' = PASSWORD('$db_password');"
  450. $sql "FLUSH PRIVILEGES"
  451. }
  452. change_db_pgsql_password() {
  453. # Defining vars
  454. host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
  455. for key in $host_str; do
  456. eval ${key%%=*}=${key#*=}
  457. done
  458. export PGPASSWORD="$PASSWORD"
  459. sql="psql -h $HOST -U $USER -d $TPL -p $PORT -c"
  460. # Checking empty vars
  461. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
  462. echo "Error: config is broken"
  463. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  464. exit $E_PARSE_ERROR
  465. fi
  466. # Checking connection
  467. $sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
  468. if [ '0' -ne "$code" ]; then
  469. echo "Error: Connect failed"
  470. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  471. exit $E_DBHOST_UNAVAILABLE
  472. fi
  473. $sql "ALTER ROLE $db_user WITH LOGIN PASSWORD '$db_password'" >/dev/null
  474. export PGPASSWORD='pgsql'
  475. }
  476. get_db_value() {
  477. # Defining vars
  478. key="$1"
  479. db_str=$(grep "DB='$database'" $V_USERS/$user/db.conf)
  480. # Parsing key=value
  481. for keys in $db_str; do
  482. eval ${keys%%=*}=${keys#*=}
  483. done
  484. # Self reference
  485. eval value="$key"
  486. # Print value
  487. echo "$value"
  488. }
  489. del_db_mysql() {
  490. # Defining vars
  491. host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
  492. for key in $host_str; do
  493. eval ${key%%=*}=${key#*=}
  494. done
  495. sql="mysql -h $HOST -u $USER -p$PASSWORD -P$PORT -e"
  496. # Checking empty vars
  497. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
  498. echo "Error: config is broken"
  499. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  500. exit $E_PARSE_ERROR
  501. fi
  502. # Checking connection
  503. $sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
  504. if [ '0' -ne "$code" ]; then
  505. echo "Error: Connect failed"
  506. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  507. exit $E_DBHOST_UNAVAILABLE
  508. fi
  509. # Deleting database & checking result
  510. $sql "DROP DATABASE $database" >/dev/null 2>&1;code="$?"
  511. if [ '0' -ne "$code" ]; then
  512. echo "Error: Connect failed"
  513. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  514. exit $E_DBHOST_UNAVAILABLE
  515. fi
  516. # Deleting user
  517. check_users=$(grep "USER='$db_user'" $V_USERS/$user/db.conf |wc -l)
  518. if [ 1 -ge "$check_users" ]; then
  519. $sql "DROP USER '$db_user'@'%'"
  520. if [ "$host" = 'localhost' ]; then
  521. $sql "DROP USER '$db_user'@'localhost'"
  522. fi
  523. else
  524. $sql "REVOKE ALL ON $database.* from '$db_user'@'%'"
  525. if [ "$host" = 'localhost' ]; then
  526. $sql "REVOKE ALL ON $database.* from '$db_user'@'localhost'"
  527. fi
  528. fi
  529. $sql "FLUSH PRIVILEGES"
  530. }
  531. del_db_pgsql() {
  532. # Defining vars
  533. host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
  534. for key in $host_str; do
  535. eval ${key%%=*}=${key#*=}
  536. done
  537. export PGPASSWORD="$PASSWORD"
  538. sql="psql -h $HOST -U $USER -d $TPL -p $PORT -c"
  539. # Checking empty vars
  540. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
  541. echo "Error: config is broken"
  542. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  543. exit $E_PARSE_ERROR
  544. fi
  545. # Checking connection
  546. $sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
  547. if [ '0' -ne "$code" ]; then
  548. echo "Error: Connect failed"
  549. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  550. exit $E_DBHOST_UNAVAILABLE
  551. fi
  552. # Deleting database & checking result
  553. $sql "DROP DATABASE $database" >/dev/null 2>&1;code="$?"
  554. if [ '0' -ne "$code" ]; then
  555. echo "Error: Connect failed"
  556. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  557. exit $E_DBHOST_UNAVAILABLE
  558. fi
  559. # Deleting user
  560. check_users=$(grep "USER='$db_user'" $V_USERS/$user/db.conf |wc -l)
  561. if [ 1 -ge "$check_users" ]; then
  562. $sql "DROP ROLE $db_user" >/dev/null 2>&1
  563. else
  564. $sql "REVOKE ALL PRIVILEGES ON $database FROM $db_user">/dev/null
  565. fi
  566. export PGPASSWORD='pgsql'
  567. }
  568. del_db_vesta() {
  569. conf="$V_USERS/$user/db.conf"
  570. # Parsing domains
  571. string=$( grep -n "DB='$database'" $conf | cut -f 1 -d : )
  572. if [ -z "$string" ]; then
  573. echo "Error: parse error"
  574. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  575. exit $E_PARSE_ERROR
  576. fi
  577. sed -i "$string d" $conf
  578. }
  579. dump_db_mysql() {
  580. # Defining vars
  581. host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
  582. for key in $host_str; do
  583. eval ${key%%=*}=${key#*=}
  584. done
  585. sql="mysql -h $HOST -u $USER -p$PASSWORD -P$PORT -e"
  586. dumper="mysqldump -h $HOST -u $USER -p$PASSWORD -P$PORT -r"
  587. # Checking empty vars
  588. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
  589. echo "Error: config is broken"
  590. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  591. exit $E_PARSE_ERROR
  592. fi
  593. # Checking connection
  594. $sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
  595. if [ '0' -ne "$code" ]; then
  596. echo "Error: Connect failed"
  597. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  598. exit $E_DBHOST_UNAVAILABLE
  599. fi
  600. # Dumping database
  601. $dumper $dump $database
  602. # Dumping user grants
  603. $sql "SHOW GRANTS FOR $db_user@localhost" | grep -v "Grants for" > $grants
  604. $sql "SHOW GRANTS FOR $db_user@'%'" | grep -v "Grants for" >> $grants
  605. }
  606. dump_db_pgsql() {
  607. # Defining vars
  608. host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
  609. for key in $host_str; do
  610. eval ${key%%=*}=${key#*=}
  611. done
  612. export PGPASSWORD="$PASSWORD"
  613. sql="psql -h $HOST -U $USER -d $TPL -p $PORT -c"
  614. dumper="pg_dump -h $HOST -U $USER -p $PORT -c -d -O -x -i -f"
  615. # Checking empty vars
  616. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
  617. echo "Error: config is broken"
  618. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  619. exit $E_PARSE_ERROR
  620. fi
  621. # Checking connection
  622. $sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
  623. if [ '0' -ne "$code" ]; then
  624. echo "Error: Connect failed"
  625. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  626. exit $E_DBHOST_UNAVAILABLE
  627. fi
  628. # Dumping database
  629. $dumper $dump $database
  630. # Dumping user grants
  631. md5=$($sql "SELECT rolpassword FROM pg_authid WHERE rolname='$db_user';")
  632. md5=$(echo "$md5" | head -n 1 | cut -f 2 -d ' ')
  633. pw_str="UPDATE pg_authid SET rolpassword='$md5' WHERE rolname='$db_user';"
  634. gr_str="GRANT ALL PRIVILEGES ON DATABASE $database to '$db_user'"
  635. echo -e "$pw_str\n$gr_str" >> $grants
  636. export PGPASSWORD='pgsql'
  637. }
  638. is_db_host_free() {
  639. # Defining vars
  640. host_str=$(grep "HOST='$host'" $V_DB/$type.conf)
  641. for key in $host_str; do
  642. eval ${key%%=*}=${key#*=}
  643. done
  644. # Checking U_DB_BASES
  645. if [ 0 -ne "$U_DB_BASES" ]; then
  646. echo "Error: host is used"
  647. log_event 'debug' "$E_DBHOST_BUSY $V_EVENT"
  648. exit $E_DBHOST_BUSY
  649. fi
  650. }
  651. del_dbhost_vesta() {
  652. conf="$V_DB/$type.conf"
  653. # Parsing domains
  654. string=$( grep -n "HOST='$host'" $conf | cut -f 1 -d : )
  655. if [ -z "$string" ]; then
  656. echo "Error: parse error"
  657. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  658. exit $E_PARSE_ERROR
  659. fi
  660. sed -i "$string d" $conf
  661. }
  662. update_db_base_value() {
  663. key="$1"
  664. value="$2"
  665. # Defining conf
  666. conf="$V_USERS/$user/db.conf"
  667. # Parsing conf
  668. db_str=$(grep -n "DB='$database'" $conf)
  669. str_number=$(echo $db_str | cut -f 1 -d ':')
  670. str=$(echo $db_str | cut -f 2 -d ':')
  671. # Reading key=values
  672. for keys in $str; do
  673. eval ${keys%%=*}=${keys#*=}
  674. done
  675. # Defining clean key
  676. c_key=$(echo "${key//$/}")
  677. eval old="${key}"
  678. # Escaping slashes
  679. old=$(echo "$old" | sed -e 's/\\/\\\\/g' -e 's/&/\\&/g' -e 's/\//\\\//g')
  680. new=$(echo "$value" | sed -e 's/\\/\\\\/g' -e 's/&/\\&/g' -e 's/\//\\\//g')
  681. # Updating conf
  682. sed -i "$str_number s/$c_key='${old//\*/\\*}'/$c_key='${new//\*/\\*}'/g"\
  683. $conf
  684. }
  685. suspend_db_mysql() {
  686. # Defining vars
  687. host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
  688. for key in $host_str; do
  689. eval ${key%%=*}=${key#*=}
  690. done
  691. sql="mysql -h $HOST -u $USER -p$PASSWORD -P$PORT -e"
  692. # Checking empty vars
  693. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
  694. echo "Error: config is broken"
  695. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  696. exit $E_PARSE_ERROR
  697. fi
  698. # Checking connection
  699. $sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
  700. if [ '0' -ne "$code" ]; then
  701. echo "Error: Connect failed"
  702. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  703. exit $E_DBHOST_UNAVAILABLE
  704. fi
  705. # Suspending user
  706. $sql "REVOKE ALL ON $database.* FROM '$db_user'@'%'"
  707. $sql "FLUSH PRIVILEGES"
  708. }
  709. suspend_db_pgsql() {
  710. # Defining vars
  711. host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
  712. for key in $host_str; do
  713. eval ${key%%=*}=${key#*=}
  714. done
  715. export PGPASSWORD="$PASSWORD"
  716. sql="psql -h $HOST -U $USER -d $TPL -p $PORT -c"
  717. # Checking empty vars
  718. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
  719. echo "Error: config is broken"
  720. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  721. exit $E_PARSE_ERROR
  722. fi
  723. # Checking connection
  724. $sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
  725. if [ '0' -ne "$code" ]; then
  726. echo "Error: Connect failed"
  727. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  728. exit $E_DBHOST_UNAVAILABLE
  729. fi
  730. # Suspending user
  731. $sql "REVOKE ALL PRIVILEGES ON $database FROM $db_user">/dev/null
  732. export PGPASSWORD='pgsql'
  733. }
  734. unsuspend_db_mysql() {
  735. # Defining vars
  736. host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
  737. for key in $host_str; do
  738. eval ${key%%=*}=${key#*=}
  739. done
  740. sql="mysql -h $HOST -u $USER -p$PASSWORD -P$PORT -e"
  741. # Checking empty vars
  742. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
  743. echo "Error: config is broken"
  744. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  745. exit $E_PARSE_ERROR
  746. fi
  747. # Checking connection
  748. $sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
  749. if [ '0' -ne "$code" ]; then
  750. echo "Error: Connect failed"
  751. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  752. exit $E_DBHOST_UNAVAILABLE
  753. fi
  754. # Unsuspending user
  755. $sql "GRANT ALL ON $database.* to '$db_user'@'%'"
  756. $sql "FLUSH PRIVILEGES"
  757. }
  758. unsuspend_db_pgsql() {
  759. # Defining vars
  760. host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
  761. for key in $host_str; do
  762. eval ${key%%=*}=${key#*=}
  763. done
  764. export PGPASSWORD="$PASSWORD"
  765. sql="psql -h $HOST -U $USER -d $TPL -p $PORT -c"
  766. # Checking empty vars
  767. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
  768. echo "Error: config is broken"
  769. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  770. exit $E_PARSE_ERROR
  771. fi
  772. # Checking connection
  773. $sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
  774. if [ '0' -ne "$code" ]; then
  775. echo "Error: Connect failed"
  776. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  777. exit $E_DBHOST_UNAVAILABLE
  778. fi
  779. # Unsuspending user
  780. $sql "GRANT ALL PRIVILEGES ON DATABASE $database TO $db_user" >/dev/null
  781. export PGPASSWORD='pgsql'
  782. }
  783. db_clear_search() {
  784. # Defining delimeter
  785. IFS=$'\n'
  786. # Reading file line by line
  787. for line in $(grep $search_string $conf); do
  788. # Parsing key=val
  789. for key in $line; do
  790. eval ${key%%=*}=${key#*=}
  791. done
  792. # Print result line
  793. eval echo "$field"
  794. done
  795. }
  796. get_disk_db_mysql() {
  797. # Defining vars
  798. host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
  799. for key in $host_str; do
  800. eval ${key%%=*}=${key#*=}
  801. done
  802. sql="mysql -h $HOST -u $USER -p$PASSWORD -P$PORT -e"
  803. # Checking empty vars
  804. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
  805. echo "Error: config is broken"
  806. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  807. exit $E_PARSE_ERROR
  808. fi
  809. # Checking connection
  810. $sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
  811. if [ '0' -ne "$code" ]; then
  812. echo "Error: Connect failed"
  813. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  814. exit $E_DBHOST_UNAVAILABLE
  815. fi
  816. # Deleting database & checking result
  817. query="SELECT sum( data_length + index_length ) / 1024 / 1024 \"Size\"
  818. FROM information_schema.TABLES WHERE table_schema='$database'"
  819. raw_size=$($sql "$query" |tail -n 1)
  820. # Checking null output (this means error btw)
  821. if [ "$raw_size" == 'NULL' ]; then
  822. raw_size='0'
  823. fi
  824. # Rounding zero size
  825. if [ "${raw_size:0:1}" -eq '0' ]; then
  826. raw_size='1'
  827. fi
  828. # Printing round size in mb
  829. printf "%0.f\n" $raw_size
  830. }
  831. get_disk_db_pgsql() {
  832. # Defining vars
  833. host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
  834. for key in $host_str; do
  835. eval ${key%%=*}=${key#*=}
  836. done
  837. export PGPASSWORD="$PASSWORD"
  838. sql="psql -h $HOST -U $USER -d $TPL -p $PORT -c"
  839. # Checking empty vars
  840. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
  841. echo "Error: config is broken"
  842. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  843. exit $E_PARSE_ERROR
  844. fi
  845. # Checking connection
  846. $sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
  847. if [ '0' -ne "$code" ]; then
  848. echo "Error: Connect failed"
  849. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  850. exit $E_DBHOST_UNAVAILABLE
  851. fi
  852. # Raw query
  853. raq_query=$($sql "SELECT pg_database_size('$database');")
  854. raw_size=$(echo "$raq_query" | grep -v "-" | grep -v 'row' |\
  855. sed -e "/^$/d" |grep -v "pg_database_size" | awk '{print $1}')
  856. # Checking null output (this means error btw)
  857. if [ -z "$raw_size" ]; then
  858. raw_size='0'
  859. fi
  860. # Converting to MB
  861. size=$(expr $raw_size / 1048576)
  862. # Rounding zero size
  863. if [ "$size" -eq '0' ]; then
  864. echo '1'
  865. else
  866. echo "$size"
  867. fi
  868. }