[英]How to create copy of database using backup and restore
Using SQL Server Management Studio 2012, I'm trying to create a copy of a local database.使用 SQL Server Management Studio 2012,我正在尝试创建本地数据库的副本。 I found a few variants of solution.
我发现了一些解决方案的变体。 One of them - backup and restore database as new one - HERE .
其中之一 - 将数据库备份和恢复为新的 - HERE 。
Currently create database backup with name Rewards2_bak
.当前创建名称为
Rewards2_bak
的数据库备份。 This copy of file place in to system catalog C:\\Program Files\\Microsoft SQL Server\\MSSQL11.MSSQLSERVER\\MSSQL\\Backup\\
此文件副本放入系统目录
C:\\Program Files\\Microsoft SQL Server\\MSSQL11.MSSQLSERVER\\MSSQL\\Backup\\
Next step - create query for restoring database as copy of existing one下一步 - 创建查询以将数据库还原为现有数据库的副本
GO
use master
RESTORE FILELISTONLY
FROM Rewards2_bak
RESTORE DATABASE Rewards2_Copy
FROM Rewards2_bak
WITH RECOVERY,
MOVE 'Rewards2_data' TO 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\Rewards2_copy.mdf',
MOVE 'Rewards2_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\Rewards2_log_copy.ldf'
GO
Got error, that I don't have a backup device Rewads2_backup
.出现错误,我没有备份设备
Rewads2_backup
。 I'm right understand that in this case like device i can use file, and also file location?我是对的,在这种情况下,我可以使用文件,也可以使用文件位置? Think something missing...
想想少了点什么……
For creating backup use next query (all OK)用于创建备份使用下一个查询(一切正常)
USE Rewards2;
GO
BACKUP DATABASE Rewards2
TO DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Backup\Rewards2_bak.bak'
WITH FORMAT,
MEDIANAME = 'SQLServerBackups',
NAME = 'Full Backup of Rewards2';
GO
Also try to use tools in SQL Server 2012 Task --> Backup
and Task --> Restore
, but got error - can't create backup.还尝试使用 SQL Server 2012
Task --> Backup
and Task --> Restore
中的工具,但出现错误 - 无法创建备份。 (Launched program with Administrator rights) (以管理员权限启动的程序)
This is screen how I config restore to copy这是我如何配置还原以复制的屏幕
But I have error, that I can't overwrite database file Rewards2
.但我有错误,我无法覆盖数据库文件
Rewards2
。 And this is question - why it wants to overwrite Rewards2
if I put new name of database Rewards2_copy
.这是一个问题 - 如果我输入数据库
Rewards2_copy
Rewards2
Or I understand something wrong?还是我理解错了?
Shure, that ther is a few more possible variants for making copy of database, but really whant to find problem for this solution.舒尔,还有更多可能的变体来制作数据库的副本,但真的想为这个解决方案找到问题。 Where i make mistake/ what I forget or don't understand.
我在哪里犯错/我忘记了什么或不明白什么。
When you restore a database from a backup it will use the same file names as the original database. 从备份还原数据库时,它将使用与原始数据库相同的文件名。 You need to change these file names during the restore.
在还原期间,您需要更改这些文件名。
On the restore window go to the Files
tab. 在还原窗口上,转到“
Files
选项卡。 On this window you have a column called Restore As
. 在此窗口中,有一列称为
Restore As
。 Change the file names at the end of the path in the column Restore As for each of the files you see. 在您看到的每个文件的“还原为”列中,更改路径末尾的文件名。
I think you are trying to overwrite the logical filenames from the database you are trying to copy...guessing a bit but try this - try it in SSMS if you don't need to script it right now: 我认为您正在尝试覆盖您要复制的数据库中的逻辑文件名...有点猜测,但请尝试执行此操作-如果现在不需要编写脚本,请在SSMS中进行尝试:
Remember in point three to change the logical filenames within the options tab - otherwise you will try and overwrite the Rewards2 files. 请记住,在第三点中更改选项卡中的逻辑文件名-否则,您将尝试覆盖Rewards2文件。
There are lots of guides around: 周围有很多指南:
Via SQL to determine the logical filenames you can use: filelistonly 通过SQL确定可以使用的逻辑文件名:filelistonly
restore filelistonly from disk='enter backup file path here'
Then use the filenames reported to build you restore query, as you have already tried 然后,请使用已报告的文件名构建您的还原查询,
Here is VBS script that will backup and restore DB to another server. 这是VBS脚本,它将备份数据库并将其还原到另一台服务器。 It assumes that each server has "Backup" file share.
假定每个服务器都有“备份”文件共享。 The script can be run from any location.
该脚本可以从任何位置运行。
sFrom = "\\server1\Backup"
sTo = "\\server2\Backup"
Set fso = CreateObject("Scripting.FileSystemObject")
CopyDb "Db1"
CopyDb "Db2"
MsgBox "Done!"
Sub CopyDb(sDB)
If fso.FileExists(sFrom & "\" & sDB & ".bak") Then
fso.DeleteFile sFrom & "\" & sDB & ".bak"
End If
ExecuteSql "server1", "BACKUP DATABASE " & sDB & " TO DISK = '" & sFrom & "\" & sDB & ".bak'"
fso.CopyFile sFrom & "\" & sDB & ".bak", sTo & "\" & sDB & ".bak"
ExecuteSql "server2", "ALTER DATABASE " & sDB & " SET SINGLE_USER WITH ROLLBACK IMMEDIATE"
ExecuteSql "server2", "RESTORE DATABASE " & sDB & " FROM DISK = N'" & sTo & "\" & sDB & ".bak'"
ExecuteSql "server2", "EXEC " & sDB & ".dbo.sp_change_users_login 'Update_One', 'user1', 'user1'"
End Sub
Sub ExecuteSql(sServer, sSql)
Set cn = CreateObject("ADODB.Connection")
cn.Open "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Data Source=" & sServer
cn.Execute sSql
cn.Close
End Sub
In addition to answer from @JodyT:除了@JodyT 的回答:
In my case you not only needed to change the logical file names, but it is also important to remove the option "Take tail-log backup before restore".在我的情况下,您不仅需要更改逻辑文件名,而且删除选项“在还原前进行尾日志备份”也很重要。 If this option is checked, then you cannot restore to Rewards2_copy if Rewards2 is in use!
如果选中此选项,那么如果 Rewards2 正在使用中,您将无法恢复到 Rewards2_copy!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.