Skip to content

Instantly share code, notes, and snippets.

@nikolasd
Last active October 23, 2018 12:19
Show Gist options
  • Save nikolasd/c296d1944ac8cb80037f320e46278ebd to your computer and use it in GitHub Desktop.
Save nikolasd/c296d1944ac8cb80037f320e46278ebd to your computer and use it in GitHub Desktop.
Backup multiple DBs at once. Exclude specific DBs
-- Check DBs
/*
DECLARE @pylonVer varchar(20) -- current pylon ver
-- specify current pylon version
SET @pylonVer = '170432'
SELECT
name
FROM master.dbo.sysdatabases
WHERE (name NOT LIKE '%' + @pylonVer + '%')
AND (name NOT IN ('master', 'model', 'msdb', 'tempdb', 'ePO_INFOSRV2', 'DUCATO', 'infomind', 'InfoUon', 'IVREPS', 'gym', 'invgalleryart')) -- exclude these databases
*/
-- Actual Backup
DECLARE @name varchar(50) -- database name
DECLARE @path varchar(256) -- path for backup files
DECLARE @fileName varchar(256) -- filename for backup
DECLARE @fileDate varchar(20) -- used for file name
DECLARE @pylonVer varchar(20) -- current pylon ver
-- specify filename format
SELECT
@fileDate = CONVERT(varchar(20), GETDATE(), 105)
-- specify database backup directory
SET @path = 'C:\infomind\clients\db_backups\'
-- specify current pylon version
SET @pylonVer = '170432'
DECLARE db_cursor CURSOR READ_ONLY FOR
SELECT
name
FROM master.dbo.sysdatabases
WHERE (name NOT LIKE '%' + @pylonVer + '%')
AND (name NOT IN ('master', 'model', 'msdb', 'tempdb', 'ePO_INFOSRV2', 'DUCATO', 'infomind', 'InfoUon', 'IVREPS', 'gym', 'invgalleryart')) -- exclude these databases
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
FETCH NEXT FROM db_cursor INTO @name
END
CLOSE db_cursor
DEALLOCATE db_cursor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment