-
Notifications
You must be signed in to change notification settings - Fork 40
/
PMDB.Create_Database_Backup.sql
78 lines (63 loc) · 2.92 KB
/
PMDB.Create_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
/*-------------------------------------------------------------------------------+
| Purpose: Create a backup of a database
| Example: EXEC admin.Create_Database_Backup 'PMDB1_TEST'
+--------------------------------------------------------------------------------*/
:setvar _server "Server1"
:setvar _user "***username***"
:setvar _password "***password***"
:setvar _database "master"
:connect $(_server) -U $(_user) -P $(_password)
USE [$(_database)];
GO
CREATE PROCEDURE [admin].[Create_Database_Backup]
(
@DatabaseName VARCHAR(50)
)
AS
BEGIN
PRINT '====================================================================='
PRINT 'set the name of the database...'
PRINT '====================================================================='
DECLARE @SourceDB VARCHAR(50)
SET @SourceDB = @DatabaseName --DB_NAME()
PRINT '====================================================================='
PRINT 'get user name...'
PRINT '====================================================================='
DECLARE @BackupUser VARCHAR(255)
SET @BackupUser = (substring(suser_sname(),charindex('\',suser_sname())+(1),len(suser_sname())-charindex('\',suser_sname())))
PRINT '====================================================================='
PRINT 'get current date and time...'
PRINT '====================================================================='
DECLARE @DateStamp VARCHAR(20)
SET @DateStamp = '_' + CONVERT(VARCHAR(20),GetDate(),112) + '_' + REPLACE(CONVERT(VARCHAR(20),GetDate(),108),':','')
PRINT '====================================================================='
PRINT 'set database backup path...'
PRINT '====================================================================='
DECLARE @TargetPath VARCHAR(255)
-- TO DO: Standardize the backup folder location for all servers
IF @@SERVERNAME = 'Server1' SET @TargetPath = 'C:\Temp\'
PRINT '====================================================================='
PRINT 'set the backup file name...'
PRINT '====================================================================='
SET @TargetPath = @TargetPath + @SourceDB + @DateStamp + '_' + @BackupUser + '.bak'''
PRINT @TargetPath
PRINT '====================================================================='
PRINT 'backup the database...'
PRINT '====================================================================='
IF EXISTS(SELECT NAME FROM sys.databases where name = @SourceDB)
BEGIN
DECLARE @BACKUP_SQL VARCHAR(MAX)
SET @BACKUP_SQL =
'BACKUP DATABASE ' + @SourceDB + '
TO DISK = ''' + @TargetPath + '
WITH FORMAT,
MEDIANAME = ''' + @BackupUser + ''',
NAME = ''' + @SourceDB + @DateStamp + ''''
PRINT @BACKUP_SQL
EXEC (@BACKUP_SQL)
END
PRINT '====================================================================='
PRINT 'Finished!'
PRINT '====================================================================='
END
GO