def do(self): if not self.is_valid: return snapshot = Snapshot.create(self.instance, self.group, self.volume) response = self.take_snapshot() snapshot.done(response) snapshot.status = Snapshot.SUCCESS snapshot.end_at = datetime.now() snapshot.save()
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) snapshot = Snapshot() snapshot.start_at = datetime.datetime.now() snapshot.type = Snapshot.SNAPSHOPT snapshot.status = Snapshot.RUNNING snapshot.instance = instance snapshot.environment = databaseinfra.environment snapshot.status = Snapshot.SUCCESS 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() 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'] try: nfs_snapshot = create_snapshot( environment=databaseinfra.environment, host=instance.hostname ) except Exception as e: errormsg = "Error creating snapshot: {}".format(e) self.set_backup_error(snapshot, errormsg) raise Exception(errormsg) finally: LOG.debug('Unlocking instance %s' % str(instance)) driver.unlock_database(client) workflow_dict['database_locked'] = False LOG.info('nfs_snapshot: {}'.format(nfs_snapshot)) if 'error' in nfs_snapshot: errormsg = nfs_snapshot['error'] self.set_backup_error(snapshot, errormsg) raise Exception(errormsg) if 'id' in nfs_snapshot and 'name' in nfs_snapshot: workflow_dict['snapshopt_id'] = nfs_snapshot['id'] workflow_dict['snapshot_name'] = nfs_snapshot['name'] snapshot.snapshopt_id = nfs_snapshot['id'] snapshot.snapshot_name = nfs_snapshot['name'] snapshot.status = Snapshot.SUCCESS snapshot.end_at = datetime.datetime.now() snapshot.save() else: errormsg = 'There is no snapshot information' self.set_backup_error(snapshot, errormsg) raise Exception(errormsg) 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_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
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