#!/usr/bin/env python

from datetime import datetime
import sys

import requests

try:
	import urllib.parse as urlparse
except ImportError:
	import urlparse

# change base_url to point to your SFTPGo installation
base_url = "http://127.0.0.1:8080"
# set to False if you want to skip TLS certificate validation
verify_tls_cert = True
# set the credentials for a valid admin here
admin_user = "admin"
admin_password = "password"

# get a JWT token
auth = requests.auth.HTTPBasicAuth(admin_user, admin_password)
r = requests.get(urlparse.urljoin(base_url, "api/v2/token"), auth=auth, verify=verify_tls_cert, timeout=10)
if r.status_code != 200:
	print("error getting access token: {}".format(r.text))
	sys.exit(1)
access_token = r.json()["access_token"]
auth_header = {"Authorization": "Bearer " + access_token}

r = requests.get(urlparse.urljoin(base_url, "api/v2/dumpdata"),
					params={"output-file":"backup_{}.json".format(datetime.today().strftime('%w'))},
					headers=auth_header, verify=verify_tls_cert, timeout=10)
if r.status_code == 200:
	print("backup OK")
else:
	print("backup error, status {}, response: {}".format(r.status_code, r.text))
