Example #1
0
def make_host_backup(database, instance, export_id):
    from backup.models import Snapshot
    from dbaas_nfsaas.models import HostAttr as Nfsaas_HostAttr
    import datetime

    LOG.info("Make instance backup for %s" % (instance))

    nfsaas_hostattr = Nfsaas_HostAttr.objects.get(nfsaas_export_id=export_id)

    snapshot = Snapshot()
    snapshot.start_at = datetime.datetime.now()
    snapshot.type = Snapshot.SNAPSHOPT
    snapshot.status = Snapshot.RUNNING
    snapshot.instance = instance
    snapshot.environment = instance.databaseinfra.environment
    snapshot.export_path = nfsaas_hostattr.nfsaas_path
    snapshot.database_name = database.name

    databaseinfra = instance.databaseinfra

    nfs_snapshot = NfsaasProvider.create_snapshot(environment=databaseinfra.environment,
                                                  host=instance.hostname)

    if 'id' in nfs_snapshot and 'snapshot' in nfs_snapshot:
        snapshot.status = Snapshot.SUCCESS
        snapshot.snapshopt_id = nfs_snapshot['id']
        snapshot.snapshot_name = nfs_snapshot['snapshot']
        snapshot.end_at = datetime.datetime.now()
        snapshot.save()
        return True
    else:
        snapshot.status = Snapshot.ERROR
        snapshot.end_at = datetime.datetime.now()
        snapshot.save()
        return False
Example #2
0
def make_host_backup(database, instance, export_id):
    from backup.models import Snapshot
    from dbaas_nfsaas.models import HostAttr as Nfsaas_HostAttr
    import datetime

    LOG.info("Make instance backup for %s" % (instance))

    nfsaas_hostattr = Nfsaas_HostAttr.objects.get(nfsaas_export_id=export_id)

    snapshot = Snapshot()
    snapshot.start_at = datetime.datetime.now()
    snapshot.type = Snapshot.SNAPSHOPT
    snapshot.status = Snapshot.RUNNING
    snapshot.instance = instance
    snapshot.environment = instance.databaseinfra.environment
    snapshot.export_path = nfsaas_hostattr.nfsaas_path
    snapshot.database_name = database.name

    databaseinfra = instance.databaseinfra

    nfs_snapshot = NfsaasProvider.create_snapshot(
        environment=databaseinfra.environment, host=instance.hostname)

    if 'id' in nfs_snapshot and 'snapshot' in nfs_snapshot:
        snapshot.status = Snapshot.SUCCESS
        snapshot.snapshopt_id = nfs_snapshot['id']
        snapshot.snapshot_name = nfs_snapshot['snapshot']
        snapshot.end_at = datetime.datetime.now()
        snapshot.save()
        return True
    else:
        snapshot.status = Snapshot.ERROR
        snapshot.end_at = datetime.datetime.now()
        snapshot.save()
        return False
Example #3
0
def make_instance_snapshot_backup(instance, error):

    LOG.info("Make instance backup for %s" % (instance))

    snapshot = Snapshot()
    snapshot.start_at = datetime.datetime.now()
    snapshot.type = Snapshot.SNAPSHOPT
    snapshot.status = Snapshot.RUNNING
    snapshot.instance = instance
    snapshot.environment = instance.databaseinfra.environment

    from dbaas_nfsaas.models import HostAttr as Nfsaas_HostAttr
    nfsaas_hostattr = Nfsaas_HostAttr.objects.get(host=instance.hostname)
    snapshot.export_path = nfsaas_hostattr.nfsaas_path

    databases = Database.objects.filter(databaseinfra=instance.databaseinfra)
    if databases:
        snapshot.database_name = databases[0].name

    snapshot.save()

    databaseinfra = instance.databaseinfra
    driver = databaseinfra.get_driver()
    client = driver.get_client(instance)
    cloudstack_hostattr = Cloudstack_HostAttr.objects.get(
        host=instance.hostname)

    try:
        LOG.debug('Locking instance %s' % str(instance))
        driver.lock_database(client)
        LOG.debug('Instance %s is locked' % str(instance))

        if type(driver).__name__ == 'MySQL':
            mysql_binlog_save(client, instance, cloudstack_hostattr)

        nfs_snapshot = NfsaasProvider.create_snapshot(
            environment=databaseinfra.environment,
            plan=databaseinfra.plan,
            host=instance.hostname)
        if 'error' in nfs_snapshot:
            errormsg = nfs_snapshot['error']
            error['errormsg'] = errormsg
            set_backup_error(databaseinfra, snapshot, errormsg)
            return False

        if 'id' in nfs_snapshot and 'snapshot' in nfs_snapshot:
            snapshot.snapshopt_id = nfs_snapshot['id']
            snapshot.snapshot_name = nfs_snapshot['snapshot']
        else:
            errormsg = 'There is no snapshot information'
            error['errormsg'] = errormsg
            set_backup_error(databaseinfra, snapshot, errormsg)
            return False

    except Exception, e:
        errormsg = "Error creating snapshot: %s" % (e)
        error['errormsg'] = errormsg
        set_backup_error(databaseinfra, snapshot, errormsg)
        return False
Example #4
0
def make_instance_snapshot_backup(instance, error):

    LOG.info("Make instance backup for %s" % (instance))

    snapshot = Snapshot()
    snapshot.start_at = datetime.datetime.now()
    snapshot.type = Snapshot.SNAPSHOPT
    snapshot.status = Snapshot.RUNNING
    snapshot.instance = instance
    snapshot.environment = instance.databaseinfra.environment

    from dbaas_nfsaas.models import HostAttr as Nfsaas_HostAttr
    nfsaas_hostattr = Nfsaas_HostAttr.objects.get(
        host=instance.hostname, is_active=True)
    snapshot.export_path = nfsaas_hostattr.nfsaas_path

    databases = Database.objects.filter(databaseinfra=instance.databaseinfra)
    if databases:
        snapshot.database_name = databases[0].name

    snapshot.save()

    databaseinfra = instance.databaseinfra
    driver = databaseinfra.get_driver()
    client = driver.get_client(instance)
    cloudstack_hostattr = Cloudstack_HostAttr.objects.get(
        host=instance.hostname)

    try:
        LOG.debug('Locking instance %s' % str(instance))
        driver.lock_database(client)
        LOG.debug('Instance %s is locked' % str(instance))

        if type(driver).__name__ == 'MySQL':
            mysql_binlog_save(client, instance, cloudstack_hostattr)

        nfs_snapshot = NfsaasProvider.create_snapshot(environment=databaseinfra.environment,
                                                      host=instance.hostname)
        if 'error' in nfs_snapshot:
            errormsg = nfs_snapshot['error']
            error['errormsg'] = errormsg
            set_backup_error(databaseinfra, snapshot, errormsg)
            return False

        if 'id' in nfs_snapshot and 'snapshot' in nfs_snapshot:
            snapshot.snapshopt_id = nfs_snapshot['id']
            snapshot.snapshot_name = nfs_snapshot['snapshot']
        else:
            errormsg = 'There is no snapshot information'
            error['errormsg'] = errormsg
            set_backup_error(databaseinfra, snapshot, errormsg)
            return False

    except Exception, e:
        errormsg = "Error creating snapshot: %s" % (e)
        error['errormsg'] = errormsg
        set_backup_error(databaseinfra, snapshot, errormsg)
        return False
Example #5
0
def make_instance_snapshot_backup(instance, error):

    LOG.info("Make instance backup for %s" % (instance))

    snapshot = Snapshot()
    snapshot.start_at = datetime.datetime.now()
    snapshot.type=Snapshot.SNAPSHOPT
    snapshot.status=Snapshot.RUNNING
    snapshot.instance = instance
    snapshot.environment = instance.databaseinfra.environment

    from dbaas_nfsaas.models import HostAttr as Nfsaas_HostAttr
    nfsaas_hostattr = Nfsaas_HostAttr.objects.get(host=instance.hostname)
    snapshot.export_path = nfsaas_hostattr.nfsaas_path

    databases = Database.objects.filter(databaseinfra=instance.databaseinfra)
    if databases:
        snapshot.database_name = databases[0].name

    snapshot.save()

    databaseinfra = instance.databaseinfra
    driver = databaseinfra.get_driver()
    client = driver.get_client(instance)
    
    try:
        driver.lock_database(client)
        nfs_snapshot = NfsaasProvider.create_snapshot(environment = databaseinfra.environment,
                                                      plan = databaseinfra.plan,
                                                      host = instance.hostname)
        driver.unlock_database(client)
        if 'error' in nfs_snapshot:
            errormsg = nfs_snapshot['error']
            error['errormsg'] = errormsg
            set_backup_error(databaseinfra, snapshot, errormsg)
            return False
            
        if 'id' in nfs_snapshot and 'snapshot' in nfs_snapshot:
            snapshot.snapshopt_id = nfs_snapshot['id']
            snapshot.snapshot_name = nfs_snapshot['snapshot']
        else:
            errormsg = 'There is no snapshot information'
            error['errormsg'] = errormsg
            set_backup_error(databaseinfra, snapshot, errormsg)
            return False
        
    except Exception, e:
        errormsg = "Error creating snapshot: %s" % (e)
        error['errormsg'] = errormsg
        set_backup_error(databaseinfra, snapshot, errormsg)
        return False
    def do(self, workflow_dict):
        try:

            databaseinfra = workflow_dict['databaseinfra']
            driver = databaseinfra.get_driver()
            instance = workflow_dict['source_instances'][0]
            client = driver.get_client(instance)

            driver.lock_database(client)
            workflow_dict['database_locked'] = True
            LOG.debug('Instance %s is locked' % str(instance))

            client.query('show master status')
            r = client.store_result()
            row = r.fetch_row(maxrows=0, how=1)
            workflow_dict['binlog_file'] = row[0]['File']
            workflow_dict['binlog_pos'] = row[0]['Position']

            nfs_snapshot = NfsaasProvider.create_snapshot(
                environment=databaseinfra.environment, host=instance.hostname)

            LOG.info('nfs_snapshot: {}'.format(nfs_snapshot))
            if 'error' in nfs_snapshot:
                errormsg = nfs_snapshot['error']
                raise Exception(errormsg)

            if 'id' in nfs_snapshot and 'snapshot' in nfs_snapshot:
                workflow_dict['snapshopt_id'] = nfs_snapshot['id']
                workflow_dict['snapshot_name'] = nfs_snapshot['snapshot']
            else:
                errormsg = 'There is no snapshot information'
                raise Exception(errormsg)

            driver.unlock_database(client)
            workflow_dict['database_locked'] = False

            return True
        except Exception:
            traceback = full_stack()

            workflow_dict['exceptions']['error_codes'].append(DBAAS_0020)
            workflow_dict['exceptions']['traceback'].append(traceback)

            return False
    def do(self, workflow_dict):
        try:

            databaseinfra = workflow_dict["databaseinfra"]
            driver = databaseinfra.get_driver()
            instance = workflow_dict["source_instances"][0]
            client = driver.get_client(instance)

            driver.lock_database(client)
            workflow_dict["database_locked"] = True
            LOG.debug("Instance %s is locked" % str(instance))

            client.query("show master status")
            r = client.store_result()
            row = r.fetch_row(maxrows=0, how=1)
            workflow_dict["binlog_file"] = row[0]["File"]
            workflow_dict["binlog_pos"] = row[0]["Position"]

            nfs_snapshot = NfsaasProvider.create_snapshot(environment=databaseinfra.environment, host=instance.hostname)

            LOG.info("nfs_snapshot: {}".format(nfs_snapshot))
            if "error" in nfs_snapshot:
                errormsg = nfs_snapshot["error"]
                raise Exception(errormsg)

            if "id" in nfs_snapshot and "snapshot" in nfs_snapshot:
                workflow_dict["snapshopt_id"] = nfs_snapshot["id"]
                workflow_dict["snapshot_name"] = nfs_snapshot["snapshot"]
            else:
                errormsg = "There is no snapshot information"
                raise Exception(errormsg)

            driver.unlock_database(client)
            workflow_dict["database_locked"] = False

            return True
        except Exception:
            traceback = full_stack()

            workflow_dict["exceptions"]["error_codes"].append(DBAAS_0020)
            workflow_dict["exceptions"]["traceback"].append(traceback)

            return False
def make_instance_snapshot_backup(instance, error):

    LOG.info("Make instance backup for %s" % (instance))

    snapshot = Snapshot()
    snapshot.start_at = datetime.datetime.now()
    snapshot.type = Snapshot.SNAPSHOPT
    snapshot.status = Snapshot.RUNNING
    snapshot.instance = instance
    snapshot.environment = instance.databaseinfra.environment

    from dbaas_nfsaas.models import HostAttr as Nfsaas_HostAttr
    nfsaas_hostattr = Nfsaas_HostAttr.objects.get(
        host=instance.hostname, is_active=True)
    snapshot.export_path = nfsaas_hostattr.nfsaas_path

    databases = Database.objects.filter(databaseinfra=instance.databaseinfra)
    if databases:
        snapshot.database_name = databases[0].name

    snapshot.save()

    databaseinfra = instance.databaseinfra
    driver = databaseinfra.get_driver()
    client = driver.get_client(instance)
    cloudstack_hostattr = Cloudstack_HostAttr.objects.get(
        host=instance.hostname)

    try:
        LOG.debug('Locking instance %s' % str(instance))
        driver.lock_database(client)
        LOG.debug('Instance %s is locked' % str(instance))

        if type(driver).__name__ == 'MySQL':
            mysql_binlog_save(client, instance, cloudstack_hostattr)

        nfs_snapshot = NfsaasProvider.create_snapshot(environment=databaseinfra.environment,
                                                      host=instance.hostname)
        if 'error' in nfs_snapshot:
            errormsg = nfs_snapshot['error']
            error['errormsg'] = errormsg
            set_backup_error(databaseinfra, snapshot, errormsg)
            return False

        if 'id' in nfs_snapshot and 'snapshot' in nfs_snapshot:
            snapshot.snapshopt_id = nfs_snapshot['id']
            snapshot.snapshot_name = nfs_snapshot['snapshot']
        else:
            errormsg = 'There is no snapshot information'
            error['errormsg'] = errormsg
            set_backup_error(databaseinfra, snapshot, errormsg)
            return False

    except Exception as e:
        errormsg = "Error creating snapshot: %s" % (e)
        error['errormsg'] = errormsg
        set_backup_error(databaseinfra, snapshot, errormsg)
        return False

    finally:
        LOG.debug('Unlocking instance %s' % str(instance))
        driver.unlock_database(client)
        LOG.debug('Instance %s is unlocked' % str(instance))

    output = {}
    command = "du -sb /data/.snapshot/%s | awk '{print $1}'" % (
        snapshot.snapshot_name)
    try:
        exec_remote_command(server=instance.hostname.address,
                            username=cloudstack_hostattr.vm_user,
                            password=cloudstack_hostattr.vm_password,
                            command=command,
                            output=output)
        size = int(output['stdout'][0])
        snapshot.size = size
    except Exception as e:
        snapshot.size = 0
        LOG.error("Error exec remote command %s" % (e))

    backup_path = databases[0].backup_path
    if backup_path:
        infraname = databaseinfra.name
        now = datetime.datetime.now()
        target_path = "{backup_path}/{today_str}/{hostname}/{now_str}/{infraname}".format(
            backup_path=backup_path,
            today_str=now.strftime("%Y_%m_%d"),
            hostname=instance.hostname.hostname.split('.')[0],
            now_str=now.strftime("%Y%m%d%H%M%S"),
            infraname=infraname)
        snapshot_path = "/data/.snapshot/{}/data/".format(snapshot.snapshot_name)
        output = {}
        command = """
        if [ -d "{backup_path}" ]
        then
            rm -rf {backup_path}/20[0-9][0-9]_[0-1][0-12]_[0-3][0-9]
            mkdir -p {target_path}
            cp -r {snapshot_path} {target_path} &
        fi
        """.format(backup_path=backup_path,
                   target_path=target_path,
                   snapshot_path=snapshot_path)
        try:
            exec_remote_command(server=instance.hostname.address,
                                username=cloudstack_hostattr.vm_user,
                                password=cloudstack_hostattr.vm_password,
                                command=command,
                                output=output)
        except Exception as e:
            LOG.error("Error exec remote command %s" % (e))

    snapshot.status = Snapshot.SUCCESS
    snapshot.end_at = datetime.datetime.now()
    snapshot.save()
    register_backup_dbmonitor(databaseinfra, snapshot)

    return True
Example #9
0
def make_instance_snapshot_backup(instance, error):

    LOG.info("Make instance backup for %s" % (instance))

    snapshot = Snapshot()
    snapshot.start_at = datetime.datetime.now()
    snapshot.type = Snapshot.SNAPSHOPT
    snapshot.status = Snapshot.RUNNING
    snapshot.instance = instance
    snapshot.environment = instance.databaseinfra.environment

    from dbaas_nfsaas.models import HostAttr as Nfsaas_HostAttr
    nfsaas_hostattr = Nfsaas_HostAttr.objects.get(host=instance.hostname,
                                                  is_active=True)
    snapshot.export_path = nfsaas_hostattr.nfsaas_path

    databases = Database.objects.filter(databaseinfra=instance.databaseinfra)
    if databases:
        snapshot.database_name = databases[0].name

    snapshot.save()

    databaseinfra = instance.databaseinfra
    driver = databaseinfra.get_driver()
    client = driver.get_client(instance)
    cloudstack_hostattr = Cloudstack_HostAttr.objects.get(
        host=instance.hostname)

    try:
        LOG.debug('Locking instance %s' % str(instance))
        driver.lock_database(client)
        LOG.debug('Instance %s is locked' % str(instance))

        if type(driver).__name__ == 'MySQL':
            mysql_binlog_save(client, instance, cloudstack_hostattr)

        nfs_snapshot = NfsaasProvider.create_snapshot(
            environment=databaseinfra.environment, host=instance.hostname)
        if 'error' in nfs_snapshot:
            errormsg = nfs_snapshot['error']
            error['errormsg'] = errormsg
            set_backup_error(databaseinfra, snapshot, errormsg)
            return False

        if 'id' in nfs_snapshot and 'snapshot' in nfs_snapshot:
            snapshot.snapshopt_id = nfs_snapshot['id']
            snapshot.snapshot_name = nfs_snapshot['snapshot']
        else:
            errormsg = 'There is no snapshot information'
            error['errormsg'] = errormsg
            set_backup_error(databaseinfra, snapshot, errormsg)
            return False

    except Exception as e:
        errormsg = "Error creating snapshot: %s" % (e)
        error['errormsg'] = errormsg
        set_backup_error(databaseinfra, snapshot, errormsg)
        return False

    finally:
        LOG.debug('Unlocking instance %s' % str(instance))
        driver.unlock_database(client)
        LOG.debug('Instance %s is unlocked' % str(instance))

    output = {}
    command = "du -sb /data/.snapshot/%s | awk '{print $1}'" % (
        snapshot.snapshot_name)
    try:
        exec_remote_command(server=instance.hostname.address,
                            username=cloudstack_hostattr.vm_user,
                            password=cloudstack_hostattr.vm_password,
                            command=command,
                            output=output)
        size = int(output['stdout'][0])
        snapshot.size = size
    except Exception as e:
        snapshot.size = 0
        LOG.error("Error exec remote command %s" % (e))

    backup_path = databases[0].backup_path
    if backup_path:
        infraname = databaseinfra.name
        now = datetime.datetime.now()
        target_path = "{backup_path}/{today_str}/{hostname}/{now_str}/{infraname}".format(
            backup_path=backup_path,
            today_str=now.strftime("%Y_%m_%d"),
            hostname=instance.hostname.hostname.split('.')[0],
            now_str=now.strftime("%Y%m%d%H%M%S"),
            infraname=infraname)
        snapshot_path = "/data/.snapshot/{}/data/".format(
            snapshot.snapshot_name)
        output = {}
        command = """
        if [ -d "{backup_path}" ]
        then
            rm -rf {backup_path}/20[0-9][0-9]_[0-1][0-12]_[0-3][0-9]
            mkdir -p {target_path}
            cp -r {snapshot_path} {target_path} &
        fi
        """.format(backup_path=backup_path,
                   target_path=target_path,
                   snapshot_path=snapshot_path)
        try:
            exec_remote_command(server=instance.hostname.address,
                                username=cloudstack_hostattr.vm_user,
                                password=cloudstack_hostattr.vm_password,
                                command=command,
                                output=output)
        except Exception as e:
            LOG.error("Error exec remote command %s" % (e))

    snapshot.status = Snapshot.SUCCESS
    snapshot.end_at = datetime.datetime.now()
    snapshot.save()
    register_backup_dbmonitor(databaseinfra, snapshot)

    return True