mirror of
https://github.com/Erreur32/nginx-proxy-manager-Bash-API.git
synced 2025-01-15 16:21:55 +01:00
Compare commits
No commits in common. "b6c9b9a0f26a1246b96699961a10dd4f45984ec2" and "a022b1393c700785424c9887a6d9fcb79d9f8209" have entirely different histories.
b6c9b9a0f2
...
a022b1393c
2 changed files with 199 additions and 143 deletions
20
README.md
20
README.md
|
@ -17,11 +17,9 @@
|
||||||
13. [TODO](#todo)
|
13. [TODO](#todo)
|
||||||
|
|
||||||
> [!WARNING]
|
> [!WARNING]
|
||||||
> The --restore command may not work correctly (a fix is in progress).
|
> The --restore command may not work correctly (a fix is in progress)
|
||||||
>
|
|
||||||
> instead try:
|
> instead try:
|
||||||
>
|
> ./nginx_proxy_manager_cli.sh --restore-id id(necessary to add hosts one by one) should work fine.
|
||||||
> ./nginx_proxy_manager_cli.sh --restore-id id (necessary to add hosts one by one and regen certificates).
|
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
|
@ -158,11 +156,8 @@ BASE_DIR="/path/nginx_proxy_script/data"
|
||||||
##### Verifying the Configuration
|
##### Verifying the Configuration
|
||||||
|
|
||||||
###### Info
|
###### Info
|
||||||
|
|
||||||
Some info settings in the script
|
|
||||||
|
|
||||||
```
|
```
|
||||||
./nginx_proxy_manager_cli_.sh --info
|
./nginx_proxy_manager_cli_new.sh --info
|
||||||
|
|
||||||
Script Info: 2.3.5
|
Script Info: 2.3.5
|
||||||
|
|
||||||
|
@ -176,7 +171,7 @@ Script Variables Information:
|
||||||
Token NPM /path/to/nginx_proxy/token/token_127.0.0.1.txt
|
Token NPM /path/to/nginx_proxy/token/token_127.0.0.1.txt
|
||||||
|
|
||||||
```
|
```
|
||||||
|
Some info settings from scriptwith `./nginx_proxy_manager_cli_.sh --info`
|
||||||
|
|
||||||
|
|
||||||
By following these steps, you can enable SSL for your proxy host for the first time using Let's Encrypt.
|
By following these steps, you can enable SSL for your proxy host for the first time using Let's Encrypt.
|
||||||
|
@ -200,17 +195,12 @@ Enable SSL for the Host:
|
||||||
|
|
||||||
./nginx_proxy_manager_cli.sh --host-ssl-enable 10
|
./nginx_proxy_manager_cli.sh --host-ssl-enable 10
|
||||||
|
|
||||||
SSl is enable successfully
|
|
||||||
|
|
||||||
Et Voilà.
|
|
||||||
|
|
||||||
|
|
||||||
Other Exemple command:
|
|
||||||
|
|
||||||
|
|
||||||
|
Other Exemple:
|
||||||
Host proxy info command `--host-show id`
|
Host proxy info command `--host-show id`
|
||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
./nginx_proxy_manager_cli_.sh --host-show 10
|
./nginx_proxy_manager_cli_.sh --host-show 10
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
VERSION="2.3.7"
|
|
||||||
# Nginx Proxy Manager CLI Script
|
# Nginx Proxy Manager CLI Script
|
||||||
# Github [ https://github.com/Erreur32/nginx-proxy-manager-Bash-API ]
|
# Github [ https://github.com/Erreur32/nginx-proxy-manager-Bash-API ]
|
||||||
# Erreur32 July 2024
|
# Erreur32 July 2024
|
||||||
|
@ -12,7 +11,6 @@ VERSION="2.3.7"
|
||||||
# Usage:
|
# Usage:
|
||||||
# ./nginx_proxy_manager_cli.sh [OPTIONS]
|
# ./nginx_proxy_manager_cli.sh [OPTIONS]
|
||||||
#
|
#
|
||||||
|
|
||||||
# Examples:
|
# Examples:
|
||||||
#
|
#
|
||||||
# 📦 Backup First!
|
# 📦 Backup First!
|
||||||
|
@ -36,7 +34,6 @@ VERSION="2.3.7"
|
||||||
# ./nginx_proxy_manager_cli.sh -d example.com -i 192.168.1.10 -p 8080 -l '[{"path":"/api","forward_host":"192.168.1.11","forward_port":8081}]'
|
# ./nginx_proxy_manager_cli.sh -d example.com -i 192.168.1.10 -p 8080 -l '[{"path":"/api","forward_host":"192.168.1.11","forward_port":8081}]'
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
|
|
||||||
# Options:
|
# Options:
|
||||||
#
|
#
|
||||||
# 🌐 Host proxy creation:
|
# 🌐 Host proxy creation:
|
||||||
|
@ -52,9 +49,9 @@ VERSION="2.3.7"
|
||||||
#
|
#
|
||||||
# 📦 Backup and Restore:
|
# 📦 Backup and Restore:
|
||||||
# --backup Backup all configurations to a file
|
# --backup Backup all configurations to a file
|
||||||
# --host-backup-id id Backup a single host configuration and its certificate (if exists)
|
# --backup-id id Backup a single host configuration and its certificate (if exists)
|
||||||
# --restore Restore configurations from a backup file
|
# --restore Restore configurations from a backup file
|
||||||
# --host-restore-id id Restore a single host configuration and its certificate (if exists)
|
# --restore-id id Restore a single host configuration and its certificate (if exists)
|
||||||
#
|
#
|
||||||
# 🔧 Miscellaneous:
|
# 🔧 Miscellaneous:
|
||||||
# --check-token Check if the current token is valid
|
# --check-token Check if the current token is valid
|
||||||
|
@ -75,7 +72,6 @@ VERSION="2.3.7"
|
||||||
# --generate-cert domain email [--custom] Generate a Let's Encrypt or Custom certificate for the given domain and email
|
# --generate-cert domain email [--custom] Generate a Let's Encrypt or Custom certificate for the given domain and email
|
||||||
# --help Display this help
|
# --help Display this help
|
||||||
|
|
||||||
|
|
||||||
################################
|
################################
|
||||||
# Variables to Edit (required) #
|
# Variables to Edit (required) #
|
||||||
################################
|
################################
|
||||||
|
@ -90,7 +86,7 @@ BASE_DIR="/path/nginx_proxy_script/data"
|
||||||
#################################
|
#################################
|
||||||
# Variables to Edit (optional) #
|
# Variables to Edit (optional) #
|
||||||
#################################
|
#################################
|
||||||
|
VERSION="2.3.5"
|
||||||
# Will create backup directory automatically
|
# Will create backup directory automatically
|
||||||
BACKUP_DIR="./backups"
|
BACKUP_DIR="./backups"
|
||||||
#DATE=$(date +"%Y%m%d%H%M%S")
|
#DATE=$(date +"%Y%m%d%H%M%S")
|
||||||
|
@ -106,7 +102,7 @@ TOKEN_FILE="$TOKEN_DIR/token_${NGINX_IP}.txt"
|
||||||
# Set Token duration validity.
|
# Set Token duration validity.
|
||||||
TOKEN_EXPIRY="1y"
|
TOKEN_EXPIRY="1y"
|
||||||
|
|
||||||
# Default variables (you can adapt)
|
# Default variables
|
||||||
CACHING_ENABLED=false
|
CACHING_ENABLED=false
|
||||||
BLOCK_EXPLOITS=true
|
BLOCK_EXPLOITS=true
|
||||||
ALLOW_WEBSOCKET_UPGRADE=1
|
ALLOW_WEBSOCKET_UPGRADE=1
|
||||||
|
@ -120,8 +116,6 @@ SSL_FORCED=0
|
||||||
HSTS_ENABLED=0
|
HSTS_ENABLED=0
|
||||||
HSTS_SUBDOMAINS=0
|
HSTS_SUBDOMAINS=0
|
||||||
|
|
||||||
|
|
||||||
# Don't touch below that line (or you know ...)
|
|
||||||
# Control variables
|
# Control variables
|
||||||
CREATE_USER=false
|
CREATE_USER=false
|
||||||
DELETE_USER=false
|
DELETE_USER=false
|
||||||
|
@ -168,7 +162,6 @@ check_dependencies() {
|
||||||
|
|
||||||
check_dependencies
|
check_dependencies
|
||||||
|
|
||||||
|
|
||||||
# Generate a new API token
|
# Generate a new API token
|
||||||
generate_token() {
|
generate_token() {
|
||||||
response=$(curl -s -X POST "$BASE_URL$API_ENDPOINT" \
|
response=$(curl -s -X POST "$BASE_URL$API_ENDPOINT" \
|
||||||
|
@ -225,17 +218,16 @@ check_nginx_access() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Display help
|
# Display help
|
||||||
usage() {
|
usage() {
|
||||||
echo -e "\n${COLOR_YELLOW}Usage: ./nginx_proxy_manager_cli.sh -d domain -i ip -p port [-f forward_scheme] [-c caching_enabled] [-b block_exploits] [-w allow_websocket_upgrade] [-a advanced_config] [-t token_expiry] [--create-user username password email] [--delete-user username] [--host-delete id] [--host-list] [--host-list-full] [--host-list-ssl-certificates] [--host-list-users] [--host-search hostname] [--host-enable id] [--host-disable id] [--check-token] [--backup] [--host-backup-id id] [--restore] [--host-restore-id id] [--generate-cert domain email [--custom]] [--host-ssl-enable id] [--host-ssl-disable id] [--host-show id] [--show-default] [--help]${COLOR_RESET}"
|
echo -e "\n${COLOR_YELLOW}Usage: ./nginx_proxy_manager_cli.sh -d domain -i ip -p port [-f forward_scheme] [-c caching_enabled] [-b block_exploits] [-w allow_websocket_upgrade] [-a advanced_config] [-t token_expiry] [--create-user username password email] [--delete-user username] [--host-delete id] [--host-list] [--host-list-full] [--host-list-ssl-certificates] [--host-list-users] [--host-search hostname] [--host-enable id] [--host-disable id] [--check-token] [--backup] [--backup-id id] [--restore] [--restore-id id] [--generate-cert domain email [--custom]] [--host-ssl-enable id] [--host-ssl-disable id] [--host-show id] [--show-default] [--help]${COLOR_RESET}"
|
||||||
echo -e ""
|
echo -e ""
|
||||||
echo -e "Examples:"
|
echo -e "Examples:"
|
||||||
echo -e "\n 📦 Backup First before doing anything!${COLOR_GREY}"
|
echo -e "\n 📦 Backup First before doing anything!${COLOR_GREY}"
|
||||||
echo -e " ./nginx_proxy_manager_cli.sh --backup"
|
echo -e " ./nginx_proxy_manager_cli.sh --backup"
|
||||||
echo -e " ./nginx_proxy_manager_cli.sh --host-backup-id 10"
|
echo -e " ./nginx_proxy_manager_cli.sh --backup-id 10"
|
||||||
echo -e " ./nginx_proxy_manager_cli.sh --restore"
|
echo -e " ./nginx_proxy_manager_cli.sh --restore"
|
||||||
echo -e " ./nginx_proxy_manager_cli.sh --host-restore-id 10"
|
echo -e " ./nginx_proxy_manager_cli.sh --restore-id 10"
|
||||||
echo -e "\n ${COLOR_RESET}🌐 Host Creation${COLOR_GREY}"
|
echo -e "\n ${COLOR_RESET}🌐 Host Creation${COLOR_GREY}"
|
||||||
echo -e " ./nginx_proxy_manager_cli.sh --show-default"
|
echo -e " ./nginx_proxy_manager_cli.sh --show-default"
|
||||||
echo -e " ./nginx_proxy_manager_cli.sh -d example.com -i 192.168.1.10 -p 8080"
|
echo -e " ./nginx_proxy_manager_cli.sh -d example.com -i 192.168.1.10 -p 8080"
|
||||||
|
@ -267,9 +259,9 @@ usage() {
|
||||||
echo -e " --info ℹ️ ${COLOR_YELLOW}Display${COLOR_RESET} Script Variables Information"
|
echo -e " --info ℹ️ ${COLOR_YELLOW}Display${COLOR_RESET} Script Variables Information"
|
||||||
echo -e " --show-default 🔍 ${COLOR_YELLOW}Show${COLOR_RESET} Default settings for creating hosts"
|
echo -e " --show-default 🔍 ${COLOR_YELLOW}Show${COLOR_RESET} Default settings for creating hosts"
|
||||||
echo -e " --backup 📦 ${COLOR_GREEN}Backup${COLOR_RESET} All configurations to a different files in \$BACKUP_DIR"
|
echo -e " --backup 📦 ${COLOR_GREEN}Backup${COLOR_RESET} All configurations to a different files in \$BACKUP_DIR"
|
||||||
echo -e " --host-backup-id id 📦 ${COLOR_GREEN}Backup${COLOR_RESET} Single host configuration and its certificate (if exists)"
|
echo -e " --backup-id id 📦 ${COLOR_GREEN}Backup${COLOR_RESET} Single host configuration and its certificate (if exists)"
|
||||||
echo -e " --restore 📦 ${COLOR_GREEN}Restore${COLOR_RESET} All configurations from a backup file"
|
echo -e " --restore 📦 ${COLOR_GREEN}Restore${COLOR_RESET} All configurations from a backup file"
|
||||||
echo -e " --host-restore-id id 📦 ${COLOR_GREEN}Restore${COLOR_RESET} Single host configuration and its certificate (if exists)"
|
echo -e " --restore-id id 📦 ${COLOR_GREEN}Restore${COLOR_RESET} Single host configuration and its certificate (if exists)"
|
||||||
echo -e " --check-token 🔧 ${COLOR_YELLOW}Check${COLOR_RESET} If the current token is valid"
|
echo -e " --check-token 🔧 ${COLOR_YELLOW}Check${COLOR_RESET} If the current token is valid"
|
||||||
echo -e " --create-user user pass email 👤 ${COLOR_GREEN}Create${COLOR_RESET} User with a ${COLOR_YELLOW}username, ${COLOR_YELLOW}password${COLOR_RESET} and ${COLOR_YELLOW}email${COLOR_RESET}"
|
echo -e " --create-user user pass email 👤 ${COLOR_GREEN}Create${COLOR_RESET} User with a ${COLOR_YELLOW}username, ${COLOR_YELLOW}password${COLOR_RESET} and ${COLOR_YELLOW}email${COLOR_RESET}"
|
||||||
echo -e " --delete-user username 💣 ${COLOR_ORANGE}Delete${COLOR_RESET} User by ${COLOR_YELLOW}username${COLOR_RESET}"
|
echo -e " --delete-user username 💣 ${COLOR_ORANGE}Delete${COLOR_RESET} User by ${COLOR_YELLOW}username${COLOR_RESET}"
|
||||||
|
@ -317,7 +309,6 @@ display_info() {
|
||||||
generate_token
|
generate_token
|
||||||
|
|
||||||
fi
|
fi
|
||||||
echo ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Vérification et création des dossiers si nécessaires
|
# Vérification et création des dossiers si nécessaires
|
||||||
|
@ -344,18 +335,6 @@ if [ ! -d "$BACKUP_DIR" ]; then
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
# shellcheck disable=SC2120
|
|
||||||
# check_no_arguments() {
|
|
||||||
# if [ $# -eq 0 ]; then
|
|
||||||
# echo -e "\n ${COLOR_RED}No arguments provided. Use --help to see all command options.${COLOR_RESET}"
|
|
||||||
# echo ""
|
|
||||||
# #display_info
|
|
||||||
# exit 1
|
|
||||||
# fi
|
|
||||||
# }
|
|
||||||
|
|
||||||
|
|
||||||
# Colorize boolean values for display
|
# Colorize boolean values for display
|
||||||
colorize_boolean() {
|
colorize_boolean() {
|
||||||
local value=$1
|
local value=$1
|
||||||
|
@ -375,9 +354,7 @@ colorize_booleanh() {
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Parse options
|
||||||
|
|
||||||
|
|
||||||
while getopts "d:i:p:f:c:b:w:a:l:-:" opt; do
|
while getopts "d:i:p:f:c:b:w:a:l:-:" opt; do
|
||||||
case $opt in
|
case $opt in
|
||||||
d) DOMAIN_NAMES="$OPTARG" ;;
|
d) DOMAIN_NAMES="$OPTARG" ;;
|
||||||
|
@ -393,7 +370,7 @@ while getopts "d:i:p:f:c:b:w:a:l:-:" opt; do
|
||||||
case "${OPTARG}" in
|
case "${OPTARG}" in
|
||||||
show-default) SHOW_DEFAULT=true ;;
|
show-default) SHOW_DEFAULT=true ;;
|
||||||
backup) BACKUP=true ;;
|
backup) BACKUP=true ;;
|
||||||
host-backup-id)
|
backup-id)
|
||||||
BACKUP_HOST=true
|
BACKUP_HOST=true
|
||||||
HOST_ID="${!OPTIND}"; shift
|
HOST_ID="${!OPTIND}"; shift
|
||||||
;;
|
;;
|
||||||
|
@ -456,8 +433,7 @@ while getopts "d:i:p:f:c:b:w:a:l:-:" opt; do
|
||||||
force-cert-creation) FORCE_CERT_CREATION=true ;;
|
force-cert-creation) FORCE_CERT_CREATION=true ;;
|
||||||
info) display_info;echo; exit 0 ;;
|
info) display_info;echo; exit 0 ;;
|
||||||
esac ;;
|
esac ;;
|
||||||
*) display_info; exit 0
|
*) display_info ;;
|
||||||
;;
|
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
|
@ -466,24 +442,8 @@ if [ $# -eq 0 ]; then
|
||||||
#echo -e "\n Unknown option --${OPTARG}" ;
|
#echo -e "\n Unknown option --${OPTARG}" ;
|
||||||
display_info
|
display_info
|
||||||
# usage
|
# usage
|
||||||
exit 0
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
######################################
|
|
||||||
|
|
||||||
|
|
||||||
# Function to check if the host ID exists
|
|
||||||
host-check-id() {
|
|
||||||
local host_id=$1
|
|
||||||
local host_list=$(./nginx_proxy_manager_cli.sh --host-list)
|
|
||||||
|
|
||||||
if echo "$host_list" | grep -q ""id": $host_id"; then
|
|
||||||
return 0
|
|
||||||
else
|
|
||||||
echo "Error: Host ID $host_id does not exist."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -710,11 +670,11 @@ restore_backup() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
######################################################
|
|
||||||
## en test
|
|
||||||
######################################################
|
|
||||||
# Function to list backup versions for a given host ID
|
# Function to list backup versions for a given host ID
|
||||||
list_backup_versions_t() {
|
list_backup_versions() {
|
||||||
echo -e "\n 🔍 Listing available backup versions for host ID $HOST_ID..."
|
echo -e "\n 🔍 Listing available backup versions for host ID $HOST_ID..."
|
||||||
ls -t "$BACKUP_DIR"/proxy_host_ID_+"${HOST_ID}"_IP_"${NGINX_IP//./_}"_*.json | head -n 10 | while read -r file; do
|
ls -t "$BACKUP_DIR"/proxy_host_ID_+"${HOST_ID}"_IP_"${NGINX_IP//./_}"_*.json | head -n 10 | while read -r file; do
|
||||||
timestamp=$(echo "$file" | grep -oE '[0-9]{14}')
|
timestamp=$(echo "$file" | grep -oE '[0-9]{14}')
|
||||||
|
@ -754,7 +714,7 @@ delete_proxy_host() {
|
||||||
echo -e "\n 💣 The --host-delete option requires a host ID."
|
echo -e "\n 💣 The --host-delete option requires a host ID."
|
||||||
usage
|
usage
|
||||||
fi
|
fi
|
||||||
echo -e " 💣 Deleting proxy host ID: $HOST_ID..."
|
echo -e " \n 💣 Deleting proxy host ID: $HOST_ID..."
|
||||||
|
|
||||||
RESPONSE=$(curl -s -w "HTTPSTATUS:%{http_code}" -X DELETE "$BASE_URL/nginx/proxy-hosts/$HOST_ID" \
|
RESPONSE=$(curl -s -w "HTTPSTATUS:%{http_code}" -X DELETE "$BASE_URL/nginx/proxy-hosts/$HOST_ID" \
|
||||||
-H "Authorization: Bearer $(cat $TOKEN_FILE)")
|
-H "Authorization: Bearer $(cat $TOKEN_FILE)")
|
||||||
|
@ -772,69 +732,52 @@ delete_proxy_host() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
## Function to restore a single host configuration
|
# Function to restore a single host configuration and its certificate (if exists)
|
||||||
restore_host_id() {
|
restore_single_host() {
|
||||||
if [ -z "$HOST_ID" ]; then
|
if [ -z "$HOST_ID" ]; then
|
||||||
echo -e "\n 🩹 The --host-restore-id option requires a host ID."
|
echo -e "\n 🩹 The --restore-id option requires a host ID."
|
||||||
usage
|
usage
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Get the current date in a formatted string
|
echo -e "\n 🩹 ${COLOR_ORANGE}Restoring backup for host ID $HOST_ID from '$BACKUP_DIR'...${COLOR_RESET}"
|
||||||
DATE=$(date +"_%Y_%m_%d__%H_%M_%S")
|
|
||||||
|
|
||||||
# Verify if host ID exists
|
# Get the current date in a formatted string
|
||||||
HOST_ID_RESPONSE=$(curl -s -X GET "$BASE_URL/nginx/proxy-hosts/$HOST_ID" -H "Authorization: Bearer $(cat $TOKEN_FILE)")
|
DATE=$(date +"%Y_%m_%d__%H_%M_%S")
|
||||||
if [ -z "$HOST_ID_RESPONSE" ] || [ "$(echo "$HOST_ID_RESPONSE" | jq -r '.id')" != "$HOST_ID" ]; then
|
|
||||||
echo -e "\n ⛔ ${COLOR_RED}Host ID $HOST_ID does not exist. Aborting restore.${COLOR_RESET}"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Fetch the host name to identify the directory
|
# Fetch the host name to identify the directory
|
||||||
HOST_NAME=$(echo "$HOST_ID_RESPONSE" | jq -r '.domain_names[0]')
|
HOST_NAME_RESPONSE=$(curl -s -X GET "$BASE_URL/nginx/proxy-hosts/$HOST_ID" -H "Authorization: Bearer $(cat $TOKEN_FILE)")
|
||||||
if [ -z "$HOST_NAME" ]; then
|
HOST_NAME=$(echo "$HOST_NAME_RESPONSE" | jq -r '.domain_names[0]')
|
||||||
echo -e "\n ⛔ ${COLOR_RED}Host name not found in the response. Aborting restore.${COLOR_RESET}"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
SANITIZED_HOST_NAME=$(echo "$HOST_NAME" | sed 's/[^a-zA-Z0-9]/_/g')
|
SANITIZED_HOST_NAME=$(echo "$HOST_NAME" | sed 's/[^a-zA-Z0-9]/_/g')
|
||||||
HOST_DIR="$BACKUP_DIR/$SANITIZED_HOST_NAME"
|
HOST_DIR="$BACKUP_DIR/$SANITIZED_HOST_NAME"
|
||||||
|
|
||||||
#echo -e " 🐛 Debug: SANITIZED_HOST_NAME = $SANITIZED_HOST_NAME"
|
# Vérifier l'existence des fichiers de sauvegarde
|
||||||
#echo -e " 🐛 Debug: HOST_DIR = $HOST_DIR"
|
BACKUP_FILES=$(ls "$HOST_DIR/proxy_host_${HOST_ID}_${NGINX_IP//./_}_*.json" 2>/dev/null)
|
||||||
|
if [ -z "$BACKUP_FILES" ]; then
|
||||||
# Verify the existence of the host directory
|
|
||||||
if [ ! -d "$HOST_DIR" ]; then
|
|
||||||
echo -e "\n ⛔ ${COLOR_RED}Backup directory for host $HOST_ID not found: $HOST_DIR${COLOR_RESET}"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Verify the existence of backup files
|
|
||||||
BACKUP_FILES=($(find "$HOST_DIR" -type f -name "proxy_host_${HOST_ID}_*.json"))
|
|
||||||
|
|
||||||
if [ ${#BACKUP_FILES[@]} -eq 0 ]; then
|
|
||||||
echo -e "\n ⛔ ${COLOR_RED}No backup file found for host ID $HOST_ID in '$HOST_DIR'. Aborting restore.${COLOR_RESET}"
|
echo -e "\n ⛔ ${COLOR_RED}No backup file found for host ID $HOST_ID in '$HOST_DIR'. Aborting restore.${COLOR_RESET}"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Count the number of backup files
|
# Compter le nombre de fichiers de sauvegarde
|
||||||
BACKUP_COUNT=${#BACKUP_FILES[@]}
|
BACKUP_COUNT=$(echo "$BACKUP_FILES" | wc -l)
|
||||||
|
echo -e "\n 🔍 Found $BACKUP_COUNT backups for host ID $HOST_ID."
|
||||||
|
|
||||||
if [ "$BACKUP_COUNT" -gt 0 ]; then
|
if [ "$BACKUP_COUNT" -gt 0 ]; then
|
||||||
echo -e "\n 🔍 Found ${COLOR_ORANGE}$BACKUP_COUNT${COLOR_RESET} backups for host ${COLOR_ORANGE}$SANITIZED_HOST_NAME${COLOR_RESET} ID $HOST_ID."
|
read -p " 👉 you want to (1) restore the latest backup, (2) list backups and choose one, or (3) abandon? (1/2/3): " -r choice
|
||||||
PROXY_HOST_FILE=$(ls -t "${BACKUP_FILES[@]}" | head -n 1)
|
|
||||||
echo -e " 🩹 Latest Backup File found: $PROXY_HOST_FILE \n"
|
|
||||||
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
|
|
||||||
case $choice in
|
case $choice in
|
||||||
1)
|
1)
|
||||||
echo -e "\n 🩹 Proxy Host backup file : $PROXY_HOST_FILE"
|
PROXY_HOST_FILE=$(ls -t "$HOST_DIR/proxy_host_${HOST_ID}_${NGINX_IP//./_}_*.json" | head -n 1)
|
||||||
;;
|
;;
|
||||||
2)
|
2)
|
||||||
BACKUP_LIST=($(ls -t "${BACKUP_FILES[@]}"))
|
list_backup_versions() {
|
||||||
echo -e "\nAvailable backups:"
|
echo -e "\n 🔍 Listing available backup versions for host ID $HOST_ID..."
|
||||||
for i in "${!BACKUP_LIST[@]}"; do
|
ls -t "$HOST_DIR/proxy_host_${HOST_ID}_${NGINX_IP//./_}_*.json" | while read -r file; do
|
||||||
echo "$i) ${BACKUP_LIST[$i]}"
|
timestamp=$(echo "$file" | grep -oE '[0-9]{4}_[0-9]{2}_[0-9]{2}__[0-9]{2}_[0-9]{2}_[0-9]{2}')
|
||||||
|
echo " - $timestamp"
|
||||||
done
|
done
|
||||||
read -p " 👉 Enter the number of the backup you want to restore: " -r backup_number
|
}
|
||||||
PROXY_HOST_FILE="${BACKUP_LIST[$backup_number]}"
|
list_backup_versions
|
||||||
|
read -p " 👉 Enter the timestamp of the backup you want to restore: " -r timestamp
|
||||||
|
PROXY_HOST_FILE="$HOST_DIR/proxy_host_${HOST_ID}_${NGINX_IP//./_}_$timestamp.json"
|
||||||
if [ ! -f "$PROXY_HOST_FILE" ]; then
|
if [ ! -f "$PROXY_HOST_FILE" ]; then
|
||||||
echo -e "\n ⛔ ${COLOR_RED}Selected backup file not found: $PROXY_HOST_FILE${COLOR_RESET}"
|
echo -e "\n ⛔ ${COLOR_RED}Selected backup file not found: $PROXY_HOST_FILE${COLOR_RESET}"
|
||||||
exit 1
|
exit 1
|
||||||
|
@ -851,19 +794,20 @@ restore_host_id() {
|
||||||
esac
|
esac
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Verify if the proxy host exists
|
echo -e "\n 🩹 Using backup file: $PROXY_HOST_FILE"
|
||||||
if [ -n "$HOST_ID_RESPONSE" ] && [ "$(echo "$HOST_ID_RESPONSE" | jq -r '.id')" = "$HOST_ID" ]; then
|
|
||||||
echo -e " 🔔 Proxy host for ID $HOST_ID already exists.\n ${COLOR_ORANGE}"
|
# Vérifier si le proxy host existe
|
||||||
|
RESPONSE=$(curl -s -X GET "$BASE_URL/nginx/proxy-hosts/$HOST_ID" -H "Authorization: Bearer $(cat $TOKEN_FILE)")
|
||||||
|
if [ -n "$RESPONSE" ] && [ "$(echo "$RESPONSE" | jq -r '.id')" = "$HOST_ID" ]; then
|
||||||
|
echo -e "\n 🔔 Proxy host for ID $HOST_ID already exists."
|
||||||
read -p " 👉 Do you want to delete the existing proxy host and restore from the backup? (y/n): " -r confirm
|
read -p " 👉 Do you want to delete the existing proxy host and restore from the backup? (y/n): " -r confirm
|
||||||
echo -e "${COLOR_RESET}"
|
|
||||||
if [[ $confirm =~ ^[Yy]$ ]]; then
|
if [[ $confirm =~ ^[Yy]$ ]]; then
|
||||||
echo -e "${COLOR_RESET}"
|
|
||||||
if ! delete_proxy_host; then
|
if ! delete_proxy_host; then
|
||||||
echo -e "${COLOR_RED} ⛔ Failed to delete existing proxy host. Aborting restore.${COLOR_RESET}\n"
|
echo -e " ⛔ ${COLOR_RED}Failed to delete existing proxy host. Aborting restore.${COLOR_RESET}\n"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
echo " ⛔ Abandoned."
|
echo "Abandoned."
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
@ -875,13 +819,35 @@ restore_host_id() {
|
||||||
-H "Content-Type: application/json; charset=UTF-8" \
|
-H "Content-Type: application/json; charset=UTF-8" \
|
||||||
--data-raw "$RESPONSE")
|
--data-raw "$RESPONSE")
|
||||||
|
|
||||||
HTTP_BODY=$(echo "$HTTP_RESPONSE" | sed -e 's/HTTPSTATUS\\:.*//g')
|
HTTP_BODY=$(echo "$HTTP_RESPONSE" | sed -e 's/HTTPSTATUS\:.*//g')
|
||||||
HTTP_STATUS=$(echo "$HTTP_RESPONSE" | tr -d '\\n' | sed -e 's/.*HTTPSTATUS://')
|
HTTP_STATUS=$(echo "$HTTP_RESPONSE" | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
|
||||||
|
|
||||||
if [ "$HTTP_STATUS" -eq 200 ] || [ "$HTTP_STATUS" -eq 201 ]; then
|
if [ "$HTTP_STATUS" -eq 200 ] || [ "$HTTP_STATUS" -eq 201 ]; then
|
||||||
echo -e " ✅ ${COLOR_GREEN}Proxy host restored 🆗 from file: $PROXY_HOST_FILE${COLOR_RESET}\n"
|
echo -e "\n ✅ ${COLOR_GREEN}Proxy host restored 🆗 from file: $PROXY_HOST_FILE${COLOR_RESET}\n"
|
||||||
|
|
||||||
|
# Restore SSL certificate if it exists
|
||||||
|
SSL_BACKUP_FILE=$(ls "$HOST_DIR/ssl_certif_${HOST_ID}_${NGINX_IP//./_}_*.json" 2>/dev/null | head -n 1)
|
||||||
|
if [ -f "$SSL_BACKUP_FILE" ]; then
|
||||||
|
CERT_RESPONSE=$(cat "$SSL_BACKUP_FILE")
|
||||||
|
HTTP_CERT_RESPONSE=$(curl -s -w "HTTPSTATUS:%{http_code}" -X POST "$BASE_URL/nginx/certificates" \
|
||||||
|
-H "Authorization: Bearer $(cat $TOKEN_FILE)" \
|
||||||
|
-H "Content-Type: application/json; charset=UTF-8" \
|
||||||
|
--data-raw "$CERT_RESPONSE")
|
||||||
|
|
||||||
|
HTTP_CERT_BODY=$(echo "$HTTP_CERT_RESPONSE" | sed -e 's/HTTPSTATUS\:.*//g')
|
||||||
|
HTTP_CERT_STATUS=$(echo "$HTTP_CERT_RESPONSE" | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
|
||||||
|
|
||||||
|
if [ "$HTTP_CERT_STATUS" -eq 200 ] || [ "$HTTP_CERT_STATUS" -eq 201 ]; then
|
||||||
|
echo -e "\n ✅ ${COLOR_GREEN}SSL certificate restored 🆗 from file: $SSL_BACKUP_FILE${COLOR_RESET}\n"
|
||||||
else
|
else
|
||||||
echo -e " ⛔ ${COLOR_RED}Failed to restore proxy host. Error: $HTTP_BODY${COLOR_RESET}\n"
|
echo -e "\n ⛔ ${COLOR_RED}Failed to restore SSL certificate. Error: $HTTP_CERT_BODY${COLOR_RESET}\n"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo -e " ⚠️ ${COLOR_YELLOW}No SSL certificate backup file found.${COLOR_RESET}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
else
|
||||||
|
echo -e "\n ⛔ ${COLOR_RED}Failed to restore proxy host. Error: $HTTP_BODY${COLOR_RESET}\n"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
|
@ -893,10 +859,13 @@ restore_host_id() {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
################################
|
################################
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Check if a proxy host with the given domain names already exists
|
# Check if a proxy host with the given domain names already exists
|
||||||
check_existing_proxy_host() {
|
check_existing_proxy_host() {
|
||||||
RESPONSE=$(curl -s -X GET "$BASE_URL/nginx/proxy-hosts" \
|
RESPONSE=$(curl -s -X GET "$BASE_URL/nginx/proxy-hosts" \
|
||||||
|
@ -1428,6 +1397,103 @@ show_default() {
|
||||||
exit 0
|
exit 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Perform a full backup of all configurations
|
||||||
|
full_backup2() {
|
||||||
|
mkdir -p "$BACKUP_DIR"
|
||||||
|
|
||||||
|
# Get the current date in a formatted string
|
||||||
|
DATE=$(date +"_%Y_%m_%d__%H_%M_%S")
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
# Function to sanitize names for directory
|
||||||
|
sanitize_name() {
|
||||||
|
echo "$1" | sed 's/[^a-zA-Z0-9]/_/g'
|
||||||
|
}
|
||||||
|
|
||||||
|
# Initialize counters
|
||||||
|
HOST_COUNT=0
|
||||||
|
USER_COUNT=0
|
||||||
|
|
||||||
|
# Backup users
|
||||||
|
USERS_FILE="$BACKUP_DIR/users_${NGINX_IP//./_}$DATE.json"
|
||||||
|
RESPONSE=$(curl -s -X GET "$BASE_URL/users" \
|
||||||
|
-H "Authorization: Bearer $(cat $TOKEN_FILE)")
|
||||||
|
if [ -n "$RESPONSE" ]; then
|
||||||
|
echo "$RESPONSE" | jq '.' > "$USERS_FILE"
|
||||||
|
USER_COUNT=$(echo "$RESPONSE" | jq '. | length')
|
||||||
|
echo -e " ✅ ${COLOR_GREEN}Users backup completed 🆗${COLOR_GREY}: $USERS_FILE${COLOR_RESET}"
|
||||||
|
else
|
||||||
|
echo -e " ⛔ ${COLOR_RED}Failed to backup users.${COLOR_RESET}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Backup settings
|
||||||
|
SETTINGS_FILE="$BACKUP_DIR/settings_${NGINX_IP//./_}$DATE.json"
|
||||||
|
RESPONSE=$(curl -s -X GET "$BASE_URL/nginx/settings" \
|
||||||
|
-H "Authorization: Bearer $(cat $TOKEN_FILE)")
|
||||||
|
if [ -n "$RESPONSE" ]; then
|
||||||
|
echo "$RESPONSE" | jq '.' > "$SETTINGS_FILE"
|
||||||
|
echo -e " ✅ ${COLOR_GREEN}Settings backup completed 🆗${COLOR_GREY}: $SETTINGS_FILE${COLOR_RESET}"
|
||||||
|
else
|
||||||
|
echo -e " ⛔ ${COLOR_RED}Failed to backup settings.${COLOR_RESET}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Backup proxy hosts
|
||||||
|
RESPONSE=$(curl -s -X GET "$BASE_URL/nginx/proxy-hosts" \
|
||||||
|
-H "Authorization: Bearer $(cat $TOKEN_FILE)")
|
||||||
|
if [ -n "$RESPONSE" ]; then
|
||||||
|
echo "$RESPONSE" | jq -c '.[]' | while read -r proxy; do
|
||||||
|
HOST_NAME=$(echo "$proxy" | jq -r '.domain_names[0]')
|
||||||
|
SANITIZED_HOST_NAME=$(sanitize_name "$HOST_NAME")
|
||||||
|
HOST_DIR="$BACKUP_DIR/$SANITIZED_HOST_NAME"
|
||||||
|
mkdir -p "$HOST_DIR"
|
||||||
|
HOST_ID=$(echo "$proxy" | jq -r '.id')
|
||||||
|
echo "$proxy" | jq '.' > "$HOST_DIR/proxy_host_${HOST_ID}_${NGINX_IP//./_}$DATE.json"
|
||||||
|
HOST_COUNT=$((HOST_COUNT + 1))
|
||||||
|
|
||||||
|
# Backup SSL certificate if it exists
|
||||||
|
CERTIFICATE_ID=$(echo "$proxy" | jq -r '.certificate_id')
|
||||||
|
if [ "$CERTIFICATE_ID" != "null" ]; then
|
||||||
|
CERT_RESPONSE=$(curl -s -X GET "$BASE_URL/nginx/certificates/$CERTIFICATE_ID" \
|
||||||
|
-H "Authorization: Bearer $(cat $TOKEN_FILE)")
|
||||||
|
if [ -n "$CERT_RESPONSE" ]; then
|
||||||
|
echo "$CERT_RESPONSE" | jq '.' > "$HOST_DIR/ssl_certif_${CERTIFICATE_ID}_${NGINX_IP//./_}$DATE.json"
|
||||||
|
else
|
||||||
|
echo -e " ⛔ ${COLOR_RED}Failed to backup SSL certificate for certificate ID $CERTIFICATE_ID.${COLOR_RESET}"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo -e " ⚠️ ${COLOR_YELLOW}No SSL certificate associated with host ID $HOST_ID.${COLOR_RESET}"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
else
|
||||||
|
echo -e " ⛔ ${COLOR_RED}Failed to backup proxy hosts.${COLOR_RESET}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo -e " ✅ ${COLOR_GREEN}Proxy host backup completed 🆗${COLOR_GREY}: $BACKUP_DIR ${COLOR_RESET}"
|
||||||
|
|
||||||
|
|
||||||
|
# Backup access lists
|
||||||
|
ACCESS_LISTS_FILE="$BACKUP_DIR/access_lists_${NGINX_IP//./_}$DATE.json"
|
||||||
|
RESPONSE=$(curl -s -X GET "$BASE_URL/nginx/access-lists" \
|
||||||
|
-H "Authorization: Bearer $(cat $TOKEN_FILE)")
|
||||||
|
if [ -n "$RESPONSE" ]; then
|
||||||
|
echo "$RESPONSE" | jq '.' > "$ACCESS_LISTS_FILE"
|
||||||
|
echo -e " ✅ ${COLOR_GREEN}Access lists backup completed 🆗${COLOR_GREY}: $ACCESS_LISTS_FILE${COLOR_RESET}"
|
||||||
|
else
|
||||||
|
echo -e " ⛔ ${COLOR_RED}Failed to backup access lists.${COLOR_RESET}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Count total number of backup files
|
||||||
|
TOTAL_BACKUPS=$(find "$BACKUP_DIR" -type f -name "*.json" | wc -l)
|
||||||
|
|
||||||
|
echo -e " ✅ ${COLOR_GREEN}Backup 🆗 ${COLOR_RESET}"
|
||||||
|
echo -e " 📦 ${COLOR_YELLOW}Backup Summary:${COLOR_RESET}"
|
||||||
|
echo -e " - Number of users backed up: ${COLOR_CYAN}$USER_COUNT${COLOR_RESET}"
|
||||||
|
echo -e " - Number of proxy hosts backed up: ${COLOR_CYAN}$HOST_COUNT${COLOR_RESET}"
|
||||||
|
echo -e " - Total number of backup files: ${COLOR_CYAN}$TOTAL_BACKUPS${COLOR_RESET}\n"
|
||||||
|
}
|
||||||
|
|
||||||
full_backup() {
|
full_backup() {
|
||||||
mkdir -p "$BACKUP_DIR"
|
mkdir -p "$BACKUP_DIR"
|
||||||
|
@ -1530,9 +1596,9 @@ full_backup() {
|
||||||
####
|
####
|
||||||
|
|
||||||
# Function to backup a single host configuration and its certificate (if exists)
|
# Function to backup a single host configuration and its certificate (if exists)
|
||||||
backup-host-id() {
|
backup_single_host() {
|
||||||
if [ -z "$HOST_ID" ]; then
|
if [ -z "$HOST_ID" ]; then
|
||||||
echo " 📦 The --host-backup-id option requires a host ID."
|
echo " 📦 The --backup-id option requires a host ID."
|
||||||
usage
|
usage
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
@ -1552,7 +1618,7 @@ backup-host-id() {
|
||||||
mkdir -p "$HOST_DIR"
|
mkdir -p "$HOST_DIR"
|
||||||
|
|
||||||
echo "$RESPONSE" | jq '.' > "$HOST_DIR/proxy_host_${HOST_ID}_${NGINX_IP//./_}_$DATE.json"
|
echo "$RESPONSE" | jq '.' > "$HOST_DIR/proxy_host_${HOST_ID}_${NGINX_IP//./_}_$DATE.json"
|
||||||
echo -e "\n ✅ ${COLOR_GREEN}Proxy host backup completed 🆗${COLOR_GREY}: ${HOST_DIR}/proxy_host_${HOST_ID}_${NGINX_IP//./_}_$DATE.json${COLOR_RESET}"
|
echo -e " ✅ ${COLOR_GREEN}Proxy host backup completed 🆗${COLOR_GREY}: ${HOST_DIR}/proxy_host_${HOST_ID}_${NGINX_IP//./_}_$DATE.json${COLOR_RESET}"
|
||||||
|
|
||||||
# Fetch SSL certificate if it exists
|
# Fetch SSL certificate if it exists
|
||||||
CERTIFICATE_ID=$(echo "$RESPONSE" | jq -r '.certificate_id')
|
CERTIFICATE_ID=$(echo "$RESPONSE" | jq -r '.certificate_id')
|
||||||
|
@ -1604,11 +1670,11 @@ elif [ "$CHECK_TOKEN" = true ]; then
|
||||||
elif [ "$BACKUP" = true ]; then
|
elif [ "$BACKUP" = true ]; then
|
||||||
full_backup
|
full_backup
|
||||||
elif [ "$BACKUP_HOST" = true ]; then
|
elif [ "$BACKUP_HOST" = true ]; then
|
||||||
backup-host-id
|
backup_single_host
|
||||||
elif [ "$RESTORE" = true ]; then
|
elif [ "$RESTORE" = true ]; then
|
||||||
restore_backup
|
restore_backup
|
||||||
elif [ "$RESTORE_HOST" = true ]; then
|
elif [ "$RESTORE_HOST" = true ]; then
|
||||||
restore_host_id
|
restore_single_host
|
||||||
elif [ "$GENERATE_CERT" = true ]; then
|
elif [ "$GENERATE_CERT" = true ]; then
|
||||||
generate_certificate
|
generate_certificate
|
||||||
elif [ "$ENABLE_SSL" = true ]; then
|
elif [ "$ENABLE_SSL" = true ]; then
|
||||||
|
|
Loading…
Reference in a new issue