Skip to content

Commit d419aa0

Browse files
authored
Update nginx_proxy_manager_cli.sh
Clean Text
1 parent fc8cd6d commit d419aa0

File tree

1 file changed

+128
-36
lines changed

1 file changed

+128
-36
lines changed

nginx_proxy_manager_cli.sh

Lines changed: 128 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -600,12 +600,12 @@ list_access() {
600600
RESPONSE=$(curl -s -X GET "$BASE_URL/nginx/access-lists" \
601601
-H "Authorization: Bearer $(cat $TOKEN_FILE)")
602602

603-
# Vérifiez si la réponse est une liste JSON valide
604-
if echo " $RESPONSE" | jq -e 'type == "array"' > /dev/null; then
605-
# Parcourez et affichez les éléments de la liste
606-
echo " $RESPONSE" | jq -r '.[] | "\(.id): \(.name)"'
607-
else
608-
# En cas d'erreur, vérifiez s'il y a un message d'erreur dans la réponse
603+
# Check if the response is a valid JSON array
604+
if echo " $RESPONSE" | jq -e 'type == "array"' > /dev/null; then
605+
# Loop through and display the elements of the list
606+
echo " $RESPONSE" | jq -r '.[] | "\(.id): \(.name)"'
607+
else
608+
# In case of an error, check if there is an error message in the response
609609
if echo " $RESPONSE" | jq -e '.error // empty' > /dev/null; then
610610
echo -e " ⛔ API Error: $(echo "$RESPONSE" | jq -r '.message')"
611611
else
@@ -917,26 +917,6 @@ create_new_proxy_host() {
917917
CUSTOM_LOCATIONS_ESCAPED="[]"
918918
fi
919919

920-
# DATA=$(printf '{
921-
# "domain_names": ["%s"],
922-
# "forward_host": "%s",
923-
# "forward_port": %s,
924-
# "access_list_id": null,
925-
# "certificate_id": null,
926-
# "ssl_forced": false,
927-
# "caching_enabled": %s,
928-
# "block_exploits": %s,
929-
# "advanced_config": "%s",
930-
# "meta": {
931-
# "dns_challenge": null
932-
# },
933-
# "allow_websocket_upgrade": %s,
934-
# "http2_support": %s,
935-
# "forward_scheme": "%s",
936-
# "enabled": true,
937-
# "locations": %s
938-
# }' "$DOMAIN_NAMES" "$FORWARD_HOST" "$FORWARD_PORT" "$CACHING_ENABLED" "$BLOCK_EXPLOITS" "$ADVANCED_CONFIG" "$ALLOW_WEBSOCKET_UPGRADE" "$HTTP2_SUPPORT" "$FORWARD_SCHEME" "$CUSTOM_LOCATIONS_ESCAPED")
939-
940920
DATA=$(jq -n \
941921
--arg domain "$DOMAIN_NAMES" \
942922
--arg host "$FORWARD_HOST" \
@@ -1846,15 +1826,9 @@ backup-host() {
18461826
fi
18471827
echo ""
18481828
}
1849-
######################################################
1850-
1851-
1852-
18531829

18541830

18551831
######################################################
1856-
## TEST
1857-
18581832
# Function to list global backup files
18591833
list_global_backup_files() {
18601834
ls -t "$BACKUP_DIR"/*_*.json
@@ -1866,8 +1840,8 @@ list_ssl_backup_files() {
18661840
}
18671841

18681842

1869-
1870-
### Function to restore a backup file
1843+
######################################################
1844+
### Function to restore from backup file
18711845
restore_backup() {
18721846
echo -e "\n 🩹 ${COLOR_ORANGE}Restoring all configurations from backup...${COLOR_RESET}"
18731847

@@ -2009,10 +1983,128 @@ show_backup_differences() {
20091983

20101984
##### restore-host
20111985
# Function to restore a single host configuration and its certificate (if exists)
2012-
restore-host() {
2013-
echo " Not finish !! "
1986+
1987+
restore_host() {
1988+
if [ -z "$HOST_ID" ]; then
1989+
echo -e "\n 🩹 The --host-restore-id option requires a host ID."
1990+
usage
1991+
fi
1992+
1993+
# Get the current date in a formatted string
1994+
DATE=$(date +"_%Y_%m_%d__%H_%M_%S")
1995+
1996+
# Verify if host ID exists
1997+
HOST_ID_RESPONSE=$(curl -s -X GET "$BASE_URL/nginx/proxy-hosts/$HOST_ID" -H "Authorization: Bearer $(cat $TOKEN_FILE)")
1998+
if [ -z "$HOST_ID_RESPONSE" ] || [ "$(echo "$HOST_ID_RESPONSE" | jq -r '.id')" != "$HOST_ID" ]; then
1999+
echo -e "\n ⛔ ${COLOR_RED}Host ID $HOST_ID does not exist. Aborting restore.${COLOR_RESET}"
2000+
exit 1
2001+
fi
2002+
2003+
# Fetch the host name to identify the directory
2004+
HOST_NAME=$(echo "$HOST_ID_RESPONSE" | jq -r '.domain_names[0]')
2005+
if [ -z "$HOST_NAME" ]; then
2006+
echo -e "\n ⛔ ${COLOR_RED}Host name not found in the response. Aborting restore.${COLOR_RESET}"
2007+
exit 1
2008+
fi
2009+
SANITIZED_HOST_NAME=$(echo "$HOST_NAME" | sed 's/[^a-zA-Z0-9]/_/g')
2010+
HOST_DIR="$BACKUP_DIR/$SANITIZED_HOST_NAME"
2011+
2012+
#echo -e " 🐛 Debug: SANITIZED_HOST_NAME = $SANITIZED_HOST_NAME"
2013+
#echo -e " 🐛 Debug: HOST_DIR = $HOST_DIR"
2014+
2015+
# Verify the existence of the host directory
2016+
if [ ! -d "$HOST_DIR" ]; then
2017+
echo -e "\n ⛔ ${COLOR_RED}Backup directory for host $HOST_ID not found: $HOST_DIR${COLOR_RESET}"
2018+
exit 1
2019+
fi
2020+
2021+
# Verify the existence of backup files
2022+
BACKUP_FILES=($(find "$HOST_DIR" -type f -name "proxy_host_${HOST_ID}_*.json"))
2023+
2024+
if [ ${#BACKUP_FILES[@]} -eq 0 ]; then
2025+
echo -e "\n ⛔ ${COLOR_RED}No backup file found for host ID $HOST_ID in '$HOST_DIR'. Aborting restore.${COLOR_RESET}"
2026+
exit 1
2027+
fi
2028+
2029+
# Count the number of backup files
2030+
BACKUP_COUNT=${#BACKUP_FILES[@]}
2031+
2032+
if [ "$BACKUP_COUNT" -gt 0 ]; then
2033+
echo -e "\n 🔍 Found ${COLOR_ORANGE}$BACKUP_COUNT${COLOR_RESET} backups for host ${COLOR_ORANGE}$SANITIZED_HOST_NAME${COLOR_RESET} ID $HOST_ID."
2034+
PROXY_HOST_FILE=$(ls -t "${BACKUP_FILES[@]}" | head -n 1)
2035+
echo -e " 🩹 Latest Backup File found: $PROXY_HOST_FILE \n"
2036+
read -p " 👉 Do you want to (1) restore the latest backup, (2) list backups and choose one, or (3) abandon? (1/2/3): " -r choice
2037+
case $choice in
2038+
1)
2039+
echo -e "\n 🩹 Proxy Host backup file : $PROXY_HOST_FILE"
2040+
;;
2041+
2)
2042+
BACKUP_LIST=($(ls -t "${BACKUP_FILES[@]}"))
2043+
echo -e "\nAvailable backups:"
2044+
for i in "${!BACKUP_LIST[@]}"; do
2045+
echo "$i) ${BACKUP_LIST[$i]}"
2046+
done
2047+
read -p " 👉 Enter the number of the backup you want to restore: " -r backup_number
2048+
PROXY_HOST_FILE="${BACKUP_LIST[$backup_number]}"
2049+
if [ ! -f "$PROXY_HOST_FILE" ]; then
2050+
echo -e "\n ⛔ ${COLOR_RED}Selected backup file not found: $PROXY_HOST_FILE${COLOR_RESET}"
2051+
exit 1
2052+
fi
2053+
;;
2054+
3)
2055+
echo -e "\n${COLOR_RED} Abandoned.${COLOR_RESET}\n"
2056+
exit 0
2057+
;;
2058+
*)
2059+
echo -e "\n ${COLOR_ORANGE}Invalid choice.${COLOR_RESET}\n"
2060+
exit 1
2061+
;;
2062+
esac
2063+
fi
2064+
2065+
# Verify if the proxy host exists
2066+
if [ -n "$HOST_ID_RESPONSE" ] && [ "$(echo "$HOST_ID_RESPONSE" | jq -r '.id')" = "$HOST_ID" ]; then
2067+
echo -e " 🔔 Proxy host for ID $HOST_ID already exists.\n ${COLOR_ORANGE}"
2068+
read -p " 👉 Do you want to delete the existing proxy host and restore from the backup? (y/n): " -r confirm
2069+
echo -e "${COLOR_RESET}"
2070+
if [[ $confirm =~ ^[Yy]$ ]]; then
2071+
echo -e "${COLOR_RESET}"
2072+
if ! delete_proxy_host; then
2073+
echo -e "${COLOR_RED} ⛔ Failed to delete existing proxy host. Aborting restore.${COLOR_RESET}\n"
2074+
exit 1
2075+
fi
2076+
else
2077+
echo " ⛔ Abandoned."
2078+
exit 0
2079+
fi
2080+
fi
2081+
2082+
if [ -f "$PROXY_HOST_FILE" ]; then
2083+
RESPONSE=$(jq 'del(.id, .created_on, .modified_on, .owner_user_id)' "$PROXY_HOST_FILE")
2084+
HTTP_RESPONSE=$(curl -s -w "HTTPSTATUS:%{http_code}" -X POST "$BASE_URL/nginx/proxy-hosts" \
2085+
-H "Authorization: Bearer $(cat $TOKEN_FILE)" \
2086+
-H "Content-Type: application/json; charset=UTF-8" \
2087+
--data-raw "$RESPONSE")
2088+
2089+
HTTP_BODY=$(echo "$HTTP_RESPONSE" | sed -e 's/HTTPSTATUS\\:.*//g')
2090+
HTTP_STATUS=$(echo "$HTTP_RESPONSE" | tr -d '\\n' | sed -e 's/.*HTTPSTATUS://')
2091+
2092+
if [ "$HTTP_STATUS" -eq 200 ] || [ "$HTTP_STATUS" -eq 201 ]; then
2093+
echo -e "${COLOR_GREEN}Proxy host restored 🆗 from file: $PROXY_HOST_FILE${COLOR_RESET}\n"
2094+
else
2095+
echo -e "${COLOR_RED}Failed to restore proxy host. Error: $HTTP_BODY${COLOR_RESET}\n"
2096+
exit 1
2097+
fi
2098+
else
2099+
echo -e "\n ⛔ ${COLOR_RED}Proxy host backup file not found: $PROXY_HOST_FILE${COLOR_RESET}\n"
2100+
exit 1
2101+
fi
20142102
}
20152103

2104+
2105+
2106+
2107+
20162108

20172109
#################################
20182110
# Main logic

0 commit comments

Comments
 (0)