Mini Shell

Direktori : /proc/self/root/proc/thread-self/root/proc/thread-self/root/opt/sharedrads/
Upload File :
Current File : //proc/self/root/proc/thread-self/root/proc/thread-self/root/opt/sharedrads/cms_dumpdb

#! /opt/imh-python/bin/python3
""" Display database credentials for CMS in path. """
# Author: Daniel K

import sys
import logging
from argparse import ArgumentParser

from rads import setup_logging
from rads.color import yellow, red, green
from cms_tools.cms import CMSFind
from cms_tools.cms import CMSStatus
from cms_tools.cms import load_modules
from cms_tools.helpers import dump_db
from cms_tools.common import find_start_path


LOGGER = logging.getLogger(__name__)


def parse_args():
    """Parse command line arguments"""
    parser = ArgumentParser(description=__doc__)
    # fmt: off
    parser.add_argument(
        "-m", "--modules", metavar='MODULE', nargs='+',
        help="Search only for specified modules.",
    )
    parser.add_argument(
        "-l", "--list", action='store_true',
        help="List available modules and quit.",
    )
    parser.add_argument(
        "-d", "--depth", action='store', type=int, default=0,
        help="Set the maximum depth to search. "
        "The default is 1 if path is set, or 8 otherwise.",
    )
    output_group = parser.add_mutually_exclusive_group()
    output_group.add_argument(
        '-v', '--verbose',
        dest='loglevel', action='store_const', const='debug',
        help="Use verbose logging.",
    )
    output_group.add_argument(
        '-q', '--quiet',
        dest='loglevel', action='store_const', const='critical',
        help='Log only critical errors',
    )
    output_group.add_argument(
        '--loglevel',
        dest='loglevel',
        choices=['error', 'info', 'debug', 'warning', 'critical'],
        help="Specify the verbosity of logging output. "
        "The default is 'warning'.",
    )
    parser.add_argument(
        "-o", "--output", help="Output logging to the specified file."
    )
    parser.add_argument(
        "-p", "--dumppath", help="Path to dump databases."
    )
    parser.add_argument(
        'user_path', metavar='(USER|PATH)', nargs='?',
        help="cPanel user. Path must be in this user's home directory, "
        "and cannot be a relative path.",
    )
    # fmt: on
    args = parser.parse_args()

    if args.loglevel is None:
        logging_level = logging.WARNING
    else:
        logging_level: int = getattr(logging, args.loglevel.upper())

    if args.output:
        setup_logging(path=args.output, loglevel=logging_level, print_out=False)
    else:
        setup_logging(
            path='/var/log/messages',
            loglevel=logging_level,
            print_out=sys.stderr,
        )

    if args.list:
        cms_search = CMSFind('List modules', 0)
        modules_list = load_modules(cms_search, args.modules)
        print("Available modules: %s" % ', '.join(modules_list))
        sys.exit(0)

    path = find_start_path(args.user_path)

    if path is None:
        sys.exit(1)

    if args.depth == 0:
        if '/' in args.user_path:
            depth = 1
        else:
            depth = 8
    else:
        depth = args.depth
    return depth, path, args.modules, args.dumppath


def main():
    """Main function for script"""
    max_depth, start_path, include_modules, dumppath = parse_args()
    cms_search = CMSFind(start_path, max_depth, 0)
    load_modules(cms_search, include_modules)
    LOGGER.debug("Starting path: %s, Maxumim depth: %d", start_path, max_depth)

    failed_cms = []
    failed_dbs = []
    dumped_dbs = {}

    for the_cms in cms_search.find_cms():
        if the_cms.status >= CMSStatus.db_is_set:
            dump_file = dump_db(
                dbuser=the_cms.db_user,
                password=the_cms.db_pass,
                dbname=the_cms.db_name,
                dump_path=dumppath or the_cms.directory_root,
            )
            if dump_file:
                dumped_dbs[the_cms.db_name] = dump_file
            else:
                failed_dbs.append(the_cms.db_name)

        else:
            failed_cms.append(the_cms.directory_root)

    print(yellow(" === Summary ==="))
    if failed_cms:
        print("\nFailed to find credentials for the follwoing CMS:")
        for docroot in failed_cms:
            print(red(docroot))
    if failed_dbs:
        print("\nFailed to export the following databases:")
        for database in failed_dbs:
            print(red(database))
    if dumped_dbs:
        print("\nSuccessful database dumps:")
        for database, file in dumped_dbs.items():
            print(green(f"{database} {file}"))


if __name__ == "__main__":
    main()

Zerion Mini Shell 1.0