cinder-backup导入导出功能

backup-export

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

usage: cinder backup-export <backup>

范例:

[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>
backup_service : 驱动的名字
backup_url : export出的序列化元数据

范例:

[root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NGFkZTJhYiIsICJ0ZW1wX3ZvbHVtZV9pZCI6IG51bGx9
+----------+--------------------------------------+
| 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()
    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,222评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,455评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,720评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,568评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,696评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,879评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,028评论 3 409
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,773评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,220评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,550评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,697评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,360评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,002评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,782评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,010评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,433评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,587评论 2 350

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,633评论 18 139
  • 1/列出mysql数据库中的所有数据库sqoop list-databases -connect jdbc:mys...
    时待吾阅读 2,739评论 1 5
  • •王越:VMware存储API整理,比如核心的存储池与卷两者的CURD。可以参考:VMware in OpenSt...
    笨手笨脚越阅读 5,633评论 0 2
  • 大学,一个特别尴尬的时段。自由着,却也受限着。心中有春暖花开的远方,却被眼前的苟且所限制。那些为了顺利毕业,为了通...
    荣儿菇娘阅读 234评论 1 2
  • 身边越来越多的朋友选择考公务员考事业单位,心里一心想着这就是所谓的稳定饭碗。但是,体制内的生活,你真的了解吗? 因...
    寒暁阅读 553评论 5 3