-- Name: sp_MScopysnapshot
-- Descriptions:
-- Parameters: as defined in create statement
-- Returns: 0 - success
-- 1 - Otherwise
-- Security:
-- Requires Certificate signature for catalog access
create procedure sys.sp_MScopysnapshot (
@source_folder nvarchar(255),
@destination_folder nvarchar(255)
)
AS
BEGIN
SET NOCOUNT ON
DECLARE @directory_exists bit
DECLARE @ftporuncdir nvarchar(5)
DECLARE @pubdir nvarchar(255)
DECLARE @timestampdir nvarchar(255)
DECLARE @bslashindex int
DECLARE @bslashindex2 int
DECLARE @bslashcounter int
DECLARE @command nvarchar(1000)
DECLARE @retcode int
DECLARE @platform_nt bit
IF platform() & 0x1 = 0x1
SELECT @platform_nt = 1
ELSE
SELECT @platform_nt = 0
-- If @source_folder is NULL then either the snapshot has not been
-- generated or it has been cleaned up
IF @source_folder IS NULL OR @source_folder = N''
BEGIN
RAISERROR(21289, 16, -1)
RETURN (1)
END
-- Make sure that the @destination folder is not null
IF @destination_folder IS NULL OR
@destination_folder = N''
BEGIN
RAISERROR(21287, 16, -1)
RETURN (1)
END
-- Append backslash to @destination_folder if it is not
-- there already
IF SUBSTRING(@destination_folder, LEN(@destination_folder), 1) <> N'\'
BEGIN
SELECT @destination_folder = @destination_folder + N'\'
END
-- Check if the destination folder exists
EXEC sys.sp_MSget_file_existence @destination_folder, @directory_exists OUTPUT
IF @directory_exists = 0
BEGIN
RAISERROR(21287, 16, -1)
RETURN (1)
END
-- Parse out the last three components in the source folder
-- Note that the source_folder must have a trailing backslash
SELECT @bslashindex = 1
SELECT @bslashindex2 = 1
SELECT @bslashcounter = 0
WHILE (@bslashindex <> 0)
BEGIN
SELECT @bslashindex = CHARINDEX(N'\', @source_folder, @bslashindex + 1)
SELECT @bslashcounter = @bslashcounter + 1
IF @bslashcounter > 4
BEGIN
SELECT @bslashindex2 = CHARINDEX(N'\', @source_folder, @bslashindex2 + 1)
END
END
SELECT @bslashindex = CHARINDEX(N'\', @source_folder, @bslashindex2 + 1)
SELECT @ftporuncdir = SUBSTRING(@source_folder, @bslashindex2 + 1, @bslashindex - @bslashindex2 - 1)
SELECT @bslashindex2 = @bslashindex
SELECT @bslashindex = CHARINDEX(N'\', @source_folder, @bslashindex2 + 1)
SELECT @pubdir = SUBSTRING(@source_folder, @bslashindex2 + 1, @bslashindex - @bslashindex2 - 1)
SELECT @bslashindex2 = @bslashindex
SELECT @bslashindex = CHARINDEX(N'\', @source_folder, @bslashindex2 + 1)
SELECT @timestampdir = SUBSTRING(@source_folder, @bslashindex2 + 1, @bslashindex - @bslashindex2 - 1)
SELECT @bslashindex2 = @bslashindex
-- Create the subdirectory structure underneath the specified snapshot
-- folder. Ignore errors for now, we will check whether the directory
-- is successfully created later on.
-- Don't suppress output from xp_cmdshell so user knows what's going on
-- in case something goes wrong
SELECT @destination_folder = @destination_folder + @ftporuncdir + '\'
SELECT @command = 'mkdir "' + sys.fn_escapecmdshellsymbolsremovequotes(@destination_folder) collate database_default + '"'
IF (@platform_nt = 1)
SELECT @command = '" ' + @command + ' "'
EXEC master.dbo.xp_cmdshell @command
SELECT @destination_folder = @destination_folder + @pubdir + '\'
SELECT @command = 'mkdir "' + sys.fn_escapecmdshellsymbolsremovequotes(@destination_folder) collate database_default + '"'
IF (@platform_nt = 1)
SELECT @command = '" ' + @command + ' "'
EXEC master.dbo.xp_cmdshell @command
SELECT @destination_folder = @destination_folder + @timestampdir + '\'
SELECT @command = 'mkdir "' + sys.fn_escapecmdshellsymbolsremovequotes(@destination_folder) collate database_default + '"'
IF (@platform_nt = 1)
SELECT @command = '" ' + @command + ' "'
EXEC master.dbo.xp_cmdshell @command
-- Check if the real destination folder exists
EXEC sys.sp_MSget_file_existence @destination_folder, @directory_exists OUTPUT
IF @directory_exists = 0
BEGIN
RAISERROR(21288, 16, -1)
RETURN (1)
END
-- Do the actual copying
SELECT @command = 'copy "' + sys.fn_escapecmdshellsymbolsremovequotes(@source_folder) collate database_default + '*.*" "' + sys.fn_escapecmdshellsymbolsremovequotes(@destination_folder) collate database_default + '"'
IF (@platform_nt = 1)
SELECT @command = '" ' + @command + ' "'
EXEC @retcode = master.dbo.xp_cmdshell @command
IF @retcode <> 0
RETURN (1)
ELSE
RETURN (0)
END