{"id":1738,"date":"2016-04-20T21:40:43","date_gmt":"2016-04-21T02:40:43","guid":{"rendered":"http:\/\/swildow.darktech.org\/wp\/?p=1738"},"modified":"2016-04-20T21:40:43","modified_gmt":"2016-04-21T02:40:43","slug":"simple-script-to-backup-all-sql-server-databases","status":"publish","type":"post","link":"http:\/\/www.wildow.com\/blog\/?p=1738","title":{"rendered":"Simple script to backup all SQL Server databases"},"content":{"rendered":"<p>Simple script to backup all SQL Server databases<br \/>\nBy: Greg Robidoux | Read Comments (168) | Related Tips: 1 | 2 | 3 | 4 | More &gt; Backup<br \/>\nProblem<br \/>\nSometimes things that seem complicated are much easier then you think and this is the power of using T-SQL to take care of repetitive tasks. One of these tasks may be the need to backup all databases on your server. This is not a big deal if you have a handful of databases, but I have seen several servers where there are 100+ databases on the same instance of SQL Server. You could use SQL Server Management Studio to backup the databases or even use Maintenance Plans, but using T-SQL is a much simpler and faster approach.<\/p>\n<p>Solution<br \/>\nWith the use of T-SQL you can generate your backup commands and with the use of cursors you can cursor through all of your databases to back them up one by one. This is a very straight forward process and you only need a handful of commands to do this.<\/p>\n<p>Here is the script that will allow you to backup each database within your instance of SQL Server. You will need to change the @path to the appropriate backup directory.<\/p>\n<p>File Naming Format DBname_YYYYDDMM.BAK<br \/>\nDECLARE @name VARCHAR(50) &#8212; database name<br \/>\nDECLARE @path VARCHAR(256) &#8212; path for backup files<br \/>\nDECLARE @fileName VARCHAR(256) &#8212; filename for backup<br \/>\nDECLARE @fileDate VARCHAR(20) &#8212; used for file name<br \/>\n&#8212; specify database backup directory<br \/>\nSET @path = &#8216;C:\\Backup\\&#8217;<br \/>\n&#8212; specify filename format<br \/>\nSELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112)<br \/>\nDECLARE db_cursor CURSOR FOR<br \/>\nSELECT name<br \/>\nFROM master.dbo.sysdatabases<br \/>\nWHERE name NOT IN (&#8216;master&#8217;,&#8217;model&#8217;,&#8217;msdb&#8217;,&#8217;tempdb&#8217;) &#8212; exclude these databases<br \/>\nOPEN db_cursor<br \/>\nFETCH NEXT FROM db_cursor INTO @name<br \/>\nWHILE @@FETCH_STATUS = 0<br \/>\nBEGIN<br \/>\nSET @fileName = @path + @name + &#8216;_&#8217; + @fileDate + &#8216;.BAK&#8217;<br \/>\nBACKUP DATABASE @name TO DISK = @fileName<br \/>\nFETCH NEXT FROM db_cursor INTO @name<br \/>\nEND<br \/>\nCLOSE db_cursor<br \/>\nDEALLOCATE db_cursor<br \/>\nFile Naming Format DBname_YYYYDDMM_HHMMSS.BAK<br \/>\nIf you want to also include the time in the filename you can replace this line in the above script:<\/p>\n<p>&#8212; specify filename format<br \/>\nSELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112)<br \/>\nwith this line:<\/p>\n<p>&#8212; specify filename format<br \/>\nSELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112) + REPLACE(CONVERT(VARCHAR(20),GETDATE(),108),&#8217;:&#8217;,&#8221;)<br \/>\nNotes<br \/>\nIn this script we are bypassing the system databases, but these could easily be included as well. You could also change this into a stored procedure and pass in a database name or if left NULL it backups all databases. Any way you choose to use it, this script gives you the starting point to simply backup all of your databases.<\/p>\n<p>Also, if you wanted to bypass some of your user databases you can include them in the NOT IN section as well.<\/p>\n<p>Next Steps<br \/>\nAdd this script to your toolbox<br \/>\nModify this script and make it a stored procedure to include one or many parameters<br \/>\nEnhance the script to use additional BACKUP options<br \/>\nLast Update: 10\/15\/2012<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Simple script to backup all SQL Server databases By: Greg Robidoux | Read Comments (168) | Related Tips: 1 | 2 | 3 | 4 | More &gt; Backup Problem Sometimes things that seem complicated are much easier then you &#8230; <a class=\"more-link\" href=\"http:\/\/www.wildow.com\/blog\/?p=1738\">Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1738","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/www.wildow.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1738","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.wildow.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.wildow.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.wildow.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.wildow.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1738"}],"version-history":[{"count":1,"href":"http:\/\/www.wildow.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1738\/revisions"}],"predecessor-version":[{"id":1742,"href":"http:\/\/www.wildow.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1738\/revisions\/1742"}],"wp:attachment":[{"href":"http:\/\/www.wildow.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1738"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.wildow.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1738"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.wildow.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}