db_func.sh 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  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 CHARACTER SET $encoding" > /dev/null 2>&1
  315. code="$?"
  316. if [ '0' -ne "$code" ]; then
  317. echo "Error: Connect failed"
  318. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  319. exit $E_DBHOST_UNAVAILABLE
  320. fi
  321. # Adding user with password (% will give access to db from any ip)
  322. $sql "GRANT ALL ON $database.* TO '$db_user'@'%' \
  323. IDENTIFIED BY '$db_password'"
  324. # Adding grant for localhost (% doesn't do that )
  325. if [ "$host" = 'localhost' ]; then
  326. $sql "GRANT ALL ON $database.* TO '$db_user'@'localhost' \
  327. IDENTIFIED BY '$db_password'"
  328. fi
  329. # Flushing priveleges
  330. $sql "FLUSH PRIVILEGES"
  331. }
  332. create_db_pgsql() {
  333. # Defining vars
  334. host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
  335. for key in $host_str; do
  336. eval ${key%%=*}=${key#*=}
  337. done
  338. export PGPASSWORD="$PASSWORD"
  339. sql="psql -h $HOST -U $USER -p $PORT -c"
  340. # Checking empty vars
  341. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
  342. echo "Error: config is broken"
  343. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  344. exit $E_PARSE_ERROR
  345. fi
  346. # Checking connection
  347. $sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
  348. if [ '0' -ne "$code" ]; then
  349. echo "Error: Connect failed"
  350. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  351. exit $E_DBHOST_UNAVAILABLE
  352. fi
  353. # Adding new role
  354. $sql "CREATE ROLE $db_user WITH LOGIN PASSWORD '$db_password'" >/dev/null
  355. code=$?
  356. if [ '0' -ne "$code" ]; then
  357. echo "Error: Connect failed"
  358. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  359. exit $E_DBHOST_UNAVAILABLE
  360. fi
  361. # Adding database & checking result
  362. sql_q="CREATE DATABASE $database OWNER $db_user" > /dev/null
  363. if [ "$TPL" = 'template0' ]; then
  364. sql_q="$sql_q ENCODING '$encoding' TEMPLATE $TPL" > /dev/null
  365. else
  366. sql_q="$sql_q TEMPLATE $TPL" > /dev/null
  367. fi
  368. $sql "$sql_q" >/dev/null
  369. $sql "GRANT ALL PRIVILEGES ON DATABASE $database TO $db_user" > /dev/null
  370. $sql "GRANT CONNECT ON DATABASE template1 to $db_user" > /dev/null
  371. export PGPASSWORD='pgsql'
  372. }
  373. is_db_host_new() {
  374. if [ -e "$V_DB/$type.conf" ]; then
  375. check_host=$(grep "HOST='$host'" $V_DB/$type.conf)
  376. if [ ! -z "$check_host" ]; then
  377. echo "Error: db host exist"
  378. log_event 'debug' "$E_DBHOST_EXIST $V_EVENT"
  379. exit $E_DBHOST_EXIST
  380. fi
  381. fi
  382. }
  383. is_mysql_host_alive() {
  384. # Checking connection
  385. sql="mysql -h $host -u $db_user -p$db_password -P$port -e"
  386. $sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
  387. if [ '0' -ne "$code" ]; then
  388. echo "Error: Connect failed"
  389. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  390. exit $E_DBHOST_UNAVAILABLE
  391. fi
  392. }
  393. is_pgsql_host_alive() {
  394. # Checking connection
  395. export PGPASSWORD="$db_password"
  396. sql="psql -h $host -U $db_user -p $port -c "
  397. $sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
  398. if [ '0' -ne "$code" ]; then
  399. echo "Error: Connect failed"
  400. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  401. exit $E_DBHOST_UNAVAILABLE
  402. fi
  403. }
  404. is_db_suspended() {
  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 suspended"
  410. log_event 'debug' "$E_DB_SUSPENDED $V_EVENT"
  411. exit $E_DB_SUSPENDED
  412. fi
  413. }
  414. is_db_unsuspended() {
  415. config="$V_USERS/$user/db.conf"
  416. check_db=$(grep "DB='$database'" $config|grep "SUSPEND='yes'")
  417. # Checking result
  418. if [ -z "$check_db" ]; then
  419. echo "Error: db unsuspended"
  420. log_event 'debug' "$E_DB_UNSUSPENDED $V_EVENT"
  421. exit $E_DB_UNSUSPENDED
  422. fi
  423. }
  424. is_db_user_valid() {
  425. config="$V_USERS/$user/db.conf"
  426. check_db=$(grep "DB='$database'" $config|grep "USER='$db_user'")
  427. # Checking result
  428. if [ -z "$check_db" ]; then
  429. echo "Error: dbuser not exist"
  430. log_event 'debug' "$E_DBUSER_NOTEXIST $V_EVENT"
  431. exit $E_DBUSER_NOTEXIST
  432. fi
  433. }
  434. change_db_mysql_password() {
  435. # Defining vars
  436. host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
  437. for key in $host_str; do
  438. eval ${key%%=*}=${key#*=}
  439. done
  440. sql="mysql -h $HOST -u $USER -p$PASSWORD -P$PORT -e"
  441. # Checking empty vars
  442. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
  443. echo "Error: config is broken"
  444. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  445. exit $E_PARSE_ERROR
  446. fi
  447. # Checking connection
  448. $sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
  449. if [ '0' -ne "$code" ]; then
  450. echo "Error: Connect failed"
  451. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  452. exit $E_DBHOST_UNAVAILABLE
  453. fi
  454. # Changing user password
  455. $sql "GRANT ALL ON $database.* TO '$db_user'@'%' \
  456. IDENTIFIED BY '$db_password'"
  457. $sql "GRANT ALL ON $database.* TO '$db_user'@'localhost' \
  458. IDENTIFIED BY '$db_password'"
  459. #$sql "SET PASSWORD FOR '$db_user'@'%' = PASSWORD('$db_password');"
  460. $sql "FLUSH PRIVILEGES"
  461. }
  462. change_db_pgsql_password() {
  463. # Defining vars
  464. host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
  465. for key in $host_str; do
  466. eval ${key%%=*}=${key#*=}
  467. done
  468. export PGPASSWORD="$PASSWORD"
  469. sql="psql -h $HOST -U $USER -p $PORT -c"
  470. # Checking empty vars
  471. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
  472. echo "Error: config is broken"
  473. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  474. exit $E_PARSE_ERROR
  475. fi
  476. # Checking connection
  477. $sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
  478. if [ '0' -ne "$code" ]; then
  479. echo "Error: Connect failed"
  480. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  481. exit $E_DBHOST_UNAVAILABLE
  482. fi
  483. $sql "ALTER ROLE $db_user WITH LOGIN PASSWORD '$db_password'" >/dev/null
  484. export PGPASSWORD='pgsql'
  485. }
  486. get_db_value() {
  487. # Defining vars
  488. key="$1"
  489. db_str=$(grep "DB='$database'" $V_USERS/$user/db.conf)
  490. # Parsing key=value
  491. for keys in $db_str; do
  492. eval ${keys%%=*}=${keys#*=}
  493. done
  494. # Self reference
  495. eval value="$key"
  496. # Print value
  497. echo "$value"
  498. }
  499. del_db_mysql() {
  500. # Defining vars
  501. host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
  502. for key in $host_str; do
  503. eval ${key%%=*}=${key#*=}
  504. done
  505. sql="mysql -h $HOST -u $USER -p$PASSWORD -P$PORT -e"
  506. # Checking empty vars
  507. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
  508. echo "Error: config is broken"
  509. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  510. exit $E_PARSE_ERROR
  511. fi
  512. # Checking connection
  513. $sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
  514. if [ '0' -ne "$code" ]; then
  515. echo "Error: Connect failed"
  516. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  517. exit $E_DBHOST_UNAVAILABLE
  518. fi
  519. # Deleting database & checking result
  520. $sql "DROP DATABASE $database" >/dev/null 2>&1;code="$?"
  521. if [ '0' -ne "$code" ]; then
  522. echo "Error: Connect failed"
  523. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  524. exit $E_DBHOST_UNAVAILABLE
  525. fi
  526. # Deleting user
  527. check_users=$(grep "USER='$db_user'" $V_USERS/$user/db.conf |wc -l)
  528. if [ 1 -ge "$check_users" ]; then
  529. $sql "DROP USER '$db_user'@'%'"
  530. if [ "$host" = 'localhost' ]; then
  531. $sql "DROP USER '$db_user'@'localhost'"
  532. fi
  533. else
  534. $sql "REVOKE ALL ON $database.* from '$db_user'@'%'"
  535. if [ "$host" = 'localhost' ]; then
  536. $sql "REVOKE ALL ON $database.* from '$db_user'@'localhost'"
  537. fi
  538. fi
  539. $sql "FLUSH PRIVILEGES"
  540. }
  541. del_db_pgsql() {
  542. # Defining vars
  543. host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
  544. for key in $host_str; do
  545. eval ${key%%=*}=${key#*=}
  546. done
  547. export PGPASSWORD="$PASSWORD"
  548. sql="psql -h $HOST -U $USER -p $PORT -c"
  549. # Checking empty vars
  550. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
  551. echo "Error: config is broken"
  552. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  553. exit $E_PARSE_ERROR
  554. fi
  555. # Checking connection
  556. $sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
  557. if [ '0' -ne "$code" ]; then
  558. echo "Error: Connect failed"
  559. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  560. exit $E_DBHOST_UNAVAILABLE
  561. fi
  562. # Deleting database & checking result
  563. $sql "REVOKE ALL PRIVILEGES ON DATABASE $database FROM $db_user">/dev/null
  564. $sql "DROP DATABASE $database" >/dev/null 2>&1;code="$?"
  565. if [ '0' -ne "$code" ]; then
  566. echo "Error: Connect failed"
  567. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  568. exit $E_DBHOST_UNAVAILABLE
  569. fi
  570. # Deleting user
  571. check_users=$(grep "USER='$db_user'" $V_USERS/$user/db.conf |wc -l)
  572. if [ 1 -ge "$check_users" ]; then
  573. $sql "REVOKE CONNECT ON DATABASE template1 FROM $db_user" >/dev/null
  574. $sql "DROP ROLE $db_user" >/dev/null
  575. fi
  576. export PGPASSWORD='pgsql'
  577. }
  578. del_db_vesta() {
  579. conf="$V_USERS/$user/db.conf"
  580. # Parsing domains
  581. string=$( grep -n "DB='$database'" $conf | cut -f 1 -d : )
  582. if [ -z "$string" ]; then
  583. echo "Error: parse error"
  584. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  585. exit $E_PARSE_ERROR
  586. fi
  587. sed -i "$string d" $conf
  588. }
  589. dump_db_mysql() {
  590. # Defining vars
  591. host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
  592. for key in $host_str; do
  593. eval ${key%%=*}=${key#*=}
  594. done
  595. sql="mysql -h $HOST -u $USER -p$PASSWORD -P$PORT -e"
  596. dumper="mysqldump -h $HOST -u $USER -p$PASSWORD -P$PORT -r"
  597. # Checking empty vars
  598. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
  599. echo "Error: config is broken"
  600. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  601. exit $E_PARSE_ERROR
  602. fi
  603. # Checking connection
  604. $sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
  605. if [ '0' -ne "$code" ]; then
  606. echo "Error: Connect failed"
  607. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  608. exit $E_DBHOST_UNAVAILABLE
  609. fi
  610. # Dumping database
  611. $dumper $dump $database
  612. # Dumping user grants
  613. $sql "SHOW GRANTS FOR $db_user@localhost" | grep -v "Grants for" > $grants
  614. $sql "SHOW GRANTS FOR $db_user@'%'" | grep -v "Grants for" >> $grants
  615. }
  616. dump_db_pgsql() {
  617. # Defining vars
  618. host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
  619. for key in $host_str; do
  620. eval ${key%%=*}=${key#*=}
  621. done
  622. export PGPASSWORD="$PASSWORD"
  623. sql="psql -h $HOST -U $USER -p $PORT -c"
  624. dumper="pg_dump -h $HOST -U $USER -p $PORT -c -d -O -x -i -f"
  625. # Checking empty vars
  626. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
  627. echo "Error: config is broken"
  628. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  629. exit $E_PARSE_ERROR
  630. fi
  631. # Checking connection
  632. $sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
  633. if [ '0' -ne "$code" ]; then
  634. echo "Error: Connect failed"
  635. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  636. exit $E_DBHOST_UNAVAILABLE
  637. fi
  638. # Dumping database
  639. $dumper $dump $database
  640. # Dumping user grants
  641. md5=$($sql "SELECT rolpassword FROM pg_authid WHERE rolname='$db_user';")
  642. md5=$(echo "$md5" | head -n 1 | cut -f 2 -d ' ')
  643. pw_str="UPDATE pg_authid SET rolpassword='$md5' WHERE rolname='$db_user';"
  644. gr_str="GRANT ALL PRIVILEGES ON DATABASE $database to '$db_user'"
  645. echo -e "$pw_str\n$gr_str" >> $grants
  646. export PGPASSWORD='pgsql'
  647. }
  648. is_db_host_free() {
  649. # Defining vars
  650. host_str=$(grep "HOST='$host'" $V_DB/$type.conf)
  651. for key in $host_str; do
  652. eval ${key%%=*}=${key#*=}
  653. done
  654. # Checking U_DB_BASES
  655. if [ 0 -ne "$U_DB_BASES" ]; then
  656. echo "Error: host is used"
  657. log_event 'debug' "$E_DBHOST_BUSY $V_EVENT"
  658. exit $E_DBHOST_BUSY
  659. fi
  660. }
  661. del_dbhost_vesta() {
  662. conf="$V_DB/$type.conf"
  663. # Parsing domains
  664. string=$( grep -n "HOST='$host'" $conf | cut -f 1 -d : )
  665. if [ -z "$string" ]; then
  666. echo "Error: parse error"
  667. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  668. exit $E_PARSE_ERROR
  669. fi
  670. sed -i "$string d" $conf
  671. }
  672. update_db_base_value() {
  673. key="$1"
  674. value="$2"
  675. # Defining conf
  676. conf="$V_USERS/$user/db.conf"
  677. # Parsing conf
  678. db_str=$(grep -n "DB='$database'" $conf)
  679. str_number=$(echo $db_str | cut -f 1 -d ':')
  680. str=$(echo $db_str | cut -f 2 -d ':')
  681. # Reading key=values
  682. for keys in $str; do
  683. eval ${keys%%=*}=${keys#*=}
  684. done
  685. # Defining clean key
  686. c_key=$(echo "${key//$/}")
  687. eval old="${key}"
  688. # Escaping slashes
  689. old=$(echo "$old" | sed -e 's/\\/\\\\/g' -e 's/&/\\&/g' -e 's/\//\\\//g')
  690. new=$(echo "$value" | sed -e 's/\\/\\\\/g' -e 's/&/\\&/g' -e 's/\//\\\//g')
  691. # Updating conf
  692. sed -i "$str_number s/$c_key='${old//\*/\\*}'/$c_key='${new//\*/\\*}'/g"\
  693. $conf
  694. }
  695. suspend_db_mysql() {
  696. # Defining vars
  697. host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
  698. for key in $host_str; do
  699. eval ${key%%=*}=${key#*=}
  700. done
  701. sql="mysql -h $HOST -u $USER -p$PASSWORD -P$PORT -e"
  702. # Checking empty vars
  703. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
  704. echo "Error: config is broken"
  705. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  706. exit $E_PARSE_ERROR
  707. fi
  708. # Checking connection
  709. $sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
  710. if [ '0' -ne "$code" ]; then
  711. echo "Error: Connect failed"
  712. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  713. exit $E_DBHOST_UNAVAILABLE
  714. fi
  715. # Suspending user
  716. $sql "REVOKE ALL ON $database.* FROM '$db_user'@'%'"
  717. $sql "FLUSH PRIVILEGES"
  718. }
  719. suspend_db_pgsql() {
  720. # Defining vars
  721. host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
  722. for key in $host_str; do
  723. eval ${key%%=*}=${key#*=}
  724. done
  725. export PGPASSWORD="$PASSWORD"
  726. sql="psql -h $HOST -U $USER -p $PORT -c"
  727. # Checking empty vars
  728. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
  729. echo "Error: config is broken"
  730. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  731. exit $E_PARSE_ERROR
  732. fi
  733. # Checking connection
  734. $sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
  735. if [ '0' -ne "$code" ]; then
  736. echo "Error: Connect failed"
  737. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  738. exit $E_DBHOST_UNAVAILABLE
  739. fi
  740. # Suspending user
  741. $sql "REVOKE ALL PRIVILEGES ON $database FROM $db_user">/dev/null
  742. export PGPASSWORD='pgsql'
  743. }
  744. unsuspend_db_mysql() {
  745. # Defining vars
  746. host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
  747. for key in $host_str; do
  748. eval ${key%%=*}=${key#*=}
  749. done
  750. sql="mysql -h $HOST -u $USER -p$PASSWORD -P$PORT -e"
  751. # Checking empty vars
  752. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
  753. echo "Error: config is broken"
  754. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  755. exit $E_PARSE_ERROR
  756. fi
  757. # Checking connection
  758. $sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
  759. if [ '0' -ne "$code" ]; then
  760. echo "Error: Connect failed"
  761. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  762. exit $E_DBHOST_UNAVAILABLE
  763. fi
  764. # Unsuspending user
  765. $sql "GRANT ALL ON $database.* to '$db_user'@'%'"
  766. $sql "FLUSH PRIVILEGES"
  767. }
  768. unsuspend_db_pgsql() {
  769. # Defining vars
  770. host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
  771. for key in $host_str; do
  772. eval ${key%%=*}=${key#*=}
  773. done
  774. export PGPASSWORD="$PASSWORD"
  775. sql="psql -h $HOST -U $USER -p $PORT -c"
  776. # Checking empty vars
  777. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
  778. echo "Error: config is broken"
  779. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  780. exit $E_PARSE_ERROR
  781. fi
  782. # Checking connection
  783. $sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
  784. if [ '0' -ne "$code" ]; then
  785. echo "Error: Connect failed"
  786. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  787. exit $E_DBHOST_UNAVAILABLE
  788. fi
  789. # Unsuspending user
  790. $sql "GRANT ALL PRIVILEGES ON DATABASE $database TO $db_user" >/dev/null
  791. export PGPASSWORD='pgsql'
  792. }
  793. db_clear_search() {
  794. # Defining delimeter
  795. IFS=$'\n'
  796. # Reading file line by line
  797. for line in $(grep $search_string $conf); do
  798. # Parsing key=val
  799. for key in $line; do
  800. eval ${key%%=*}=${key#*=}
  801. done
  802. # Print result line
  803. eval echo "$field"
  804. done
  805. }
  806. get_disk_db_mysql() {
  807. # Defining vars
  808. host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
  809. for key in $host_str; do
  810. eval ${key%%=*}=${key#*=}
  811. done
  812. sql="mysql -h $HOST -u $USER -p$PASSWORD -P$PORT -e"
  813. # Checking empty vars
  814. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
  815. echo "Error: config is broken"
  816. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  817. exit $E_PARSE_ERROR
  818. fi
  819. # Checking connection
  820. $sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
  821. if [ '0' -ne "$code" ]; then
  822. echo "Error: Connect failed"
  823. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  824. exit $E_DBHOST_UNAVAILABLE
  825. fi
  826. # Deleting database & checking result
  827. query="SELECT sum( data_length + index_length ) / 1024 / 1024 \"Size\"
  828. FROM information_schema.TABLES WHERE table_schema='$database'"
  829. raw_size=$($sql "$query" |tail -n 1)
  830. # Checking null output (this means error btw)
  831. if [ "$raw_size" == 'NULL' ]; then
  832. raw_size='0'
  833. fi
  834. # Rounding zero size
  835. if [ "${raw_size:0:1}" -eq '0' ]; then
  836. raw_size='1'
  837. fi
  838. # Printing round size in mb
  839. printf "%0.f\n" $raw_size
  840. }
  841. get_disk_db_pgsql() {
  842. # Defining vars
  843. host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
  844. for key in $host_str; do
  845. eval ${key%%=*}=${key#*=}
  846. done
  847. export PGPASSWORD="$PASSWORD"
  848. sql="psql -h $HOST -U $USER -p $PORT -c"
  849. # Checking empty vars
  850. if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
  851. echo "Error: config is broken"
  852. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  853. exit $E_PARSE_ERROR
  854. fi
  855. # Checking connection
  856. $sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
  857. if [ '0' -ne "$code" ]; then
  858. echo "Error: Connect failed"
  859. log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
  860. exit $E_DBHOST_UNAVAILABLE
  861. fi
  862. # Raw query
  863. raq_query=$($sql "SELECT pg_database_size('$database');")
  864. raw_size=$(echo "$raq_query" | grep -v "-" | grep -v 'row' |\
  865. sed -e "/^$/d" |grep -v "pg_database_size" | awk '{print $1}')
  866. # Checking null output (this means error btw)
  867. if [ -z "$raw_size" ]; then
  868. raw_size='0'
  869. fi
  870. # Converting to MB
  871. size=$(expr $raw_size / 1048576)
  872. # Rounding zero size
  873. if [ "$size" -eq '0' ]; then
  874. echo '1'
  875. else
  876. echo "$size"
  877. fi
  878. }