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
| DECLARE @v_DefaultDataPath NVARCHAR(250) = CONVERT(NVARCHAR,SERVERPROPERTY('InstanceDefaultDataPath'));
DECLARE @v_DefaultLogPath NVARCHAR(250) = CONVERT(NVARCHAR,SERVERPROPERTY('InstanceDefaultLogPath'));
DECLARE @v_FinalDataFilePath NVARCHAR(250) = CONCAT(@v_DefaultDataPath,'\{DatabaseName}.mdf');
DECLARE @v_FinalLogFilePath NVARCHAR(250) = CONCAT(@v_DefaultLogPath,'\{DatabaseName}_Log.ldf');
DECLARE @v_LogicalDataFileName NVARCHAR(128), @v_LogicalLogFileName NVARCHAR(128);
DECLARE @tbl_FileList TABLE
(
LogicalName NVARCHAR(128) ,
PhysicalName NVARCHAR(260) ,
[Type] CHAR(1) ,
FileGroupName NVARCHAR(128) ,
Size NUMERIC(20, 0) ,
MaxSize NUMERIC(20, 0) ,
FileID BIGINT ,
CreateLSN NUMERIC(25, 0) ,
DropLSN NUMERIC(25, 0) ,
UniqueID UNIQUEIDENTIFIER ,
ReadOnlyLSN NUMERIC(25, 0) ,
ReadWriteLSN NUMERIC(25, 0) ,
BackupSizeInBytes BIGINT ,
SourceBlockSize INT ,
FileGroupID INT ,
LogGroupGUID UNIQUEIDENTIFIER ,
DifferentialBaseLSN NUMERIC(25, 0) ,
DifferentialBaseGUID UNIQUEIDENTIFIER ,
IsReadOnl BIT ,
IsPresent BIT ,
TDEThumbprint VARBINARY(32)
);
INSERT INTO @tbl_FileList
EXEC
( 'RESTORE FILELISTONLY FROM DISK = N''{BackupSharedFolder}\{DatabaseName}.bak'''
);
-- Extract the logical data file name from the backup file
SELECT @v_LogicalDataFileName = LogicalName
FROM @tbl_FileList AS tfl
WHERE [tfl].[Type] = 'D';
-- Extract the logical log file name from the backup file
SELECT @v_LogicalLogFileName = LogicalName
FROM @tbl_FileList AS tfl
WHERE [tfl].[Type] = 'L';
RESTORE DATABASE [EyeTeaERP]
FROM DISK = N'{BackupSharedFolder}\{DatabaseName}.bak'
WITH FILE = 1,
MOVE @v_LogicalDataFileName TO @v_FinalDataFilePath,
MOVE @v_LogicalLogFileName TO @v_FinalLogFilePath,
NORECOVERY,
NOUNLOAD;
RESTORE LOG [{DatabaseName}]
FROM DISK = N'{BackupSharedFolder}\{DatabaseName}.log'
WITH FILE = 1,
NOUNLOAD;
|