-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.py
146 lines (121 loc) · 4.28 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
135
136
137
138
139
140
141
142
143
144
145
146
import datetime
import socket
import boto3
import requests
"""
Script to make daily/monthly or whatever backup of volume using AWS API
"""
VOLUME_ID = ""
"""ID of volume"""
REGION_NAME = ""
"""AWS region name containing above volume (eu-central-1, eu-west-1 etc)"""
RETENTION_DAYS = 3
"""How many days should the snapshots last before deletion (integer)"""
INTERVAL_TYPE = "daily"
"""Interval type: daily/weekly/monthly etc"""
DRY_RUN = False
"""Dry run switch for testing purposes"""
EMOJI = ""
HOOK_URL = ""
"""
Notifier variables:
EMOJI: used to set rocket.chat bot avatar
HOOK_URL: URL to API of notifications
"""
class Host:
def __init__(self):
self.today = datetime.date.today().strftime('%Y-%m-%d')
self.now = datetime.datetime.now(datetime.timezone.utc)
self.name = socket.gethostname()
def get_hostname(self):
"""Return hostname of machine running script"""
return self.name
def get_today(self):
"""Return today's date"""
return self.today
def get_retention_time(self):
"""Return date of retention"""
return self.now - datetime.timedelta(days=int(RETENTION_DAYS)) + datetime.timedelta(seconds=int(10))
class Snapshot:
def __init__(self):
self.resource = boto3.client('ec2', region_name=REGION_NAME)
self.created_id = None
self.deleted_ids = []
@staticmethod
def _get_snapshot_description():
"""Return new snapshot description"""
return '{}-{}-backup-{}'.format(Host().get_hostname(), INTERVAL_TYPE, Host().get_today())
def _list_snapshots(self):
"""Return all snapshots tagged as CreatedBy with value AutomatedBackupDaily"""
return self.resource.describe_snapshots(
Filters=[
{
'Name': 'tag:CreatedBy',
'Values': [
'AutomatedBackup{}'.format(INTERVAL_TYPE.capitalize())
]
}
]
)
def create(self):
"""Creates new snapshot of volume"""
snap = self.resource.create_snapshot(
Description=self._get_snapshot_description(),
VolumeId=VOLUME_ID,
TagSpecifications=[
{
'ResourceType': 'snapshot',
'Tags': [
{
'Key': 'CreatedBy',
'Value': 'AutomatedBackup{}'.format(INTERVAL_TYPE.capitalize())
},
]
},
],
DryRun=DRY_RUN
)
self.created_id = snap['SnapshotId']
def delete_old(self):
"""Delete old snapshots of volume if retention time exceeded, send notify after all"""
retention_time = Host().get_retention_time()
snapshots = self._list_snapshots()
for snap in snapshots['Snapshots']:
snapshot_id = snap['SnapshotId']
start_time = snap['StartTime']
if start_time <= retention_time:
self.resource.delete_snapshot(
SnapshotId=snapshot_id,
DryRun=DRY_RUN
)
self.deleted_ids.append(snapshot_id)
notify = Notifier()
notify.send(self.created_id, self.deleted_ids)
class Notifier:
def __init__(self):
self.url = HOOK_URL
self.emoji = EMOJI
def send(self, created_id, deleted_ids):
"""
Send notification with json data.
:param created_id
:type created_id str
:param deleted_ids
:type deleted_ids list
"""
if deleted_ids:
deleted_ids = "\n".join(deleted_ids)
else:
deleted_ids = 'none'
data = {
'username': '{} {} Backup'.format(INTERVAL_TYPE.capitalize(), Host().get_hostname().capitalize()),
'icon_emoji': '{}'.format(self.emoji),
'text': '[{}] Created snapshot of {} (snapshot id: {})\n'
'Deleted old snapshots with IDs:\n{}'.format(INTERVAL_TYPE.upper(), VOLUME_ID, created_id,
deleted_ids)
}
requests.post(self.url, json=data)
if __name__ == '__main__':
backup = Snapshot()
backup.create()
backup.delete_old()