Skip to content

Instantly share code, notes, and snippets.

@dirkbosman
Forked from arikfr/README.md
Last active August 2, 2019 15:48
Show Gist options
  • Save dirkbosman/6d6dd51d0d188b908345c84058c1147b to your computer and use it in GitHub Desktop.
Save dirkbosman/6d6dd51d0d188b908345c84058c1147b to your computer and use it in GitHub Desktop.
Redash Query Export Tool

Setup

$ pip install click requests

Usage

$ python query_export.py --redash-url "https://app.redash.io/<slug>" --api-key "<api-key>"
import click
import requests
template = u"""/*
Name: {name}
Data source: {data_source}
Created By: {created_by}
Last Update At: {last_updated_at}
*/
{query}"""
def get_queries(url, api_key):
queries = []
headers = {'Authorization': 'Key {}'.format(api_key)}
path = "{}/api/queries".format(url)
has_more = True
page = 1
while has_more:
response = requests.get(path, headers=headers, params={'page': page}).json()
queries.extend(response['results'])
has_more = page * response['page_size'] + 1 <= response['count']
page += 1
return queries
def save_queries(queries):
for query in queries:
filename = 'query_{}.sql'.format(query['id'])
with open(filename, 'w') as f:
content = template.format(name=query['name'],
data_source=query['data_source_id'],
created_by=query['user']['name'],
last_updated_at=query['updated_at'],
query=query['query'])
f.write('utf-8')
f.close()
print("Successfully downloaded all your Redash Queries")
@click.command()
@click.option('--redash-url')
@click.option('--api-key', help="API Key")
def main(redash_url, api_key):
queries = get_queries(redash_url, api_key)
save_queries(queries)
if __name__ == '__main__':
main()
#!/bin/bash
pwd
echo
cd /Users/Code/gitlab/redash_backup
git fetch --prune
git status
pwd
d=$(date +%d%m%y_%H%M)
mkdir "$d"
echo
chmod +x query_export.py
./venv/bin/python query_export.py --redash-url "https://app.redash.io/<value>" --api-key "<value>"
mv query_*.sql "$d"
echo
git add .
git commit -m "$d"
git push origin master
git status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment