oVirt备份-还原磁盘快照

备份-还原磁盘快照

摘要

此功能是补充组件,用于提供备份和还原虚拟机的功能。该功能引入了对使用REST-API下载和上传磁盘快照的支持。使用ovirt-imageio促进图像传输

使用oVirt Python-SDK可简化以下备份/还原示例。oVirt REST-API / SDK可以类似的方式使用。

后备

下载VM OVF

完整的例子

为了获取VM配置数据,应指定'all_content'标志。然后,可以将其写入一个新文件-OVF。注意:OVF包含有关磁盘和快照的足够信息,可以将VM恢复到原始状态(例如,别名/描述/日期)。但是,它不是由系统自动解析的,而是应该由SDK用户完成的。

vm_name = 'myvm'

vm = vms_service.list(search="name=%s" % vm_name, all_content=True)[0]

ovf_filename = "%s.ovf" % vm.id

with open(ovf_filename, "wb") as ovf_file:

    ovf_file.write(vm.initialization.configuration.data)

下载磁盘

完整的例子

以下示例演示了下载指定磁盘的非活动磁盘快照的过程。因此,为了也包括活动层,请预先为磁盘创建一个新的快照,或者使用“ 下载磁盘示例下载活动磁盘快照

为了下载活跃

  • 对于每个磁盘,遍历其磁盘快照并执行映像传输。
  •   # Set relevant disk and stroage domain IDs
  •   disk_id = 'ccdd6487-0a8f-40c8-9f45-40e0e2b30d79'
  •   sd_name = 'mydata'
  •         
  •   # Get a reference to the storage domains service:
  •   storage_domains_service = system_service.storage_domains_service()
  •         
  •   # Look up for the storage domain by name:
  •   storage_domain = storage_domains_service.list(search='name=%s' % sd_name)[0]
  •         
  •   # Get a reference to the storage domain service in which the disk snapshots reside:
  •   storage_domain_service = storage_domains_service.storage_domain_service(storage_domain.id)
  •         
  •   # Get a reference to the disk snapshots service:
  •   disk_snapshot_service = storage_domain_service.disk_snapshots_service()
  •         
  •   # Get a list of disk snapshots by a disk ID
  •   all_disk_snapshots = disk_snapshot_service.list()
  •         
  •   # Filter disk snapshots list by disk id
  •   disk_snapshots = [s for s in all_disk_snapshots if s.disk.id == disk_id]
  •         
  •   # Download disk snapshots
  •   for disk_snapshot in disk_snapshots:
  •       download_disk_snapshot(disk_snapshot)
  • 应使用“快照”属性指定ImageTransfer对象。
  •   transfer = transfers_service.add(
  •       types.ImageTransfer(
  •           snapshot=types.DiskSnapshot(id=disk_snapshot_id),
  •           direction=types.ImageTransferDirection.DOWNLOAD,
  •       )
  •   )

恢复

完整的例子

组成磁盘快照链

使用保存的图像文件,创建一个磁盘快照链。通过获取每个文件的卷信息(使用qemu-img)并找到后备文件名(卷的父文件)来构建链。通过维护父卷和子卷之间的映射,我们可以从基本卷开始构建链。

    volumes_info = {}   # {filename -> vol_info}

    backing_files = {}  # {backing_file (parent) -> vol_info (child)}

    for root, dirs, file_names in os.walk(disk_path):

        for file_name in file_names:

            volume_info = get_volume_info("%s/%s" % (disk_path, file_name))

            volumes_info[file_name] = volume_info

            if 'full-backing-filename' in volume_info:

                backing_files[volume_info['full-backing-filename']] = volume_info

   

    base_volume = [v for v in volumes_info.values() if 'full-backing-filename' not in v ][0]

    child = backing_files[base_volume['filename']]

    images_chain = [base_volume]

    while child != None:

        images_chain.append(child)

        parent = child

        if parent['filename'] in backing_files:

            child = backing_files[parent['filename']]

        else:

            child = None

   

    return images_chain

创建磁盘

对于每个磁盘,使用上一步中生成的映像链调用基本映像的磁盘创建。磁盘的内容将在以后的步骤中上载。

    base_image = images_chain[0]

    initial_size = base_image['actual-size']

    provisioned_size = base_image['virtual-size']

    image_id = os.path.basename(base_image['filename'])

 

    disk = disks_service.add(

        types.Disk(

            id=disk_id,

            image_id=image_id,

            name=disk_id,

            format=types.DiskFormat.RAW,

            provisioned_size=provisioned_size,

            initial_size=initial_size,

            storage_domains=[

                types.StorageDomain(

                    name=sd_name

                )

            ]

        )

    )

OVF添加VM

通过在配置元素中指定ovf数据来添加保存的VM

    ovf_file_path = 'c3a8e806-106d-4aff-b59a-3a113eabf5a9.ovf'

    ovf_data = open(ovf_file_path, 'r').read()

    vms_service = system_service.vms_service()

    vm = vms_service.add(

        types.Vm(

            cluster=types.Cluster(

                name='Default',

            ),

            initialization = types.Initialization(

                configuration = types.Configuration(

                    type = types.ConfigurationType.OVF,

                    data = ovf_data

                )

            ),

        ),

    )

创建快照

对于链中的每个磁盘快照,使用磁盘ID和映像ID(以及可选的已保存描述)创建快照。

    # Locate the service that manages the snapshots of the virtual machine:

    snapshots_service = vm_service.snapshots_service()

 

    # Add the new snapshot:

    snapshot = snapshots_service.add(

        types.Snapshot(

            description=description,

            disk_attachments=[

                types.DiskAttachment(

                    disk=types.Disk(

                        id=disk_id,

                        image_id=image_id

                    )

                )

            ]

        ),

    )

上传磁盘

对于链中的每个磁盘快照,开始上载传输。

    # Get a reference to the service that manages the image transfer:

    transfers_service = system_service.image_transfers_service()

 

    # Add a new image transfer:

    transfer = transfers_service.add(

        types.ImageTransfer(

            snapshot=types.DiskSnapshot(id=disk_snapshot_id),

            direction=types.ImageTransferDirection.UPLOAD,

        )

    )

 

    # Get reference to the created transfer service:

    transfer_service = transfers_service.image_transfer_service(transfer.id)

 

    while transfer.phase == types.ImageTransferPhase.INITIALIZING:

        time.sleep(1)

        transfer = transfer_service.get()

 

    try:

        proxy_url = urlparse(transfer.proxy_url)

        proxy_connection = get_proxy_connection(proxy_url)

        path = disk_path

 

        # Set needed headers for uploading:

        upload_headers = {

            'Authorization': transfer.signed_ticket,

        }

 

        with open(path, "rb") as disk:

            size = os.path.getsize(path)

            chunk_size = 1024 * 1024 * 8

            pos = 0

            while pos < size:

                # Extend the transfer session.

                transfer_service.extend()

                # Set the content range, according to the chunk being sent.

                upload_headers['Content-Range'] = "bytes %d-%d/%d" % (pos, min(pos + chunk_size, size) - 1, size)

                # Perform the request.

                proxy_connection.request(

                    'PUT',

                    proxy_url.path,

                    disk.read(chunk_size),

                    headers=upload_headers,

                )

                # Print response

                r = proxy_connection.getresponse()

                print r.status, r.reason, "Completed", "{:.0%}".format(pos / float(size))

                # Continue to next chunk.

                pos += chunk_size

 

        print "Completed", "{:.0%}".format(pos / float(size))

    finally:

        # Finalize the session.

        transfer_service.finalize()

附录

Python SDK范例

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
oVirt备份还原可以通过多种方式完成,下面我将为您介绍两种具体的操作方法。 1. 使用 oVirt 自带的备份还原工具 oVirt 自带了一个备份还原工具,可以通过 Web 界面来进行备份还原操作。具体操作步骤如下: 备份: 1. 登录 oVirt Web 界面,点击左侧菜单栏的“管理”。 2. 选择“备份/还原”选项卡,点击“新建”。 3. 在“新建备份”窗口中,选择需要备份的虚拟机,设置备份文件的存储位置和备份类型(完整备份或增量备份),点击“确定”按钮。 4. 等待备份任务完成后,可以在“备份/还原”选项卡中查看备份历史记录。 还原: 1. 在“备份/还原”选项卡中选择需要还原备份记录,点击“还原”按钮。 2. 在“新建还原”窗口中,选择需要还原的虚拟机,设置还原点和还原选项,例如是否覆盖现有虚拟机等。 3. 点击“确定”按钮,等待还原任务完成后,可以在 oVirt 中查看还原后的虚拟机。 2. 使用备份软件进行备份还原 oVirt备份还原也可以使用第三方备份软件,例如 Veeam Backup & Replication、Bacula、Bareos 等。具体操作步骤如下: 备份: 1. 安装并配置备份软件,根据软件提供的向导进行配置。 2. 在备份软件中设置备份任务,选择需要备份oVirt 虚拟机和配置文件。 3. 执行备份任务,等待备份任务完成。 还原: 1. 在备份软件中选择需要还原备份点,可以选择全量备份或者增量备份。 2. 选择需要还原的虚拟机,执行还原操作。 3. 等待还原任务完成后,在 oVirt 中查看还原后的虚拟机。 需要注意的是,使用备份软件进行备份还原需要一定的技术和操作经验,建议在实际操作前先进行充分的了解和测试。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值