postgresql-v5.sh 13 KB

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