def exe_query(self, file_name, serv_name, base_name, db_query): conf = ConfigManager("config/" + file_name + ".yaml") cnf = conf.get(serv_name) conn = cnf["connection"] database = cnf["databases"][base_name] if conn["type"] == "ssh": cmd = self.connect_command_builder(conn, 'no') common.conn.send(cmd) ans = None while ans == None: ans = common.conn.get_state() status, hostname, db_port = ans.split("_") adr = "localhost" if status == "ok": #udało się utworzyć tunel self.psycop_query(database["name"], database["user"], conf.get_password(serv_name + '.' + base_name), adr, db_port, db_query) else: print('--------------------') print('Error: Unable to create ssh tunnel') print('--------------------') elif conn["type"] == "direct": self.psycop_query(database["name"], database["user"], conf.get_password(serv_name + '.' + base_name), conn["adress"], conn["remoteport"], db_query)
def exe_restore(self, file_name, serv_name, base_name, backup_name, type): try: conf = ConfigManager("config/" + file_name + ".yaml") cnf = conf.get(serv_name) conn = cnf["connection"] db = cnf["databases"][base_name] db_name = db["name"] db_user = db["user"] db_pass = conf.get_password(serv_name + '.' + base_name) conn_adr = '' conn_port = None if conn["type"] == "ssh": #Dla połączeń ssh cmd = self.connect_command_builder(conn, 'no') common.conn.send(cmd) ans = None while ans == None: ans = common.conn.get_state() status, hostname, db_port = ans.split("_") if status == "ok": #udało się utworzyć tunel conn_adr = 'localhost' conn_port = db_port else: raise PostgressError('Unable to create ssh tunnel') elif conn["type"] == "direct": conn_adr = conn["adress"] conn_port = conn["remoteport"] local_version = self.get_local_version('pg_dump --version') remote_version = self.get_pg_version(db_name, db_user, db_pass, conn_adr, conn_port) if not self.is_valid_versions(local_version, remote_version): raise PostgressError("You have too old version of pg_dump") self.restore(db_name, db_user, db_pass, conn_adr, conn_port, backup_name, type) except ConnectionRefusedError: print('--------------------') print('ERROR: Connection Refused by host') print('--------------------') except TimeoutError: print('--------------------') print('ERROR: Connection timeout') print('--------------------') except paramiko.ssh_exception.AuthenticationException: print('--------------------') print('ERROR: Authentication problem') print('--------------------') except KeyError as e: print('--------------------') print('ERROR: Unable to find key:',e) print('--------------------') except PostgressError as e: print('--------------------') print('ERROR:',e) print('--------------------') except Exception as e: print('--------------------') print('ERROR:',e) print('--------------------')
def exe_dump(self, file_name, serv_name, base_name, backup_name, type): date = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S") dump_file_name = backup_name+'_'+file_name+'_'+serv_name+'_'+base_name+'_'+date dumper = """pg_dump.exe -U %s -d %s -h %s -p %s -f %s -C --column-inserts""" try: conf = ConfigManager("config/" + file_name + ".yaml") cnf = conf.get(serv_name) conn = cnf["connection"] db = cnf["databases"][base_name] db_name = db["name"] db_user = db["user"] db_pass = conf.get_password(serv_name + '.' + base_name) conn_adr = '' conn_port = None if conn["type"] == "ssh": #Dla połączeń ssh cmd = self.connect_command_builder(conn, 'no') common.conn.send(cmd) ans = None while ans == None: ans = common.conn.get_state() status, hostname, db_port = ans.split("_") if status == "ok": #udało się utworzyć tunel conn_adr = 'localhost' conn_port = db_port else: raise PostgressError('Unable to create ssh tunnel') elif conn["type"] == "direct": conn_adr = conn["adress"] conn_port = conn["remoteport"] local_version = self.get_local_version('pg_dump --version') remote_version = self.get_pg_version(db_name, db_user, db_pass, conn_adr, conn_port) if not self.is_valid_versions(local_version, remote_version): raise PostgressError("You have too old version of pg_dump") if type == 'tar': dumper += ' -Ft' dump_file_name += '.tar' else: dump_file_name += '.sql' command = dumper % (db_user, db_name, conn_adr, conn_port, dump_file_name) os.putenv('PGPASSWORD', db_pass) try: common.set_cdir_and_store() proc = Popen(command, stdout=PIPE, stderr=PIPE) except FileNotFoundError: raise PostgressError(" ERROR: pg_dump not found") finally: common.restore_cdir() out, err = proc.communicate() if err != b'': raise PostgressError(err.decode('utf8', 'ignore')) except ConnectionRefusedError: print('--------------------') print('ERROR: Connection Refused by host') print('--------------------') except TimeoutError: print('--------------------') print('ERROR: Connection timeout') print('--------------------') except paramiko.ssh_exception.AuthenticationException: print('--------------------') print('ERROR: Authentication problem') print('--------------------') except KeyError as e: print('--------------------') print('ERROR: Unable to find key:',e) print('--------------------') except PostgressError as e: print('--------------------') print('ERROR:',e) print('--------------------') except Exception as e: print('--------------------') print('ERROR:',e) print('--------------------')