postgresql-v5.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. #!/usr/bin/env bash
  2. # Copyright (c) 2021-2023 tteck
  3. # Author: tteck (tteckster)
  4. # License: MIT
  5. # https://github.com/tteck/Proxmox/raw/main/LICENSE
  6. function header_info {
  7. clear
  8. cat <<"EOF"
  9. ____ __ _____ ____ __
  10. / __ \____ _____/ /_____ _________ / ___// __ \ / /
  11. / /_/ / __ \/ ___/ __/ __ / ___/ _ \\__ \/ / / / / /
  12. / ____/ /_/ (__ ) /_/ /_/ / / / __/__/ / /_/ / / /___
  13. /_/ \____/____/\__/\__, /_/v5 \___/____/\___\_\/_____/
  14. /____/
  15. EOF
  16. }
  17. header_info
  18. echo -e "Loading..."
  19. APP="PostgreSQL"
  20. var_disk="4"
  21. var_cpu="1"
  22. var_ram="1024"
  23. var_os="debian"
  24. var_version="11"
  25. NSAPP=$(echo ${APP,,} | tr -d ' ')
  26. var_install="${NSAPP}-v5-install"
  27. timezone=$(cat /etc/timezone)
  28. INTEGER='^[0-9]+([.][0-9]+)?$'
  29. YW=$(echo "\033[33m")
  30. BL=$(echo "\033[36m")
  31. RD=$(echo "\033[01;31m")
  32. BGN=$(echo "\033[4;92m")
  33. GN=$(echo "\033[1;92m")
  34. DGN=$(echo "\033[32m")
  35. CL=$(echo "\033[m")
  36. BFR="\\r\\033[K"
  37. HOLD="-"
  38. CM="${GN}✓${CL}"
  39. CROSS="${RD}✗${CL}"
  40. set -Eeuo pipefail
  41. trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
  42. function error_handler() {
  43. local exit_code="$?"
  44. local line_number="$1"
  45. local command="$2"
  46. local error_message="${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}"
  47. echo -e "\n$error_message\n"
  48. }
  49. function msg_info() {
  50. local msg="$1"
  51. echo -ne " ${HOLD} ${YW}${msg}..."
  52. }
  53. function msg_ok() {
  54. local msg="$1"
  55. echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
  56. }
  57. function msg_error() {
  58. local msg="$1"
  59. echo -e "${BFR} ${CROSS} ${RD}${msg}${CL}"
  60. }
  61. function PVE_CHECK() {
  62. if [ $(pveversion | grep -c "pve-manager/7\.[0-9]") -eq 0 ]; then
  63. echo -e "${CROSS} This version of Proxmox Virtual Environment is not supported"
  64. echo -e "Requires PVE Version 7.0 or higher"
  65. echo -e "Exiting..."
  66. sleep 2
  67. exit
  68. fi
  69. }
  70. function ARCH_CHECK() {
  71. if [ "$(dpkg --print-architecture)" != "amd64" ]; then
  72. echo -e "\n ${CROSS} This script will not work with PiMox! \n"
  73. echo -e "Exiting..."
  74. sleep 2
  75. exit
  76. fi
  77. }
  78. function default_settings() {
  79. echo -e "${DGN}Using Container Type: ${BGN}Unprivileged${CL} ${RD}NO DEVICE PASSTHROUGH${CL}"
  80. CT_TYPE="1"
  81. echo -e "${DGN}Using Root Password: ${BGN}Automatic Login${CL}"
  82. PW=""
  83. echo -e "${DGN}Using Container ID: ${BGN}$NEXTID${CL}"
  84. CT_ID=$NEXTID
  85. echo -e "${DGN}Using Hostname: ${BGN}$NSAPP${CL}"
  86. HN=$NSAPP
  87. echo -e "${DGN}Using Disk Size: ${BGN}$var_disk${CL}${DGN}GB${CL}"
  88. DISK_SIZE="$var_disk"
  89. echo -e "${DGN}Allocated Cores ${BGN}$var_cpu${CL}"
  90. CORE_COUNT="$var_cpu"
  91. echo -e "${DGN}Allocated Ram ${BGN}$var_ram${CL}"
  92. RAM_SIZE="$var_ram"
  93. echo -e "${DGN}Using Bridge: ${BGN}vmbr0${CL}"
  94. BRG="vmbr0"
  95. echo -e "${DGN}Using Static IP Address: ${BGN}dhcp${CL}"
  96. NET=dhcp
  97. echo -e "${DGN}Using Gateway Address: ${BGN}Default${CL}"
  98. GATE=""
  99. echo -e "${DGN}Disable IPv6: ${BGN}No${CL}"
  100. DISABLEIP6="no"
  101. echo -e "${DGN}Using Interface MTU Size: ${BGN}Default${CL}"
  102. MTU=""
  103. echo -e "${DGN}Using DNS Search Domain: ${BGN}Host${CL}"
  104. SD=""
  105. echo -e "${DGN}Using DNS Server Address: ${BGN}Host${CL}"
  106. NS=""
  107. echo -e "${DGN}Using MAC Address: ${BGN}Default${CL}"
  108. MAC=""
  109. echo -e "${DGN}Using VLAN Tag: ${BGN}Default${CL}"
  110. VLAN=""
  111. echo -e "${DGN}Enable Root SSH Access: ${BGN}No${CL}"
  112. SSH="no"
  113. echo -e "${DGN}Enable Verbose Mode: ${BGN}No${CL}"
  114. VERB="no"
  115. echo -e "${BL}Creating a ${APP} LXC using the above default settings${CL}"
  116. }
  117. function exit-script() {
  118. clear
  119. echo -e "⚠ User exited script \n"
  120. exit
  121. }
  122. function advanced_settings() {
  123. if CT_TYPE=$(whiptail --title "CONTAINER TYPE" --radiolist "Choose Type" 10 58 2 \
  124. "1" "Unprivileged" ON \
  125. "0" "Privileged" OFF \
  126. 3>&1 1>&2 2>&3); then
  127. echo -e "${DGN}Using Container Type: ${BGN}$CT_TYPE${CL}"
  128. else
  129. exit-script
  130. fi
  131. if PW1=$(whiptail --inputbox "\nSet Root Password (needed for root ssh access)" 9 58 --title "PASSWORD(leave blank for automatic login)" 3>&1 1>&2 2>&3); then
  132. if [ -z $PW1 ]; then
  133. PW1="Automatic Login"
  134. PW=" "
  135. else
  136. PW="-password $PW1"
  137. fi
  138. echo -e "${DGN}Using Root Password: ${BGN}$PW1${CL}"
  139. else
  140. exit-script
  141. fi
  142. if CT_ID=$(whiptail --inputbox "Set Container ID" 8 58 $NEXTID --title "CONTAINER ID" 3>&1 1>&2 2>&3); then
  143. if [ -z "$CT_ID" ]; then
  144. CT_ID="$NEXTID"
  145. echo -e "${DGN}Using Container ID: ${BGN}$CT_ID${CL}"
  146. else
  147. echo -e "${DGN}Container ID: ${BGN}$CT_ID${CL}"
  148. fi
  149. else
  150. exit
  151. fi
  152. if CT_NAME=$(whiptail --inputbox "Set Hostname" 8 58 $NSAPP --title "HOSTNAME" 3>&1 1>&2 2>&3); then
  153. if [ -z "$CT_NAME" ]; then
  154. HN="$NSAPP"
  155. else
  156. HN=$(echo ${CT_NAME,,} | tr -d ' ')
  157. fi
  158. echo -e "${DGN}Using Hostname: ${BGN}$HN${CL}"
  159. else
  160. exit-script
  161. fi
  162. if DISK_SIZE=$(whiptail --inputbox "Set Disk Size in GB" 8 58 $var_disk --title "DISK SIZE" 3>&1 1>&2 2>&3); then
  163. if [ -z "$DISK_SIZE" ]; then
  164. DISK_SIZE="$var_disk"
  165. echo -e "${DGN}Using Disk Size: ${BGN}$DISK_SIZE${CL}"
  166. else
  167. if ! [[ $DISK_SIZE =~ $INTEGER ]]; then
  168. echo -e "${RD}⚠ DISK SIZE MUST BE AN INTEGER NUMBER!${CL}"
  169. advanced_settings
  170. fi
  171. echo -e "${DGN}Using Disk Size: ${BGN}$DISK_SIZE${CL}"
  172. fi
  173. else
  174. exit-script
  175. fi
  176. if CORE_COUNT=$(whiptail --inputbox "Allocate CPU Cores" 8 58 $var_cpu --title "CORE COUNT" 3>&1 1>&2 2>&3); then
  177. if [ -z "$CORE_COUNT" ]; then
  178. CORE_COUNT="$var_cpu"
  179. echo -e "${DGN}Allocated Cores: ${BGN}$CORE_COUNT${CL}"
  180. else
  181. echo -e "${DGN}Allocated Cores: ${BGN}$CORE_COUNT${CL}"
  182. fi
  183. else
  184. exit-script
  185. fi
  186. if RAM_SIZE=$(whiptail --inputbox "Allocate RAM in MiB" 8 58 $var_ram --title "RAM" 3>&1 1>&2 2>&3); then
  187. if [ -z "$RAM_SIZE" ]; then
  188. RAM_SIZE="$var_ram"
  189. echo -e "${DGN}Allocated RAM: ${BGN}$RAM_SIZE${CL}"
  190. else
  191. echo -e "${DGN}Allocated RAM: ${BGN}$RAM_SIZE${CL}"
  192. fi
  193. else
  194. exit-script
  195. fi
  196. if BRG=$(whiptail --inputbox "Set a Bridge" 8 58 vmbr0 --title "BRIDGE" 3>&1 1>&2 2>&3); then
  197. if [ -z "$BRG" ]; then
  198. BRG="vmbr0"
  199. echo -e "${DGN}Using Bridge: ${BGN}$BRG${CL}"
  200. else
  201. echo -e "${DGN}Using Bridge: ${BGN}$BRG${CL}"
  202. fi
  203. else
  204. exit-script
  205. fi
  206. if NET=$(whiptail --inputbox "Set a Static IPv4 CIDR Address(/24)" 8 58 dhcp --title "IP ADDRESS" 3>&1 1>&2 2>&3); then
  207. if [ -z $NET ]; then
  208. NET="dhcp"
  209. echo -e "${DGN}Using IP Address: ${BGN}$NET${CL}"
  210. else
  211. echo -e "${DGN}Using IP Address: ${BGN}$NET${CL}"
  212. fi
  213. else
  214. exit-script
  215. fi
  216. if GATE1=$(whiptail --inputbox "Set a Gateway IP (mandatory if Static IP was used)" 8 58 --title "GATEWAY IP" 3>&1 1>&2 2>&3); then
  217. if [ -z $GATE1 ]; then
  218. GATE1="Default"
  219. GATE=""
  220. else
  221. GATE=",gw=$GATE1"
  222. fi
  223. echo -e "${DGN}Using Gateway IP Address: ${BGN}$GATE1${CL}"
  224. else
  225. exit-script
  226. fi
  227. if (whiptail --defaultno --title "IPv6" --yesno "Disable IPv6?" 10 58); then
  228. DISABLEIP6="yes"
  229. else
  230. DISABLEIP6="no"
  231. fi
  232. echo -e "${DGN}Disable IPv6: ${BGN}$DISABLEIP6${CL}"
  233. if MTU1=$(whiptail --inputbox "Set Interface MTU Size (leave blank for default)" 8 58 --title "MTU SIZE" 3>&1 1>&2 2>&3); then
  234. if [ -z $MTU1 ]; then
  235. MTU1="Default"
  236. MTU=""
  237. else
  238. MTU=",mtu=$MTU1"
  239. fi
  240. echo -e "${DGN}Using Interface MTU Size: ${BGN}$MTU1${CL}"
  241. else
  242. exit-script
  243. fi
  244. if SD=$(whiptail --inputbox "Set a DNS Search Domain (leave blank for HOST)" 8 58 --title "DNS Search Domain" 3>&1 1>&2 2>&3); then
  245. if [ -z $SD ]; then
  246. SX=Host
  247. SD=""
  248. else
  249. SX=$SD
  250. SD="-searchdomain=$SD"
  251. fi
  252. echo -e "${DGN}Using DNS Search Domain: ${BGN}$SX${CL}"
  253. else
  254. exit-script
  255. fi
  256. if NX=$(whiptail --inputbox "Set a DNS Server IP (leave blank for HOST)" 8 58 --title "DNS SERVER IP" 3>&1 1>&2 2>&3); then
  257. if [ -z $NX ]; then
  258. NX=Host
  259. NS=""
  260. else
  261. NS="-nameserver=$NX"
  262. fi
  263. echo -e "${DGN}Using DNS Server IP Address: ${BGN}$NX${CL}"
  264. else
  265. exit-script
  266. fi
  267. if MAC1=$(whiptail --inputbox "Set a MAC Address(leave blank for default)" 8 58 --title "MAC ADDRESS" 3>&1 1>&2 2>&3); then
  268. if [ -z $MAC1 ]; then
  269. MAC1="Default"
  270. MAC=""
  271. else
  272. MAC=",hwaddr=$MAC1"
  273. echo -e "${DGN}Using MAC Address: ${BGN}$MAC1${CL}"
  274. fi
  275. else
  276. exit-script
  277. fi
  278. if VLAN1=$(whiptail --inputbox "Set a Vlan(leave blank for default)" 8 58 --title "VLAN" 3>&1 1>&2 2>&3); then
  279. if [ -z $VLAN1 ]; then
  280. VLAN1="Default"
  281. VLAN=""
  282. else
  283. VLAN=",tag=$VLAN1"
  284. fi
  285. echo -e "${DGN}Using Vlan: ${BGN}$VLAN1${CL}"
  286. else
  287. exit-script
  288. fi
  289. if (whiptail --defaultno --title "SSH ACCESS" --yesno "Enable Root SSH Access?" 10 58); then
  290. SSH="yes"
  291. else
  292. SSH="no"
  293. fi
  294. echo -e "${DGN}Enable Root SSH Access: ${BGN}$SSH${CL}"
  295. if (whiptail --defaultno --title "VERBOSE MODE" --yesno "Enable Verbose Mode?" 10 58); then
  296. VERB="yes"
  297. else
  298. VERB="no"
  299. fi
  300. echo -e "${DGN}Enable Verbose Mode: ${BGN}$VERB${CL}"
  301. if (whiptail --title "ADVANCED SETTINGS COMPLETE" --yesno "Ready to create ${APP} LXC?" 10 58); then
  302. echo -e "${RD}Creating a ${APP} LXC using the above advanced settings${CL}"
  303. else
  304. clear
  305. header_info
  306. echo -e "${RD}Using Advanced Settings${CL}"
  307. advanced_settings
  308. fi
  309. }
  310. function install_script() {
  311. ARCH_CHECK
  312. PVE_CHECK
  313. NEXTID=$(pvesh get /cluster/nextid)
  314. header_info
  315. if (whiptail --title "SETTINGS" --yesno "Use Default Settings?" --no-button Advanced 10 58); then
  316. header_info
  317. echo -e "${BL}Using Default Settings${CL}"
  318. default_settings
  319. else
  320. header_info
  321. echo -e "${RD}Using Advanced Settings${CL}"
  322. advanced_settings
  323. fi
  324. }
  325. function update_script() {
  326. header_info
  327. msg_info "Updating ${APP} LXC"
  328. apt-get update &>/dev/null
  329. apt-get -y upgrade &>/dev/null
  330. msg_ok "Updated ${APP} LXC"
  331. msg_ok "Update Successfull"
  332. exit
  333. }
  334. if command -v pveversion >/dev/null 2>&1; then
  335. if ! (whiptail --title "${APP} LXC" --yesno "This will create a New ${APP} LXC. Proceed?" 10 58); then
  336. clear
  337. echo -e "⚠ User exited script \n"
  338. exit
  339. fi
  340. install_script
  341. fi
  342. if ! command -v pveversion >/dev/null 2>&1 && [[ ! -f /etc/apt/sources.list.d/pgdg.list ]]; then
  343. msg_error "No ${APP} Installation Found!"
  344. exit
  345. fi
  346. if ! command -v pveversion >/dev/null 2>&1; then
  347. if ! (whiptail --title "${APP} LXC UPDATE" --yesno "This will update ${APP} LXC. Proceed?" 10 58); then
  348. clear
  349. echo -e "⚠ User exited script \n"
  350. exit
  351. fi
  352. update_script
  353. fi
  354. if [ "$VERB" == "yes" ]; then set -x; fi
  355. if [ "$CT_TYPE" == "1" ]; then
  356. FEATURES="nesting=1,keyctl=1"
  357. else
  358. FEATURES="nesting=1"
  359. fi
  360. TEMP_DIR=$(mktemp -d)
  361. pushd $TEMP_DIR >/dev/null
  362. export tz=$timezone
  363. export DISABLEIPV6=$DISABLEIP6
  364. export APPLICATION=$APP
  365. export VERBOSE=$VERB
  366. export SSH_ROOT=${SSH}
  367. export CTID=$CT_ID
  368. export PCT_OSTYPE=$var_os
  369. export PCT_OSVERSION=$var_version
  370. export PCT_DISK_SIZE=$DISK_SIZE
  371. export PCT_OPTIONS="
  372. -features $FEATURES
  373. -hostname $HN
  374. $SD
  375. $NS
  376. -net0 name=eth0,bridge=$BRG$MAC,ip=$NET$GATE$VLAN$MTU
  377. -onboot 1
  378. -cores $CORE_COUNT
  379. -memory $RAM_SIZE
  380. -unprivileged $CT_TYPE
  381. $PW
  382. "
  383. bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/ct/create_lxc.sh)" || exit
  384. msg_info "Starting LXC Container"
  385. pct start $CTID
  386. msg_ok "Started LXC Container"
  387. lxc-attach -n $CTID -- bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/install/$var_install.sh)" || exit
  388. IP=$(pct exec $CTID ip a s dev eth0 | awk '/inet / {print $2}' | cut -d/ -f1)
  389. pct set $CTID -description "# ${APP} LXC
  390. ### https://tteck.github.io/Proxmox/
  391. <a href='https://ko-fi.com/D1D7EP4GF'><img src='https://img.shields.io/badge/☕-Buy me a coffee-red' /></a>"
  392. msg_ok "Completed Successfully!\n"