用于克隆数据库的 SQL 脚本,保持原始数据库不变

编程入门 行业动态 更新时间:2024-10-24 18:26:57
本文介绍了用于克隆数据库的 SQL 脚本,保持原始数据库不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们目前有一个基本的 CMS 安装,在这个 CMS 中它包含一个完整的用户、产品、内容等工作数据集.我们希望增加我们的安装时间,因为现在我们目前必须进入 SQL Server 2012,创建一个新数据库,然后从现有的基本安装数据库中恢复该数据库.

we currently have a base installation of our CMS, in this CMS it contains a complete working dataset for users, products, content etc. We are looking to increase our installation time because right now we currently have to go into SQL Server 2012, create a new DB and then restore the DB from an existing base installation db.

我们每次安装最多可能需要 10 到 15 分钟.

This can take up to 10 - 15 minutes each installation we do.

我们还确保我们的基础数据库满足我们构建的网站的所有要求.

We also make sure our base database has all the requirements for sites we build.

我们的问题是,我们想做以下事情.

Our issue is, we would like to do the following.

  • 让脚本创建一个全新的空数据库
  • 将此数据库的克隆复制到一个新的 .bak 文件中
  • 获取这个 .bak 文件,然后将其复制到一个全新的数据库中,分别使用自己的 MDF 和 LDF 文件.
  • 数据库位于同一台服务器上,因此我们无需将其迁移到任何其他机器或实例.

    the databases are on the same server so we dont need to migrate this across to any other machine or instance.

    我们的代码如下

    CREATE database my_test BACKUP DATABASE test_db TO DISK = 'C:\my_test\my_test.bak' WITH INIT; EXEC internal_lab_test.dbo.sp_helpfile; RESTORE FILELISTONLY FROM DISK = 'C:\my_test\my_test.bak' RESTORE DATABASE my_test FROM DISK = 'C:\my_test\my_test.bak' WITH MOVE 'my_test' TO 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\test_db.mdf', MOVE 'my_test_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\test_db_log.ldf'

    我们希望确保一切都是新鲜干净的,但仍然包含所有数据,但是每次运行此代码时,我们都会收到以下错误消息

    we want to make sure that everything is fresh and clean, but also still contain all the data, however everytime we run this code, we get the following error messages

    我们还希望确保原始数据库的 mdf 和 ldf 文件完好无损,不会在新数据库中使用

    we also want to make sure the original database mdf and ldf files are left in tact and arent used in the new database

    Msg 3154, Level 16, State 4, Line 10 The backup set holds a backup of a database other than the existing 'my_test' database. Msg 3013, Level 16, State 1, Line 10 RESTORE DATABASE is terminating abnormally.

    推荐答案

    我知道这是旧的,但它是谷歌的第二个条目,所以为了完整性.

    I know this is old, but it was the 2nd entry in google so for completeness.

    这是因为数据库已经存在.所以要么删除数据库,要么添加替换.

    It is because the database already exists. so either drop the database or add with Replace.

    此外,my_test 和 my_test_log 名称必须是 restore filelistonly 命令中的逻辑名称.

    Also the my_test and my_test_log names need to be the logical names from the restore filelistonly command.

    RESTORE DATABASE my_test FROM DISK = 'C:\my_test\my_test.bak' WITH Replace, MOVE 'my_test' TO 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\test_db.mdf', MOVE 'my_test_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\test_db_log.ldf'

    更多推荐

    用于克隆数据库的 SQL 脚本,保持原始数据库不变

    本文发布于:2023-07-08 20:16:49,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1080295.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:数据库   脚本   原始   SQL

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!