def rotate(self, filename, object_name, objects_list, rotate_limit=ROTATE_LIMIT): """ Arguments: filename: filename of backup target object_name: name of object on Swift objects_list: list of objects on Swift rotate_limit: limitation of backup rotation """ # copy current object to new object timestamp = datetime.now().strftime("%Y%m%d-%H%M%S") new_object_name = object_name + '_' + timestamp rc = client.copy_object(self.token, self.storage_url, self.container_name, object_name, new_object_name, verify=self.verify) if rc != 201: raise RuntimeError('Failed to copy object "%s"' % new_object_name) # create new object rc = client.create_object(self.token, self.storage_url, self.container_name, filename, verify=self.verify) if rc != 201: raise RuntimeError('Failed to create the object "%s"' % object_name) # delete old objects archive_list = [obj for obj in objects_list if obj.startswith(object_name + '_')] archive_list.reverse() [client.delete_object(self.token, self.storage_url, self.container_name, obj, verify=self.verify) for i, obj in enumerate(archive_list) if i + 1 > rotate_limit - 1] return True
def delete_backup_data(self, object_name): """ Argument: object_name: delete target object name """ if (client.is_container(self.token, self.storage_url, self.container_name, verify=self.verify) and client.is_object(self.token, self.storage_url, self.container_name, object_name, verify=self.verify)): rc = client.delete_object(self.token, self.storage_url, self.container_name, object_name, veirfy=self.verify) if not rc == 204: raise RuntimeError('Failed to delete the object "%s"' % object_name) return True else: raise RuntimeError('No such object "%s"' % object_name)