-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSQL015-CreateSubDirectoriesForBackup.sql
More file actions
55 lines (45 loc) · 1.69 KB
/
SQL015-CreateSubDirectoriesForBackup.sql
File metadata and controls
55 lines (45 loc) · 1.69 KB
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
/*
-----------------------------------------------@kisinamso-----------------------------------------------
|This server trigger create directory for backups |
|If you add new databases, this trigger will create new subdirectories automatically. |
-----------------------------------------------@kisinamso-----------------------------------------------
*/
USE [master]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [BackupPathCreate]
ON ALL SERVER
FOR CREATE_DATABASE
AS
DECLARE
@DatabaseName NVARCHAR(128)
,@SQL NVARCHAR(4000);
SELECT @DatabaseName = EVENTDATA().value('(/EVENT_INSTANCE/DatabaseName)[1]','NVARCHAR(128)')
--Creating sub directories for databases on a server
--Yearly --> Preview: \\BackupPath\Yearly\ServerName\DatabaseName
SET @SQL = '
declare @Path varchar(500)
set @Path = ''\\BackupPath\Yearly\' + CAST(@@SERVERNAME AS VARCHAR) + '\' + @DatabaseName + '''
EXEC master.dbo.xp_create_subdir @Path
';
EXEC (@SQL);
--Monthly --> Preview: \\BackupPath\Monthly\ServerName\DatabaseName
SET @SQL = '
declare @Path varchar(500)
set @Path = ''\\BackupPath\Monthly\' + CAST(@@SERVERNAME AS VARCHAR) + '\' + @DatabaseName + '''
EXEC master.dbo.xp_create_subdir @Path
';
EXEC (@SQL);
--Daily --> Preview: \\BackupPath\Daily\ServerName\DatabaseName
SET @SQL = '
declare @Path varchar(500)
set @Path = ''\\BackupPath\Daily\' + CAST(@@SERVERNAME AS VARCHAR) + '\' + @DatabaseName + '''
EXEC master.dbo.xp_create_subdir @Path
';
EXEC (@SQL);
GO
ENABLE TRIGGER [BackupPathCreate] ON ALL SERVER
GO