{"id":1886,"date":"2023-04-03T23:37:51","date_gmt":"2023-04-03T20:37:51","guid":{"rendered":"https:\/\/burakcaliskan.org\/blog\/?p=1886"},"modified":"2023-04-03T23:41:25","modified_gmt":"2023-04-03T20:41:25","slug":"mssql-command-line-backup","status":"publish","type":"post","link":"https:\/\/burakcaliskan.org\/blog\/mssql-command-line-backup\/","title":{"rendered":"MSSQL Command Line Backup"},"content":{"rendered":"\n<pre class=\"wp-block-preformatted\">DECLARE @BackupFile varchar(255), @DB varchar(30), @Description varchar(255), @LogFile varchar(50)\nDECLARE @Name varchar(30), @MediaName varchar(30), @BackupDirectory nvarchar(200) \n\n--Change this variable\nSET @BackupDirectory = 'D:\\MSSQL_YEDEK\\'\n\n--Add a list of all databases you don't want to backup to this.\nDECLARE Database_CURSOR CURSOR FOR SELECT name FROM sysdatabases WHERE name &lt;&gt; 'tempdb' AND name &lt;&gt; 'model' AND name &lt;&gt; 'Northwind'\nOPEN Database_Cursor\nFETCH next FROM Database_CURSOR INTO @DB\nWHILE @@fetch_status = 0\n\n    BEGIN\n    \tSET @Name = @DB + '( Daily BACKUP )'\n    \tSET @MediaName = @DB + '_Dump' + CONVERT(varchar, CURRENT_TIMESTAMP , 112)\n    \tSET @BackupFile = @BackupDirectory + + @DB + '_' + 'Full' + '_' + \n    \t\tCONVERT(varchar, CURRENT_TIMESTAMP , 112) + '.bak'\n    \tSET @Description = 'Normal' + ' BACKUP at ' + CONVERT(varchar, CURRENT_TIMESTAMP) + '.' \n\n    \tIF (SELECT COUNT(*) FROM msdb.dbo.backupset WHERE database_name = @DB) &gt; 0 OR @DB = 'master'\n    \t\tBEGIN\n    \t\t\tSET @BackupFile = @BackupDirectory + @DB + '_' + 'Full' + '_' + \n    \t\t\t\tCONVERT(varchar, CURRENT_TIMESTAMP , 112) + '.bak'\n    \t\t\t--SET some more pretty stuff for sql server.\n    \t\t\tSET @Description = 'Full' + ' BACKUP at ' + CONVERT(varchar, CURRENT_TIMESTAMP) + '.' \n    \t\tEND\t\n    \tELSE\n    \t\tBEGIN\n    \t\t\tSET @BackupFile = @BackupDirectory + @DB + '_' + 'Full' + '_' + \n    \t\t\t\tCONVERT(varchar, CURRENT_TIMESTAMP , 112) + '.bak'\n    \t\t\t--SET some more pretty stuff for sql server.\n    \t\t\tSET @Description = 'Full' + ' BACKUP at ' + CONVERT(varchar, CURRENT_TIMESTAMP) + '.' \n    \t\tEND\n    \t\tBACKUP DATABASE @DB TO DISK = @BackupFile \n    \t\tWITH NAME = @Name, DESCRIPTION = @Description , \n    \t\tMEDIANAME = @MediaName, MEDIADESCRIPTION = @Description , \n    \t\tSTATS = 10\n    \tFETCH next FROM Database_CURSOR INTO @DB\nEND\nCLOSE Database_Cursor\nDEALLOCATE Database_Cursor<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>sqlcmd -S \"SERVER-ADI\" -U sa -P sifre123 -i \"D:\\MSSQL_YEDEK\\sorgu.sql\"\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>DECLARE @BackupFile varchar(255), @DB varchar(30), @Description varchar(255), @LogFile varchar(50) DECLARE @Name varchar(30), @MediaName varchar(30), @BackupDirectory nvarchar(200) &#8211;Change this variable SET @BackupDirectory = &#8216;D:\\MSSQL_YEDEK\\&#8217; &#8211;Add a list of all databases you don&#8217;t want to backup to this. DECLARE Database_CURSOR CURSOR FOR SELECT name FROM sysdatabases WHERE name &lt;&gt; &#8216;tempdb&#8217; AND name &lt;&gt; &#8216;model&#8217; AND name &lt;&gt; [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1833,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1096,20],"tags":[328,388,1245,1244,1246],"_links":{"self":[{"href":"https:\/\/burakcaliskan.org\/blog\/wp-json\/wp\/v2\/posts\/1886"}],"collection":[{"href":"https:\/\/burakcaliskan.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/burakcaliskan.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/burakcaliskan.org\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/burakcaliskan.org\/blog\/wp-json\/wp\/v2\/comments?post=1886"}],"version-history":[{"count":2,"href":"https:\/\/burakcaliskan.org\/blog\/wp-json\/wp\/v2\/posts\/1886\/revisions"}],"predecessor-version":[{"id":1889,"href":"https:\/\/burakcaliskan.org\/blog\/wp-json\/wp\/v2\/posts\/1886\/revisions\/1889"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/burakcaliskan.org\/blog\/wp-json\/wp\/v2\/media\/1833"}],"wp:attachment":[{"href":"https:\/\/burakcaliskan.org\/blog\/wp-json\/wp\/v2\/media?parent=1886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/burakcaliskan.org\/blog\/wp-json\/wp\/v2\/categories?post=1886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/burakcaliskan.org\/blog\/wp-json\/wp\/v2\/tags?post=1886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}