#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 数据全面同步到线上: 1. 校验并补齐 public/uploads/ 所有文件至线上对应目录 2. 完整同步本地数据库各业务表到线上数据库 3. 清理线上缓存并验证接口 """ import os import sys import io import time 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 # 核心同步表列表 SYNC_TABLES = [ "fa_user", "fa_user_ext", "fa_attachment", "fa_fc_category", "fa_fc_post", "fa_fc_banner", "fa_fc_comment", "fa_fc_favorite", "fa_fc_follow", "fa_fc_message", "fa_fc_plan", "fa_fc_config", "fa_fc_area", "fa_fc_merchant", ] 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/3] 检查并同步本地 uploads 文件到线上服务器 ===") 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 连接成功") sftp = ssh.open_sftp() # 扫描本地 public/uploads/ 下所有文件 local_uploads = os.path.join(LOCAL_ROOT, "public", "uploads") uploaded_cnt = 0 for root, dirs, files in os.walk(local_uploads): rel_dir = os.path.relpath(root, local_uploads) if rel_dir == ".": remote_sub_dir = f"{REMOTE_ROOT}/public/uploads" else: remote_sub_dir = f"{REMOTE_ROOT}/public/uploads/{rel_dir.replace(chr(92), '/')}" # 确保远程目录存在 ssh_exec(ssh, f"mkdir -p '{remote_sub_dir}'") for f in files: if f == ".gitkeep": continue loc_file = os.path.join(root, f) rem_file = f"{remote_sub_dir}/{f}" # 检查远程文件是否存在且大小一致 try: r_stat = sftp.stat(rem_file) l_size = os.path.getsize(loc_file) if r_stat.st_size == l_size: continue # 大小一致跳过 except IOError: pass # 远程不存在 sftp.put(loc_file, rem_file) ssh_exec(ssh, f"chown www:www '{rem_file}'") uploaded_cnt += 1 print(f" ✓ 补充上传文件: uploads/{rel_dir}/{f}") print(f"✓ 文件检查完毕,本次共增量补传文件: {uploaded_cnt} 个") sftp.close() print("\n=== [2/3] 全量同步本地数据库表到线上 ===") 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 ) sync_sqls = [ "SET FOREIGN_KEY_CHECKS = 0;", "SET NAMES utf8mb4;" ] with local_conn.cursor() as cur: for t in SYNC_TABLES: try: cur.execute(f"SELECT * FROM `{t}`;") rows = cur.fetchall() cur.execute(f"DESCRIBE `{t}`;") cols = [f[0] for f in cur.fetchall()] except Exception as e: print(f" - 跳过表 {t} ({e})") continue col_str = ", ".join([f"`{c}`" for c in cols]) sync_sqls.append(f"TRUNCATE TABLE `{t}`;") for r in rows: val_list = [] for v in r: if v is None: val_list.append("NULL") elif isinstance(v, (int, float)): val_list.append(str(v)) else: esc = str(v).replace("\\", "\\\\").replace("'", "\\'") val_list.append(f"'{esc}'") sync_sqls.append(f"INSERT INTO `{t}` ({col_str}) VALUES ({', '.join(val_list)});") print(f" ✓ 表 [{t}] 同步准备完毕: {len(rows)} 条记录") sync_sqls.append("SET FOREIGN_KEY_CHECKS = 1;") full_sql = "\n".join(sync_sqls) # 在线上执行 mysql_cmd = f'mysql -u{DB_USER} -p{DB_PASS} --default-character-set=utf8mb4 {DB_NAME} 2>/dev/null <<\'SQLEOF\'\n{full_sql}\nSQLEOF' out, err = ssh_exec(ssh, mysql_cmd) if err and "error" in err.lower(): print(f"✗ 线上数据库更新失败: {err}") else: print("✓ 线上数据库所有业务表已全量重写同步完成!") local_conn.close() print("\n=== [3/3] 清理缓存并校验 ===") ssh_exec(ssh, f"rm -rf {REMOTE_ROOT}/runtime/temp/* {REMOTE_ROOT}/runtime/cache/*") print("✓ 线上 ThinkPHP 运行时缓存清理完成") # 验证分类接口 cmd_api = "curl -s -k 'https://fenleixinxi.gzyakj.com.cn/api/v1/home/categories'" api_res, _ = ssh_exec(ssh, cmd_api) if '"code":0' in api_res: print("✓ 线上分类 API (GET /api/v1/home/categories) 校验通过 (HTTP 200 OK)") # 验证信息流接口 cmd_feed = "curl -s -k 'https://fenleixinxi.gzyakj.com.cn/api/v1/home/feed'" feed_res, _ = ssh_exec(ssh, cmd_feed) if '"code":0' in feed_res: print("✓ 线上信息流 API (GET /api/v1/home/feed) 校验通过 (HTTP 200 OK)") ssh.close() print("\n✓ 数据同步到线上全部圆满完成!") if __name__ == "__main__": main()