postgresql-v5.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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=$(timedatectl | grep "Time zone" | awk '{print $3}')
  28. INTEGER='^[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 advanced_settings() {
  118. CT_TYPE=$(whiptail --title "CONTAINER TYPE" --radiolist --cancel-button Exit-Script "Choose Type" 10 58 2 \
  119. "1" "Unprivileged" ON \
  120. "0" "Privileged" OFF \
  121. 3>&1 1>&2 2>&3)
  122. exitstatus=$?
  123. if [ $exitstatus = 0 ]; then
  124. echo -e "${DGN}Using Container Type: ${BGN}$CT_TYPE${CL}"
  125. fi
  126. 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)
  127. exitstatus=$?
  128. if [ $exitstatus = 0 ]; then
  129. if [ -z $PW1 ]; then
  130. PW1="Automatic Login" PW=" "
  131. echo -e "${DGN}Using Root Password: ${BGN}$PW1${CL}"
  132. else
  133. PW="-password $PW1"
  134. echo -e "${DGN}Using Root Password: ${BGN}$PW1${CL}"
  135. fi
  136. fi
  137. CT_ID=$(whiptail --inputbox "Set Container ID" 8 58 $NEXTID --title "CONTAINER ID" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  138. exitstatus=$?
  139. if [ -z $CT_ID ]; then
  140. CT_ID="$NEXTID"
  141. echo -e "${DGN}Container ID: ${BGN}$CT_ID${CL}"
  142. else
  143. if [ $exitstatus = 0 ]; then echo -e "${DGN}Using Container ID: ${BGN}$CT_ID${CL}"; fi
  144. fi
  145. CT_NAME=$(whiptail --inputbox "Set Hostname" 8 58 $NSAPP --title "HOSTNAME" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  146. exitstatus=$?
  147. if [ -z $CT_NAME ]; then
  148. HN="$NSAPP"
  149. echo -e "${DGN}Using Hostname: ${BGN}$HN${CL}"
  150. else
  151. if [ $exitstatus = 0 ]; then
  152. HN=$(echo ${CT_NAME,,} | tr -d ' ')
  153. echo -e "${DGN}Using Hostname: ${BGN}$HN${CL}"
  154. fi
  155. fi
  156. 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)
  157. exitstatus=$?
  158. if [ -z $DISK_SIZE ]; then
  159. DISK_SIZE="$var_disk"
  160. echo -e "${DGN}Using Disk Size: ${BGN}$DISK_SIZE${CL}"
  161. else
  162. if [ $exitstatus = 0 ]; then echo -e "${DGN}Using Disk Size: ${BGN}$DISK_SIZE${CL}"; fi
  163. if ! [[ $DISK_SIZE =~ $INTEGER ]]; then
  164. echo -e "${RD}⚠ DISK SIZE MUST BE A INTEGER NUMBER!${CL}"
  165. advanced_settings
  166. fi
  167. fi
  168. CORE_COUNT=$(whiptail --inputbox "Allocate CPU Cores" 8 58 $var_cpu --title "CORE COUNT" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  169. exitstatus=$?
  170. if [ -z $CORE_COUNT ]; then
  171. CORE_COUNT="$var_cpu"
  172. echo -e "${DGN}Allocated Cores: ${BGN}$CORE_COUNT${CL}"
  173. else
  174. if [ $exitstatus = 0 ]; then echo -e "${DGN}Allocated Cores: ${BGN}$CORE_COUNT${CL}"; fi
  175. fi
  176. RAM_SIZE=$(whiptail --inputbox "Allocate RAM in MiB" 8 58 $var_ram --title "RAM" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  177. exitstatus=$?
  178. if [ -z $RAM_SIZE ]; then
  179. RAM_SIZE="$var_ram"
  180. echo -e "${DGN}Allocated RAM: ${BGN}$RAM_SIZE${CL}"
  181. else
  182. if [ $exitstatus = 0 ]; then echo -e "${DGN}Allocated RAM: ${BGN}$RAM_SIZE${CL}"; fi
  183. fi
  184. BRG=$(whiptail --inputbox "Set a Bridge" 8 58 vmbr0 --title "BRIDGE" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  185. exitstatus=$?
  186. if [ -z $BRG ]; then
  187. BRG="vmbr0"
  188. echo -e "${DGN}Using Bridge: ${BGN}$BRG${CL}"
  189. else
  190. if [ $exitstatus = 0 ]; then echo -e "${DGN}Using Bridge: ${BGN}$BRG${CL}"; fi
  191. fi
  192. 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)
  193. exitstatus=$?
  194. if [ -z $NET ]; then
  195. NET="dhcp"
  196. echo -e "${DGN}Using IP Address: ${BGN}$NET${CL}"
  197. else
  198. if [ $exitstatus = 0 ]; then echo -e "${DGN}Using IP Address: ${BGN}$NET${CL}"; fi
  199. fi
  200. 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)
  201. exitstatus=$?
  202. if [ $exitstatus = 0 ]; then
  203. if [ -z $GATE1 ]; then
  204. GATE1="Default" GATE=""
  205. echo -e "${DGN}Using Gateway IP Address: ${BGN}$GATE1${CL}"
  206. else
  207. GATE=",gw=$GATE1"
  208. echo -e "${DGN}Using Gateway IP Address: ${BGN}$GATE1${CL}"
  209. fi
  210. fi
  211. if (whiptail --defaultno --title "IPv6" --yesno "Disable IPv6?" 10 58); then
  212. echo -e "${DGN}Disable IPv6: ${BGN}Yes${CL}"
  213. DISABLEIP6="yes"
  214. else
  215. echo -e "${DGN}Disable IPv6: ${BGN}No${CL}"
  216. DISABLEIP6="no"
  217. fi
  218. 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)
  219. exitstatus=$?
  220. if [ $exitstatus = 0 ]; then
  221. if [ -z $MTU1 ]; then
  222. MTU1="Default" MTU=""
  223. echo -e "${DGN}Using Interface MTU Size: ${BGN}$MTU1${CL}"
  224. else
  225. MTU=",mtu=$MTU1"
  226. echo -e "${DGN}Using Interface MTU Size: ${BGN}$MTU1${CL}"
  227. fi
  228. fi
  229. 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)
  230. exitstatus=$?
  231. if [ $exitstatus = 0 ]; then
  232. if [ -z $SD ]; then
  233. SD=""
  234. echo -e "${DGN}Using DNS Search Domain: ${BGN}Host${CL}"
  235. else
  236. SX=$SD
  237. SD="-searchdomain=$SD"
  238. echo -e "${DGN}Using DNS Search Domain: ${BGN}$SX${CL}"
  239. fi
  240. fi
  241. 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)
  242. exitstatus=$?
  243. if [ $exitstatus = 0 ]; then
  244. if [ -z $NS ]; then
  245. NS=""
  246. echo -e "${DGN}Using DNS Server IP Address: ${BGN}Host${CL}"
  247. else
  248. NX=$NS
  249. NS="-nameserver=$NS"
  250. echo -e "${DGN}Using DNS Server IP Address: ${BGN}$NX${CL}"
  251. fi
  252. fi
  253. 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)
  254. exitstatus=$?
  255. if [ $exitstatus = 0 ]; then
  256. if [ -z $MAC1 ]; then
  257. MAC1="Default" MAC=""
  258. echo -e "${DGN}Using MAC Address: ${BGN}$MAC1${CL}"
  259. else
  260. MAC=",hwaddr=$MAC1"
  261. echo -e "${DGN}Using MAC Address: ${BGN}$MAC1${CL}"
  262. fi
  263. fi
  264. VLAN1=$(whiptail --inputbox "Set a Vlan(leave blank for default)" 8 58 --title "VLAN" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  265. exitstatus=$?
  266. if [ $exitstatus = 0 ]; then
  267. if [ -z $VLAN1 ]; then
  268. VLAN1="Default" VLAN=""
  269. echo -e "${DGN}Using Vlan: ${BGN}$VLAN1${CL}"
  270. else
  271. VLAN=",tag=$VLAN1"
  272. echo -e "${DGN}Using Vlan: ${BGN}$VLAN1${CL}"
  273. fi
  274. fi
  275. if (whiptail --defaultno --title "SSH ACCESS" --yesno "Enable Root SSH Access?" 10 58); then
  276. echo -e "${DGN}Enable Root SSH Access: ${BGN}Yes${CL}"
  277. SSH="yes"
  278. else
  279. echo -e "${DGN}Enable Root SSH Access: ${BGN}No${CL}"
  280. SSH="no"
  281. fi
  282. if (whiptail --defaultno --title "VERBOSE MODE" --yesno "Enable Verbose Mode?" 10 58); then
  283. echo -e "${DGN}Enable Verbose Mode: ${BGN}Yes${CL}"
  284. VERB="yes"
  285. else
  286. echo -e "${DGN}Enable Verbose Mode: ${BGN}No${CL}"
  287. VERB="no"
  288. fi
  289. if (whiptail --title "ADVANCED SETTINGS COMPLETE" --yesno "Ready to create ${APP} LXC?" --no-button Do-Over 10 58); then
  290. echo -e "${RD}Creating a ${APP} LXC using the above advanced settings${CL}"
  291. else
  292. clear
  293. header_info
  294. echo -e "${RD}Using Advanced Settings${CL}"
  295. advanced_settings
  296. fi
  297. }
  298. function install_script() {
  299. ARCH_CHECK
  300. PVE_CHECK
  301. NEXTID=$(pvesh get /cluster/nextid)
  302. header_info
  303. if (whiptail --title "SETTINGS" --yesno "Use Default Settings?" --no-button Advanced 10 58); then
  304. header_info
  305. echo -e "${BL}Using Default Settings${CL}"
  306. default_settings
  307. else
  308. header_info
  309. echo -e "${RD}Using Advanced Settings${CL}"
  310. advanced_settings
  311. fi
  312. }
  313. function update_script() {
  314. header_info
  315. msg_info "Updating ${APP} LXC"
  316. apt-get update &>/dev/null
  317. apt-get -y upgrade &>/dev/null
  318. msg_ok "Updated ${APP} LXC"
  319. msg_ok "Update Successfull"
  320. exit
  321. }
  322. if command -v pveversion >/dev/null 2>&1; then
  323. if ! (whiptail --title "${APP} LXC" --yesno "This will create a New ${APP} LXC. Proceed?" 10 58); then
  324. clear
  325. echo -e "⚠ User exited script \n"
  326. exit
  327. fi
  328. install_script
  329. fi
  330. if ! command -v pveversion >/dev/null 2>&1 && [[ ! -f /etc/apt/sources.list.d/pgdg.list ]]; then
  331. msg_error "No ${APP} Installation Found!"
  332. exit
  333. fi
  334. if ! command -v pveversion >/dev/null 2>&1; then
  335. if ! (whiptail --title "${APP} LXC UPDATE" --yesno "This will update ${APP} LXC. Proceed?" 10 58); then
  336. clear
  337. echo -e "⚠ User exited script \n"
  338. exit
  339. fi
  340. update_script
  341. fi
  342. if [ "$VERB" == "yes" ]; then set -x; fi
  343. if [ "$CT_TYPE" == "1" ]; then
  344. FEATURES="nesting=1,keyctl=1"
  345. else
  346. FEATURES="nesting=1"
  347. fi
  348. TEMP_DIR=$(mktemp -d)
  349. pushd $TEMP_DIR >/dev/null
  350. export tz=$timezone
  351. export DISABLEIPV6=$DISABLEIP6
  352. export APPLICATION=$APP
  353. export VERBOSE=$VERB
  354. export SSH_ROOT=${SSH}
  355. export CTID=$CT_ID
  356. export PCT_OSTYPE=$var_os
  357. export PCT_OSVERSION=$var_version
  358. export PCT_DISK_SIZE=$DISK_SIZE
  359. export PCT_OPTIONS="
  360. -features $FEATURES
  361. -hostname $HN
  362. $SD
  363. $NS
  364. -net0 name=eth0,bridge=$BRG$MAC,ip=$NET$GATE$VLAN$MTU
  365. -onboot 1
  366. -cores $CORE_COUNT
  367. -memory $RAM_SIZE
  368. -unprivileged $CT_TYPE
  369. $PW
  370. "
  371. bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/ct/create_lxc.sh)" || exit
  372. msg_info "Starting LXC Container"
  373. pct start $CTID
  374. msg_ok "Started LXC Container"
  375. lxc-attach -n $CTID -- bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/install/$var_install.sh)" || exit
  376. IP=$(pct exec $CTID ip a s dev eth0 | awk '/inet / {print $2}' | cut -d/ -f1)
  377. pct set $CTID -description "# ${APP} LXC
  378. ### https://tteck.github.io/Proxmox/
  379. <a href='https://ko-fi.com/D1D7EP4GF'><img src='https://img.shields.io/badge/☕-Buy me a coffee-red' /></a>"
  380. msg_ok "Completed Successfully!\n"