设为首页 收藏本站
查看: 437|回复: 0

[经验分享] cinder - 备份glusterfs卷到nfs server

[复制链接]

尚未签到

发表于 2018-10-22 06:29:59 | 显示全部楼层 |阅读模式
def create(self, context, name, description, volume_id,  
               container, incremental=False, availability_zone=None,
  
               force=False, snapshot_id=None):
  
        """Make the RPC call to create a volume backup."""
  
        check_policy(context, 'create')                     # 校验policy权限
  
        volume = self.volume_api.get(context, volume_id)
  
        snapshot = None
  
        if snapshot_id:
  
            snapshot = self.volume_api.get_snapshot(context, snapshot_id)
  
        if volume['status'] not in ["available", "in-use"]:
  
            msg = (_('Volume to be backed up must be available '
  
                     'or in-use, but the current status is "%s".')
  
                   % volume['status'])
  
            raise exception.InvalidVolume(reason=msg)
  
        elif volume['status'] in ["in-use"] and not snapshot_id and not force:
  
            msg = _('Backing up an in-use volume must use '
  
                    'the force flag.')
  
            raise exception.InvalidVolume(reason=msg)
  
        elif snapshot_id and snapshot['status'] not in ["available"]:
  
            msg = (_('Snapshot to be backed up must be available, '
  
                     'but the current status is "%s".')
  
                   % snapshot['status'])
  
            raise exception.InvalidSnapshot(reason=msg)
  
        previous_status = volume['status']
  
        host = self._get_available_backup_service_host(
  
            None, volume.availability_zone,
  
            volume_utils.extract_host(volume.host, 'host'))
  
        # Reserve a quota before setting volume status and backup status
  
        try:
  
            reserve_opts = {'backups': 1,
  
                            'backup_gigabytes': volume['size']}
  
            reservations = QUOTAS.reserve(context, **reserve_opts)
  
        except exception.OverQuota as e:
  
            overs = e.kwargs['overs']
  
            usages = e.kwargs['usages']
  
            quotas = e.kwargs['quotas']
  
            def _consumed(resource_name):
  
                return (usages[resource_name]['reserved'] +
  
                        usages[resource_name]['in_use'])
  
            for over in overs:
  
                if 'gigabytes' in over:
  
                    msg = _LW("Quota exceeded for %(s_pid)s, tried to create "
  
                              "%(s_size)sG backup (%(d_consumed)dG of "
  
                              "%(d_quota)dG already consumed)")
  
                    LOG.warning(msg, {'s_pid': context.project_id,
  
                                      's_size': volume['size'],
  
                                      'd_consumed': _consumed(over),
  
                                      'd_quota': quotas[over]})
  
                    raise exception.VolumeBackupSizeExceedsAvailableQuota(
  
                        requested=volume['size'],
  
                        consumed=_consumed('backup_gigabytes'),
  
                        quota=quotas['backup_gigabytes'])
  
                elif 'backups' in over:
  
                    msg = _LW("Quota exceeded for %(s_pid)s, tried to create "
  
                              "backups (%(d_consumed)d backups "
  
                              "already consumed)")
  
                    LOG.warning(msg, {'s_pid': context.project_id,
  
                                      'd_consumed': _consumed(over)})
  
                    raise exception.BackupLimitExceeded(
  
                        allowed=quotas[over])
  
        # Find the latest backup and use it as the parent backup to do an
  
        # incremental backup.
  
        latest_backup = None
  
        if incremental:
  
            backups = objects.BackupList.get_all_by_volume(context.elevated(),
  
                                                           volume_id)
  
            if backups.objects:
  
                # NOTE(xyang): The 'data_timestamp' field records the time
  
                # when the data on the volume was first saved. If it is
  
                # a backup from volume, 'data_timestamp' will be the same
  
                # as 'created_at' for a backup. If it is a backup from a
  
                # snapshot, 'data_timestamp' will be the same as
  
                # 'created_at' for a snapshot.
  
                # If not backing up from snapshot, the backup with the latest
  
                # 'data_timestamp' will be the parent; If backing up from
  
                # snapshot, the backup with the latest 'data_timestamp' will
  
                # be chosen only if 'data_timestamp' is earlier than the
  
                # 'created_at' timestamp of the snapshot; Otherwise, the
  
                # backup will not be chosen as the parent.
  
                # For example, a volume has a backup taken at 8:00, then
  
                # a snapshot taken at 8:10, and then a backup at 8:20.
  
                # When taking an incremental backup of the snapshot, the
  
                # parent should be the backup at 8:00, not 8:20, and the
  
                # 'data_timestamp' of this new backup will be 8:10.
  
                latest_backup = max(
  
                    backups.objects,
  
                    key=lambda x: x['data_timestamp']
  
                    if (not snapshot or (snapshot and x['data_timestamp']
  
                                         < snapshot['created_at']))
  
                    else datetime(1, 1, 1, 1, 1, 1, tzinfo=timezone('UTC')))
  
            else:
  
                msg = _('No backups available to do an incremental backup.')
  
                raise exception.InvalidBackup(reason=msg)
  
        parent_id = None
  
        if latest_backup:
  
            parent_id = latest_backup.id
  
            if latest_backup['status'] != fields.BackupStatus.AVAILABLE:
  
                msg = _('The parent backup must be available for '
  
                        'incremental backup.')
  
                raise exception.InvalidBackup(reason=msg)
  
        data_timestamp = None
  
        if snapshot_id:
  
            snapshot = objects.Snapshot.get_by_id(context, snapshot_id)
  
            data_timestamp = snapshot.created_at
  
        self.db.volume_update(context, volume_id,
  
                              {'status': 'backing-up',
  
                               'previous_status': previous_status})
  
        backup = None
  
        try:
  
            kwargs = {
  
                'user_id': context.user_id,
  
                'project_id': context.project_id,
  
                'display_name': name,
  
                'display_description': description,
  
                'volume_id': volume_id,
  
                'status': fields.BackupStatus.CREATING,
  
                'container': container,
  
                'parent_id': parent_id,
  
                'size': volume['size'],
  
                'host': host,
  
                'snapshot_id': snapshot_id,
  
                'data_timestamp': data_timestamp,
  
            }
  
            backup = objects.Backup(context=context, **kwargs)
  
            backup.create()
  
            if not snapshot_id:
  
                backup.data_timestamp = backup.created_at
  
                backup.save()
  
            QUOTAS.commit(context, reservations)
  
        except Exception:
  
            with excutils.save_and_reraise_exception():
  
                try:
  
                    if backup and 'id' in backup:
  
                        backup.destroy()
  
                finally:
  
                    QUOTAS.rollback(context, reservations)
  
        # TODO(DuncanT): In future, when we have a generic local attach,
  
        #                this can go via the scheduler, which enables
  
        #                better load balancing and isolation of services
  
        self.backup_rpcapi.create_backup(context, backup)   # rpc请求通过消息队列这里不贴出来了
  
        return backup



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-624642-1-1.html 上篇帖子: SQL Server实现类似于自动刷新数据的功能 下篇帖子: oracle sql语言模糊查询--通配符like的使用教程
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表