Home
%3CLINGO-SUB%20id%3D%22lingo-sub-370522%22%20slang%3D%22en-US%22%3EHow%20to%3A%20Restoring%20SQL%20File%20stream%20Data%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-370522%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSTRONG%3E%20First%20published%20on%20MSDN%20on%20Jul%2023%2C%202013%20%3C%2FSTRONG%3E%3C%2FP%3E%0A%20%0A%3CP%3EIt%20is%20not%20uncommon%20to%20have%20multiple%20copies%20of%20the%20same%20database%20on%20a%20single%20instance%20or%20to%20restore%20copy%20of%20a%20database%20from%20one%20instance%20to%20another%20for%20testing.%26nbsp%3B%20I%20was%20asked%20-%20%E2%80%9CHow%20does%20this%20work%20when%20I%20have%20filestream%20data%20in%20my%20database%3F%E2%80%9D%26nbsp%3B%20The%20thought%20process%20is%20pretty%20much%20the%20same%2C%20but%20you%20do%20need%20to%20take%20into%20consideration%20where%20the%20filestream%20data%20will%20go.%20The%20filestream%20files%20are%20backed%20up%20and%20restored%20along%20with%20the%20SQL%20backup.%26nbsp%3B%20Just%20think%20of%20the%20main%20folder%20for%20the%20filestream%20data%20as%20another%20database%20file.%26nbsp%3BI%20will%20walk%20through%20some%20testing%20that%20I%20did%20in%20order%20to%20give%20you%20a%20better%20understanding%20of%20this%20process.%3C%2FP%3E%0A%20%0A%3CP%3ETo%20validate%20that%20Filestream%20is%20enabled%20and%20set%20for%20remote%20access%2C%20we%20can%20leverage%20the%20following%20(remote%20access%20is%20not%20required%20for%20this%20demo%20but%20required%20for%20additional%20testing%20I%20am%20doing).%3C%2FP%3E%0A%20%0A%3CP%3E%3CA%20href%3D%22http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fcc645923(v%3Dsql.105).aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20How%20to%20enable%20Filestream%20%3C%2FA%3E%3C%2FP%3E%0A%0A%3CPRE%3E%0ASELECT%20SERVERPROPERTY('FileStreamShareName')%20AS%20ShareName%0A%20%2CSERVERPROPERTY('FIleStreamConfiguredLevel')%20AS%20ConfigLevel%0A%20%2CSERVERPROPERTY('FileStreamEffectiveLevel')%20AS%20EffectiveLevel%0A%3C%2FPRE%3E%0A%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20657px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F89495i7D9C54E22EFFBB57%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%20%0A%3CP%3EYou%20can%20also%20see%20the%20share%20for%20the%20instance%20with%20the%20net%20share%20command%20from%20the%20command%20prompt.%20MSSQLSERVER%20is%20the%20default%20value%20for%20a%20default%20instance%20of%20SQL%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20696px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F89496iD5C8F542D564B879%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%20%0A%3CP%3EI%20then%20created%20a%20database%2C%20a%20table%20with%20filestream%20data%2C%20and%20entered%20a%20few%20rows.%3C%2FP%3E%0A%0A%3CPRE%3E%0A--create%20a%20database%20%20%20%20%0A--Ensure%20you%20update%20the%20path%20to%20your%20folders%20for%20data%20and%20Filestream%20folders%0A--Note%3A%20The%20filestream%20folder%20C%3A%5CFSData%5CFSTest%20must%20exist%20prior%20to%20running%20below.%0ACREATE%20DATABASE%20%5BFSTest%5D%20ON%20PRIMARY%20(%0A%20NAME%20%3D%20N'FSTest'%0A%20%2CFILENAME%20%3D%20N'C%3A%5CProgram%20Files%5CMicrosoft%20SQL%20Server%5CMSSQL10_50.MSSQLSERVER%5CMSSQL%5CDATA%5CFSTest.mdf'%0A%20%2CSIZE%20%3D%203072%20KB%0A%20%2CMAXSIZE%20%3D%20UNLIMITED%0A%20%2CFILEGROWTH%20%3D%20512000%20KB%0A%20)%0A%20%2CFILEGROUP%20%5BFSFiles%5D%20CONTAINS%20FILESTREAM%20DEFAULT(NAME%20%3D%20N'FSFiles'%2C%20FILENAME%20%3D%20N'C%3A%5CFSData%5CFSTest%5CFSFiles')%20LOG%20ON%20(%0A%20NAME%20%3D%20N'FSTest_log'%0A%20%2CFILENAME%20%3D%20N'C%3A%5CProgram%20Files%5CMicrosoft%20SQL%20Server%5CMSSQL10_50.MSSQLSERVER%5CMSSQL%5CDATA%5CFSTest_log.ldf'%0A%20%2CSIZE%20%3D%201024%20KB%0A%20%2CMAXSIZE%20%3D%202048%20GB%0A%20%2CFILEGROWTH%20%3D%20512000%20KB%0A%20)%0AGO%0A%0AUSE%20FSTest%0AGO%0A%0ACREATE%20TABLE%20%5BFileStreamData%5D%20(%0A%20%5BID%5D%20%5BINT%5D%20IDENTITY(1%2C%201)%20NOT%20NULL%0A%20%2C%5BFileStreamData%5D%20VARBINARY(MAX)%20FILESTREAM%20NULL%0A%20%2C%5BFileStreamDataGUID%5D%20UNIQUEIDENTIFIER%20ROWGUIDCOL%20NOT%20NULL%20UNIQUE%20DEFAULT%20NEWSEQUENTIALID()%0A%20%2C%5BDateTime%5D%20DATETIME%20DEFAULT%20GETDATE()%0A%20)%20ON%20%5BPRIMARY%5D%20FILESTREAM_ON%20FSFiles%0AGO%0A%0A--no%20file%20created%20on%20filesystem%0AINSERT%20INTO%20%5BFileStreamData%5D%20(%0A%20FileStreamData%0A%20%2CFileStreamDataGUID%0A%20%2CDATETIME%0A%20)%0AVALUES%20(%0A%20NULL%0A%20%2CNEWID()%0A%20%2CGETDATE()%0A%20)%3B%0A%0A--insert%20a%200%20length%20record%20-%20obtains%20a%20file%20handle%0AINSERT%20INTO%20%5BFileStreamData%5D%20(%0A%20FileStreamData%0A%20%2CFileStreamDataGUID%0A%20%2CDATETIME%0A%20)%0AVALUES%20(%0A%20CAST(''%20AS%20VARBINARY(max))%0A%20%2CNEWID()%0A%20%2CGETDATE()%0A%20)%3B%0A%0A--inserts%20a%20record%20and%20creates%20a%20file%20with%20some%20data%20in%20it%0AINSERT%20INTO%20%5BFileStreamData%5D%20(%0A%20FileStreamData%0A%20%2CFileStreamDataGUID%0A%20%2CDATETIME%0A%20)%0AVALUES%20(%0A%20CAST('This%20is%20test%20data'%20AS%20VARBINARY(max))%0A%20%2CNEWID()%0A%20%2CGETDATE()%0A%20)%3B%0A%0A--%20Execute%20the%20below%20mentioned%20TSQL%20code%20to%20retrieve%20the%20data%20from%0A--%20FileStreamDataStorage%20table.%0ASELECT%20ID%0A%20%2CCAST(%5BFileStreamData%5D%20AS%20VARCHAR)%20AS%20%5BFileStreamData%5D%0A%20%2CFileStreamDataGUID%0A%20%2C%5BDateTime%5D%0A%20%2CFileStreamData.PathName(0)%20AS%20FilePath%0AFROM%20%5BFileStreamData%5D%0AGO%0A%3C%2FPRE%3E%0A%0A%0A%0A%3CP%3EYou%20can%20see%20the%20results%20here%3A%3C%2FP%3E%0A%20%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20893px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F89497iEC4427AE19D57822%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%20%0A%3CP%3EI%20then%20backed%20up%20the%20database%20using%20the%20GUI%20and%20%3CA%20href%3D%22http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fms186858(v%3DSQL.100).aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20restored%20%3C%2FA%3E%20the%20database%20to%20a%20new%20database%20on%20the%20same%20instance%20with%20a%20new%20name%20(FSTest2).%20Note%20that%20I%20put%20the%20mdf%20and%20ldf%20files%20in%20the%20same%20location%20as%20the%20other%20database%2C%20but%20I%20created%20a%20separate%20folder%20for%20the%20filestream%20data%3A%3C%2FP%3E%0A%20%0A%3CPRE%3E%0ARESTORE%20DATABASE%20%5BFSTest2%5D%0A%0A--Backup%20the%20DB%20with%20GUI%20-%20then%20restore%20-%20note%20filestream%20new%20path%0A--Ensure%20you%20update%20the%20path%20to%20your%20folders%20for%20backup%2C%20data%20and%20Filestream%20folders%0A--Note%3A%20The%20filestream%20folder%20C%3A%5CFSData%5CFSTest2%20must%20exist%20prior%20to%20running%20below.%0ARESTORE%20DATABASE%20%5BFSTest2%5D%0AFROM%20DISK%20%3D%20N'C%3A%5CProgram%20Files%5CMicrosoft%20SQL%20Server%5CMSSQL10_50.MSSQLSERVER%5CMSSQL%5CBackup%5CFSTest.bak'%0AWITH%20FILE%20%3D%201%0A%20%2CMOVE%20N'FSTest'%20TO%20N'C%3A%5CProgram%20Files%5CMicrosoft%20SQL%20Server%5CMSSQL10_50.MSSQLSERVER%5CMSSQL%5CDATA%5CFSTest2.mdf'%0A%20%2CMOVE%20N'FSTest_log'%20TO%20N'C%3A%5CProgram%20Files%5CMicrosoft%20SQL%20Server%5CMSSQL10_50.MSSQLSERVER%5CMSSQL%5CDATA%5CFSTest2_1.ldf'%0A%20%2CMOVE%20N'FSFiles'%20TO%20%3CMARK%3EN'C%3A%5CFSData%5CFSTest2%5CFSFiles'%3C%2FMARK%3E%0A%20%2CNOUNLOAD%0A%20%2CREPLACE%0A%20%2CSTATS%20%3D%2010%0AGO%0A%3C%2FPRE%3E%0A%0A%20%0A%3CP%3EThe%20data%20is%20identical%20from%20the%20below%20script%3A%3C%2FP%3E%0A%20%0A%3CPRE%3E%0ASELECT%20ID%0A%20%2CCAST(%5BFileStreamData%5D%20AS%20VARCHAR)%20AS%20%5BFileStreamData%5D%0A%20%2CFileStreamDataGUID%0A%20%2C%5BDateTime%5D%0A%20%2CFileStreamData.PathName(0)%20AS%20FilePath%0AFROM%20FSTest..%5BFileStreamData%5D%0AGO%0A%0ASELECT%20ID%0A%20%2CCAST(%5BFileStreamData%5D%20AS%20VARCHAR)%20AS%20%5BFileStreamData%5D%0A%20%2CFileStreamDataGUID%0A%20%2C%5BDateTime%5D%0A%20%2CFileStreamData.PathName(0)%20AS%20FilePath%0AFROM%20FSTest2..%5BFileStreamData%5D%0AGO%0A%3C%2FPRE%3E%0A%20%0A%20%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20890px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F89498iAFD9031039657A95%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%20%0A%20%0A%3CP%3ELooking%20at%20the%20file%20system%2C%20you%20can%20see%20that%20the%20data%20is%20identical%20there%20as%20well.%3C%2FP%3E%0A%20%0A%20%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20724px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F89499i85B2B1BC2BA84F91%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%20%0A%20%0A%3CP%3EYou%20dig%20deeper%20into%20the%20folder%20structure%20and%20you%20can%20see%20the%202%20identical%20files%20are%20there.%20NOTE%3A%20There%20are%202%20files%20but%203%20rows%20of%20data%20in%20the%20table%E2%80%A6.why%3F%26nbsp%3BIf%20you%20recall%2C%20the%20first%20record%20inserted%20had%20a%20NULL%20value%20for%20the%20filestream%20data%20therefore%20there%20is%20no%20file.%20This%20is%20often%20done%20when%20you%20want%20a%20placeholder%20for%20a%20file%20you%20will%20import%20later.%3C%2FP%3E%0A%20%0A%20%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20724px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F89500i6D2247C962C511A3%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%20%0A%20%0A%20%0A%20%0A%20%0A%3CP%3ENow%20let%E2%80%99s%20add%20another%20instance%20in%20to%20the%20mix.%26nbsp%3B%20For%20time%20savings%2C%20I%20used%20a%20SQL%20named%20instance%20that%20I%20already%20had%20on%20this%20VM.%26nbsp%3B%20The%20instance%20name%20is%20SSL.%20I%20configured%20it%20for%20Filestream%20which%20you%20can%20see%20below%3A%3C%2FP%3E%0A%20%0A%20%0A%20%0A%20%0A%20%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20654px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F89501i3D16EDD7CD818C3C%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%20%0A%3CP%3ENow%20running%20net%20share%20you%20can%20see%20that%20I%20have%202%20shares%20for%20the%202%20instances%20of%20SQL%20each%20with%20their%20own%20filestream%20share.%3C%2FP%3E%0A%20%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20724px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F89502i94B844E279BDC9B0%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%20%0A%3CP%3EI%20restored%20the%20same%20database%20backup%20but%20restored%20it%20as%20FSTest3.%26nbsp%3B%20I%20also%20named%20the%20directory%20FSTest3.%26nbsp%3B%20You%20can%20now%20see%20all%203%20have%20the%20same%20structure%20(and%20all%203%20have%20the%20same%20files)%3C%2FP%3E%0A%20%0A%20%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20724px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F89503i5E45BA4AD3E34A7B%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%20%0A%3CP%3EYou%20can%20also%20see%20that%20the%20data%20in%20the%20db%20in%20the%20second%20instance%20is%20the%20same%20as%20the%20data%20in%20the%20other%20databases%3A%3C%2FP%3E%0A%0A%3CPRE%3E%0ASELECT%20ID%0A%20%2CCAST(%5BFileStreamData%5D%20AS%20VARCHAR)%20AS%20%5BFileStreamData%5D%0A%20%2CFileStreamDataGUID%0A%20%2C%5BDateTime%5D%0A%20%2CFileStreamData.PathName(0)%20AS%20FilePath%0AFROM%20%5BFileStreamData%5D%0AGO%0A%3C%2FPRE%3E%0A%0A%3CP%3EYou%20can%20see%20the%20results%20here%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20906px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F89504iC5081586DF34A31D%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EA%20few%20key%20takeaways%20from%20this%20post%3A%3C%2FP%3E%0A%20%0A%20%0A%3CP%3E1.%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Filestream%20files%20are%20in%20fact%20backed%20up%20and%20restored%20along%20with%20your%20database.%26nbsp%3BTherefore%20you%20database%20backup%20file%20may%20seem%20quite%20large%20compared%20to%20the%20mdf%20files.%3C%2FP%3E%0A%20%0A%20%0A%3CP%3E2.%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20The%20filestream%20share%20name%20is%20at%20the%20instance%20level%20while%20the%20local%20file%20system%20path%20to%20the%20directory%20is%20set%20at%20the%20database%20level.%26nbsp%3B%20All%20filestream%20databases%20on%20an%20instance%20will%20use%20the%20same%20share%20name%20yet%20each%20database%20will%20have%20its%20own%20folder%20structure.%3C%2FP%3E%0A%20%0A%20%0A%3CP%3E3.%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Create%20a%20standard%20for%20your%20filestream%20data%20folder%20structures.%26nbsp%3B%20Consider%20if%20you%20will%20have%20multiple%20instances%20and%2For%20databases%20on%20the%20same%20server%20as%20well%20as%20multiple%20servers%20with%20filestream%20data.%26nbsp%3B%20You%20may%20want%20to%20derive%20a%20standard%20such%20as%20instancename%5Cdatabasename%20to%20make%20it%20easy%20to%20know%20where%20the%20filestream%20data%20resides%20and%20identify%20to%20which%20database%20it%20belongs.%3C%2FP%3E%0A%20%0A%3CP%3ELisa%20Gardner%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-370522%22%20slang%3D%22en-US%22%3E%3CP%3EFirst%20published%20on%20MSDN%20on%20Jul%2023%2C%202013%20It%20is%20not%20uncommon%20to%20have%20multiple%20copies%20of%20the%20same%20database%20on%20a%20single%20instance%20or%20to%20restore%20copy%20of%20a%20database%20from%20one%20instance%20to%20another%20for%20testing.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-370522%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Edatabase%20engine%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EHow%20to%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Occasional Visitor

First published on MSDN on Jul 23, 2013

It is not uncommon to have multiple copies of the same database on a single instance or to restore copy of a database from one instance to another for testing.  I was asked - “How does this work when I have filestream data in my database?”  The thought process is pretty much the same, but you do need to take into consideration where the filestream data will go. The filestream files are backed up and restored along with the SQL backup.  Just think of the main folder for the filestream data as another database file. I will walk through some testing that I did in order to give you a better understanding of this process.

To validate that Filestream is enabled and set for remote access, we can leverage the following (remote access is not required for this demo but required for additional testing I am doing).

How to enable Filestream

SELECT SERVERPROPERTY('FileStreamShareName') AS ShareName
	,SERVERPROPERTY('FIleStreamConfiguredLevel') AS ConfigLevel
	,SERVERPROPERTY('FileStreamEffectiveLevel') AS EffectiveLevel

You can also see the share for the instance with the net share command from the command prompt. MSSQLSERVER is the default value for a default instance of SQL

I then created a database, a table with filestream data, and entered a few rows.

--create a database    
--Ensure you update the path to your folders for data and Filestream folders
--Note: The filestream folder C:\FSData\FSTest must exist prior to running below.
CREATE DATABASE [FSTest] ON PRIMARY (
	NAME = N'FSTest'
	,FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\FSTest.mdf'
	,SIZE = 3072 KB
	,MAXSIZE = UNLIMITED
	,FILEGROWTH = 512000 KB
	)
	,FILEGROUP [FSFiles] CONTAINS FILESTREAM DEFAULT(NAME = N'FSFiles', FILENAME = N'C:\FSData\FSTest\FSFiles') LOG ON (
	NAME = N'FSTest_log'
	,FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\FSTest_log.ldf'
	,SIZE = 1024 KB
	,MAXSIZE = 2048 GB
	,FILEGROWTH = 512000 KB
	)
GO

USE FSTest
GO

CREATE TABLE [FileStreamData] (
	[ID] [INT] IDENTITY(1, 1) NOT NULL
	,[FileStreamData] VARBINARY(MAX) FILESTREAM NULL
	,[FileStreamDataGUID] UNIQUEIDENTIFIER ROWGUIDCOL NOT NULL UNIQUE DEFAULT NEWSEQUENTIALID()
	,[DateTime] DATETIME DEFAULT GETDATE()
	) ON [PRIMARY] FILESTREAM_ON FSFiles
GO

--no file created on filesystem
INSERT INTO [FileStreamData] (
	FileStreamData
	,FileStreamDataGUID
	,DATETIME
	)
VALUES (
	NULL
	,NEWID()
	,GETDATE()
	);

--insert a 0 length record - obtains a file handle
INSERT INTO [FileStreamData] (
	FileStreamData
	,FileStreamDataGUID
	,DATETIME
	)
VALUES (
	CAST('' AS VARBINARY(max))
	,NEWID()
	,GETDATE()
	);

--inserts a record and creates a file with some data in it
INSERT INTO [FileStreamData] (
	FileStreamData
	,FileStreamDataGUID
	,DATETIME
	)
VALUES (
	CAST('This is test data' AS VARBINARY(max))
	,NEWID()
	,GETDATE()
	);

-- Execute the below mentioned TSQL code to retrieve the data from
-- FileStreamDataStorage table.
SELECT ID
	,CAST([FileStreamData] AS VARCHAR) AS [FileStreamData]
	,FileStreamDataGUID
	,[DateTime]
	,FileStreamData.PathName(0) AS FilePath
FROM [FileStreamData]
GO

You can see the results here:

I then backed up the database using the GUI and restored the database to a new database on the same instance with a new name (FSTest2). Note that I put the mdf and ldf files in the same location as the other database, but I created a separate folder for the filestream data:

RESTORE DATABASE [FSTest2]

--Backup the DB with GUI - then restore - note filestream new path
--Ensure you update the path to your folders for backup, data and Filestream folders
--Note: The filestream folder C:\FSData\FSTest2 must exist prior to running below.
RESTORE DATABASE [FSTest2]
FROM DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Backup\FSTest.bak'
WITH FILE = 1
	,MOVE N'FSTest' TO N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\FSTest2.mdf'
	,MOVE N'FSTest_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\FSTest2_1.ldf'
	,MOVE N'FSFiles' TO N'C:\FSData\FSTest2\FSFiles'
	,NOUNLOAD
	,REPLACE
	,STATS = 10
GO

The data is identical from the below script:

SELECT ID
	,CAST([FileStreamData] AS VARCHAR) AS [FileStreamData]
	,FileStreamDataGUID
	,[DateTime]
	,FileStreamData.PathName(0) AS FilePath
FROM FSTest..[FileStreamData]
GO

SELECT ID
	,CAST([FileStreamData] AS VARCHAR) AS [FileStreamData]
	,FileStreamDataGUID
	,[DateTime]
	,FileStreamData.PathName(0) AS FilePath
FROM FSTest2..[FileStreamData]
GO

Looking at the file system, you can see that the data is identical there as well.

You dig deeper into the folder structure and you can see the 2 identical files are there. NOTE: There are 2 files but 3 rows of data in the table….why? If you recall, the first record inserted had a NULL value for the filestream data therefore there is no file. This is often done when you want a placeholder for a file you will import later.

Now let’s add another instance in to the mix.  For time savings, I used a SQL named instance that I already had on this VM.  The instance name is SSL. I configured it for Filestream which you can see below:

Now running net share you can see that I have 2 shares for the 2 instances of SQL each with their own filestream share.

I restored the same database backup but restored it as FSTest3.  I also named the directory FSTest3.  You can now see all 3 have the same structure (and all 3 have the same files)

You can also see that the data in the db in the second instance is the same as the data in the other databases:

SELECT ID
	,CAST([FileStreamData] AS VARCHAR) AS [FileStreamData]
	,FileStreamDataGUID
	,[DateTime]
	,FileStreamData.PathName(0) AS FilePath
FROM [FileStreamData]
GO

You can see the results here:

A few key takeaways from this post:

1.      Filestream files are in fact backed up and restored along with your database. Therefore you database backup file may seem quite large compared to the mdf files.

2.      The filestream share name is at the instance level while the local file system path to the directory is set at the database level.  All filestream databases on an instance will use the same share name yet each database will have its own folder structure.

3.      Create a standard for your filestream data folder structures.  Consider if you will have multiple instances and/or databases on the same server as well as multiple servers with filestream data.  You may want to derive a standard such as instancename\databasename to make it easy to know where the filestream data resides and identify to which database it belongs.

Lisa Gardner