Last active
September 13, 2022 12:31
-
-
Save timtadh/5630947 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# backup-trello | |
description "regular background program processing daemon" | |
start on (local-filesystems and net-device-up IFACE!=lo) | |
setuid hendersont | |
setgid hendersont | |
env HOME=/home/hendersont | |
export HOME | |
exec /usr/bin/env python $HOME/.trello/trello_backup.py | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, sys, json, glob, time | |
import datetime | |
from logging import getLogger, basicConfig | |
basicConfig(level='DEBUG') | |
log = getLogger('trello_backup') | |
import requests | |
export_url = 'https://trello.com/board/[[REDACTED]]/[[REDACTED]].json' | |
login_url = 'https://trello.com/login' | |
auth_url = 'https://trello.com/authenticate' | |
creds = { | |
'user':'[[REDACTED]]', | |
'password':'[[REDACTED]]', | |
'submit':'Log In', 'returnUrl':'/' | |
} | |
HOME = os.environ['HOME'] | |
PATH = os.path.join(HOME, '.trello') | |
def export(): | |
trello = requests.session() | |
trello.get(login_url).text | |
trello.post(auth_url, creds).text | |
return trello.get(export_url).json() | |
def time(): | |
return datetime.datetime.now().isoformat() | |
def name(): | |
return "trello-dump-%s.json" % time() | |
def clean(): | |
now = datetime.datetime.now() | |
paths = sorted(glob.glob(os.path.join(PATH, '*.json'))) | |
if len(paths) < 5: return | |
for path in paths[:-5]: | |
t_format = '%Y-%m-%dT%H:%M:%S.%f' | |
str_time = ( | |
os.path.splitext(os.path.basename(path))[0].lstrip('trello-dump-') | |
) | |
dt = datetime.datetime.strptime(str_time, t_format) | |
if (now - dt) > datetime.timedelta(weeks=2): | |
os.unlink(path) | |
log.info('deleted %s' % path) | |
clean() | |
RETRIES = 5 | |
for i in xrange(RETRIES): | |
try: | |
data = export() | |
break | |
except: | |
if i+1 == RETRIES: | |
raise | |
else: | |
time.sleep(5) | |
with open(os.path.join(PATH, name()), 'w') as f: | |
json.dump(data, f, indent=2) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment