Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/sharedra.../mysql
File: myisam_repair.py
#!/opt/imh-python/bin/python3
[0] Fix | Delete
"""Mass MyISAM table fixer"""
[1] Fix | Delete
# 9/19/14 Vanessa Vasile vanessav@inmotion.net
[2] Fix | Delete
# This script does not require MySQL to be running
[3] Fix | Delete
[4] Fix | Delete
import glob
[5] Fix | Delete
from pathlib import Path
[6] Fix | Delete
import argparse
[7] Fix | Delete
import subprocess
[8] Fix | Delete
[9] Fix | Delete
[10] Fix | Delete
def iter_databases():
[11] Fix | Delete
"""Iterate over folders in /var/lib/mysql"""
[12] Fix | Delete
# Generally speaking, every folder in datadir should be a database
[13] Fix | Delete
# if it's not, no biggie
[14] Fix | Delete
for entry in Path('/var/lib/mysql').iterdir():
[15] Fix | Delete
if entry.is_dir():
[16] Fix | Delete
yield entry.name
[17] Fix | Delete
[18] Fix | Delete
[19] Fix | Delete
def repair_database(database: str, tmpdir: str) -> bool:
[20] Fix | Delete
"""Runs a repair on a table"""
[21] Fix | Delete
Path(tmpdir).mkdir(mode=0o755, exist_ok=True, parents=True)
[22] Fix | Delete
# List MyISAM tables
[23] Fix | Delete
if tables := glob.glob(f'/var/lib/mysql/{database}/*.MYI'):
[24] Fix | Delete
print(f'Fixing {len(tables)} tables in database {database}')
[25] Fix | Delete
else:
[26] Fix | Delete
print('No MyISAM tables to fix in database', database)
[27] Fix | Delete
return True
[28] Fix | Delete
repaired = True
[29] Fix | Delete
for table in tables:
[30] Fix | Delete
print(" Fixing %s" % table)
[31] Fix | Delete
try:
[32] Fix | Delete
subprocess.run(
[33] Fix | Delete
['myisamchk', '-r', '-q', '-t', tmpdir, table],
[34] Fix | Delete
check=True,
[35] Fix | Delete
stderr=subprocess.DEVNULL,
[36] Fix | Delete
stdout=subprocess.DEVNULL,
[37] Fix | Delete
)
[38] Fix | Delete
except subprocess.CalledProcessError:
[39] Fix | Delete
repaired = False
[40] Fix | Delete
return repaired
[41] Fix | Delete
[42] Fix | Delete
[43] Fix | Delete
def main():
[44] Fix | Delete
"""Default function"""
[45] Fix | Delete
args = parse_args()
[46] Fix | Delete
if args.repair_all is True:
[47] Fix | Delete
# Get a list of databases
[48] Fix | Delete
for dbname in iter_databases():
[49] Fix | Delete
repair_database(dbname, args.tmpdir)
[50] Fix | Delete
if args.database:
[51] Fix | Delete
repair_database(args.database, args.tmpdir)
[52] Fix | Delete
[53] Fix | Delete
[54] Fix | Delete
def parse_args():
[55] Fix | Delete
"""Defines valid command line options"""
[56] Fix | Delete
parser = argparse.ArgumentParser(description=__doc__)
[57] Fix | Delete
group = parser.add_mutually_exclusive_group(required=True)
[58] Fix | Delete
# fmt: off
[59] Fix | Delete
group.add_argument(
[60] Fix | Delete
'-a', '--all', dest='repair_all', action='store_true',
[61] Fix | Delete
help="Repair all databases",
[62] Fix | Delete
)
[63] Fix | Delete
group.add_argument(
[64] Fix | Delete
'-d', '--database', dest='database',
[65] Fix | Delete
help="The name of a specific database to repair",
[66] Fix | Delete
)
[67] Fix | Delete
parser.add_argument('-t', '--tmpdir', dest='tmpdir', default='/root/tmp')
[68] Fix | Delete
# fmt: on
[69] Fix | Delete
return parser.parse_args()
[70] Fix | Delete
[71] Fix | Delete
[72] Fix | Delete
if __name__ == '__main__':
[73] Fix | Delete
try:
[74] Fix | Delete
main()
[75] Fix | Delete
except KeyboardInterrupt:
[76] Fix | Delete
pass
[77] Fix | Delete
[78] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function