forked from Arvindpoongothai/SQLDBAInventory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackup--All DB's at once with copy_only
35 lines (27 loc) · 1.12 KB
/
Backup--All DB's at once with copy_only
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
--TSQL Script to take all user DB Backups with Copy_only, Compression.
use master
go
DECLARE @name VARCHAR(100) -- database name
DECLARE @path VARCHAR(256) -- path for backup files
DECLARE @fileName VARCHAR(256) -- filename for backup
DECLARE @fileDate VARCHAR(100) -- used for file name
-- specify database backup directory
SET @path = 'C:\Backup\' -------------------------PUT YOUR REQUIRED BACKUP DIRECTORY OR PATH HERE
-- specify filename format
--SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112)
SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112) + REPLACE(CONVERT(VARCHAR(20),GETDATE(),108),':','')
DECLARE db_cursor CURSOR READ_ONLY FOR
SELECT name
FROM master.dbo.sysdatabases
WHERE name NOT IN ('master','model','msdb','tempdb') -- Exclude databases of your choice here
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN
SET @fileName = @path + @name + '_' + @fileDate + '.BAK'
BACKUP DATABASE @name TO DISK = @fileName
with copy_only,compression, stats=5,
FETCH NEXT FROM db_cursor INTO @name
END
CLOSE db_cursor
DEALLOCATE db_cursor