#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 清理前一批错误的二级分类卡通图片(本地与线上同步清理): 1. 清理 public/uploads/20261028/*.png (本地与线上) 2. 清理 public/static/categories/job-*.png 和 poultry-*.png (本地与线上) 3. 清理 uniapp/static/categories/job-*.png 和 poultry-*.png (本地) 4. 清理数据库 fa_attachment 中 url 结尾为 .png 的 20261028 记录 (本地与线上) """ import os import sys import io import paramiko import pymysql sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace') LOCAL_ROOT = r"D:\Softwares\phpstudy_pro\WWW\fenleixinxi" REMOTE_HOST = "8.137.181.63" REMOTE_PORT = 22 REMOTE_USER = "root" REMOTE_PASS = "liyanxia@6688" REMOTE_ROOT = "/www/wwwroot/fenleixinxi.gzyakj.com.cn" DB_USER = "fenleixinxi" DB_PASS = "fenleixinxi" DB_NAME = "fenleixinxi" LOCAL_DB_HOST = "127.0.0.1" LOCAL_DB_PORT = 3307 WRONG_CAT_PNGS = [ 'job-tech.png', 'job-catering.png', 'job-clean.png', 'job-rider.png', 'job-sales.png', 'job-driver.png', 'job-edu.png', 'job-parttime.png', 'poultry-egg.png', 'poultry-chicken.png', 'poultry-pork.png', 'poultry-beef.png', 'poultry-fish.png' ] def ssh_exec(client, cmd): stdin, stdout, stderr = client.exec_command(cmd) out = stdout.read().decode("utf-8", errors="replace").strip() err = stderr.read().decode("utf-8", errors="replace").strip() return out, err def main(): print("=== [1/4] 清理本地错误的图片文件 ===") # 1. 清理 public/uploads/20261028/*.png local_up_dir = os.path.join(LOCAL_ROOT, "public", "uploads", "20261028") del_up_cnt = 0 if os.path.exists(local_up_dir): for f in os.listdir(local_up_dir): if f.endswith('.png'): p = os.path.join(local_up_dir, f) os.remove(p) del_up_cnt += 1 print(f" ✓ 已清理本地 public/uploads/20261028/*.png 错误图片: {del_up_cnt} 个") # 2. 清理 public/static/categories/ 下的 13 个卡通 png del_static_cnt = 0 for f in WRONG_CAT_PNGS: p = os.path.join(LOCAL_ROOT, "public", "static", "categories", f) if os.path.exists(p): os.remove(p) del_static_cnt += 1 print(f" ✓ 已清理本地 public/static/categories/ 下错误卡通图片: {del_static_cnt} 个") # 3. 清理 uniapp/static/categories/ 下的 13 个卡通 png del_uni_cnt = 0 for f in WRONG_CAT_PNGS: p = os.path.join(LOCAL_ROOT, "uniapp", "static", "categories", f) if os.path.exists(p): os.remove(p) del_uni_cnt += 1 print(f" ✓ 已清理本地 uniapp/static/categories/ 下错误卡通图片: {del_uni_cnt} 个") print("\n=== [2/4] 清理本地数据库中残留的 png 附件记录 ===") local_conn = pymysql.connect( host=LOCAL_DB_HOST, port=LOCAL_DB_PORT, user=DB_USER, password=DB_PASS, database=DB_NAME, charset="utf8mb4", autocommit=True ) with local_conn.cursor() as cur: cur.execute("DELETE FROM fa_attachment WHERE url LIKE '/uploads/20261028/%.png';") print(f" ✓ 已删除本地 fa_attachment 中错误 png 记录: {cur.rowcount} 条") local_conn.close() print("\n=== [3/4] 连接线上服务器并清理远程错误文件 ===") ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(REMOTE_HOST, port=REMOTE_PORT, username=REMOTE_USER, password=REMOTE_PASS, timeout=30) print("✓ 远程 SSH 连接成功") # 清理线上 uploads/20261028/*.png cmd1 = f"rm -f {REMOTE_ROOT}/public/uploads/20261028/*.png" ssh_exec(ssh, cmd1) print(" ✓ 线上 /public/uploads/20261028/*.png 已清理") # 清理线上 static/categories/*.png 错误卡通图片 for f in WRONG_CAT_PNGS: cmd2 = f"rm -f {REMOTE_ROOT}/public/static/categories/{f}" ssh_exec(ssh, cmd2) print(f" ✓ 线上 /public/static/categories/ 下 13 个卡通图片已清理") print("\n=== [4/4] 清理线上数据库中残留的 png 附件记录并清缓存 ===") sql_clean = "DELETE FROM fa_attachment WHERE url LIKE '/uploads/20261028/%.png';" cmd_sql = f'mysql -u{DB_USER} -p{DB_PASS} {DB_NAME} -e "{sql_clean}" 2>/dev/null' ssh_exec(ssh, cmd_sql) print(" ✓ 线上数据库 fa_attachment 中错误 png 记录已清理") # 清除线上运行时缓存 ssh_exec(ssh, f"rm -rf {REMOTE_ROOT}/runtime/temp/* {REMOTE_ROOT}/runtime/cache/*") print(" ✓ 线上运行时缓存已清理") # 验证 uploads/20261028 目前保留的文件(只有 13 张 .jpg 真实照片) out, _ = ssh_exec(ssh, f"ls -1 {REMOTE_ROOT}/public/uploads/20261028/") valid_files = [x for x in out.splitlines() if x.strip()] print(f"\n线上 uploads/20261028/ 当前现存有效文件数: {len(valid_files)} 个 (全为 .jpg 真实实拍图)") for vf in valid_files[:3]: print(f" - {vf}") if len(valid_files) > 3: print(f" ... 等共 {len(valid_files)} 张真实实拍图片") ssh.close() print("\n✓ 前一批错误图片清理全部圆满完成!") if __name__ == "__main__": main()