Skip to content

Commit

Permalink
add an example backup script
Browse files Browse the repository at this point in the history
  • Loading branch information
drakkan committed May 16, 2021
1 parent 60cfbd2 commit 7d059ef
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
17 changes: 17 additions & 0 deletions examples/backup/README.md
Original file line number Diff line number Diff line change
@@ -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.
36 changes: 36 additions & 0 deletions examples/backup/backup
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit 7d059ef

Please sign in to comment.