简体   繁体   English

如何使用备份和还原创建数据库副本

[英]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中进行尝试:

  1. Backup the database you want to copy (Rewards2_bak.bak is fine) 备份要复制的数据库(Rewards2_bak.bak可以)
  2. Create a new database (Rewards2_copy) 创建一个新的数据库(Rewards2_copy)
  3. Restore into Rewards2_copy using Rewards2_bak.bak 使用Rewards2_bak.bak还原到Rewards2_copy

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.

相关问题 如何在同一台服务器上备份和还原数据库作为副本? - How do you backup and restore a database as a copy on the same server? 如何还原msdb数据库的备份 - How to restore backup of msdb database 如何使用 C# 中的 SMO 使用文件 STREAM 备份和恢复数据库 - How To Backup And Restore DataBase With FILE STREAM Using SMO in C# 如何为数据库创建备份 - How to create a backup for database 使用SSMS 2016连接到远程数据库-备份和还原功能不可用 - Connecting to remote database using SSMS 2016 - backup and restore functions not available 如何备份和还原表 - How to backup and restore table 使用脚本来恢复数据库,错误说备份集保存了一个非现有数据库的备份 - Using scripts to restore database, error says backup set holds a backup of a database other than existing 单击netbeans jbutton时如何备份和恢复sql数据库 - How to backup and restore sql database when I clicked netbeans jbutton 如何将本地 PostgreSQL 数据库备份恢复到 Azure PostgreSQL 服务器? - How to restore local PostgreSQL database backup to Azure PostgreSQL server? SQL Server-使用RESTORE复制数据库,结果复制没有数据 - SQL Server - Using RESTORE to copy database, resulting copy has no data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM