#!/opt/imh-python/bin/python3
"""Mass MyISAM table fixer"""
# 9/19/14 Vanessa Vasile vanessav@inmotion.net
# This script does not require MySQL to be running
"""Iterate over folders in /var/lib/mysql"""
# Generally speaking, every folder in datadir should be a database
for entry in Path('/var/lib/mysql').iterdir():
def repair_database(database: str, tmpdir: str) -> bool:
"""Runs a repair on a table"""
Path(tmpdir).mkdir(mode=0o755, exist_ok=True, parents=True)
if tables := glob.glob(f'/var/lib/mysql/{database}/*.MYI'):
print(f'Fixing {len(tables)} tables in database {database}')
print('No MyISAM tables to fix in database', database)
print(" Fixing %s" % table)
['myisamchk', '-r', '-q', '-t', tmpdir, table],
stderr=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
except subprocess.CalledProcessError:
if args.repair_all is True:
# Get a list of databases
for dbname in iter_databases():
repair_database(dbname, args.tmpdir)
repair_database(args.database, args.tmpdir)
"""Defines valid command line options"""
parser = argparse.ArgumentParser(description=__doc__)
group = parser.add_mutually_exclusive_group(required=True)
'-a', '--all', dest='repair_all', action='store_true',
help="Repair all databases",
'-d', '--database', dest='database',
help="The name of a specific database to repair",
parser.add_argument('-t', '--tmpdir', dest='tmpdir', default='/root/tmp')
return parser.parse_args()
if __name__ == '__main__':
except KeyboardInterrupt: