-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestore-script-for-sqlcmd.sql
144 lines (110 loc) · 4.2 KB
/
restore-script-for-sqlcmd.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
-- restore backups utility script.
-- note this is a standalone.
-- To organize things you could import or alter this script to run a "main" procedure or use from a program; Used from a program the program must parse
-- batch delimeter "GO"
-- schema for program to operate in
-- backup files
CREATE TABLE #BackupFile (
FolderPath nvarchar(512) not null,
FileName nvarchar(512) not null,
PathFileName as (FolderPath+'/'+Filename),
PathFileNameHash as (cast(hashbytes('sha2_256',FolderPath+'/'+Filename) as varbinary(32))) persisted not null primary key,
Selection varchar(30) not null default ('Append'),
constraint check_selection check (Selection in ('Append','Remain','Delete')));
-- Specifications to find backup files
CREATE TABLE #BackupFileSpec (
FolderPath nvarchar(512) not null Primary Key,
FileSpec nvarchar(128) not null default('%'));
GO
-- proc to add backup file specs
create procedure #NewBackupFileSpec
@FolderPath nvarchar(512), @FileSpec nvarchar(128) = null as Begin
Set Nocount on
insert #BackupFileSpec (FolderPath,FileSpec) values(@FolderPath,@FileSpec);
End
GO
-- table to manage xp_dirtree results
--Create a temp table to hold the results.
IF OBJECT_ID('tempdb..#DirectoryTree') IS NOT NULL
DROP TABLE #DirectoryTree;
CREATE TABLE #DirectoryTree (
id int IDENTITY(1,1)
,fullpath varchar(2000)
,subdirectory nvarchar(512)
,depth int
,isfile bit);
--Create a clustered primary to keep everything in order.
ALTER TABLE #DirectoryTree
ADD CONSTRAINT PK_DirectoryTree PRIMARY KEY CLUSTERED (id);
-- create unique c to find things and enforce actual data constraints
==========================================================================================
DECLARE
@BasePath varchar(1000)
,@Path varchar(1000)
,@FullPath varchar(2000)
,@Id int;
--This is your starting point.
SET @BasePath = 'D:\Backup';
--Create a temp table to hold the results.
IF OBJECT_ID('tempdb..#DirectoryTree') IS NOT NULL
DROP TABLE #DirectoryTree;
CREATE TABLE #DirectoryTree (
id int IDENTITY(1,1)
,fullpath varchar(2000)
,subdirectory nvarchar(512)
,depth int
,isfile bit);
--Create a clustered index to keep everything in order.
ALTER TABLE #DirectoryTree
ADD CONSTRAINT PK_DirectoryTree PRIMARY KEY CLUSTERED (id);
--Populate the table using the initial base path.
INSERT #DirectoryTree (subdirectory,depth,isfile)
EXEC master.sys.xp_dirtree @BasePath,1,1;
UPDATE #DirectoryTree SET fullpath = @BasePath;
--Loop through the table as long as there are still folders to process.
WHILE EXISTS (SELECT id FROM #DirectoryTree WHERE isfile = 0)
BEGIN
--Select the first row that is a folder.
SELECT TOP (1)
@Id = id
,@FullPath = fullpath
,@Path = @BasePath + '\' + subdirectory
FROM #DirectoryTree WHERE isfile = 0;
IF @FullPath = @Path
BEGIN
--Do this section if the we are still in the same folder.
INSERT #DirectoryTree (subdirectory,depth,isfile)
EXEC master.sys.xp_dirtree @Path,1,1;
UPDATE #DirectoryTree
SET fullpath = @Path
WHERE fullpath IS NULL;
--Delete the processed folder.
DELETE FROM #DirectoryTree WHERE id = @Id;
END
ELSE
BEGIN
--Do this section if we need to jump down into another subfolder.
SET @BasePath = @FullPath;
--Select the first row that is a folder.
SELECT TOP (1)
@Id = id
,@FullPath = fullpath
,@Path = @BasePath + '\' + subdirectory
FROM #DirectoryTree WHERE isfile = 0;
INSERT #DirectoryTree (subdirectory,depth,isfile)
EXEC master.sys.xp_dirtree @Path,1,1;
UPDATE #DirectoryTree
SET fullpath = @Path
WHERE fullpath IS NULL;
--Delete the processed folder.
DELETE FROM #DirectoryTree WHERE id = @Id;
END
END
--Output the results.
SELECT fullpath + '\' + subdirectory AS 'CompleteFileList'
FROM #DirectoryTree
ORDER BY fullpath,subdirectory;
--Cleanup.
IF OBJECT_ID('tempdb..#DirectoryTree') IS NOT NULL
DROP TABLE #DirectoryTree;
GO