-
Notifications
You must be signed in to change notification settings - Fork 12
/
backup_protectedtext.py
executable file
·71 lines (55 loc) · 2.13 KB
/
backup_protectedtext.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/python3
# Decription
# -----------
# * Backup secrets from www.protectedtext.com to local storage
# * If file is changed, keep previous version with date postfix
# * Only backups once per day
# * Can be decrypted using `base64 -d BACKUP_FILE | openssl aes-256-cbc -d -k PASSWORD`
# * More info <http://developer.run/13>
# Author: [Dmitry](http://dmi3.net) [Source](https://github.com/dmi3/bin)
# Usage
# -----
# Run from commandline or add to cron
import urllib.request
import json
import os
import shutil
import datetime
import logging
import sys
SECRETS = ["anything"] # name of secret i.e. `https://www.protectedtext.com/anything` → `anything`
PATH = "/media/backup/"
SUCESS_PING_URL = "https://hchk.io/..." # https://healthchecks.io/ url
SECONDS_IN_DAY = 86400
logging.basicConfig(filename=PATH + 'manager.log',
level=logging.DEBUG,
format='%(asctime)s %(levelname)s %(message)s')
def log_except_hook(*exc_info):
exc = "".join(traceback.format_exception(*exc_info))
logging.critical(exc)
failure(exc)
sys.excepthook = log_except_hook
logging.debug("Start")
for SECRET in SECRETS:
SECRET_PATH = PATH + SECRET
if os.path.isfile(SECRET_PATH) and (datetime.datetime.now().timestamp() - os.path.getmtime(SECRET_PATH)) < SECONDS_IN_DAY:
logging.debug("Skipping as files were updated today")
exit()
r = urllib.request.urlopen("https://www.protectedtext.com/%s?action=getJSON" % SECRET)
content = json.loads(r.read().decode("utf-8"))['eContent']
if content == "":
logging.debug("There no secret " + SECRET)
exit()
if os.path.isfile(SECRET_PATH):
f = open(SECRET_PATH, "r")
latest = f.read()
f.close()
if latest == content:
logging.debug("No changes " + SECRET)
continue
shutil.move(SECRET_PATH, SECRET_PATH + datetime.datetime.now().strftime("_%d_%m_%Y"))
with open(SECRET_PATH, "w") as f:
f.write(content)
logging.debug("Wrote " + SECRET)
# If app produces exception or exit(), will get error from Healthcheks
urllib.request.urlopen(SUCESS_PING_URL)