Beispiel #1
0
def main():
    home_dir = os.path.expanduser('~')
    program_dir = os.path.abspath(os.path.join(home_dir, '.dbackups'))
    db_config_file = os.path.join(program_dir, 'databases.ini')

    if not os.path.isdir(os.path.join(BASE_DIR, '../logs')):
        os.mkdir(os.path.join(BASE_DIR, '../logs'))

    if not os.path.isfile(db_config_file):
        print('Config File not found. {}'.format(db_config_file))
        sys.exit(1)

    #logging_config = resource_filename(__name__, '../config/logging.ini')
    #logging.config.fileConfig(logging_config)

    logging.basicConfig(level=logging.DEBUG,
                        format='%(asctime)s %(levelname)-6s line %(lineno)-4s %(message)s')

    args = parser.parse_args()
    pprint(args)
    config = ConfigParser.ConfigParser()
    config.read(db_config_file)

    logging.debug(config.sections())

    if not config.has_section(args.database):
        logging.info('DB alias not found in the config file {} -> [{}]'.format(db_config_file, args.database))
        sys.exit(1)
    else:
        logging.info('Found the DB settings in the config file. Continuing.')
        db_type = config.get(args.database, 'db_type')
        db_host = config.get(args.database, 'db_host')
        db_user = config.get(args.database, 'db_user')
        db_pass = config.get(args.database, 'db_pass')
        db_port = config.get(args.database, 'db_port')
        db_name = config.get(args.database, 'db_name')

    db_object = get_database_object(db_type, db_host, db_name, db_user, db_pass, db_port)
    logging.debug('DB object created: {}'.format(db_object))

    if args.command == 'backup':
        logging.info('Chose to backup {}'.format(db_object.db_host))
        logging.info('Dump file: [{}]'.format(db_object.dump_file_name))
        db_object.dump()
        logging.info('Dumping DB finished.')
        if args.upload_url:
            print('Uploading to the desired URL: {}'.format(args.upload_url))
            upload_http_put(db_object.dump_file, args.upload_url)

    if args.command == 'clone':
        logging.info('Going to clone_to from one DB to another.')
        dev_db = get_database_object(db_type, args.dev_host, args.dev_name, args.dev_user, args.dev_pass,
                                     args.dev_port)
        db_object.clone_to(dev_db, args.latest_local)

    if args.command == 'clean':
        logging.info('Cleaning the dumps directory to make room for more dumps.')
        file_list = cleanup_backups.find_files_for_delete(db_object.get_dump_dir(), db_object.db_host,
                                                          db_object.db_name)
        cleanup_backups.delete_file_list(file_list)
Beispiel #2
0
def main():
    home_dir = os.path.expanduser('~')
    program_dir = os.path.abspath(os.path.join(home_dir, '.dbackups'))
    db_config_file = os.path.join(program_dir, 'databases.ini')

    if not os.path.isdir(os.path.join(BASE_DIR, '../logs')):
        os.mkdir(os.path.join(BASE_DIR, '../logs'))

    if not os.path.isfile(db_config_file):
        print('Config File not found. {}'.format(db_config_file))
        sys.exit(1)

    #logging_config = resource_filename(__name__, '../config/logging.ini')
    #logging.config.fileConfig(logging_config)

    logging.basicConfig(
        level=logging.DEBUG,
        format='%(asctime)s %(levelname)-6s line %(lineno)-4s %(message)s')

    args = parser.parse_args()
    pprint(args)
    config = ConfigParser.ConfigParser()
    config.read(db_config_file)

    logging.debug(config.sections())

    if not config.has_section(args.database):
        logging.info('DB alias not found in the config file {} -> [{}]'.format(
            db_config_file, args.database))
        sys.exit(1)
    else:
        logging.info('Found the DB settings in the config file. Continuing.')
        db_type = config.get(args.database, 'db_type')
        db_host = config.get(args.database, 'db_host')
        db_user = config.get(args.database, 'db_user')
        db_pass = config.get(args.database, 'db_pass')
        db_port = config.get(args.database, 'db_port')
        db_name = config.get(args.database, 'db_name')

    db_object = get_database_object(db_type, db_host, db_name, db_user,
                                    db_pass, db_port)
    logging.debug('DB object created: {}'.format(db_object))

    if args.command == 'backup':
        logging.info('Chose to backup {}'.format(db_object.db_host))
        logging.info('Dump file: [{}]'.format(db_object.dump_file_name))
        db_object.dump()
        logging.info('Dumping DB finished.')
        if args.upload_url:
            print('Uploading to the desired URL: {}'.format(args.upload_url))
            upload_http_put(db_object.dump_file, args.upload_url)

    if args.command == 'clone':
        logging.info('Going to clone_to from one DB to another.')
        dev_db = get_database_object(db_type, args.dev_host, args.dev_name,
                                     args.dev_user, args.dev_pass,
                                     args.dev_port)
        db_object.clone_to(dev_db, args.latest_local)

    if args.command == 'clean':
        logging.info(
            'Cleaning the dumps directory to make room for more dumps.')
        file_list = cleanup_backups.find_files_for_delete(
            db_object.get_dump_dir(), db_object.db_host, db_object.db_name)
        cleanup_backups.delete_file_list(file_list)
Beispiel #3
0
def main():
    home_dir = os.path.expanduser('~')
    program_dir = os.path.abspath(os.path.join(home_dir, '.dbackups'))
    db_config_file = os.path.join(program_dir, 'databases.ini')

    if not os.path.isdir(program_dir):
        os.mkdir(program_dir)

    log_dir = os.path.join(program_dir, 'logs')
    if not os.path.isdir(log_dir):
        os.mkdir(log_dir)

    #logging_config = resource_filename(__name__, '../config/cron_logging.ini')
    #logging.config.fileConfig(logging_config)
    logging.basicConfig(
        level=logging.DEBUG,
        filename=os.path.expanduser('~') +
        '/.dbackups/logs/database_backup_cron.log',
        format='%(asctime)s %(levelname)-6s line %(lineno)-4s %(message)s')

    if not os.path.isfile(db_config_file):
        print('Config File not found. {}'.format(db_config_file))
        sys.exit(1)

    config = ConfigParser.ConfigParser()
    config.read(db_config_file)

    map_of_db_objects = {}

    for db_alias in config.sections():
        if config.getboolean(db_alias, 'enabled'):
            db = get_database_object(config.get(db_alias, 'db_type'),
                                     config.get(db_alias, 'db_host'),
                                     config.get(db_alias, 'db_name'),
                                     config.get(db_alias, 'db_user'),
                                     config.get(db_alias, 'db_pass'),
                                     config.get(db_alias, 'db_port'))
            map_of_db_objects[db_alias] = db

    for db_section, db_obj in map_of_db_objects.iteritems():
        logging.info('Starting backup of {}'.format(db_obj.db_name))
        try:
            db_obj.dump()
        except CommandError as e:
            logging.exception(e)
            logging.error('Failed to dump database: {}'.format(db_obj.db_name))
            logging.info('Moving on to the next database.')
            continue
        try:
            upload_http_put(db_obj.dump_file,
                            config.get(db_section, 'upload_url'),
                            config.get(db_section, 'upload_user'),
                            config.get(db_section, 'upload_pass'),
                            verify_request=False)
            logging.info(
                'Successfully uploaded database back of [{}] to [{}] '.format(
                    db_obj.db_name,
                    config.get(db_section, 'upload_url'),
                ))
        except HTTPError as httperror:
            logging.exception(httperror)
            logging.error(
                'Failed to Upload the backup to the configured location.')

        file_list = cleanup_backups.find_files_for_delete(
            db_obj.get_dump_dir(), db_obj.db_host, db_obj.db_name)
        cleanup_backups.delete_file_list(file_list)
Beispiel #4
0
def main():
    home_dir = os.path.expanduser('~')
    program_dir = os.path.abspath(os.path.join(home_dir, '.dbackups'))
    db_config_file = os.path.join(program_dir, 'databases.ini')

    if not os.path.isdir(program_dir):
        os.mkdir(program_dir)

    log_dir = os.path.join(program_dir, 'logs')
    if not os.path.isdir(log_dir):
        os.mkdir(log_dir)

    #logging_config = resource_filename(__name__, '../config/cron_logging.ini')
    #logging.config.fileConfig(logging_config)
    logging.basicConfig(level=logging.DEBUG,
                        filename=os.path.expanduser('~') + '/.dbackups/logs/database_backup_cron.log',
                        format='%(asctime)s %(levelname)-6s line %(lineno)-4s %(message)s')

    if not os.path.isfile(db_config_file):
        print('Config File not found. {}'.format(db_config_file))
        sys.exit(1)

    config = ConfigParser.ConfigParser()
    config.read(db_config_file)

    map_of_db_objects = {}

    for db_alias in config.sections():
        if config.getboolean(db_alias, 'enabled'):
            db = get_database_object(config.get(db_alias, 'db_type'),
                                     config.get(db_alias, 'db_host'),
                                     config.get(db_alias, 'db_name'),
                                     config.get(db_alias, 'db_user'),
                                     config.get(db_alias, 'db_pass'),
                                     config.get(db_alias, 'db_port'))
            map_of_db_objects[db_alias] = db

    for db_section, db_obj in map_of_db_objects.iteritems():
        logging.info('Starting backup of {}'.format(db_obj.db_name))
        try:
            db_obj.dump()
        except CommandError as e:
            logging.exception(e)
            logging.error('Failed to dump database: {}'.format(db_obj.db_name))
            logging.info('Moving on to the next database.')
            continue
        try:
            upload_http_put(db_obj.dump_file,
                            config.get(db_section, 'upload_url'),
                            config.get(db_section, 'upload_user'),
                            config.get(db_section, 'upload_pass'),
                            verify_request=False)
            logging.info('Successfully uploaded database back of [{}] to [{}] '.format(db_obj.db_name,
                                                                                       config.get(db_section,
                                                                                                  'upload_url'), ))
        except HTTPError as httperror:
            logging.exception(httperror)
            logging.error('Failed to Upload the backup to the configured location.')

        file_list = cleanup_backups.find_files_for_delete(db_obj.get_dump_dir(), db_obj.db_host, db_obj.db_name)
        cleanup_backups.delete_file_list(file_list)