Commits

Fabian Topfstedt committed 46b4d10

delete dispensable snapshots

  • Participants
  • Parent commits bebf1cf

Comments (0)

Files changed (1)

File snapshotter.py

     def _get_volume_ids(self, instance):
         return [b.volume_id for b in instance.block_device_mapping.values()]
 
+    def delete_dispensable_snapshots(self):
+        """
+        Deletes snapshots that are not required anymore.
+        """
+        c = EC2Connection(self.aws_key, self.aws_secret)
+        snapshots = [s for s in c.get_all_snapshots(owner='self') if \
+            s.description.startswith('snapshotter:')]
+        for s in snapshots:
+            now = datetime.now()
+            dt = datetime(*map(int, s.description.split(':')[1:6]))
+            delta = (now - dt)
+            if delta.days == 0:
+                continue  # all for 24 hours
+            elif delta.days < 8:
+                if dt.hour == 0:
+                    continue  # first per day for last week
+            elif delta.days < 31:
+                if dt.hour == 0 and dt.weekday() == 0:
+                    continue  # first per week for last month
+            elif delta.days < 365:
+                if dt.day < 8 and dt.hour == 0 and dt.weekday() == 0:
+                    continue  # first per month for last year
+            s.delete()
+
     def create_snapshots(self):
         """
         Create snapshots of all EBS volumes that are currently attached.
         mysql_username=MYSQL_USERNAME,
         mysql_password=MYSQL_PASSWORD)
     s.create_snapshots()
-
+    s.delete_dispensable_snapshots()