简体   繁体   English

创建SQL Server数据库的工作副本的最佳方法是什么?

[英]What's the best way to create a working copy of a SQL Server database?

I have a SQL database that I am currently converting from an Access database. 我有一个正在从Access数据库转换的SQL数据库。 One of the features of the Access database is to 'Copy DB' and is used when working in a 'dev' site - it copies all of the production data by physically copying the production file into the dev site. Access数据库的功能之一是“复制数据库”,并在“开发”站点中使用时使用-它通过将生产文件物理复制到开发站点来复制所有生产数据。 In this way, all production data and structure and queries and everything is duplicated into the dev version. 这样,所有生产数据,结构和查询以及所有内容都会复制到开发版本中。 This only needs to be done occasionally when we are testing why something is occurring and we don't want to play in production data; 仅当我们正在测试为什么会发生某些事情并且我们不想播放生产数据时,才需要偶尔执行此操作。 not rare, but not frequent either - perhaps once or twice a month. 并不罕见,但也不频繁-每月一次或两次。 I'm wondering what have other people done to accomplish this when working in SQL? 我想知道在使用SQL时其他人做了什么来完成此任务?

I'm thinking that I could do a DB backup followed by a restore to the DEV version, but I don't want this backup to interfere with normal backup processes. 我认为我可以先进行数据库备份,然后再还原到DEV版本,但是我不希望此备份干扰正常的备份过程。 Is there any way to do this from one DB straight to another instead of going to the file system, and have the backup seem like it never happened (ie the REAL backup will still back up all items that really needed to be backed up)? 是否有任何方法可以将一个数据库直接移到另一个数据库,而不是直接移至文件系统,并且备份似乎从未发生过(即REAL备份仍会备份所有真正需要备份的项目)?

What other options are there? 还有什么其他选择? I have SQL Compare and SQL Data Compare from Red Gate, but I need to expose this functionality to certain users (with high privs and access to the DEV site), and they don't have it. 我有Red Gate的SQL比较和SQL数据比较,但是我需要向某些用户(具有较高的特权和对DEV站点的访问权限)公开此功能,而他们却没有。

Ok well after looking around a bit, I've come to the conclusion that I do have to go thru the file system, but there is a way to do a backup/restore without affecting the normal backup process by using 'Copy Only' mode. 环顾四周后,我得出结论,我确实必须通过文件系统,但是通过使用“仅复制”模式,有一种方法可以进行备份/还原而不影响正常的备份过程。 Here's script to do it: 这是执行此操作的脚本:

BACKUP DATABASE [ProductionDB] 
TO DISK = N'D:\ProductionDBToDevTransfer.bak' 
WITH
    COPY_ONLY, 
    NOFORMAT, 
    INIT,  
    NAME = N'DB-Full Backup', 
    SKIP, 
    NOREWIND, 
    NOUNLOAD, 
    STATS = 10

RESTORE DATABASE [DevDB] 
FROM DISK = N'D:\ProductionDBToDevTransfer.bak' 
WITH 
    FILE = 1,  
    MOVE N'ProductionDB' TO N'D:\Microsoft\SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\DevDB.mdf',  
    MOVE N'ProductionDB_log' TO N'D:\Microsoft\SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\DevDB_log.ldf',  
    NOUNLOAD,  
    REPLACE,  
    STATS = 10

Take particular note of the MOVE statements in the RESTORE command... by default, the RESTORE will restore files to the originally backed up physical files, NOT the Dev DB files, despite the fact that you are restoring to the DEV db... I ALMOST found out the hard way, when I did the restore, and SSMS complained that the files were in use by ANOTHER db... OMG, how non-intuitive. 请特别注意 RESTORE命令中的MOVE语句...默认情况下,尽管您正在还原到DEV数据库,但是RESTORE会将文件还原到最初备份的物理文件,而不是Dev DB文件。我进行还原时,ALMOST发现了很难的方法,SSMS抱怨文件被ANOTHER数据库使用... OMG,多么不直观。

The script above works, but doesn't change the logical file name for the copied server. 上面的脚本可以运行,但是不会更改复制服务器的逻辑文件名。 So, if you try and run it again to reverse the process it will fail in the MOVE statements. 因此,如果您尝试再次运行它以撤消该过程,它将在MOVE语句中失败。

I modified the script a little and came up with the following which seems to work for me. 我对脚本进行了一些修改,并提出了以下对我似乎有用的方法。 I'm new to this, so be careful! 我是新来的,所以要小心!

DECLARE @SOURCEDB nvarchar(100)
DECLARE @SOURCEDBLOG nvarchar(100)
DECLARE @DESTINATIONDB nvarchar(100)
DECLARE @DESTINATIONDBLOG nvarchar(100)
DECLARE @BACKUPDIR nvarchar(100)
DECLARE @BACKUPFILE nvarchar(100)
DECLARE @BACKUPNAME nvarchar(100)
DECLARE @SQLDATADIR nvarchar(100)
DECLARE @SQLDATABACKUPFILE nvarchar(100)
DECLARE @SQLDATABACKUPLOGFILE nvarchar(100)

--CHANGE THESE VALUES TO MATCH YOUR SYSTEM
SET @SOURCEDB = N'test'
SET @DESTINATIONDB = N'test-backup'
SET @BACKUPDIR = N'C:\SHARED\'
SET @SQLDATADIR = N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\'

--CALCULATED VALUES
SET @SOURCEDBLOG = @SOURCEDB + N'_log'
SET @DESTINATIONDBLOG = @DESTINATIONDB + N'_log'
SET @BACKUPFILE = @BACKUPDIR + @SOURCEDB + N'-to-' + @DESTINATIONDB + N'.bak'
SET @BACKUPNAME = @SOURCEDB + N'-Full Backup'
SET @SQLDATABACKUPFILE = @SQLDATADIR + @DESTINATIONDB + N'.mdf'
SET @SQLDATABACKUPLOGFILE = @SQLDATADIR + @DESTINATIONDBLOG + N'.ldf'

--BACKUP THE DATABASE
BACKUP DATABASE @SOURCEDB
TO DISK = @BACKUPFILE
WITH
        COPY_ONLY, 
        NOFORMAT, 
        INIT,  
        NAME = @BACKUPNAME, 
        SKIP, 
        NOREWIND, 
        NOUNLOAD, 
        STATS = 10

--RESTORE THE BACKUP TO THE NEW DATABASE NAME
RESTORE DATABASE @DESTINATIONDB 
FROM DISK = @BACKUPFILE
WITH 
        FILE = 1,  
        MOVE @SOURCEDB TO @SQLDATABACKUPFILE,  
        MOVE @SOURCEDBLOG TO @SQLDATABACKUPLOGFILE,  
        NOUNLOAD,  
        REPLACE,  
        STATS = 10

--UPDATE THE LOGICAL FILE NAMES
DECLARE @TEMPLATE varchar(500)
DECLARE @SCRIPT varchar(500)
SET @TEMPLATE = N'ALTER DATABASE [{DBNAME}] MODIFY FILE (NAME = [{OLD}], NEWNAME = [{NEW}])'
SET @SCRIPT = REPLACE(REPLACE(REPLACE(@TEMPLATE, '{DBNAME}', @DESTINATIONDB),'{OLD}',@SOURCEDB),'{NEW}',@DESTINATIONDB)
EXEC(@SCRIPT)
SET @SCRIPT = REPLACE(REPLACE(REPLACE(@TEMPLATE, '{DBNAME}', @DESTINATIONDB),'{OLD}',@SOURCEDBLOG),'{NEW}',@DESTINATIONDBLOG)
EXEC(@SCRIPT)

You can restore a database directly from another database. 您可以直接从另一个数据库还原数据库。

If you're using SQL Management Studio, select "From database" instead of "From device" in the Restore Database dialog box. 如果使用的是SQL Management Studio,请在“还原数据库”对话框中选择“来自数据库”,而不是“来自设备”。

You generally want to restore a whole database from a backup. 通常,您想从备份中还原整个数据库。 Trying to do it directly from a live running prod database could cause locking issues for your users. 直接从实时运行的产品数据库中尝试这样做可能会导致用户锁定问题。 You can do this using SSIS, but it is neither a simple nor quick thing to set up properly. 您可以使用SSIS进行此操作,但是正确地设置它既不是简单也不是快速的事情。

Another possibility is if you can turn off prod for a time being (only if you have a time period when users are not inthe database). 另一种可能性是,您可以暂时关闭产品(仅当您有一段时间不在数据库中时)。 Then detach the database. 然后分离数据库。 Detach the dev database and delete it. 分离dev数据库并删除它。 Copy the file to the dev server and attach both databases again. 将文件复制到开发服务器,然后再次附加两个数据库。 This can be faster than a restore, but it is a rare environment now that doesn't have 24 hour data access on production. 这可能比还原要快,但是这是一种罕见的环境,现在没有24小时的生产数据访问权限。

Incidentally it is very much preferable to have dev and prod on separate servers. 顺便说一句,最好将开发和生产放在单独的服务器上。

And if you are restoring to dev, you need to make sure any dev changes that have not yet been committed to prod are scripted, so they can be run immediately after the restore. 而且,如果要还原到dev,则需要确保所有尚未提交到prod的dev变更都已编写脚本,以便可以在还原后立即运行它们。 It's best if you script any and all database changes and store them in source control. 最好对所有数据库更改进行脚本编写并将其存储在源代码管理中。 That makes it easier to do this. 这样可以更容易地做到这一点。

我们对生产数据进行按需备份,然后在开发机器上还原备份。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 创建SQL Server数据库的开发版本的最佳方法是什么? - What's the best way to create a dev version of a SQL Server database? 复制数据库的最佳方法(SQL Server 2008) - Best way to copy a database (SQL Server 2008) 创建“丢弃”SQL 服务器数据库的最快方法是什么? - What's the fastest way to create a 'throw away' SQL Server database? 将本地 SQL Server 数据库同步到 Azure Windows Server SQL Server 的最佳方法是什么 - What's the best way to Sync on premise SQL Server database to Azure Windows Server SQL Server 将从Web API请求接收的数据复制和比较到SQL Server数据库的最佳方法是什么? - What is the best way to copy and compare data received from Web API request to SQL Server database? 复制数据库的最佳方法是什么? - What is the best way to copy a database? 在SQL Server 2008 R2中从数据库发送电子邮件的最佳方法是什么? - What's the best way send email from the database in SQL Server 2008 R2? 使用MS Sql Server 2005区分两个数据库备份文件的最佳方法是什么? - What's the best way to diff two database backup files with MS Sql Server 2005? 在视图定义(SQL Server 2008 R2)中更改硬编码数据库引用的最佳方法是什么? - What's the best way to change hard coded database references in view definitions (SQL Server 2008 R2)? 在 asp.net 中显示来自 sql 服务器数据库的图像的最佳方式是什么? - What's the best way to display an image from a sql server database in asp.net?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM