-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_function.py
61 lines (46 loc) · 1.4 KB
/
lambda_function.py
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
import json
import os
from datetime import datetime
from urllib import parse
import boto3
def timeStamp() -> str:
"""
Returns the current time in the format YYYY-MM-DD
Returns:
-------
str
The current time in the format YYYY-MM-DD
"""
return datetime.now().strftime("%Y-%m-%d")
def backupMysql(prefix: str = "", suffix: str = "") -> str:
"""
Creates a backup of the MySQL database
Parameters:
-----------
prefix: str
The prefix to add to the file name
suffix: str
The suffix to add to the file name
Returns:
-------
str
The path to the backup file
"""
fileName = "/tmp/" + prefix + timeStamp() + suffix + ".sql"
os.system(
f'mysqldump -p{os.getenv("PASSWORD")} --port {os.getenv("PORT")} --host {os.getenv("HOST")} --user root {os.getenv("DATABASE_NAME")} > {fileName}'
)
return fileName
def lambda_handler(event, context):
bucketName = os.getenv("BUCKET_NAME")
bucketFolder = os.getenv("BUCKET_FOLDER")
tags = {} # NOTE: you can add tags if you want. Ex.: tags = {"tag1": "value1", "tag2": "value2"}
s3 = boto3.client("s3")
fileName = backupMysql()
s3.upload_file(
fileName,
bucketName,
fileName.replace("/tmp", bucketFolder),
ExtraArgs={"Tagging": parse.urlencode(tags)},
)
return {"statusCode": 200, "body": json.dumps("success")}