def fetch_log_entries(server, args_array, opt_arg_list, **kwargs): """Function: fetch_log_entries Description: Prints out the binary log entries that are between the start and stop datetimes. Arguments: (input) server -> Server instance. (input) args_array -> Array of command line options and values. (input) opt_arg_list -> Arguments to be added to command line. """ args_array = dict(args_array) opt_arg_list = list(opt_arg_list) status, binlog_list = process_logs_list(server, args_array) if status[0]: lines = fetch_binlog(server, opt_arg_list=opt_arg_list, start_dt=args_array.get("-s"), stop_dt=args_array.get("-t"), binlog_files=binlog_list, bin_path=arg_parser.arg_set_path( args_array, "-p")) for item in lines: print(item, end="") else: print("Error encountered: %s" % (status[1]))
def create_cmd(mongo, args_array, prog_name, path_opt, **kwargs): """Function: create_cmd Description: Create a basic command line for a mongo utility program and then add required arguments and additional options if they are present. Arguments: (input) mongo -> Database instance. (input) args_array -> Array of command line options and values. (input) prog_name -> Name of utility program. (input) path_opt -> Option containing the path dir to program. (input) **kwargs: req_arg -> List of options to add to cmd line. opt_arg -> Dictionary of additional options to add. use_repset -> True|False - Use repset name connection. (i.e. repset_name/host1,host2,...) no_pass -> True|False - Turn off --password= option. (output) -> Mongo utility command line. """ args_array = dict(args_array) cmd = crt_base_cmd( mongo, arg_parser.arg_set_path(args_array, path_opt) + prog_name, **kwargs) # Process required arguments. for arg in list(kwargs.get("req_arg", [])): cmd = cmds_gen.add_cmd(cmd, arg=arg) # Process optional arguments. return cmds_gen.is_add_cmd(args_array, cmd, dict(kwargs.get("opt_arg", {})))
def fetch_log_pos(server, args_array, opt_arg_list=None, **kwargs): """Function: fetch_log_pos Description: Gets the server's file name and position that are between the start and stop datetimes. Arguments: (input) server -> Server instance. (input) args_array -> Array of command line options and values. (input) opt_arg_list -> Arguments to be added to command line. """ args_array = dict(args_array) if opt_arg_list is None: opt_arg_list = list() else: opt_arg_list = list(opt_arg_list) # Get Position class from file and log position. pos = find_dt_pos(server, args_array.get("-s"), args_array.get("-t"), opt_arg_list, arg_parser.arg_set_path(args_array, "-p")) print("Filename: {0}, Position: {1}".format(pos.file, pos.pos))
def crt_dump_cmd(server, args_array, opt_arg_list, opt_dump_list, **kwargs): """Function: crt_dump_cmd Description: Create the database dump command line. Arguments: (input) server -> Database server instance. (input) args_array -> Array of command line options and values. (input) opt_arg_list -> List of commands to add to cmd line. (input) opt_dump_list -> Dictionary of additional options. (output) -> Database dump command line. """ args_array = dict(args_array) opt_arg_list = list(opt_arg_list) opt_dump_list = dict(opt_dump_list) dump_args = mysql_libs.crt_cmd( server, arg_parser.arg_set_path(args_array, "-p") + "mysqldump") # Add arguments to dump command. for arg in opt_arg_list: dump_args = cmds_gen.add_cmd(dump_args, arg=arg) # Append additional options to command. return cmds_gen.is_add_cmd(args_array, dump_args, opt_dump_list)
def load_log(server, args_array, opt_arg_list, **kwargs): """Function: load_log Description: Get the binary logs from the source database, then fetch the revelant binary log entries and load them into the target database before closing all connections. Arguments: (input) server -> Server instance. (input) args_array -> Array of command line options and values. (input) opt_arg_list -> Arguments to be added to command line. """ subp = gen_libs.get_inst(subprocess) args_array = dict(args_array) opt_arg_list = list(opt_arg_list) status, binlog_list = process_logs_list(server, args_array) if status[0]: target = mysql_libs.create_instance(args_array["-e"], args_array["-d"], mysql_class.Server) target.connect(silent=True) if not target.conn_msg: cmd = mysql_libs.crt_cmd( target, arg_parser.arg_set_path(args_array, "-p") + "mysql") # Fetch binary logs and restore to target database proc1 = fetch_binlog(server, args_array.get("-s"), args_array.get("-t"), binlog_list, opt_arg_list, arg_parser.arg_set_path(args_array, "-p")) proc2 = subp.Popen(cmd, stdin=proc1) proc2.wait() cmds_gen.disconnect(target) else: print("load_log: Error encountered on slave(%s): %s" % (target.name, target.conn_msg)) else: print("load_log: Error encountered in process_logs_list: %s" % (status[1]))
def mongo_dump(server, args_array, **kwargs): """Function: mongo_dump Description: Create the dump command and execute it. Arguments: (input) server -> Database server instance. (input) args_array -> Array of command line options and values. (input) **kwargs: opt_arg -> Dictionary of additional options to add. mail -> Email class instance. req_arg -> List of required options for the command line. (output) False -> If an error has occurred. (output) None -> Error message. """ err_flag = False err_msg = None subp = gen_libs.get_inst(subprocess) args_array = dict(args_array) mail = kwargs.get("mail", None) sup_std = args_array.get("-x", False) dtg = datetime.datetime.strftime(datetime.datetime.now(), "%Y%m%d_%H%M%S") f_name = os.path.join(args_array["-o"], "dump_log_file_" + dtg + ".log") dump_cmd = mongo_libs.create_cmd( server, args_array, "mongodump", arg_parser.arg_set_path(args_array, "-p"), **kwargs) with open(f_name, "w") as l_file: proc1 = subp.Popen(dump_cmd, stderr=l_file) proc1.wait() if not gen_libs.is_empty_file(f_name): log_list = gen_libs.file_2_list(f_name) for line in log_list: if not sup_std: print(line) if mail: mail.add_2_msg(line) if mail: mail.send_mail() return err_flag, err_msg
def dump_load_dbs(source, clone, args_array, req_rep_cfg, opt_arg_list, **kwargs): """Function: dump_load_dbs Description: Dumps and loads all databases in a single transaction. Arguments: (input) source -> Source server instance. (input) clone -> Destination server instance. (input) args_array -> Array of command line options and values. (input) req_rep_cfg -> Required replication config settings. (input) opt_arg_list -> List of options to add to dump cmd line. (input) **kwargs: opt_dump_list -> Dictionary of additional options. """ args_array = dict(args_array) req_rep_cfg = dict(req_rep_cfg) opt_arg_list = list(opt_arg_list) subp = gen_libs.get_inst(subprocess) dump_cmd = crt_dump_cmd(source, args_array, opt_arg_list, kwargs.get("opt_dump_list", [])) if source.gtid_mode != clone.gtid_mode and not clone.gtid_mode \ and "-n" in args_array and "-r" not in args_array: dump_cmd = cmds_gen.is_add_cmd({"-r": "True"}, dump_cmd, kwargs.get("opt_dump_list", [])) load_cmd = mysql_libs.crt_cmd( clone, arg_parser.arg_set_path(args_array, "-p") + "mysql") if clone.gtid_mode: mysql_libs.reset_master(clone) # Dump databases, pipe into load, and wait until completed. proc1 = subp.Popen(dump_cmd, stdout=subp.PIPE) proc2 = subp.Popen(load_cmd, stdin=proc1.stdout) proc2.wait()