Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ShExBy/smshex_r.../opt/sharedra.../mysql
File: mysqlrepair.py
#!/opt/imh-python/bin/python3
[0] Fix | Delete
"""repairs database tables"""
[1] Fix | Delete
from argparse import ArgumentParser
[2] Fix | Delete
from pathlib import Path
[3] Fix | Delete
import sys
[4] Fix | Delete
from typing import Literal
[5] Fix | Delete
import pymysql
[6] Fix | Delete
from pymysql.cursors import Cursor
[7] Fix | Delete
[8] Fix | Delete
# specify the socket file for pymysql
[9] Fix | Delete
if Path("/etc/redhat-release").exists():
[10] Fix | Delete
SOCK = "/var/lib/mysql/mysql.sock"
[11] Fix | Delete
else:
[12] Fix | Delete
# ubuntu support
[13] Fix | Delete
SOCK = "/run/mysqld/mysqld.sock"
[14] Fix | Delete
[15] Fix | Delete
[16] Fix | Delete
def repair_tables(
[17] Fix | Delete
conn: pymysql.Connection, dbcmd: Literal['OPTIMIZE', 'REPAIR']
[18] Fix | Delete
) -> int:
[19] Fix | Delete
rcode = 0
[20] Fix | Delete
with conn.cursor(Cursor) as cur:
[21] Fix | Delete
try:
[22] Fix | Delete
cur.execute("SHOW TABLES")
[23] Fix | Delete
tablenames = [x[0] for x in cur.fetchall()]
[24] Fix | Delete
except pymysql.Error as exc:
[25] Fix | Delete
sys.exit(exc)
[26] Fix | Delete
for table in tablenames:
[27] Fix | Delete
escaped = str(table).replace('`', '``')
[28] Fix | Delete
try:
[29] Fix | Delete
cur.execute(f"{dbcmd} TABLE `{escaped}`")
[30] Fix | Delete
print(*cur.fetchone())
[31] Fix | Delete
except pymysql.Error as exc:
[32] Fix | Delete
print(
[33] Fix | Delete
f"Error running {dbcmd} TABLE on {table}: {exc}",
[34] Fix | Delete
file=sys.stderr,
[35] Fix | Delete
)
[36] Fix | Delete
rcode = 2
[37] Fix | Delete
continue
[38] Fix | Delete
return rcode
[39] Fix | Delete
[40] Fix | Delete
[41] Fix | Delete
def parse_args() -> tuple[Literal['OPTIMIZE', 'REPAIR'], str]:
[42] Fix | Delete
parser = ArgumentParser(description=__doc__)
[43] Fix | Delete
dbcmd = parser.add_mutually_exclusive_group(required=True)
[44] Fix | Delete
dbcmd.add_argument(
[45] Fix | Delete
'--repair', action='store_const', const='REPAIR', dest='dbcmd'
[46] Fix | Delete
)
[47] Fix | Delete
dbcmd.add_argument(
[48] Fix | Delete
'--optimize', action='store_const', const='OPTIMIZE', dest='dbcmd'
[49] Fix | Delete
)
[50] Fix | Delete
parser.add_argument('dbname')
[51] Fix | Delete
args = parser.parse_args()
[52] Fix | Delete
return args.dbcmd, args.dbname
[53] Fix | Delete
[54] Fix | Delete
[55] Fix | Delete
def main():
[56] Fix | Delete
dbcmd, dbname = parse_args()
[57] Fix | Delete
with pymysql.connect(
[58] Fix | Delete
read_default_file="/root/.my.cnf",
[59] Fix | Delete
unix_socket=SOCK,
[60] Fix | Delete
database=dbname,
[61] Fix | Delete
) as conn:
[62] Fix | Delete
rcode = repair_tables(conn, dbcmd)
[63] Fix | Delete
sys.exit(rcode)
[64] Fix | Delete
[65] Fix | Delete
[66] Fix | Delete
if __name__ == '__main__':
[67] Fix | Delete
main()
[68] Fix | Delete
[69] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function