Skip to content

Instantly share code, notes, and snippets.

@alexkutsan
Created June 13, 2022 13:00
Show Gist options
  • Save alexkutsan/2819bf1361022e55dba164805e0777b8 to your computer and use it in GitHub Desktop.
Save alexkutsan/2819bf1361022e55dba164805e0777b8 to your computer and use it in GitHub Desktop.
Nginx proxy manager migrate
import requests
import argparse
import json
parser = argparse.ArgumentParser(description='Transfer proxy hosts from one nginx_proxy_manager instance to another')
parser.add_argument('--source', type=str, help='url of source nginx_proxy_manager')
parser.add_argument('--source_auth', type=str, help='Authorization header of source nginx proxy manager')
parser.add_argument('--dest', type=str, help='url of destanation nginx_proxy_manager or file')
parser.add_argument('--dest_auth', type=str, help='Authorization header of dest nginx proxy manager')
args = parser.parse_args()
def save(url, auth):
response = requests.get(f'{url}/api/nginx/proxy-hosts',
headers={'Authorization': auth})
with open("proxy-hosts.json", 'w') as f:
json.dump(response.json(), f)
return response.json()
def post_body(domain_names, forward_host, forward_port):
return {
"domain_names": domain_names,
"forward_scheme": "http",
"forward_host": forward_host,
"forward_port": forward_port,
"access_list_id": "0",
"certificate_id": 0,
"meta": {
"letsencrypt_agree": False,
"dns_challenge": False
},
"advanced_config": "",
"locations": [],
"block_exploits": False,
"caching_enabled": False,
"allow_websocket_upgrade": False,
"http2_support": False,
"hsts_enabled": False,
"hsts_subdomains": False,
"ssl_forced": False
}
if args.source:
if args.source.startswith(("http://", "https://")):
source = save(args.source, args.source_auth)
else:
with open(args.source, 'r') as f:
source = json.load(f)
for host in source:
domain_names = host["domain_names"]
forward_host = host["forward_host"]
forward_port = int(host["forward_port"])
data = post_body(domain_names, forward_host, forward_port)
response = requests.post(f'{args.dest}/api/nginx/proxy-hosts',
json = data,
headers={'Authorization': args.dest_auth})
if response.ok:
print(f"Transfered {domain_names}, {forward_host}, {forward_port}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment