Created
November 9, 2017 19:17
-
-
Save jeffjohnson9046/6394a37c4e945815860344dd9ff252ca to your computer and use it in GitHub Desktop.
MS SQL Server: Create full database backups of all "user" databases. This script overwrites the previous backup, so there's only ever one backup of the db. Fine for development, not so much for production.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DECLARE | |
@backupMsg varchar(max), | |
@backupPath varchar(max), | |
@backupCommand varchar(max), | |
@dbName varchar(max); | |
SELECT | |
@backupMsg = 'Full backup', | |
@backupPath = 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Backup\'; | |
DECLARE cur_dbBackups CURSOR FOR | |
SELECT | |
name | |
FROM | |
sys.databases | |
WHERE | |
name NOT IN ('master', 'model', 'msdb', 'tempdb'); | |
OPEN cur_dbBackups; | |
FETCH NEXT FROM cur_dbBackups INTO @dbName | |
WHILE @@FETCH_STATUS = 0 | |
BEGIN | |
PRINT 'Performing full backup of ' + @dbName + ' database...'; | |
SET @backupCommand = 'BACKUP DATABASE ' + @dbName + ' TO DISK = ''' + @backupPath + @dbName + '_full.bak'' WITH STATS, INIT, CHECKSUM'; | |
EXEC(@backupCommand); | |
PRINT ' Backup of ' + @dbName + ' complete.'; | |
PRINT ''; | |
FETCH NEXT FROM cur_dbBackups INTO @dbName | |
END | |
CLOSE cur_dbBackups; | |
DEALLOCATE cur_dbBackups; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment