-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_function.py
More file actions
66 lines (51 loc) · 1.63 KB
/
Copy pathlambda_function.py
File metadata and controls
66 lines (51 loc) · 1.63 KB
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
#!/usr/bin/python
import datetime
import time
import subprocess
import boto3
import os
import shutil
import gzip
# config
AWS_KEY = os.environ['LambdaAccessKeyId']
AWS_SECRET = os.environ['LambdaSecretAccessKey']
BUCKET = os.environ['s3Bucket']
#DB array goes host, db, user, password
DATABASES = [(os.environ['hostDB1'],os.environ['databaseDB1'], os.environ['userDB1'], os.environ['passDB1'])]
def lambda_handler(event, context):
# move binary to tmp folder and set as executable
shutil.copyfile('/var/task/mysqldump', '/tmp/mysqldump')
os.chmod('/tmp/mysqldump', 0755)
# get current date and time
timer = datetime.datetime.utcnow().isoformat()+ "Z"
timer = str(timer).replace(':', '.')
# init s3 client
s3 = boto3.client('s3')
# folder & path stuff
dt = datetime.date.today()
tf = str(dt.year)+'-'+str(dt.month)
path_base = '/tmp/dumps/tf'
#create dir to storage dumps
subprocess.call(['mkdir', '-p', path_base])
# loops db and make backups
for i in DATABASES:
host = i[0]
database = i[1]
user = i[2]
password = i[3]
dump_name = 'production-'+database+'-'+timer+'.sql'
file_name = os.path.join(path_base, dump_name)
print ''
print '- - - - - - - - - - - - - - - - - - - - - - - - - - - - '
print ''
print 'Dump from: '+database
#run mysqldump as a subprocess
p = subprocess.Popen("/tmp/mysqldump -h" + host + " -u" + user + " -p'" + password + "' --all-databases > " + file_name, shell=True)
p.communicate()
# sleep while we wait for dump to complete
time.sleep(20)
# upload to s3
s3.upload_file(file_name,BUCKET,dump_name)
# lambda cleanup
os.remove('/tmp/mysqldump')
os.remove(file_name)