postgresql-v5.sh 13 KB

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