def dump(schema=None): """ Dump and download all configured, or given, schemas. :param schema: Specific shema to dump and download. """ if not schema: schemas = blueprint.get('schemas', {}).keys() for i, schema in enumerate(schemas, start=1): print("{i}. {schema}".format(i=i, schema=schema)) valid_indices = '[1-{}]+'.format(len(schemas)) schema_choice = prompt('Select schema to dump:', default='1', validate=valid_indices) schema = schemas[int(schema_choice) - 1] with sudo('postgres'): now = datetime.now().strftime('%Y-%m-%d') output_file = '/tmp/{}_{}.backup'.format(schema, now) filename = os.path.basename(output_file) options = dict(format='tar', output_file=output_file, schema=schema) info('Dumping schema {}...', schema) run('pg_dump -c -F {format} -f {output_file} {schema}'.format( **options)) info('Downloading dump...') local_file = '~/{}'.format(filename) files.get(output_file, local_file) with sudo(), silent(): debian.rm(output_file) info('New smoking hot dump at {}', local_file)
def backup(): timestamp = datetime.now().strftime('%Y-%m-%d_%H%M') dump_file = '%s-remote-%s.dmp' % (APP_NAME, timestamp) pg_dump_cmd = 'pg_dump {} -U {} -h localhost -x -Fc -f {}' \ .format(APP_NAME, APP_NAME, dump_file) sudo(pg_dump_cmd) if not os.path.exists(LOCAL_BACKUPS_DIR): local('mkdir {}'.format(LOCAL_BACKUPS_DIR)) files.get(dump_file, LOCAL_BACKUPS_DIR) sudo("rm %s" % dump_file)
def created_key(self, pub_key_path): key_name = raw_input('Enter your ssh key name: ') key_id = '/%s/keys/%s' % ( env.joyent_account, key_name) allow_agent = env.get('allow_agent', False) sdc = DataCenter(key_id=key_id, allow_agent=allow_agent) with tempfile.TemporaryFile() as f: get(pub_key_path, f) f.seek(0) data = f.read() sdc.add_key(env.host_string, data)
def download(self, remote_path, rel_destination_path, role=None): """ Currently only supports downloading single file """ destination_path = self.get_user_template_path(rel_destination_path, role=role) # Append filename to destination if missing filename = os.path.basename(remote_path) if os.path.basename(destination_path) != filename: destination_path = os.path.join(destination_path, filename) with sudo("root"): info("Downloading {} -> {}", remote_path, destination_path) files.get(remote_path, destination_path)
def download(self, remote_path, rel_destination_path, role=None): """ Currently only supports downloading single file """ destination_path = self.get_user_template_path(rel_destination_path, role=role) # Append filename to destination if missing filename = os.path.basename(remote_path) if os.path.basename(destination_path) != filename: destination_path = os.path.join(destination_path, filename) with sudo('root'): info('Downloading {} -> {}', remote_path, destination_path) files.get(remote_path, destination_path)
def dump(schema=None): """ Dump and download all configured, or given, schemas. :param schema: Specific shema to dump and download. """ if not schema: schemas = blueprint.get('schemas', {}).keys() for i, schema in enumerate(schemas, start=1): print("{i}. {schema}".format(i=i, schema=schema)) valid_indices = '[1-{}]+'.format(len(schemas)) schema_choice = prompt('Select schema to dump:', default='1', validate=valid_indices) schema = schemas[int(schema_choice)-1] with sudo('postgres'): now = datetime.now().strftime('%Y-%m-%d') output_file = '/tmp/{}_{}.backup'.format(schema, now) filename = os.path.basename(output_file) options = dict( format='tar', output_file=output_file, schema=schema ) info('Dumping schema {}...', schema) run('pg_dump -c -F {format} -f {output_file} {schema}'.format(**options)) info('Downloading dump...') local_file = '~/{}'.format(filename) files.get(output_file, local_file) with sudo(), silent(): debian.rm(output_file) info('New smoking hot dump at {}', local_file)
def load_remote_file(path): content = {} filename = path.split('/')[-1] log.info('Remotely loading {} file'.format(filename)) if exists(path, verbose=True): log.debug('Found remote file {}, downloading it'.format(path)) local_paths = get(path) assert not len(local_paths.failed) for path in local_paths: if os.path.exists(path): with open(path, 'r') as fd: content = json.load(fd) shutil.rmtree(path[:path.rfind('/')]) else: log.warning('! Could not find local file {}'.format(path)) content = {} else: log.warning('! Could not find remote file {}'.format(path)) log.debug(content) return content