def do_restore(self, args): "Restore database from file\n\tUsage:\trestore file \t\t(restore to all server lists)\n\t\trestore list file \t\t(restore to server list)\n\t\trestore list.base file \t(restore to database in list)" try: (values, num) = self.parse_args(args, 1, 2) if num == 2: common.set_cdir_and_store() if tarfile.is_tarfile(values[1]): type = 'tar' else: type = 'sql' common.restore_cdir() self.exec_on_config(self.exe_restore, [values[1], type], values[0], 'tree') elif num == 1: common.set_cdir_and_store() if tarfile.is_tarfile(values[0]): type = 'tar' else: type = 'sql' common.restore_cdir() self.exec_on_config(self.exe_restore, [values[0], type], '', 'tree') except ConfigManagerError as e: print('--------------------') print('ERROR:',e) print('--------------------') except ParseArgsException as e: print('--------------------') print('ERROR:',e) print('--------------------') except KeyError as e: print('--------------------') print('ERROR: Unable to find key:',e) print('--------------------') except PostgressError as e: print('--------------------') print('ERROR:',e) print('--------------------') except FileNotFoundError as e: print('--------------------') print('ERROR:',e) print('--------------------') finally: common.restore_cdir()
def restore(self, db_name, db_user, db_pass, host, port, file_name, type = 'sql'): if type == 'tar': #jeżeli restorujemy tar'a restorer = "pg_restore -U %s -d %s -h %s -p %s" command = restorer % (db_user, db_name, host, port) os.putenv('PGPASSWORD', db_pass) common.set_cdir_and_store() bytes_read = open(file_name, "rb") common.restore_cdir() try: proc = Popen(command, stdout=PIPE, stderr=PIPE, stdin=bytes_read) except FileNotFoundError: raise PostgressError(" ERROR: pg_restore not found") out, err = proc.communicate() if err != b'': raise PostgressError(err.decode('utf8', 'ignore')) else: restorer = "psql -U %s -d %s -h %s -p %s" command = restorer % (db_user, db_name, host, port) os.putenv('PGPASSWORD', db_pass) common.set_cdir_and_store() bytes_read = open(file_name, "rb") common.restore_cdir() try: proc = Popen(command, stdout=PIPE, stderr=PIPE, stdin=bytes_read) except FileNotFoundError: raise PostgressError(" ERROR: pg_restore not found") out, err = proc.communicate() if err != b'': raise PostgressError(err.decode('utf8', 'ignore'))
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('--------------------')