Advertisement
ecuageek

Backup All DBs in MSSQL

Jan 2nd, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 0.83 KB | None | 0 0
  1. DECLARE @name VARCHAR(50) -- database name  
  2. DECLARE @path VARCHAR(256) -- path for backup files  
  3. DECLARE @fileName VARCHAR(256) -- filename for backup  
  4. DECLARE @fileDate VARCHAR(20) -- used for file name
  5.  
  6. -- specify database backup directory
  7. SET @path = 'C:\Backup\'  
  8.  
  9. -- specify filename format
  10. SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112)
  11.  
  12. DECLARE db_cursor CURSOR READ_ONLY FOR  
  13. SELECT name
  14. FROM master.dbo.sysdatabases
  15. WHERE name NOT IN ('master','model','msdb','tempdb')  -- exclude these databases
  16.  
  17. OPEN db_cursor  
  18. FETCH NEXT FROM db_cursor INTO @name  
  19.  
  20. WHILE @@FETCH_STATUS = 0  
  21. BEGIN  
  22.   SET @fileName = @path + @name + '_' + @fileDate + '.BAK'  
  23.   BACKUP DATABASE @name TO DISK = @fileName  
  24.  
  25.   FETCH NEXT FROM db_cursor INTO @name  
  26. END  
  27.  
  28.  
  29. CLOSE db_cursor  
  30. DEALLOCATE db_cursor
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement