博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SQL Server数据库快照
阅读量:2517 次
发布时间:2019-05-11

本文共 8497 字,大约阅读时间需要 28 分钟。

介绍 (Introduction)

MS SQL Server 2005 and later versions include the Database Snapshot feature to have snapshot of the database for reports, as a copy in different periods.

MS SQL Server 2005和更高版本包含“数据库快照”功能,以具有用于报告的数据库快照,作为不同时期的副本。

The Database Snapshot can be created multiple times and it can only be created using the T-SQL.

数据库快照可以创建多次,并且只能使用T-SQL创建。

In this article, we will show how to create a Database Snapshot, how to see the snapshot created in the SQL Server Management Studio (SSMS), how to recover objects dropped or data removed using the snapshot.

在本文中,我们将展示如何创建数据库快照,如何查看在SQL Server Management Studio(SSMS)中创建的快照,如何恢复使用快照删除的对象或删除的数据。

Finally, we will learn how to create Snapshots automatically for reporting purposes.

最后,我们将学习如何为报告目的自动创建快照。

要求 (Requirements)

  • SQL Server Enterprise or Evaluation Edition is required

    需要SQL Server Enterprise或评估版
  • We are using SQL Server 2014, but earlier versions can be used

    我们正在使用SQL Server 2014,但是可以使用早期版本
  • The Adventureworks Database is required

    需要Adventureworks数据库
  • The Adventurewoks database has to be online

    Adventurewoks数据库必须在线

入门 (Getting started)

To create a database snapshot, we need to use the T-SQL. It is the only way to do it. You cannot create a Database Snapshot in the SSMS.

要创建数据库快照,我们需要使用T-SQL。 这是唯一的方法。 您不能在SSMS中创建数据库快照。

The syntax is the following:

语法如下:

 CREATE DATABASE AdventureWorks_snapshot ON ( NAME = AdventureWorks2012_Data,  --Name of the snapshot file FILENAME = 'C:\script2\AdventureWorks_data_1800.ss' ) --It is a Snapshot of the adventureworks2012 database AS SNAPSHOT OF [AdventureWorks2012]; GO 

As you can see, the syntax is similar to a normal database creation except for two things:

如您所见,该语法与普通数据库的创建相似,除了两点:

  1. We use the word AS SNAPSHOT OF DATABASE_NAME to specify the name of the database that requires a snapshot.

    我们使用单词AS SNAPSHOT OF DATABASE_NAME来指定需要快照的数据库的名称。
  2. By default, it is better to specify the extension of the snapshot datafile as .ss (which means SnapShot.

    默认情况下,最好将快照数据文件的扩展名指定为.ss(即SnapShot。

If everything is OK, you will be able to see the snapshot created in the SSMS:

如果一切正常,您将能够看到在SSMS中创建的快照:

Adwentureworks snapshot
Figure 1

图1

The snapshots have read-only tables. If you try to update or delete the data you will not be able to do it. You will receive a read only message:

快照具有只读表。 如果您尝试更新或删除数据,则将无法执行。 您将收到一条只读消息:

Read only message is shown - No row was updated
Figure 2

图2

You can also use the T-SQL to try to update tables in the database snapshot with the same results:

您也可以使用T-SQL尝试以相同的结果更新数据库快照中的表:

Use T-SQL query to update tables in the database snapshot
Figure 3

图3

The database snapshots files have a similar size that a normal database, but a smaller size on disk. This is because each time that the original database changes, the snapshot grows. This reduces a little bit the database performance because of the synchronization.

数据库快照文件的大小与普通数据库相似,但磁盘上的文件较小。 这是因为原始数据库每次更改时,快照都会增长。 由于同步,这会降低一点数据库性能。

Database Snapshot properties
Figure 4

图4

The file size of the Snapshot Database is just 3.31 MB. The Adventureworks2012 database has the same size and size in disk as shown in the picture 5:

快照数据库的文件大小仅为3.31 MB。 Adventureworks2012数据库的磁盘大小和大小相同,如图5所示:

Adventureworks2012 database properties
Figure 5

图5

The size on this of the source database is equal to 205 MB.

源数据库的大小等于205 MB。

使用快照恢复对象 (Using snapshots to recover objects)

If by mistake (or any other circumstance) a user drops a stored procedure, a view or a table or any object, you can recover the database object using the snapshot.

如果用户由于错误(或任何其他情况)删除了存储过程,视图或表或任何对象,则可以使用快照恢复数据库对象。

(Example)

Imagine that an evil person drops the dbo.uspGetBillOfMaterials stored procedure in the Database

想象一个邪恶的人在数据库中删除了dbo.uspGetBillOfMaterials存储过程

 Drop procedure dbo.uspGetBillOfMaterials 

Now, imagine that you are a smart person and you recover the stored procedure using the stored procedure from the Database Snapshot. To do this, generate the CREATE PROCEDURE from the Snapshot Database:

现在,假设您是一个聪明的人,并且使用数据库快照中的存储过程来恢复存储过程。 为此,请从快照数据库生成CREATE PROCEDURE:

Figure 6

图6

In the code generated, just replace the first part like this:

Replace

在生成的代码中,只需像这样替换第一部分:

更换
USE  [AdventureWorks_snapshot]
USE [AdventureWorks2012]

Once replaced press F5 to execute the script:

替换后,按F5键执行脚本:

Executed script
Figure 7

图7

If you follow all the steps, you will have your stored procedure restored. The same concept is applicable to any Database Object. You can easily recreate specific objects from the Snapshot Database to the source Database.

如果执行所有步骤,将还原存储过程。 相同的概念适用于任何数据库对象。 您可以轻松地将特定对象从快照数据库重新创建到源数据库。

从表中还原数据 (Restore the data from a table)

In this new demo, we will truncate all the data from the table Person.Password and restore the information from the Database Snapshot snapshot.

在这个新的演示中,我们将截断表Person.Password中的所有数据,并从数据库快照快照中还原信息。

First, truncate all the data from a table:

首先,截断表中的所有数据:

truncate table [Person].[Password]

Secondly, restore the information from the Snapshot Database table to the source Database:

其次,将信息从“快照数据库”表还原到源数据库:

 USE [AdventureWorks2012]GOINSERT INTO [Person].PasswordSelect	*From [AdventureWorks_snapshot].[Person].Password 

We just inserted the information from the snapshot table to the empty Person.Table table from the Adventureworks2012 database.

我们只是将快照表中的信息插入到Adventureworks2012数据库中的空Person.Table表中。

从快照还原整个数据库 (Restore the entire database from the snapshot)

If all the objects and data were dropped, it would possible to recover all the information from the snapshot to the original database.

如果删除了所有对象和数据,则可以将所有信息从快照恢复到原始数据库。

In this new example, we will delete all the views from the AdventureWorks database and recover all the information from the snapshot.

在这个新示例中,我们将从AdventureWorks数据库中删除所有视图,并从快照中恢复所有信息。

  1. First we will have all the views of the AdventureWorks database:

    首先,我们将拥有AdventureWorks数据库的所有视图:

    Views of the AdventureWorks database
    Figure 8

    图8

  2. Then we will drop all the views:

    然后,我们将删除所有视图:

     DECLARE @sql VARCHAR(MAX)='';SELECT @sql=@sql+'DROP VIEW ['+name +'];' FROM sys.views;EXEC(@sql); 

    As you can see in the Figure 9, all the views were removed:

    如您在图9中看到的,所有视图都被删除:

    All the views are removed
    Figure 9

    图9

  3. Now, revert your database. You may have problems to restore the database because there are multiple connection available. If that is the case, set the AdventureWorks database in a single user mode to close the other connections.

    现在,还原数据库。 由于存在多个可用连接,因此您可能在还原数据库时遇到问题。 如果是这种情况,请在单用户模式下设置AdventureWorks数据库以关闭其他连接。

     USE master;GOALTER DATABASE AdventureWorks2012SET SINGLE_USERWITH ROLLBACK IMMEDIATE;GO 
  4. Finally, restore the AdventureWorks Database from the snapshot:

    最后,从快照还原AdventureWorks数据库:

     RESTORE DATABASE AdventureWorks2012 from DATABASE_SNAPSHOT = 'AdventureWorks_snapshot'; 

If everything is OK, you will be able to see your dropped views again:

如果一切正常,您将能够再次看到您的拖放视图:

Views should be back
Figure 10

图10

每月创建数据库快照 (Create Database Snapshots every month)

Finally, we will show how to create Database snapshots every month.

最后,我们将展示如何每月创建数据库快照。

Here you have the T-SQL code to create a Database with the current month number:

在这里,您具有使用当前月份号创建数据库的T-SQL代码:

 DECLARE @SQL VARCHAR(MAX)=''; --GET THE MONTH NUMBER IN THE @MONTH VARIABLE DECLARE @MONTH VARCHAR(2)= MONTH(GETDATE()) --CONCATENATE THE SNAPSHOT DATABASE AND THE MONTH NUMBER SELECT @SQL ='CREATE DATABASE ADVENTUREWORKS_SNAPSHOT_'+@MONTH+ ' ON (NAME=AdventureWorks2012_Data, FILENAME=''C:\scripts\adventure.ss'') AS SNAPSHOT OF Adventureworks2012' EXECUTE (@SQL) 

The code creates a Database Snapshot with the current number of the month.

该代码使用当前的月份数创建一个数据库快照。

Database Snapshot created
Figure 11

图11

To create snapshots of the database, it is necessary to schedule a Job each month:

要创建数据库的快照,有必要每月计划一次作业:

Creating New Job
Figure 12

图12

Specify a Name and optionally a description of the job:

指定名称和可选的作业描述:

Specifying Job name and description
Figure 13

图13

In the steps page create a new page. Specify a Name and select the T-SQL Type (which is the first option by default):

在步骤页面中创建一个新页面。 指定名称并选择T-SQL类型(默认情况下是第一个选项):

Figure 14

图14

Finally, schedule the job every month:

最后,每月安排工作:

Scheduling New Job every month
Figure 15

图15

As you can see, it is very simple to automate tasks and work with Snapshot Databases.

如您所见,自动化任务和使用快照数据库非常简单。

结论 (Conclusion)

As you can see, Snapshots is a simple way to create copies of your information to partially restore the information. The Snapshots cannot replace the traditional backups, because it depends on the source database. If the source database is corrupted, the Snapshot will not be able to restore the database because there is a dependency between them. However, you can restore data and objects from the snapshot. This is very useful if the database is big and we want to restore some few objects or some specific rows.

如您所见,快照是创建信息副本以部分还原信息的简单方法。 快照不能替换传统备份,因为它取决于源数据库。 如果源数据库已损坏,则快照将无法还原数据库,因为它们之间存在依赖性。 但是,您可以从快照还原数据和对象。 如果数据库很大并且我们要还原一些对象或某些特定的行,这将非常有用。

翻译自:

转载地址:http://hynwd.baihongyu.com/

你可能感兴趣的文章
将java保存成.xml文件
查看>>
C语言学习第二章
查看>>
SQl server更新某阶段的匹配关系。
查看>>
go语言练习
查看>>
org.apache.jasper.JasperException: Unable to compile class for JSP
查看>>
UVa 11796 Dog Distance
查看>>
动态库与主程序共享全局变量 -- (转)
查看>>
leetcode 13. 罗马数字转整数(Roman to Integer)
查看>>
iOS 委托模式
查看>>
Ctrl-A全选这点事(C#,WinForm)
查看>>
发布TrajStat 1.4.4
查看>>
MongoDB学习记录
查看>>
linux 安装JDK
查看>>
12/4个人站立会议
查看>>
ubuntu更改主机名
查看>>
WordCount优化
查看>>
回调函数
查看>>
[转]protobuf-2.5.0.tar.gz的下载与安装
查看>>
关于IT概念的一些思考
查看>>
java开发必备的工具
查看>>