import paramiko import os ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect('8.137.181.63', port=22, username='root', password='liyanxia@6688', timeout=15) def run(cmd): stdin, stdout, stderr = ssh.exec_command(cmd) out = stdout.read().decode('utf-8', errors='ignore') err = stderr.read().decode('utf-8', errors='ignore') return out, err print('Step 1: Backup current remote yjyf database...') out, err = run('mysqldump -uyjyf -pyjyf --default-character-set=utf8mb4 yjyf > /tmp/yjyf_remote_backup_before_deploy.sql') out2, _ = run('ls -lh /tmp/yjyf_remote_backup_before_deploy.sql') print('Backup done:', out2.strip()) print('Step 2: Upload local_yjyf_deploy.sql to remote /tmp...') sftp = ssh.open_sftp() local_file = r'd:\Projects\phpstudy_pro\WWW\yjyf\db\local_yjyf_deploy.sql' remote_file = '/tmp/local_yjyf_deploy.sql' sftp.put(local_file, remote_file) sftp.close() out3, _ = run('ls -lh /tmp/local_yjyf_deploy.sql') print('Uploaded:', out3.strip()) print('Step 3: Import into remote yjyf database...') out4, err4 = run('mysql -uyjyf -pyjyf --default-character-set=utf8mb4 yjyf < /tmp/local_yjyf_deploy.sql') if err4 and 'Warning' not in err4: print('Import error:', err4) else: print('Import command finished.') print('Step 4: Verify tables count and key tables...') out5, _ = run("mysql -uyjyf -pyjyf yjyf -e 'SHOW TABLES;'") tables = [l.strip() for l in out5.strip().splitlines() if l.strip()] print('Remote table count now:', max(0, len(tables) - 1)) out6, _ = run("mysql -uyjyf -pyjyf yjyf -e 'SELECT \"fa_user\", COUNT(*) FROM fa_user UNION ALL SELECT \"fa_master\", COUNT(*) FROM fa_master UNION ALL SELECT \"fa_order\", COUNT(*) FROM fa_order UNION ALL SELECT \"fa_service\", COUNT(*) FROM fa_service UNION ALL SELECT \"fa_agreement\", COUNT(*) FROM fa_agreement;'") print('Key tables counts:\n' + out6) # 删除远程临时 sql 文件保障安全 run('rm -f /tmp/local_yjyf_deploy.sql') ssh.close()