|
In case anyone googles this or something, here is the solution I came up with. It uses putty + ssh and the system() function to make sql dumps before performing the script, but if you are gutsy you can just omit that. It also uses my database class, but it would be pretty easy to just swap them out for generic mysql statements.
- <p> //update the remote database
-
- //print a message
- function msg($msg) {
- print "$msg <br /><hr>";
- }
-
- msg("Connecting to databases");
-
- //open connections to both DB's
- include("php/db.php");
- $locdb = new DB(true);
- $remdb = new DB();
- $remdb->connect("server", "user", "pass", "database_name");
-
- if ($locdb->error) {
- msg($locdb->err);
- exit;
- }
- if ($remdb->error) {
- msg($remdb->err);
- exit;
- }
- msg("Connected!");
-
- //file path info
- //where you want the dump files to go
- $filedump = 'c:\web\summit_new\dumps\\';
- //where your mysql bin is (not needed for *nix)
- $mysqlbin = 'c:\wamp\mysql\bin\\';
- //where your putty is (not needed for *nix, use openssh)
- $putty = 'c:\Program Files\PuTTy\\';</p><p>
- msg("Backing up both databases");
-
- //array of system commands
- $system = array(
- "{$mysqlbin}mysqldump -u root --skip-add-locks --add-drop-table --create-options --extended-insert --compatible=mysql40 database_name tables > {$filedump}local_dump." .time() .".sql",
- ""{$putty}plink" -pw ssh_pass ssh_user@server mysqldump -u db_user -pdb_pass --skip-lock-tables --skip-add-locks --add-drop-table --create-options --extended-insert db_name db_tables > {$filedump}remote_dump.".time().".sql"
- );
-
-
- foreach($system as $cmd) {
- msg("System Call:<br />$cmd");
- system($cmd);
- }
-
- msg("Backup complete. Dump files at $filedump");
- </p><p> //array of tables to sync
- $tables = array("page_info", "page_content");
-
- foreach ($tables as $table) {
-
- msg("Using table `$table`");
-
- $locdb->table = $table;
- $remdb->table = $table;
- if (mysql_num_fields($locdb->select()) != mysql_num_fields($remdb->select())) {
- msg("Column mismatch in table `$table`. Use dump files to import data");
- break;
- }
-
- msg("Emptying remote table `$table`");
- $remdb->remove();
- if ($remdb->error) {
- msg($remdb->error);
- }
-
- msg("Importing local db content into remote db");
- while ($locrow = mysql_fetch_assoc($locdb->result)) {
- $rows = "";
- $vals = array();
-
- foreach ($locrow as $key => $value) {
- $rows .= ",$key";
- $vals[] = $value;
- }
- $rows = preg_replace("/^,/", "", $rows);
-
- $remdb->insert($rows, $vals);
- if ($remdb->error) {
- msg($remdb->error);
- }
- }
-
- }
- msg("Remote database updated");
- exit; </p>
复制代码 still having problems with this line though:
- ""{$putty}plink" -pw ssh_pass <a href="mailto:ssh_user@server">ssh_user@server</a> mysqldump -u db_user -pdb_pass --skip-lock-tables --skip-add-locks --add-drop-table --create-options --extended-insert db_name db_tables > {$filedump}remote_dump.".time().".sql"
复制代码
try another approach (using mysqldump remotely) rather than instantiating a new SSH session, provided of course the -h parameter will work in this situation.
Works great now with the "-h server" or --host flag set, and is much easier on the eyes. Plus it eliminates the middle man all together.
|
|