db_func.sh 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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='pgsqk'
  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 "SET PASSWORD FOR '$db_user'@'%' = PASSWORD('$db_password');"
  448. $sql "FLUSH PRIVILEGES"
  449. }
  450. change_db_pgsql_password() {
  451. # Defining vars
  452. host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
  453. for key in $host_str; do
  454. eval ${key%%=*}=${key#*=}
  455. done
  456. export PGPASSWORD="$PASSWORD"
  457. sql="psql -h $HOST -U $USER -d $TPL -p $PORT -c"
  458. # Checking empty vars
  459. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
  460. echo "Error: config is broken"
  461. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  462. exit $E_PARSE_ERROR
  463. fi
  464. # Checking connection
  465. $sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
  466. if [ '0' -ne "$code" ]; then
  467. echo "Error: Connect failed"
  468. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  469. exit $E_DBHOST_UNAVAILABLE
  470. fi
  471. $sql "ALTER ROLE $db_user WITH LOGIN PASSWORD '$db_password'" >/dev/null
  472. export PGPASSWORD='pgsqk'
  473. }
  474. get_db_value() {
  475. # Defining vars
  476. key="$1"
  477. db_str=$(grep "DB='$database'" $V_USERS/$user/db.conf)
  478. # Parsing key=value
  479. for keys in $db_str; do
  480. eval ${keys%%=*}=${keys#*=}
  481. done
  482. # Self reference
  483. eval value="$key"
  484. # Print value
  485. echo "$value"
  486. }
  487. del_db_mysql() {
  488. # Defining vars
  489. host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
  490. for key in $host_str; do
  491. eval ${key%%=*}=${key#*=}
  492. done
  493. sql="mysql -h $HOST -u $USER -p$PASSWORD -P$PORT -e"
  494. # Checking empty vars
  495. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
  496. echo "Error: config is broken"
  497. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  498. exit $E_PARSE_ERROR
  499. fi
  500. # Checking connection
  501. $sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
  502. if [ '0' -ne "$code" ]; then
  503. echo "Error: Connect failed"
  504. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  505. exit $E_DBHOST_UNAVAILABLE
  506. fi
  507. # Deleting database & checking result
  508. $sql "DROP DATABASE $database" >/dev/null 2>&1;code="$?"
  509. if [ '0' -ne "$code" ]; then
  510. echo "Error: Connect failed"
  511. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  512. exit $E_DBHOST_UNAVAILABLE
  513. fi
  514. # Deleting user
  515. check_users=$(grep "USER='$db_user'" $V_USERS/$user/db.conf |wc -l)
  516. if [ 1 -ge "$check_users" ]; then
  517. $sql "DROP USER '$db_user'@'%'"
  518. if [ "$host" = 'localhost' ]; then
  519. $sql "DROP USER '$db_user'@'localhost'"
  520. fi
  521. else
  522. $sql "REVOKE ALL ON $database.* from '$db_user'@'%'"
  523. if [ "$host" = 'localhost' ]; then
  524. $sql "REVOKE ALL ON $database.* from '$db_user'@'localhost'"
  525. fi
  526. fi
  527. $sql "FLUSH PRIVILEGES"
  528. }
  529. del_db_pgsql() {
  530. # Defining vars
  531. host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
  532. for key in $host_str; do
  533. eval ${key%%=*}=${key#*=}
  534. done
  535. export PGPASSWORD="$PASSWORD"
  536. sql="psql -h $HOST -U $USER -d $TPL -p $PORT -c"
  537. # Checking empty vars
  538. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
  539. echo "Error: config is broken"
  540. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  541. exit $E_PARSE_ERROR
  542. fi
  543. # Checking connection
  544. $sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
  545. if [ '0' -ne "$code" ]; then
  546. echo "Error: Connect failed"
  547. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  548. exit $E_DBHOST_UNAVAILABLE
  549. fi
  550. # Deleting database & checking result
  551. $sql "DROP DATABASE $database" >/dev/null 2>&1;code="$?"
  552. if [ '0' -ne "$code" ]; then
  553. echo "Error: Connect failed"
  554. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  555. exit $E_DBHOST_UNAVAILABLE
  556. fi
  557. # Deleting user
  558. check_users=$(grep "USER='$db_user'" $V_USERS/$user/db.conf |wc -l)
  559. if [ 1 -ge "$check_users" ]; then
  560. $sql "DROP ROLE $db_user" >/dev/null 2>&1
  561. else
  562. $sql "REVOKE ALL PRIVILEGES ON $database FROM $db_user">/dev/null
  563. fi
  564. export PGPASSWORD='pgsqk'
  565. }
  566. del_db_vesta() {
  567. conf="$V_USERS/$user/db.conf"
  568. # Parsing domains
  569. string=$( grep -n "DB='$database'" $conf | cut -f 1 -d : )
  570. if [ -z "$string" ]; then
  571. echo "Error: parse error"
  572. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  573. exit $E_PARSE_ERROR
  574. fi
  575. sed -i "$string d" $conf
  576. }
  577. is_db_host_free() {
  578. # Defining vars
  579. host_str=$(grep "HOST='$host'" $V_DB/$type.conf)
  580. for key in $host_str; do
  581. eval ${key%%=*}=${key#*=}
  582. done
  583. # Checking U_DB_BASES
  584. if [ 0 -ne "$U_DB_BASES" ]; then
  585. echo "Error: host is used"
  586. log_event 'debug' "$E_DBHOST_BUSY $V_EVENT"
  587. exit $E_DBHOST_BUSY
  588. fi
  589. }
  590. del_dbhost_vesta() {
  591. conf="$V_DB/$type.conf"
  592. # Parsing domains
  593. string=$( grep -n "HOST='$host'" $conf | cut -f 1 -d : )
  594. if [ -z "$string" ]; then
  595. echo "Error: parse error"
  596. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  597. exit $E_PARSE_ERROR
  598. fi
  599. sed -i "$string d" $conf
  600. }
  601. update_db_base_value() {
  602. key="$1"
  603. value="$2"
  604. # Defining conf
  605. conf="$V_USERS/$user/db.conf"
  606. # Parsing conf
  607. db_str=$(grep -n "DB='$database'" $conf)
  608. str_number=$(echo $db_str | cut -f 1 -d ':')
  609. str=$(echo $db_str | cut -f 2 -d ':')
  610. # Reading key=values
  611. for keys in $str; do
  612. eval ${keys%%=*}=${keys#*=}
  613. done
  614. # Defining clean key
  615. c_key=$(echo "${key//$/}")
  616. eval old="${key}"
  617. # Escaping slashes
  618. old=$(echo "$old" | sed -e 's/\\/\\\\/g' -e 's/&/\\&/g' -e 's/\//\\\//g')
  619. new=$(echo "$value" | sed -e 's/\\/\\\\/g' -e 's/&/\\&/g' -e 's/\//\\\//g')
  620. # Updating conf
  621. sed -i "$str_number s/$c_key='${old//\*/\\*}'/$c_key='${new//\*/\\*}'/g"\
  622. $conf
  623. }
  624. suspend_db_mysql() {
  625. # Defining vars
  626. host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
  627. for key in $host_str; do
  628. eval ${key%%=*}=${key#*=}
  629. done
  630. sql="mysql -h $HOST -u $USER -p$PASSWORD -P$PORT -e"
  631. # Checking empty vars
  632. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
  633. echo "Error: config is broken"
  634. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  635. exit $E_PARSE_ERROR
  636. fi
  637. # Checking connection
  638. $sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
  639. if [ '0' -ne "$code" ]; then
  640. echo "Error: Connect failed"
  641. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  642. exit $E_DBHOST_UNAVAILABLE
  643. fi
  644. # Suspending user
  645. $sql "REVOKE ALL ON $database.* FROM '$db_user'@'%'"
  646. $sql "FLUSH PRIVILEGES"
  647. }
  648. suspend_db_pgsql() {
  649. # Defining vars
  650. host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
  651. for key in $host_str; do
  652. eval ${key%%=*}=${key#*=}
  653. done
  654. export PGPASSWORD="$PASSWORD"
  655. sql="psql -h $HOST -U $USER -d $TPL -p $PORT -c"
  656. # Checking empty vars
  657. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
  658. echo "Error: config is broken"
  659. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  660. exit $E_PARSE_ERROR
  661. fi
  662. # Checking connection
  663. $sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
  664. if [ '0' -ne "$code" ]; then
  665. echo "Error: Connect failed"
  666. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  667. exit $E_DBHOST_UNAVAILABLE
  668. fi
  669. # Suspending user
  670. $sql "REVOKE ALL PRIVILEGES ON $database FROM $db_user">/dev/null
  671. export PGPASSWORD='pgsqk'
  672. }
  673. unsuspend_db_mysql() {
  674. # Defining vars
  675. host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
  676. for key in $host_str; do
  677. eval ${key%%=*}=${key#*=}
  678. done
  679. sql="mysql -h $HOST -u $USER -p$PASSWORD -P$PORT -e"
  680. # Checking empty vars
  681. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
  682. echo "Error: config is broken"
  683. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  684. exit $E_PARSE_ERROR
  685. fi
  686. # Checking connection
  687. $sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
  688. if [ '0' -ne "$code" ]; then
  689. echo "Error: Connect failed"
  690. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  691. exit $E_DBHOST_UNAVAILABLE
  692. fi
  693. # Unsuspending user
  694. $sql "GRANT ALL ON $database.* to '$db_user'@'%'"
  695. $sql "FLUSH PRIVILEGES"
  696. }
  697. unsuspend_db_pgsql() {
  698. # Defining vars
  699. host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
  700. for key in $host_str; do
  701. eval ${key%%=*}=${key#*=}
  702. done
  703. export PGPASSWORD="$PASSWORD"
  704. sql="psql -h $HOST -U $USER -d $TPL -p $PORT -c"
  705. # Checking empty vars
  706. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
  707. echo "Error: config is broken"
  708. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  709. exit $E_PARSE_ERROR
  710. fi
  711. # Checking connection
  712. $sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
  713. if [ '0' -ne "$code" ]; then
  714. echo "Error: Connect failed"
  715. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  716. exit $E_DBHOST_UNAVAILABLE
  717. fi
  718. # Unsuspending user
  719. $sql "GRANT ALL PRIVILEGES ON DATABASE $database TO $db_user" >/dev/null
  720. export PGPASSWORD='pgsqk'
  721. }
  722. db_clear_search() {
  723. # Defining delimeter
  724. IFS=$'\n'
  725. # Reading file line by line
  726. for line in $(grep $search_string $conf); do
  727. # Parsing key=val
  728. for key in $line; do
  729. eval ${key%%=*}=${key#*=}
  730. done
  731. # Print result line
  732. eval echo "$field"
  733. done
  734. }
  735. get_disk_db_mysql() {
  736. # Defining vars
  737. host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
  738. for key in $host_str; do
  739. eval ${key%%=*}=${key#*=}
  740. done
  741. sql="mysql -h $HOST -u $USER -p$PASSWORD -P$PORT -e"
  742. # Checking empty vars
  743. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
  744. echo "Error: config is broken"
  745. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  746. exit $E_PARSE_ERROR
  747. fi
  748. # Checking connection
  749. $sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
  750. if [ '0' -ne "$code" ]; then
  751. echo "Error: Connect failed"
  752. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  753. exit $E_DBHOST_UNAVAILABLE
  754. fi
  755. # Deleting database & checking result
  756. query="SELECT sum( data_length + index_length ) / 1024 / 1024 \"Size\"
  757. FROM information_schema.TABLES WHERE table_schema='$database'"
  758. raw_size=$($sql "$query" |tail -n 1)
  759. # Checking null output (this means error btw)
  760. if [ "$raw_size" == 'NULL' ]; then
  761. raw_size='0'
  762. fi
  763. # Rounding zero size
  764. if [ "${raw_size:0:1}" -eq '0' ]; then
  765. raw_size='1'
  766. fi
  767. # Printing round size in mb
  768. printf "%0.f\n" $raw_size
  769. }
  770. get_disk_db_pgsql() {
  771. # Defining vars
  772. host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
  773. for key in $host_str; do
  774. eval ${key%%=*}=${key#*=}
  775. done
  776. export PGPASSWORD="$PASSWORD"
  777. sql="psql -h $HOST -U $USER -d $TPL -p $PORT -c"
  778. # Checking empty vars
  779. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
  780. echo "Error: config is broken"
  781. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  782. exit $E_PARSE_ERROR
  783. fi
  784. # Checking connection
  785. $sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
  786. if [ '0' -ne "$code" ]; then
  787. echo "Error: Connect failed"
  788. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  789. exit $E_DBHOST_UNAVAILABLE
  790. fi
  791. # Raw query
  792. raq_query=$($sql "SELECT pg_database_size('$database');")
  793. raw_size=$(echo raq_query | grep -v "-" | grep -v 'row' | sed -e "/^$/d"|\
  794. awk '{print $1}')
  795. # Checking null output (this means error btw)
  796. if [ -z "$raw_size" ]; then
  797. raw_size='0'
  798. fi
  799. # Converting to MB
  800. size=$(expr $raw_size \ 1048576)
  801. # Rounding zero size
  802. if [ "$size" -eq '0' ]; then
  803. echo '1'
  804. else
  805. echo "$size"
  806. fi
  807. }