码迷,mamicode.com
首页 > 数据库 > 详细

本地WordPress文章离线撰写后同步到远端(仅数据库)

时间:2018-01-24 14:09:11      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:查询   ace   lte   replace   替换   rem   数据   gpo   bsp   

import console;
import mysql.client;
 
//本地数据库
console.log("正在连接本地数据库服务器...");
var localdb,err = mysql.client(
    server = "localhost";
    uid = "root";
    pwd = "123456";
);
 
if(!localdb){
    console.log("连接数据库失败",err)
    return;
}
localdb.selectDb("blog");
console.log("本地数据库连接成功");
 
//远端数据库
console.log("正在连接远端数据库服务器...");
var remotedb,err = mysql.client(
    server = "8.8.8.8"; 
    uid = "xxxxx";
    pwd = "xxxxx";
);
 
if(!remotedb){
    console.log("连接数据库失败",err)
    return;
}
remotedb.selectDb("blog");
console.log("远端数据库连接成功");
 
//同步本地日志到远端
console.log("同步本地日志到远端");
//查询最大ID
var result = remotedb.query("SELECT max(ID) as MAXID FROM wp_posts");
var remote_post_max_id = result.fetchObject()["MAXID"];
//console.log("远端最大ID:"++remote_post_max_id)
result.free();
//查询本地
var result = localdb.query("SELECT ID FROM wp_posts where ID>"++tostring(remote_post_max_id));
var local_post_sync_count = result.numRows();
console.log("需同步"++local_post_sync_count)
result.free();
//执行同步
 
if(local_post_sync_count>0){
//首先替换网址
localdb.exec("update wp_posts set post_content=replace(post_content,‘thinkai.net‘,‘thinkai.net‘) where ID>"++tostring(remote_post_max_id));
 
var result = localdb.query("SELECT * FROM wp_posts where ID>"++tostring(remote_post_max_id));
for(i=1;local_post_sync_count){
var row = result.fetchObject();
    console.log(string.concat("同步ID:",row[‘ID‘]," 标题:",row[‘post_title‘]));
    remotedb.exec("insert into wp_posts values (@ID, @post_author, @post_date, @post_date_gmt, @post_content, @post_title, @post_excerpt, @post_status, @comment_status, @ping_status, @post_password, @post_name, @to_ping, @pinged, @post_modified, @post_modified_gmt, @post_content_filtered, @post_parent, @guid, @menu_order, @post_type, @post_mime_type, @comment_count)",row)
}
}
 
//同步本地日志分类到远端
console.log("同步本地日志分类到远端");
//查询最大ID
var result = remotedb.query("SELECT max(object_id) as MAXID FROM wp_term_relationships");
var remote_term_max_id = result.fetchObject()["MAXID"];
result.free();
//查询本地
var result = localdb.query("SELECT object_id FROM wp_term_relationships where object_id>"++tostring(remote_term_max_id));
var local_term_sync_count = result.numRows();
console.log("需同步"++local_term_sync_count)
result.free();
//执行同步
 
if(local_term_sync_count>0){
var result = localdb.query("SELECT * FROM wp_term_relationships where object_id>"++tostring(remote_term_max_id));
for(i=1;local_term_sync_count){
var row = result.fetchObject();
    console.log(string.concat("同步ID:",row[‘object_id‘]));
    remotedb.exec("insert into wp_term_relationships values (@object_id, @term_taxonomy_id, @term_order)",row)
}
}
 
//同步本地分类项目到远端
console.log("同步本地分类项目到远端");
//查询最大ID
var result = remotedb.query("SELECT max(term_taxonomy_id) as MAXID FROM wp_term_taxonomy");
var remote_termt_max_id = result.fetchObject()["MAXID"];
result.free();
//查询本地
var result = localdb.query("SELECT term_taxonomy_id FROM wp_term_taxonomy where term_taxonomy_id>"++tostring(remote_termt_max_id));
var local_termt_sync_count = result.numRows();
console.log("需同步"++local_termt_sync_count)
result.free();
//执行同步
 
if(local_termt_sync_count>0){
var result = localdb.query("SELECT * FROM wp_term_taxonomy where term_taxonomy_id>"++tostring(remote_termt_max_id));
for(i=1;local_termt_sync_count){
var row = result.fetchObject();
    console.log(string.concat("同步ID:",row[‘term_taxonomy_id‘]));
    remotedb.exec("insert into wp_term_taxonomy values (@term_taxonomy_id, @term_id, @taxonomy, @description, @parent, @count)",row)
}
}
//查询最大ID
var result = remotedb.query("SELECT max(term_id) as MAXID FROM wp_terms");
var remote_term_max_id = result.fetchObject()["MAXID"];
result.free();
//查询本地
var result = localdb.query("SELECT term_id FROM wp_terms where term_id>"++tostring(remote_term_max_id));
var local_term_sync_count = result.numRows();
console.log("需同步"++local_term_sync_count)
result.free();
//执行同步
 
if(local_term_sync_count>0){
var result = localdb.query("SELECT * FROM wp_terms where term_id>"++tostring(remote_term_max_id));
for(i=1;local_term_sync_count){
var row = result.fetchObject();
    console.log(string.concat("同步ID:",row[‘term_id‘]));
    remotedb.exec("insert into wp_terms values (@term_id, @name, @slug, @term_group)",row)
}
}
localdb.close();
remotedb.close();
console.pause();

 

本地WordPress文章离线撰写后同步到远端(仅数据库)

标签:查询   ace   lte   replace   替换   rem   数据   gpo   bsp   

原文地址:https://www.cnblogs.com/basstk/p/8340990.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!