设为首页 收藏本站
查看: 436|回复: 0

[经验分享] SQL Server 2005 – Automating Creation of Database Snapshots

[复制链接]

尚未签到

发表于 2016-11-4 01:48:15 | 显示全部楼层 |阅读模式
UPDATE:上一文另一版本

  SQL Server 2005 is packed with many  new features. One of the new features that I would like to discuss in this  article is Database Snapshots, which are read only static views of a database.  SQL Server 2005 allows you to create multiple snapshots on a database. In this  article, I would like to demonstrate the creation of database snapshots and  automating the creation of database snapshots.

  First let's simulate the whole snapshot creation  process.
  Step 1

  Copy and paste the query below into the Microsoft SQL Server  Management Studio's Query window and execute it. This statement creates the  database demo.  

Create Database demo on Primary  

(Name ='demo_Data',  

FileName=  'E:\MSSQL2005\MSSQL.1\MSSQL\Data\demo_Data.Mdf',  

Size=100MB,  

MaxSize=200MB,  

FILEGROWTH=10%)  

Log on  

(Name = 'demo_Log',  

FileName=  'E:\MSSQL2005\MSSQL.1\MSSQL\Data\demo_Log.Ldf',  

Size=30MB,  

MaxSize=50MB,  


FILEGROWTH=10%);


   DSC0000.jpg


  Figure 1



  


  
  
  
  Setp  2
  
  Let us create a table and insert some rows.  

  Copy and paste the query below into the Microsoft SQL Server  Management Studio's Query window and execute it. This statement creates the  table info  and inserts 5 rows to the table.  

Use demo  

go  

Create table info (Id int, name  varchar(100),cname varchar(100))  

go  

insert into info values (1,'longrujun','沧海笑一声')  

insert into info values (2,'zengyu','无梨头')  

insert into info values (3,'zhy','美女杀手')  

insert into info values (4,'xiongfei','六煞')  

insert into info values (5,'hanlei','酒鬼')  


go


DSC0001.jpg


Figure 2





Step 3

Now let's create  a snapshot for the database demo.  


Copy and paste  the query below into the Microsoft SQL Server Management Studio's Query window  and execute it. This statement creates the snapshot demo_Snapshot1 for the  database demo.  


use master  


go  


Create Database demo_Snapshot1 on  


(Name ='demo_Data',  


FileName= 'E:\MSSQL2005\MSSQL.1\MSSQL\Data\demo_Data.SS1')  


AS SNAPSHOT of demo;  


Go  



This would create  a file E:\MSSQL2005\MSSQL.1\MSSQL\Data\demo_Data.SS1 as  shown in the following Figure


DSC0002.jpg

  Figure 3,4
DSC0003.jpg














Step  4


Now let's delete  some rows from the original database.  


Copy and paste  the query below into the Microsoft SQL Server Management Studio's Query window  and execute it. This statement deletes two rows from the demo  database.  


Use demo  


Go  


Select * from  info  


   DSC0004.jpg

  Figure 5

use demo  

go  

delete from info where id in (2,4)  

go
  

Step 5
  Now let's query the table info from the original database demo and from the SnapShot view demo_Snapshot1.  


  Copy and paste the query below into the Microsoft SQL Server Management Studio's Query window and execute it. This statement displays all of the rows from the table info from the database demo. [Refer Fig ]  


use demo  

go  

Select * from info  

go

   DSC0005.jpg

  Figure 6

Copy and paste the query below into the Microsoft SQL Server Management Studio's Query window and execute it. This statement displays all of the rows from the table info, from the snapshot demo_Snapshot1 (the snapshot view of the database info.)

use demo_Snapshot1  

go  

Select * from info  

go


DSC0006.jpg
  Figure 7

  

    Now, let's automate creation of a daily database snapshot and let's keep one week worth of snapshots.  


  Create a SQL Server Scheduled job [Refer Fig 8,9,10,11] to execute the following statement every night at 3:00 am

  

  

   DSC0007.jpg

  

  Figure 8
     DSC0008.jpg


    Figure 9


    


    the code is here


    


      declare @query varchar(1000)  


  declare @DatabaseName varchar(128)  


  declare @snapshotName varchar(128)  


  declare @snapDataName varchar(128)  


  declare @snapFileName varchar(128)  


  declare @snapFilePath varchar(128)  


  declare @nalja varchar(12)  


  set @nalja=convert(varchar(4), datepart(yyyy, getdate())) + convert  


  (varchar(4), datepart(mm, getdate())) + convert(varchar(4), datepart  


  (dd, getdate()))  


    


  print 'It is ' + @nalja  


  Set @DatabaseName ='demo'  


  Set @SnapDataName='demo_Data'  


  Set @SnapshotName ='demo_Snapshot'+'_'+@nalja  


  Set @SnapFilename ='"E:\MSSQL2005\MSSQL.1\MSSQL\Data\demo_Data'+'_'+@nalja+'.ss"'  


    


  Print 'Snapshot name is ' +@SnapshotName  


  select * from sys.databases where source_database_id =db_id(@databasename) and name = @SnapshotName   


  if @@rowcount <>0  


  begin  


  set @query = 'Drop database '+ @SnapshotName  


  print @query  


  exec(@query)  


  end  


  set @query ='Create database ' + @SnapshotName + ' on (Name = ' +@snapDataName +', FileName=' +@SnapFilename +') AS SNAPSHOT of ' + @databasename+';'  


  print @query  


  exec(@query)  




     DSC0009.jpg


      Figure 10



      



        The naming convention of the database snapshot is demo_Snapshot_Day  


  The following SQL statement will list all of the database snapshots available on a SQL Server instance. [Refer Fig 11]  


  select * from sys.databases where source_database_id is not null  





      



       DSC00010.jpg



        Figure 11




        




Note: By changing the naming convention and the schedule of this process,
you can convert this daily database snapshot process to a weekly or monthly
database snapshot process.  





Conclusion


  



This article demonstrated the creation of a database snapshot and the automating of the creation of database snapshots.
By creating a database snapshot everyday, it would be more convenient to go back to a particular day to get data, or compare
or to monitor the status of the data. The database can be reverted to a database snapshot using the RESTORE command.  

  
  
  



      






  



  


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-295261-1-1.html 上篇帖子: SQL Server数据库锁机制及类型 下篇帖子: 微软今夏将发布SQL Server 2016预览版
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表