forked from Arvindpoongothai/SQLDBAInventory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackup-Generate Backup Script Dynamically
86 lines (69 loc) · 3.25 KB
/
Backup-Generate Backup Script Dynamically
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
--Dynamic Script to Generate Backup Script
--https://www.sqlservercentral.com/scripts/dynamic-script-to-generate-backup-script
Use this script to generate BACKUP DATABASE scripts for selective or all databases during Instance or Database migration.
Query result generated from above dynamic script will produce same result as Backup taken from backup job.
Execute the script on source Instance. At the top, under multi line comment, one can see text like "Total Inputs: ",
/* Created By: AJAY DWIVEDI
Created Date: NOV 25, 2014
Purpose: Script out Take Backups
Total Inputs: 3
*/
This hints that user input is required at 3 points within scripts. One can find input lines by looking for pattern like
--1) specify database backup directory
--2) Specify (True=1) or (False=0) for COPY_ONLY backup option
--3) Specify your DB names for backup in case of data migration
===============================================================================================
/* Created By: AJAY DWIVEDI
Created Date: NOV 25, 2014
Purpose: Script out Take Backups
Total Inputs: 3
*/
DECLARE @ID TINYINT --DB No
DECLARE @name VARCHAR(50) -- database name
DECLARE @Is_Copy_only TINYINT
DECLARE @path VARCHAR(256) -- path for backup files
DECLARE @fileName VARCHAR(256) -- filename for backup
DECLARE @fileDate VARCHAR(20) -- used for file name
DECLARE @BackupString NVARCHAR(2000);
DECLARE @VerificationString NVARCHAR(2000);
--1) specify database backup directory
SET @path = 'F:\Backups'
--2) Specify (True=1) or (False=0) for COPY_ONLY backup option
SET @Is_Copy_only = 1;
SELECT @fileDate = DATENAME(DAY,GETDATE())+CAST(DATENAME(MONTH,GETDATE()) AS VARCHAR(3))
+DATENAME(YEAR,GETDATE())+'_'+REPLACE(REPLACE(RIGHT(CONVERT(VARCHAR, GETDATE(), 100),7),':',''), ' ','0')
--3) Specify your DB names for backup in case of data migration
DECLARE db_cursor CURSOR FOR
SELECT ROW_NUMBER() OVER (ORDER BY name) as ID, name
FROM master.dbo.sysdatabases
WHERE DATABASEPROPERTYEX(NAME,'status') = 'ONLINE'
--AND name IN ('Pubs') -- Data Migration
AND name NOT IN ('master','model','msdb','tempdb') -- Instance Migration
ORDER BY name
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @ID, @name;
WHILE @@FETCH_STATUS = 0
BEGIN
SET @BackupString = '
-- '+CAST(@ID AS VARCHAR(2))+') ['+@name+']
EXEC master.sys.xp_create_subdir '''+@path+'\'+@name+''';
GO
BACKUP DATABASE ['+@name+'] TO DISK = '''+@path+'\'+@name+'\'+ @name + '_' + @fileDate + '.BAK''
WITH ';
IF(@Is_Copy_only = 1)
SET @BackupString = @BackupString + 'COPY_ONLY, ';
SET @BackupString = @BackupString + 'STATS = 5 ,CHECKSUM;
GO';
SET @VerificationString = '
declare @backupSetId as int
select @backupSetId = position from msdb..backupset where database_name=N'''+@name+''' and backup_set_id=(select max(backup_set_id) from msdb..backupset where database_name=N'''+@name+''' )
if @backupSetId is null begin raiserror(N''Verify failed. Backup information for database '''''+@name+''''' not found.'', 16, 1) end
RESTORE VERIFYONLY FROM DISK = N'''+@path+'\'+@name+'\'+ @name + '_' + @fileDate + '.BAK'' WITH FILE = @backupSetId, NOUNLOAD, NOREWIND
GO
';
PRINT @BackupString;
PRINT @VerificationString;
FETCH NEXT FROM db_cursor INTO @ID, @name;
END
CLOSE db_cursor
DEALLOCATE db_cursor