diff --git a/examples/backup/README.md b/examples/backup/README.md new file mode 100644 index 00000000..e11e06b1 --- /dev/null +++ b/examples/backup/README.md @@ -0,0 +1,17 @@ +# Data Backup + +The `backup` example script shows how to use the SFTPGo REST API to backup your data. + +The script is written in Python and has the following requirements: + +- python3 or python2 +- python [Requests](https://requests.readthedocs.io/en/master/) module + +The provided example tries to connect to an SFTPGo instance running on `127.0.0.1:8080` using the following credentials: + +- username: `admin` +- password: `password` + +and, if you execute it daily, it saves a different backup file for each day of the week. The backups will be saved within the configured `backups_path`. + +Please edit the script according to your needs. diff --git a/examples/backup/backup b/examples/backup/backup new file mode 100755 index 00000000..4bdea55d --- /dev/null +++ b/examples/backup/backup @@ -0,0 +1,36 @@ +#!/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))