-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare_local_db.py
33 lines (27 loc) · 1.1 KB
/
prepare_local_db.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
""" Prepare Local DB - Downloads the latest db backup and uploads to local postgres instance """
import subprocess
from pathlib import Path
import boto3
S3_BUCKET: str = "amp-aurora-backup-prod"
LOCAL_DIR_PATH: Path = Path("data", "s3_files")
if __name__ == "__main__":
s3_client = boto3.client("s3")
db_backups: list = []
for key in s3_client.list_objects(Bucket=S3_BUCKET)["Contents"]:
if "aws_aurora_backup/" in key["Key"] and "prodenv" in key["Key"]:
db_backups.append(key)
db_backups.sort(key=lambda x: x["LastModified"])
file_name: str = db_backups[-1]["Key"].split("/")[-1]
s3_path: str = db_backups[-1]["Key"]
LOCAL_DIR_PATH.mkdir(parents=True, exist_ok=True)
local_file_path: Path = LOCAL_DIR_PATH / file_name
if not local_file_path.exists():
print(f">>> downloading {s3_path}")
s3_client.download_file(S3_BUCKET, s3_path, str(local_file_path))
subprocess.call(
"psql "
+ "postgres://admin:secret@localhost:5432/accessibility_monitoring_app "
+ "< "
+ str(local_file_path),
shell=True,
)