postgresql-v5.sh 13 KB

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