Created
August 7, 2012 11:29
-
-
Save owainlewis/3284662 to your computer and use it in GitHub Desktop.
Boxuk database backup utility
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
#!/usr/bin/python3 | |
from urllib.request import * | |
import re | |
import shutil | |
import os | |
# Will look through all available databases and list them | |
# Can also pull down the database into your current directory if required | |
class FetchDatabase(object): | |
def __init__(self, path, db_name): | |
self.path = path | |
self.db_name = db_name | |
def getFileName(self, url,openUrl): | |
if 'Content-Disposition' in openUrl.info(): | |
cd = dict(map( | |
lambda x: x.strip().split('=') if '=' in x else (x.strip(),''), | |
openUrl.info()['Content-Disposition'].split(';'))) | |
if 'filename' in cd: | |
filename = cd['filename'].strip("\"'") | |
if filename: return filename | |
return os.path.basename(urlsplit(openUrl.url)[2]) | |
def download(self, url, fileName=None): | |
""" Downloads a file from remote location to current directory """ | |
r = urlopen(Request(url)) | |
try: | |
fileName = self.getFileName(url,r) | |
with open(fileName, 'wb') as f: | |
shutil.copyfileobj(r,f) | |
finally: | |
r.close() | |
def page(self): | |
return urlopen(self.path).read() | |
def parse_links(self): | |
""" Will return a map of links to available databases """ | |
matches = re.finditer(b'href="?([^\s^"]+)', self.page()) | |
links = map(lambda x: x.group(1), matches) | |
return filter(lambda link: link.decode().startswith(self.db_name), links) | |
def parse_full_links(self): | |
""" Returns a map of the full path to available databases """ | |
urls = self.parse_links() | |
return list(map(lambda x: self.path + x.decode(), urls)) | |
if __name__ == '__main__': | |
# Assumes databases are logically named like db-name-backup-2012-08-05.sql.gz | |
r = FetchDatabase("super_secret_db_url", "db-name") | |
urls = r.parse_full_links() | |
for db in urls: | |
print("Downloading database %s" % db) | |
r.download(db) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment