-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathSSDB.Restore_Database_Backup.sql
109 lines (92 loc) · 3.96 KB
/
SSDB.Restore_Database_Backup.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
/*----------------------------------------------------------------------------------------------------------+
| Purpose: Restore a backup of a database
| Example: EXEC admin.Restore_Database_Backup 'db1_20130619_073701_firstname.lastname.bak', 'db1_Backup'
+-----------------------------------------------------------------------------------------------------------*/
:setvar _server "Server1"
:setvar _user "***username***"
:setvar _password "***password***"
:setvar _database "master"
:connect $(_server) -U $(_user) -P $(_password)
USE [$(_database)];
GO
CREATE PROCEDURE [admin].[Restore_Database_Backup]
(
@FileName VARCHAR(255)
, @DatabaseName VARCHAR(50)
)
AS
BEGIN
PRINT '====================================================================='
PRINT 'set the name and path for the restore...'
PRINT '====================================================================='
DECLARE @BackupFile VARCHAR(200)
DECLARE @DataFile VARCHAR(200)
-- TO DO: Standardize the backup folder location for all servers
IF @@SERVERNAME = 'Server1' SET @BackupFile = 'C:\Temp\' + @FileName
PRINT '====================================================================='
PRINT 'set the data path for sql server...'
PRINT '====================================================================='
SELECT @DataFile = SUBSTRING(physical_name, 1, CHARINDEX(N'master.mdf', LOWER(physical_name)) - 1)
FROM master.sys.master_files
WHERE database_id = 1 AND file_id = 1
PRINT '====================================================================='
PRINT 'get the logical names from the backup file...'
PRINT '====================================================================='
DECLARE @Table TABLE (
[LogicalName] varchar(128)
, [PhysicalName] varchar(128)
, [Type] varchar
, [FileGroupName] varchar(128)
, [Size] varchar(128)
, [MaxSize] varchar(128)
, [FileId]varchar(128)
, [CreateLSN]varchar(128)
, [DropLSN]varchar(128)
, [UniqueId]varchar(128)
, [ReadOnlyLSN]varchar(128)
, [ReadWriteLSN]varchar(128)
, [BackupSizeInBytes]varchar(128)
, [SourceBlockSize]varchar(128)
, [FileGroupId]varchar(128)
, [LogGroupGUID]varchar(128)
, [DifferentialBaseLSN]varchar(128)
, [DifferentialBaseGUID]varchar(128)
, [IsReadOnly]varchar(128)
, [IsPresent]varchar(128)
, [TDEThumbprint]varchar(128)
)
DECLARE @LogicalNameData varchar(128)
DECLARE @LogicalNameLog varchar(128)
INSERT INTO @table EXEC('RESTORE FILELISTONLY FROM DISK=''' + @BackupFile + '''')
SET @LogicalNameData=(SELECT LogicalName FROM @Table WHERE Type='D')
SET @LogicalNameLog=(SELECT LogicalName FROM @Table WHERE Type='L')
PRINT '====================================================================='
PRINT 'Restore the Database starting with a file from a Full Backup...'
PRINT '====================================================================='
BEGIN
DECLARE @RESTORE_SQL VARCHAR(MAX)
SET @RESTORE_SQL =
'RESTORE DATABASE ' + @DatabaseName + '
FROM DISK = ''' + @BackupFile + '''
WITH
RECOVERY
, STATS = 10
, MOVE ''' + @LogicalNameData + ''' TO ''' + @DataFile + @DatabaseName + '.mdf''
, MOVE ''' + @LogicalNameLog + ''' TO ''' + @DataFile + @DatabaseName + '.ldf'''
PRINT @RESTORE_SQL
EXEC (@RESTORE_SQL)
END
PRINT '====================================================================='
PRINT 'Update owner of the database to standard...'
PRINT '====================================================================='
BEGIN
DECLARE @OWNER_SQL VARCHAR(MAX)
SET @OWNER_SQL = 'ALTER AUTHORIZATION ON DATABASE::' + @DatabaseName + ' TO sa;'
PRINT @OWNER_SQL
EXEC (@OWNER_SQL)
END
PRINT '====================================================================='
PRINT 'Finished!'
PRINT '====================================================================='
END
GO