def r_create(): args_rules = [ Rules.GUEST_UUID.value ] if 'label' in request.json: args_rules.append( Rules.LABEL.value, ) try: ret = dict() ret['state'] = ji.Common.exchange_state(20000) ji.Check.previewing(args_rules, request.json) snapshot = Snapshot() guest = Guest() guest.uuid = request.json.get('guest_uuid') guest.get_by('uuid') snapshot.label = request.json.get('label', '') snapshot.status = guest.status snapshot.guest_uuid = guest.uuid snapshot.snapshot_id = '_'.join(['tmp', ji.Common.generate_random_code(length=8)]) snapshot.parent_id = '-' snapshot.progress = 0 snapshot.create() snapshot.get_by('snapshot_id') message = { '_object': 'snapshot', 'action': 'create', 'uuid': guest.uuid, 'node_id': guest.node_id, 'passback_parameters': {'id': snapshot.id} } Utils.emit_instruction(message=json.dumps(message, ensure_ascii=False)) ret['data'] = snapshot.__dict__ return ret except ji.PreviewingError, e: return json.loads(e.message)
def snapshot(ctx, board, cycle_time, done): ctx.obj['board_id'] = board ts = TrelloStats(ctx.obj) Snapshot.create_table(fail_silently=True) """ Recording mode - Daily snapshots of a board for ongoing reporting: -> trellis report --board=87hiudhw --cycle-time --spend --revenue --done=Done """ if cycle_time: done_id = ts.get_list_id_from_name(done) cards = ts.get_list_data(done_id) ct = ts.cycle_time(cards) print ct # Create snapshot Snapshot.create(board_id=board, done_id=done_id, cycle_time=ct)
def make_instance_snapshot_backup(instance, error, group, provider_class=VolumeProviderSnapshot, target_volume=None, current_hour=None): LOG.info("Make instance backup for {}".format(instance)) provider = provider_class(instance) infra = instance.databaseinfra database = infra.databases.first() snapshot = Snapshot.create( instance, group, target_volume or provider.volume, environment=provider.environment ) snapshot_final_status = Snapshot.SUCCESS locked = None driver = infra.get_driver() client = None try: client = driver.get_client(instance) locked = lock_instance(driver, instance, client) if not locked: snapshot_final_status = Snapshot.WARNING if 'MySQL' in type(driver).__name__: mysql_binlog_save(client, instance) current_time = datetime.now() has_snapshot = Snapshot.objects.filter( status=Snapshot.WARNING, instance=instance, end_at__year=current_time.year, end_at__month=current_time.month, end_at__day=current_time.day ) backup_hour_list = Configuration.get_by_name_as_list( 'make_database_backup_hour' ) if (snapshot_final_status == Snapshot.WARNING and has_snapshot): if str(current_hour) in backup_hour_list: raise Exception( "Backup with WARNING already created today." ) else: response = provider.take_snapshot() snapshot.done(response) snapshot.save() except Exception as e: errormsg = "Error creating snapshot: {}".format(e) error['errormsg'] = errormsg set_backup_error(infra, snapshot, errormsg) return snapshot finally: unlock_instance(driver, instance, client) if not snapshot.size: command = "du -sb /data/.snapshot/%s | awk '{print $1}'" % ( snapshot.snapshot_name ) try: output = instance.hostname.ssh.run_script(command) size = int(output['stdout'][0]) snapshot.size = size except Exception as e: snapshot.size = 0 LOG.error("Error exec remote command {}".format(e)) backup_path = database.backup_path if backup_path: now = datetime.now() target_path = "{}/{}/{}/{}/{}".format( backup_path, now.strftime("%Y_%m_%d"), instance.hostname.hostname.split('.')[0], now.strftime("%Y%m%d%H%M%S"), infra.name ) snapshot_path = "/data/.snapshot/{}/data/".format( snapshot.snapshot_name ) command = """ if [ -d "{backup_path}" ] then rm -rf {backup_path}/20[0-9][0-9]_[0-1][0-9]_[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: instance.hostname.ssh.run_script(command) except Exception as e: LOG.error("Error exec remote command {}".format(e)) snapshot.status = snapshot_final_status snapshot.end_at = datetime.now() snapshot.save() register_backup_dbmonitor(infra, snapshot) return snapshot
def make_instance_snapshot_backup(instance, error, group, provider_class=VolumeProviderBase): LOG.info("Make instance backup for {}".format(instance)) provider = provider_class(instance) infra = instance.databaseinfra database = infra.databases.first() snapshot = Snapshot.create(instance, group, provider.volume) snapshot_final_status = Snapshot.SUCCESS locked = None driver = infra.get_driver() client = None try: client = driver.get_client(instance) locked = lock_instance(driver, instance, client) if not locked: snapshot_final_status = Snapshot.WARNING if 'MySQL' in type(driver).__name__: mysql_binlog_save(client, instance) response = provider.take_snapshot() snapshot.done(response) except Exception as e: errormsg = "Error creating snapshot: {}".format(e) error['errormsg'] = errormsg set_backup_error(infra, snapshot, errormsg) return snapshot finally: unlock_instance(driver, instance, client) output = {} command = "du -sb /data/.snapshot/%s | awk '{print $1}'" % ( snapshot.snapshot_name) try: exec_remote_command_host(instance.hostname, command, output) size = int(output['stdout'][0]) snapshot.size = size except Exception as e: snapshot.size = 0 LOG.error("Error exec remote command {}".format(e)) backup_path = database.backup_path if backup_path: now = datetime.now() target_path = "{}/{}/{}/{}/{}".format( backup_path, now.strftime("%Y_%m_%d"), instance.hostname.hostname.split('.')[0], now.strftime("%Y%m%d%H%M%S"), infra.name) 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-9]_[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_host(instance.hostname, command, output) except Exception as e: LOG.error("Error exec remote command {}".format(e)) snapshot.status = snapshot_final_status snapshot.end_at = datetime.now() snapshot.save() register_backup_dbmonitor(infra, snapshot) return snapshot
def make_instance_snapshot_backup(instance, error, group, provider_class=VolumeProviderBase): LOG.info("Make instance backup for {}".format(instance)) provider = provider_class(instance) infra = instance.databaseinfra database = infra.databases.first() snapshot = Snapshot.create(instance, group, provider.volume) snapshot_final_status = Snapshot.SUCCESS locked = None driver = infra.get_driver() client = None try: client = driver.get_client(instance) locked = lock_instance(driver, instance, client) if not locked: snapshot_final_status = Snapshot.WARNING if 'MySQL' in type(driver).__name__: mysql_binlog_save(client, instance) response = provider.take_snapshot() snapshot.done(response) except Exception as e: errormsg = "Error creating snapshot: {}".format(e) error['errormsg'] = errormsg set_backup_error(infra, snapshot, errormsg) return snapshot finally: if locked: unlock_instance(driver, instance, client) output = {} command = "du -sb /data/.snapshot/%s | awk '{print $1}'" % ( snapshot.snapshot_name ) try: exec_remote_command_host(instance.hostname, command, output) size = int(output['stdout'][0]) snapshot.size = size except Exception as e: snapshot.size = 0 LOG.error("Error exec remote command {}".format(e)) backup_path = database.backup_path if backup_path: now = datetime.now() target_path = "{}/{}/{}/{}/{}".format( backup_path, now.strftime("%Y_%m_%d"), instance.hostname.hostname.split('.')[0], now.strftime("%Y%m%d%H%M%S"), infra.name ) 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-9]_[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_host(instance.hostname, command, output) except Exception as e: LOG.error("Error exec remote command {}".format(e)) snapshot.status = snapshot_final_status snapshot.end_at = datetime.now() snapshot.save() register_backup_dbmonitor(infra, snapshot) return snapshot