Zblog迁移至Typecho,Python脚本

图片
168 ℃

注意,迁移之前一定要全部备份。
我在迁移评论的时候不小心把旧博客的评论表数据给清空了,还好有数据备份
文章迁移脚本
此脚本需要先将分类表手动迁移,注意ID以及名称要和原来的一致

# -*- coding: utf-8 -*-
"""
-------------------------------------------------
@ Author :Lan
@ Blog :www.lanol.cn
@ Date : 2021/6/29
@ Description:I'm in charge of my Code
-------------------------------------------------
"""
import time

import pymysql

HOST = "数据库HOST地址"
USER = "数据库用户名"
PASSWORD = "数据库密码"
PORT = 数据库端口
db = pymysql.connect(HOST, USER, PASSWORD, "旧数据库名称", PORT, charset='utf8')
selectSql = "select * from zbp_post where log_cateID!=0 and log_ID!=436"
cursor = db.cursor()
cursor.execute(selectSql)
source = cursor.fetchall()
db.close()
db = pymysql.connect(HOST, USER, PASSWORD, "新数据库名称", PORT, charset='utf8')
cursor = db.cursor()
a = 1
for i in source:
    cid = i[0]
    title = i[9].replace("'", '"')
    slug = i[0]
    created = i[12]
    modified = i[12]
    text = i[11].replace('{#ZC_BLOG_HOST#}', '新的博客地址')
    order = 0
    authorid = 1
    template = None
    type = 'post'
    status = 'publish'
    password = ''
    commentsNum = i[13]
    allowComment = 1
    allowPing = 1
    allowFeed = 1
    parent = 0
    views = i[14]
    agree = 0
    try:

        insertSql = f"INSERT INTO typecho_contents VALUES({cid},'{title}','{slug}','{created}','{modified}','{text}','{order}','{authorid}',NULL,'{type}','{status}','{password}','{commentsNum}','{allowComment}','{allowPing}','{allowFeed}','{parent}','{views}','{agree}') "
        cursor.execute(insertSql)
        insertSql = f"Insert into typecho_relationships values('{cid}','{i[1]}')"
        cursor.execute(insertSql)
        db.commit()
        print(f'{cid}迁移成功')
    except:
        print(f'{cid}迁移失败')
db.commit()
db.close()

评论数据迁移脚本

# -*- coding: utf-8 -*-
"""
-------------------------------------------------
@ Author :Lan
@ Blog :www.lanol.cn
@ Date : 2021/6/29
@ Description:I'm in charge of my Code
-------------------------------------------------
"""

import pymysql
import time

HOST = "数据库HOST地址"
USER = "数据库用户名"
PASSWORD = "数据库密码"
PORT = 数据库端口
db = pymysql.connect(HOST, USER, PASSWORD, "旧数据库名称", PORT, charset='utf8')
selectSql = "select * from zbp_comment"
cursor = db.cursor()
cursor.execute(selectSql)
source = cursor.fetchall()
db.close()
db = pymysql.connect(HOST, USER, PASSWORD, "新数据库名称", PORT, charset='utf8')
cursor = db.cursor()
for i in source:
    cid = i[1]
    created = i[10]
    author = i[6]
    authorId = i[5]
    ownerid = i[5]
    mail = i[7]
    url = i[8]
    ip = i[11]
    agent = i[12]
    text = i[9]
    type = 'comment'
    status = 'approved'
    parent = i[4]
    try:
        insertSql = f"Insert into typecho_comments values (NULL ,'{cid}','{created}','{author}','{authorId}','{ownerid}','{mail}','{url}'" \
                    f",'{ip}','{agent[:100]}','{text}','{type}','{status}','{parent}')"
        cursor.execute(insertSql)
        db.commit()
    except:
        print(f'{cid}迁移失败')
db.commit()
db.close()

下载有疑问看下这里

相关推荐:

我要评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

添加新评论