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

[经验分享] [SQL Server][FILESTREAM] -- How to Backup and Restore a SQL Server FILESTREAM En

[复制链接]

尚未签到

发表于 2018-10-13 11:51:43 | 显示全部楼层 |阅读模式
  From: https://www.mssqltips.com/sqlservertip/1854/how-to-backup-and-restore-a-sql-server-filestream-enabled-database/
Problem
  Most SQL Server DBAs have questions about backing up and restoring FILESTREAM enabled databases. In this tip, we will take a look at the steps Database Administrators need to follow in order to perform a backup and restore of a FILESTREAM database.  This tip includes a general explanation of the FILESTREAM technology introduced with SQL Server 2008.  This is followed by examples and scripts to setup the backup and recovery process in your environment.
Solution
  In SQL Server 2008 one can store BLOBs (e.g. Images, Video, Word, Excel, PDF, MP3, etc files) in the NT file system rather than in a database file. This can be achieved by using the new FILESTREAM feature which was introduced in SQL Server 2008.  However, the big question in the mind of many DBA is how FILESTREAM enabled databases can be backed up and restored.   Are there any differences from a typical database?  In this tip, we will go through an example of how to backup and restore a FILESTREAM enabled database.
  Creating a FILESTREAM Enabled Database
  Let us start by creating a FILESTREAM enabled database namely FileStreamDB by executing the TSQL code below.
Use Master  GO
  IF EXISTS (SELECT name FROM sys.databases WHERE name = N'FileStreamDB')
  DROP DATABASE FileStreamDB
  GO
  USE master
  GO
  CREATE DATABASE [FileStreamDB] ON PRIMARY
  ( NAME = N'FileStreamDB', FILENAME = N'D:\FileStreamDB\FileStreamDB.mdf',
  SIZE = 10MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10% )
  LOG ON
  ( NAME = N'FileStreamDB_log', FILENAME = N'D:\FileStreamDB\FileStreamDB_log.ldf' ,
  SIZE = 10MB , MAXSIZE = UNLIMITED , FILEGROWTH = 10%)
  GO
  ALTER DATABASE [FileStreamDB]
  ADD FILEGROUP [FileStreamGroup] CONTAINS FILESTREAM
  GO
  ALTER DATABASE [FileStreamDB]
  ADD FILE (NAME = N'FileStreamDB_FSData', FILENAME = N'D:\FileStreamDB\FileStreamData')
  TO FILEGROUP FileStreamGroup
GO  Creating a table with FILESTREAM columns
  Let us now create the FileStreamDataStorage table by executing the TSQL code below.  This table will be used to store FILESTREAM data:
Use FileStreamDB  GO
  IF EXISTS (SELECT name FROM sys.all_objects WHERE name = N'FileStreamDataStorage')
  DROP TABLE FileStreamDataStorage
  GO
  CREATE TABLE [FileStreamDataStorage]
  (

  [ID] [INT]>  [FileStreamData] VARBINARY(MAX) FILESTREAM NULL,
  [FileStreamDataGUID] UNIQUEIDENTIFIER ROWGUIDCOL NOT NULL UNIQUE DEFAULT NEWSEQUENTIALID(),
  [DateTime] DATETIME DEFAULT GETDATE()
  )
  ON [PRIMARY]
  FILESTREAM_ON FileStreamGroup
  GO
  To store a BLOB using FILESTREAM feature, you must have a column of datatype VARBINARY (MAX) along with the FILESTREAM attribute. In addition to this the table must also have a UNIQUEIDENTIFIER column with the ROWGUIDCOL attribute.
  Inserting FILESTREAM Data
  Let us now add a row to FileStreamDataStorage table by execute the below mentioned TSQL code.
Use FileStreamDB  GO
  INSERT INTO [FileStreamDataStorage] (FileStreamData)
  SELECT * FROM
  OPENROWSET(BULK N'C:\SampleFiles\Image1.JPG' ,SINGLE_BLOB) AS Document
  GO
  -- Execute the below mentioned TSQL code to retrieve the data from
  -- FileStreamDataStorage table.
  USE FileStreamDB
  GO
  SELECT>  , CAST([FileStreamData] AS VARCHAR) as [FileStreamData]
  , FileStreamDataGUID
  , [DateTime]
  FROM [FileStreamDataStorage]
  GO
DSC0000.png

  For more information about inserting, updating or deleting FILESTREAM data, check out - Creating a SQL Server 2008 FILESTREAM Enabled Database and Using INSERT, UPDATE and DELETE statements to manage FILESTREAM Data.
  Backup FILESTREAM Enabled Database
  A DBA can perform a full backup of a FileStreamDB database by executing the T-SQL Code below.  In this tip, all of the backups are using the database backup compression feature which was introduced in SQL Server 2008.
/* Perform a Full Backup of FileStreamDB */  Use master
  GO
  BACKUP DATABASE FileStreamDB
  TO DISK =N'C:\DBBackup\FileStreamDB.BAK'
  WITH COMPRESSION
  GO
  /* Perform a Tail Log Backup of FileStreamDB */
  BACKUP LOG FileStreamDB
  TO DISK =N'C:\DBBackup\FileStreamDB.TRN'
  WITH COMPRESSION, NORECOVERY
  GO
DSC0001.png

  Once the database backups have successfully completed, the next step will be to go ahead and restore the FileStreamDB database.
  Restore FILESTREAM Enabled Database
  Let us now go ahead and restore the Full backup of the FileStreamDB database with the NORECOVERY option.
/*>Use master
  GO
  RESTORE FILELISTONLY
  FROM DISK = N'C:\DBBackup\FileStreamDB.BAK'
  GO
  /* Restore the Full Backup with NORECOVERY option */
  RESTORE DATABASE FileStreamDB
  FROM DISK = N'C:\DBBackup\FileStreamDB.BAK'
  WITH NORECOVERY, FILE =1
  GO
  Next, you need to restore the transaction log backup of the FileStreamDB database with the RECOVERY option to complete the restoration process.
/*Restore Tail Log Backup with RECOVERY option */  RESTORE DATABASE FileStreamDB
  FROM DISK = N'C:\DBBackup\FileStreamDB.TRN'
  WITH RECOVERY, FILE =1
  GO
DSC0002.png

  Once the database restoration has successfully completed you can execute the T-SQL code below to check whether the earlier inserted record is still available.
USE FileStreamDB  GO

  SELECT>  , CAST([FileStreamData] AS VARCHAR) as [FileStreamData]
  , FileStreamDataGUID
  , [DateTime]
  FROM [FileStreamDataStorage]
  GO
DSC0003.png

  You will are able retrieve the same record values from FileStreamDataStorage table as the database was restored successfully.
  Restore FILESTREAM Enabled Database Using MOVE Files Option

  A DBA can execute the T-SQL code below to>SELECT name AS [File Name]  , physical_name AS [Physical Name]
  , type_desc AS [File Type]
  , state_desc AS [Database Status]
  FROM FileStreamDB.sys.database_files
  GO
DSC0004.png

  Note: Prior to executing the T-SQL code below, delete the earlier backup files and perform a new Full and Transaction Log backup of the FileStreamDB database.
  The T-SQL code below will restore the FileStreamDB database using the MOVE option.
Use master  GO
  RESTORE FILELISTONLY
  FROM DISK = N'C:\DBBackup\FileStreamDB.BAK'
  WITH FILE =1
  GO
  /* Restore Full Backup with MOVE & NORECOVERY */
  RESTORE DATABASE FileStreamDB
  FROM DISK = N'C:\DBBackup\FileStreamDB.BAK'
  WITH
  MOVE 'FileStreamDB' to 'E:\FileStreamDB\FileStreamDB.mdf',
  MOVE 'FileStreamDB_log' to 'E:\FileStreamDB\FileStreamDB_log.ldf',
  MOVE 'FileStreamDB_FSData' to 'E:\FileStreamDB\FileStreamData',
  NORECOVERY, FILE =1
  GO
  /* Restore Tail Log Backup with RECOVERY */
  RESTORE DATABASE FileStreamDB
  FROM DISK = N'C:\DBBackup\FileStreamDB.TRN'
  WITH RECOVERY, FILE =1
  GO
DSC0005.png


  Once the FileStreamDB database is restored successfully, a DBA can execute the T-SQL code below to>SELECT name AS [File Name]  , physical_name AS [Physical Name]
  , type_desc AS [File Type]
  , state_desc AS [Database Status]
  FROM FileStreamDB.sys.database_files
  GO
DSC0006.png

  You could see in the above snippet that the all the database files are now available in E:\FileStreamDB location after the successful restoration.


运维网声明 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-621110-1-1.html 上篇帖子: SQL Server -- 基于sqlcmd命令行工具管理SQL Server 下篇帖子: [SQL Server][FILESTREAM] -- Best Practices When Using FILESTREAM Feature of SQL
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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