cinder-backup导入导出功能

backup-export

cinder backup支持将元数据序列化导出(export record),这样即使数据库中的数据丢失了,也能从导出的元数据中快速恢复。

usage: cinder backup-export

范例:

[root@node1 ~]# cinder backup-export 51e2c303-6490-433c-a097-cf3742a7e991 
+----------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Property       | Value                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
+----------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| backup_service | cinder.backup.drivers.nfs                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| backup_url     | eyJzdGF0dXMiOiAiYXZhaWxhYmxlIiwgInRlbXBfc25hcHNob3RfaWQiOiBudWxsLCAiZGlzcGxheV9uYW1lIjogbnVsbCwgImF2YWlsYWJpbGl0eV96b25lIjogIm5vdmEiLCAiZGVsZXRlZCI6IGZhbHNlLCAidm9sdW1lX2lkIjogIjA0ODAyZDQ4LWJmYTktNDY2Yy1iM2E4LTQyNGIwMTRkNzVkNSIsICJyZXN0b3JlX3ZvbHVtZV9pZCI6IG51bGwsICJ1cGRhdGVkX2F0IjogIjIwMTctMDctMTdUMDQ6NTM6MTZaIiwgImhvc3QiOiAibm9kZTEiLCAic25hcHNob3RfaWQiOiBudWxsLCAidXNlcl9pZCI6ICIzZjU2ZDI5NzZlYWM0NjhkYmU4MzZlYmNjNDQwMDg1OCIsICJzZXJ2aWNlX21ldGFkYXRhIjogImJhY2t1cCIsICJpZCI6ICI1MWUyYzMwMy02NDkwLTQzM2MtYTA5Ny1jZjM3NDJhN2U5OTEiLCAic2l6ZSI6IDEsICJvYmplY3RfY291bnQiOiA1LCAiZGVsZXRlZF9hdCI6IG51bGwsICJjb250YWluZXIiOiAiNTEvZTIvNTFlMmMzMDMtNjQ5MC00MzNjLWEwOTctY2YzNzQyYTdlOTkxIiwgInNlcnZpY2UiOiAiY2luZGVyLmJhY2t1cC5kcml2ZXJzLm5mcyIsICJkcml2ZXJfaW5mbyI6IHt9LCAiY3JlYXRlZF9hdCI6ICIyMDE3LTA3LTE3VDA0OjUyOjM4WiIsICJkaXNwbGF5X2Rlc2NyaXB0aW9uIjogbnVsbCwgImRhdGFfdGltZXN0YW1wIjogIjIwMTctMDctMTdUMDQ6NTI6MzhaIiwgInBhcmVudF9pZCI6IG51bGwsICJudW1fZGVwZW5kZW50X2JhY2t1cHMiOiAwLCAiZmFpbF9yZWFzb24iOiBudWxsLCAicHJvamVjdF9pZCI6ICI3YjdkNjYyN2NmMDQ0ZDgwYWE2ZWMzZjJmNGFkZTJhYiIsICJ0ZW1wX3ZvbHVtZV9pZCI6IG51bGx9 |
+----------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

cinder.backup.manager.BackupManager#export_record:


    backup_service = self.service.get_backup_driver(context)  
    # 得到 cinder.backup.drivers.nfs.NFSBackupDriver 服务
    
    driver_info = backup_service.export_record(backup)
    # 如果特殊的后端驱动需要补充其他必要信息,需要通过这个方法导出。默认驱动没有额外信息。cinder.backup.driver.BackupDriver#export_record 返回一个{}
    
    backup_url = backup.encode_record(driver_info=driver_info)
    # 把backup对象信息和driver_info 做Base64序列号成字符串,实例如下
    
    backup_record['backup_url'] = backup_url

cinder.objects.backup.Backup#encode_record:

def encode_record(self, **kwargs):
    """Serialize backup object, with optional extra info, into a string."""
    # We don't want to export extra fields and we want to force lazy
    # loading, so we can't use dict(self) or self.obj_to_primitive
    record = {name: field.to_primitive(self, name, getattr(self, name))
              for name, field in self.fields.items()}
    # We must update kwargs instead of record to ensure we don't overwrite
    # "real" data from the backup
    kwargs.update(record)
    retval = jsonutils.dump_as_bytes(kwargs)
    return base64.encode_as_text(retval)


record:        
{
    'availability_zone': u'nova',
    'updated_at': '2017-07-17T04: 53: 16Z',
    'container': u'51/e2/51e2c303-6490-433c-a097-cf3742a7e991',
    'snapshot_id': None,
    'service_metadata': u'backup',
    'id': '51e2c303-6490-433c-a097-cf3742a7e991',
    'size': 1,
    'object_count': 5,
    'deleted_at': None,
    'user_id': u'3f56d2976eac468dbe836ebcc4400858',
    'service': u'cinder.backup.drivers.nfs',
    'display_description': None,
    'data_timestamp': '2017-07-17T04: 52: 38Z',
    'parent_id': None,
    'project_id': u'7b7d6627cf044d80aa6ec3f2f4ade2ab',
    'temp_volume_id': None,
    'status': u'available',
    'deleted': False,
    'restore_volume_id': None,
    'host': u'node1',
    'volume_id': '04802d48-bfa9-466c-b3a8-424b014d75d5',
    'display_name': None,
    'temp_snapshot_id': None,
    'fail_reason': None,
    'created_at': '2017-07-17T04: 52: 38Z',
    'num_dependent_backups': 0
}

序列化后:
u'eyJzdGF0dXMiOiAiYXZhaWxhYmxlIiwgInRlbXBfc25hcHNob3RfaWQiOiBudWxsLCAiZGlzcGxheV9uYW1lIjogbnVsbCwgImF2YWlsYWJpbGl0eV96b25lIjogIm5vdmEiLCAiZGVsZXRlZCI6IGZhbHNlLCAidm9sdW1lX2lkIjogIjA0ODAyZDQ4LWJmYTktNDY2Yy1iM2E4LTQyNGIwMTRkNzVkNSIsICJyZXN0b3JlX3ZvbHVtZV9pZCI6IG51bGwsICJ1cGRhdGVkX2F0IjogIjIwMTctMDctMTdUMDQ6NTM6MTZaIiwgImhvc3QiOiAibm9kZTEiLCAic25hcHNob3RfaWQiOiBudWxsLCAidXNlcl9pZCI6ICIzZjU2ZDI5NzZlYWM0NjhkYmU4MzZlYmNjNDQwMDg1OCIsICJzZXJ2aWNlX21ldGFkYXRhIjogImJhY2t1cCIsICJpZCI6ICI1MWUyYzMwMy02NDkwLTQzM2MtYTA5Ny1jZjM3NDJhN2U5OTEiLCAic2l6ZSI6IDEsICJvYmplY3RfY291bnQiOiA1LCAiZGVsZXRlZF9hdCI6IG51bGwsICJjb250YWluZXIiOiAiNTEvZTIvNTFlMmMzMDMtNjQ5MC00MzNjLWEwOTctY2YzNzQyYTdlOTkxIiwgInNlcnZpY2UiOiAiY2luZGVyLmJhY2t1cC5kcml2ZXJzLm5mcyIsICJkcml2ZXJfaW5mbyI6IHt9LCAiY3JlYXRlZF9hdCI6ICIyMDE3LTA3LTE3VDA0OjUyOjM4WiIsICJkaXNwbGF5X2Rlc2NyaXB0aW9uIjogbnVsbCwgImRhdGFfdGltZXN0YW1wIjogIjIwMTctMDctMTdUMDQ6NTI6MzhaIiwgInBhcmVudF9pZCI6IG51bGwsICJudW1fZGVwZW5kZW50X2JhY2t1cHMiOiAwLCAiZmFpbF9yZWFzb24iOiBudWxsLCAicHJvamVjdF9pZCI6ICI3YjdkNjYyN2NmMDQ0ZDgwYWE2ZWMzZjJmNGFkZTJhYiIsICJ0ZW1wX3ZvbHVtZV9pZCI6IG51bGx9'

cinder.objects.backup.Backup#encode_record 这个方法注意下,以后可以用于对象序列化!!

backup-import

如果backup记录信息被从数据库里删除了,可以通过之前export出的backup_url做恢复,恢复出的数据连backup_id都跟之前一样。

usage: cinder backup-import
backup_service : 驱动的名字
backup_url : export出的序列化元数据

范例:

[root@node1 ~]# cinder backup-import cinder.backup.drivers.nfs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tZV9pZCI6IG51bGx9
+----------+--------------------------------------+
| Property | Value                                |
+----------+--------------------------------------+
| id       | 51e2c303-6490-433c-a097-cf3742a7e991 |
| name     | None                                 |
+----------+--------------------------------------+

逻辑:

  1. backup_url 反序列化 backup_option,检查数据库里是否存在有效的backup_option['id']记录,如果不存在则创建一个id = backup_option['id']的back记录。并且'user_id'= context.user_id,'project_id'= context.project_id,'volume_id'= IMPORT_VOLUME_ID,'status'= fields.BackupStatus.CREATING,
  2. 把backup_url的其它元数据保存至步骤1创建的back记录中。

代码分析:

cinder.backup.manager.BackupManager#import_record


def import_record(self,
                  context,
                  backup,
                  backup_service,
                  backup_url,
                  backup_hosts):
                  
    backup_options = backup.decode_record(backup_url) # backup_url反序列化

    # Extract driver specific info and pass it to the driver
    driver_options = backup_options.pop('driver_info', {}) # 拿到驱动的额外信息
    backup_service = self.service.get_backup_driver(context) 
    backup_service.import_record(backup, driver_options) # 把驱动的额外信息导入
    
    # 检查 backup_options 里包含必须元素
    required_import_options = {
        'display_name',
        'display_description',
        'container',
        'size',
        'service_metadata',
        'object_count',
        'id'
    }

    # Check for missing fields in imported data
    missing_opts = required_import_options - set(backup_options)
    if missing_opts:
        raise exception.InvalidBackup(reason=msg)
        
    # 检查backup_options['id'] 和 数据库的一致
    backup_id = backup_options['id']
    if backup_id != backup.id:
        raise exception.InvalidBackup(reason=msg)        
        
    # 复写一些元素值
    backup_options['status'] = fields.BackupStatus.AVAILABLE
    backup_options['service'] = self.driver_name
    backup_options['availability_zone'] = self.az
    backup_options['host'] = self.host

    # 移除一些元素值
    for key in ('name', 'user_id', 'project_id', 'deleted_at',
                'deleted', 'fail_reason'):
        backup_options.pop(key, None)

    # 更新进数据库
    backup.update(backup_options)
    backup.save()
    

你可能感兴趣的:(cinder-backup导入导出功能)