· 9 years ago · Jan 04, 2017, 01:30 AM
1#!/usr/bin/python
2
3# This is a hacked version of the thing we use to download dumps to boot slaves on production. Don't judge. ;)
4
5import os
6import sys
7import re
8import time
9import shutil
10import random
11import tempfile
12import subprocess
13import ConfigParser
14import boto.s3.connection
15from subprocess import CalledProcessError, check_output
16
17from pprint import pprint
18
19configuration_defaults = {
20 'bucket': 'marketplace-local-dumps',
21 'access_key': 'RIIIIIIIIIIIIIIIIIIIIIIIIIICK',
22 'secret_key': 'MOOOOOOOOOOOOORTY'
23}
24
25
26def get_configuration():
27 configuration = configuration_defaults
28
29 return configuration
30
31
32def get_backup_key(connection, configuration):
33 bucket = connection.get_bucket(configuration['bucket'])
34
35 keys = bucket.list()
36 print keys
37
38 key_names = [k.name for k in keys if 'marketplace' in k.name]
39 print key_names
40
41 key_names.sort()
42 key_names.reverse()
43
44 date_stamps = [k[:19] for k in key_names]
45
46 date_stamps.sort()
47 date_stamps.reverse()
48 date_stamps = date_stamps[:1]
49
50 date_stamp = random.choice(date_stamps)
51 key_names = [k for k in key_names if date_stamp in k]
52 key_name = random.choice(key_names)
53
54 print "Final choice for key_name:", key_name
55
56 keys = [k for k in keys if k.name == key_name]
57
58 key = keys[0]
59
60 print key
61
62 return key
63
64
65def prepare_backup_from_key(key):
66
67 local_file_name = "mysql.tgz"
68 # Checking to see if you've already downloaded the file
69
70 if os.path.exists('mysql.tgz'):
71
72 subprocess.check_call("rm -f mysql.tgz", shell=True)
73
74 print "Removing old database tar"
75
76 subprocess.check_call("rm -rf var", shell=True)
77
78 print "Removing temp directory"
79
80 if os.path.exists('mysql'):
81
82 subprocess.check_call("mv mysql mysql.`date +%s`", shell=True)
83
84 print "Moving current mysql to backup directory"
85
86 #subprocess.check_call("mkdir -p mysql", shell=True)
87
88 key.get_contents_to_filename(local_file_name, headers=None, cb=get_progress)
89
90 print "Unzipping mysql.tgz"
91
92 subprocess.check_call("tar -xvzf mysql.tgz", shell=True)
93
94 #Added to check to see if we have a marketplace directory
95
96
97 subprocess.check_call("mv var/lib/mysql ../marketplace/mysql", shell=True)
98
99
100 print "Removing mysql.tgz"
101
102 subprocess.check_call("rm -f mysql.tgz", shell=True)
103
104 subprocess.check_call("rm -rf var", shell=True)
105
106 print "Dump should be ready, now provision vagrant"
107
108def get_progress(downloaded, size):
109
110 print "Remaining:", ((size - downloaded)/1024)/1024,'Mb'
111
112def clean_up_temporary_files(tmpdir):
113
114 if os.path.isdir(tmpdir):
115 print "Removing " + tmpdir
116 shutil.rmtree(tmpdir)
117
118
119def restore_from_s3():
120 configuration = get_configuration()
121 print "Configuration", configuration
122
123 connection = boto.s3.connection.S3Connection(aws_access_key_id=configuration['access_key'],
124 aws_secret_access_key=configuration['secret_key'])
125
126 key = get_backup_key(connection, configuration)
127
128 prepare_backup_from_key(key)
129
130
131restore_from_s3()