python批量还原数据库的代码怎么写

这篇“python批量还原数据库的代码怎么写”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“python批量还原数据库的代码怎么写”文章吧。

代码如下:

# -*- coding: utf-8 -*-
import shutil,os

# 根据指定路径下的数据库备份文件生成还原脚本
def getFilesAndBuildSql(path,restorePath):
    for root,dirs,files in os.walk(path):
        for nfile in files:
            if os.path.splitext(nfile)[1] == ".bak":        # 筛选bak文件
                dbName = os.path.splitext(nfile)[0]
                logName = dbName + "_log"

                # 创建数据还原脚本并写入文件
                sqlFileName = 'C:\\Users\\merox-mac-win\\Desktop\\SqlRestore\\restore.sql'
                with open(sqlFileName, 'a') as file_object:
                        file_object.write("--还原数据库{0}--\n".format(dbName).decode("utf-8").encode("utf-8"))
                        file_object.write("RESTORE DATABASE {0}  FROM DISK = 'C:\\SqlRestore\\bak\\{1}' \n".format(dbName,nfile).decode("utf-8").encode("utf-8"))
                        file_object.write("WITH\n".decode("utf-8").encode("utf-8"))
                        file_object.write("     MOVE '{0}' TO '{2}\\{1}.mdf',\n ".format(dbName.replace("Kernel_HY_",""),dbName,restorePath).decode("utf-8").encode("utf-8"))
                        file_object.write("     MOVE '{0}' TO '{2}\\{1}.ldf'\n ".format(logName.replace("Kernel_HY_",""),logName,restorePath).decode("utf-8").encode("utf-8"))
                        file_object.write("GO\n\n".decode("utf-8").encode("utf-8"))

                # 生成删除数据库脚本
                with open('C:\\Users\\merox-mac-win\\Desktop\\SqlRestore\\drop.sql', 'a') as file_object:
                        file_object.write("--删除数据库{0}--\n".format(dbName).decode("utf-8").encode("utf-8"))
                        file_object.write("DROP DATABASE {0} \n".format(dbName).decode("utf-8").encode("utf-8"))
                        file_object.write("GO\n\n".decode("utf-8").encode("utf-8"))    
                            
                # 控制台输出
                print("RESTORE DATABASE {0}  FROM DISK = 'C:\\{1}' ".format(dbName,nfile))
                print("WITH")
                print("     MOVE '{0}' TO '{2}\\{1}.mdf', ".format(dbName.replace("Kernel_HY_",""),dbName,restorePath))
                print("     MOVE '{0}' TO '{2}\\{1}.ldf' ".format(logName.replace("Kernel_HY_",""),logName,restorePath))
                print("GO")
                print("")

# 创建发布文件夹
os.makedirs("C:\\Users\\merox-mac-win\\Desktop\\SqlRestore")
# 根据数据库备份文件目录生成还原脚本
getFilesAndBuildSql("C:\\Game_ZS\\Database\\dbbak","C:\\db")

# 生成还原BAT文件
with open("C:\\Users\\merox-mac-win\\Desktop\\SqlRestore\\restore.bat", 'a') as file_object:
                         file_object.write("md c:\\db\n".decode("utf-8").encode("utf-8"))
                         file_object.write("osql -E -i c:\\SqlRestore\\restore.sql\n".decode("utf-8").encode("utf-8"))
                         file_object.write("--还原数据库--\n".decode("utf-8").encode("gbk"))
                         for root,dirs,files in os.walk("C:\\Game_ZS\\Database\\dbbak\\link"):
                            for nfile in files:
                                print("osql -E -i c:\\{0}".format(nfile))
                                with open("C:\\Users\\merox-mac-win\\Desktop\\SqlRestore\\restore.bat", 'a') as file_object:
                                    file_object.write("osql -E -i c:\\SqlRestore\\link\\{0}\n".format(nfile))

# 复制链接服务器脚本文件夹
shutil.copytree("C:\\Game_ZS\\Database\\dbbak\\link","C:\\Users\\merox-mac-win\\Desktop\\SqlRestore\\link")

# 复制bak备份文件
shutil.copytree("C:\\Game_ZS\\Database\\dbbak\\bak","C:\\Users\\merox-mac-win\\Desktop\\SqlRestore\\bak")

# 压缩数据发布文件夹 
os.system("rar a   restore.rar SqlRestore")
# 测试压缩文件
os.system("rar t restore.rar")
# 删除生成的发布目录
shutil.rmtree("C:\\Users\\merox-mac-win\\Desktop\\SqlRestore")

python主要应用领域有哪些

1、云计算,典型应用OpenStack。

2、WEB前端开发,众多大型网站均为Python开发。

3、人工智能应用,基于大数据分析和深度学习而发展出来的人工智能本质上已经无法离开python。

4、系统运维工程项目,自动化运维的标配就是python+Django/flask。

5、金融理财分析,量化交易,金融分析。

6、大数据分析。

以上就是关于“python批量还原数据库的代码怎么写”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注蜗牛博客行业资讯频道。

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:niceseo99@gmail.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

评论

有免费节点资源,我们会通知你!加入纸飞机订阅群

×
天气预报查看日历分享网页手机扫码留言评论电报频道链接