-
Notifications
You must be signed in to change notification settings - Fork 5
/
backup.py
executable file
·134 lines (122 loc) · 4.24 KB
/
backup.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#! /bin/python3
# -*- coding: utf-8 -*-
# Copyright: (c) 2021, Davinder Pal (@116davinder) <[email protected]>
import boto3
import logging
from common import common
import sys
import botocore.config
from botocore.exceptions import ClientError
import json
import tempfile
import pendulum
import argparse
class SSMBackup:
def __init__(self, ssm_paths, ssm_region, bucket, bucket_prefix):
self.ssm_region = ssm_region
self.ssm_client_config = botocore.config.Config(
region_name=self.ssm_region,
retries={
"max_attempts": 3,
},
)
self.s3_bucket = bucket
self.s3_bucket_prefix = bucket_prefix
self.ssm_paths = ssm_paths
self.temp_file = tempfile.NamedTemporaryFile(
prefix="ssm_backup_", suffix=".json"
)
self.backup_file = self.s3_bucket_prefix + "SSM_BACKUP_" + pendulum.today().strftime("%Y-%m-%d") + ".json"
def _get_ssm_values(self):
_results = []
try:
ssm_client = boto3.client("ssm", config=self.ssm_client_config)
except ClientError as e:
logging.error(e)
sys.exit(1)
for _path in self.ssm_paths:
try:
logging.info(f"pulling data from {_path}")
_output = ssm_client.get_parameter(
Name=_path,
WithDecryption=True,
)
_results.append(_output["Parameter"])
except ssm_client.exceptions.ParameterNotFound as e:
logging.error(f"No Path found {_path}: {e}")
except ClientError as e:
logging.info("Please blame yourself for this error or AWS")
logging.error(e)
sys.exit(1)
if len(_results) > 0:
with open(self.temp_file.name, "w") as f:
for item in _results:
_dict = {
"Name": item["Name"],
"Type": item["Type"],
"Value": item["Value"],
"DataType": item["DataType"],
}
f.write(json.dumps(_dict))
f.write("\n")
logging.debug(f"storing copy of {item['Name']} in temporary file")
logging.debug(f"temp backup file: {self.temp_file.name}")
else:
logging.error("I am not AI, please provide correct SSM Paths")
sys.exit(1)
def backup(self):
self._get_ssm_values()
try:
s3_client = boto3.client("s3")
s3_client.upload_file(self.temp_file.name, self.s3_bucket, self.backup_file)
logging.info(
f"backup is parked in AWS S3 at s3://{self.s3_bucket}/{self.backup_file}"
)
self.temp_file.close()
logging.info("cleaned temp files")
except ClientError as e:
logging.info("Please blame yourself for this error or AWS")
logging.error(e)
if __name__ == "__main__":
common.setLoggingFormat()
parser = argparse.ArgumentParser(description='AWS Parameter Store Backup Script Options')
parser.add_argument(
"--ssm-paths",
nargs='+',
required=True,
type=str,
help="Provide ssm path with space separated"
" Example: --ssm-paths /Test/Path /Test1/Path"
)
parser.add_argument(
"--region",
nargs=1,
required=True,
type=str,
help="Provide ssm region from where backup should be taken."
" Example: --region us-east-1"
)
parser.add_argument(
"--bucket",
nargs=1,
required=True,
type=str,
help="Provide name of the aws s3 bucket."
" Example: --bucket test-davinder-s3"
)
parser.add_argument(
"--bucket-prefix",
nargs=1,
type=str,
required=True,
help="Provide bucket prefix of the aws s3 bucket."
" Example: --bucket-prefix SSM/"
)
args = parser.parse_args()
backup = SSMBackup(
ssm_paths=args.ssm_paths,
ssm_region=args.region[0].strip(),
bucket=args.bucket[0].strip(),
bucket_prefix=args.bucket_prefix[0].strip(),
)
backup.backup()