{"id":43,"date":"2017-09-28T05:01:04","date_gmt":"2017-09-28T05:01:04","guid":{"rendered":"http:\/\/www.spktechfit.com\/?p=43"},"modified":"2017-09-28T05:13:41","modified_gmt":"2017-09-28T05:13:41","slug":"sql-server-database-backups","status":"publish","type":"post","link":"https:\/\/www.spktechfit.com\/?p=43","title":{"rendered":"SQL Server Database Backups"},"content":{"rendered":"<p>Backups are crucial. You need to have the right backups in place to have your data covered in the case of any disasters. In this post I go through the different backup types and some of the different strategies for implementing them.<\/p>\n<p><!--more--><\/p>\n<h2><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-50 aligncenter\" src=\"http:\/\/www.spktechfit.com\/wp-content\/uploads\/2017\/09\/backup.jpg\" alt=\"\" width=\"197\" height=\"197\" \/><\/h2>\n<h2><b>Recovery Models<\/b><\/h2>\n<p>The first thing to decide on before the backup type, is the Recovery Model. You set the Recovery Model type for each database. The Recovery Model is what decides what data to keep in the transaction log and for how long<\/p>\n<ol>\n<li data-aria-posinset=\"1\" data-aria-level=\"2\"><strong>Simple<\/strong>:\u202f the transaction is written to the transaction log, but as soon as the transaction is complete and the data has been written to file, the space that was used in the transaction log is now reusable by new transactions. This\u202f means you wont be able to do point in time recovery, you can only restore to your last Full or Differential backup<\/li>\n<li data-aria-posinset=\"2\" data-aria-level=\"2\"><strong>Full<\/strong>:\u202f This is basically us telling SQL Server to leave the transaction log alone. It puts the responsibility on us to make sure the transaction log is emptied. It is emptied when you do a transaction log backup.If you aren&#8217;t backing up the transaction log it can fill up. This model allows you to do a point-in-time restore<\/li>\n<li data-aria-posinset=\"3\" data-aria-level=\"2\"><strong>Bulk Logged<\/strong>: this is for when you are doing bulk operations and you want to minimize the amount of logging during that operation. You cant do point in time recovery with this model as it doesn\u2019t have enough information recorded.<\/li>\n<\/ol>\n<h2>Backup Types<\/h2>\n<ol>\n<li data-aria-posinset=\"1\" data-aria-level=\"2\"><strong>Full Backups<\/strong> take a complete copy of the database and allow for a complete restore to a certain point in time. The good side of these is they backup everything, bad side is they take the longest.<\/li>\n<li data-aria-posinset=\"2\" data-aria-level=\"2\"><strong>Differential Backups<\/strong> allow to take a snapshot of all the data that has changed since the last full backup.\u202f These backups are cumulative, if you have multiple differential backups they will have all the changes that were included in the previous differential backups. EG: if you do a full backup on Sunday, then a differential backup each weekday, Friday will include all the changes that happened on Mon, Tue, Wed, Thurs. and Friday.<\/li>\n<li data-aria-posinset=\"3\" data-aria-level=\"2\"><strong>Transaction Log backups<\/strong>\u202f include data that has been modified since last full, differential or T-log backup. These are not cumulative because every time a transaction log backup occurs it truncates the log, which gets rid of all the inactive or old transactions\u202f that have been committed to the database. Transaction log backups give us point in time recovery.<br \/>\n<i>The transaction log is a log of all the transactions that have occurred since the last truncation. Anytime a transaction modifies data it occurs first in the log, then when SQL server gets around to committing that modification to the database data, that entry in the transaction log is marked as inactive and the next time a truncation occurs it will get rid of that record.<\/i><\/li>\n<\/ol>\n<h2><b>Factors to consider when planning the backup strategy\u202f<\/b><\/h2>\n<ul>\n<li data-aria-posinset=\"1\" data-aria-level=\"2\"><strong>Acceptable data loss<\/strong>: is it a day? An hour? 5 mins? If it\u2019s a day you could away with full and differential backups. If its down to minutes then you probably need transaction log backups<\/li>\n<li data-aria-posinset=\"2\" data-aria-level=\"2\"><strong>Type of database<\/strong>: if you have a database that has lots of transactions and low acceptable amount of data loss you do something like:<br \/>\n1 x nightly full backup<br \/>\nhourly differential backup<br \/>\n10 minutely transaction log backups.<br \/>\nThis is a good strategy for a small to medium size database where acceptable data loss is around 10 mins.<\/li>\n<li data-aria-posinset=\"3\" data-aria-level=\"2\"><strong>Size of database:<\/strong> if the database is very big you wouldn\u2019t be able to have the same setup as we had in the example above as it would take way to long. In this case you might do:<br \/>\nfull backup x 1 a week<br \/>\ndifferential x 2\u202f a day<br \/>\nTransaction log x 1 an hour<br \/>\nIf you had an exceptionally large database, you split the backups into filegroups. Most large databases have a of lot of archive data that is read only. You can put this into its own filegroup and set that filegroup to backup once a month.<br \/>\nThen put the data that has lots of action on it into its own filegroup and back it up more often<\/li>\n<li data-aria-posinset=\"4\" data-aria-level=\"2\"><strong>Restoration Time:<\/strong> this is a factor for businesses that don&#8217;t implement a high availability or mirroring option. When their system goes down they are going to have to do a\u00a0full restore.\u00a0<em><b>NOTE: transaction logs take the longest to restore, and full backups are the quickest to restore. This is because transaction logs almost have to go through statement by statement.<\/b>\u00a0<\/em><\/li>\n<\/ul>\n<p><b>The restore process goes like this:<\/b><\/p>\n<ol>\n<li data-aria-posinset=\"1\" data-aria-level=\"2\">Restore the last created full backup<\/li>\n<li data-aria-posinset=\"2\" data-aria-level=\"2\">Restore the last created differential backups<\/li>\n<li data-aria-posinset=\"3\" data-aria-level=\"2\">Restore the transaction log backups in sequence (because it truncates the logs you need them all to complete the restore). If for any reason one of the log backups was corrupted or couldn\u2019t be restored, this means the chain is broken and you can only restore to the log backup before the chain was broken.<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h2><b>Backing Up Databases<\/b><\/h2>\n<h3><b>Full Backups<\/b><\/h3>\n<p><b>How to run a Full database backup using the interface:<\/b><\/p>\n<ol>\n<li data-aria-posinset=\"1\" data-aria-level=\"2\">Right click on the database and go to &#8216;Tasks -&gt; Backup&#8217;<\/li>\n<li data-aria-posinset=\"2\" data-aria-level=\"2\">Select &#8216;Full&#8217; in the backup type drop down menu<\/li>\n<li data-aria-posinset=\"3\" data-aria-level=\"2\">In the &#8216;Destination&#8217; box you select where you want to save the backup to. If you don&#8217;t want it in the default area, click &#8216;Remove&#8217; and then &#8216;Add&#8217; to select your location of choice.<br \/>\n<b>NOTE:\u00a0<\/b>you add multiple destinations and it will split the database backup file across these destinations. If you have a very large database you could select 3 locations across 3 drives, this would be a big performance improvement when running the backup<\/li>\n<li data-aria-posinset=\"4\" data-aria-level=\"2\">Click OK to run the Full backup<\/li>\n<\/ol>\n<p><b>How to run a basic Full database backup using T-SQL:<\/b><\/p>\n<ol>\n<li data-aria-posinset=\"1\" data-aria-level=\"2\">Run the following code in a query window. Make sure to change the name of the database and the location you want to save the backup to-<\/li>\n<\/ol>\n<p><em>BACKUP DATABASE [AdventureWorks2012]\u00a0<\/em><br \/>\n<em>\u202f\u202f\u202f TO DISK = &#8216;E:\\msft_sql_70_462\\13-Backup\\AdventureWorks2012_FULL.bak&#8217;\u202f\u00a0\u202f\u202f\u202f WITH NAME = &#8216;AdventureWorks2012-Full Database Backup&#8217;\u00a0<\/em><br \/>\n<em>GO\u00a0<\/em><\/p>\n<h3><b>Differential Backups<\/b><\/h3>\n<p><b>How to run a differential backup through the interface:<\/b><\/p>\n<ol>\n<li data-aria-posinset=\"1\" data-aria-level=\"1\">Right click on the database and go to &#8216;Tasks -&gt; Backup&#8217;<\/li>\n<li data-aria-posinset=\"2\" data-aria-level=\"1\">Select &#8216;Differential&#8217; in the backup type drop down menu<\/li>\n<li data-aria-posinset=\"3\" data-aria-level=\"1\">In the &#8216;Destination&#8217; box you select where you want to save the backup to. If you don&#8217;t want it in the default area, click &#8216;Remove&#8217; and then &#8216;Add&#8217; to select your location of choice.<\/li>\n<li data-aria-posinset=\"4\" data-aria-level=\"1\">Click OK to run the Full backup<\/li>\n<\/ol>\n<p><b>How to run a Differential database backup using T-SQL:<\/b><\/p>\n<ol>\n<li data-aria-posinset=\"1\" data-aria-level=\"2\">Run the following code in a query window. Make sure to change the name of the database and the location you want to save the backup to-<br \/>\n(you may notice it is very similar to the full backup T-SQL code except for the &#8216;DIFFERENTIAL&#8217; flag at the end)<\/li>\n<\/ol>\n<p><em>BACKUP DATABASE [AdventureWorks2012]<br \/>\n<\/em><em>\u202f\u202f\u202f TO DISK = &#8216;E:\\msft_sql_70_462\\13-Backup\\AdventureWorks2012_DIFF.bak&#8217;\u202f\u202f\u00a0<\/em><em>\u202f\u202f\u202f WITH NAME = &#8216;AdventureWorks2012-Full Database Backup&#8217;, DIFFERENTIAL<br \/>\n<\/em><em>GO\u00a0<\/em><\/p>\n<h3><span class=\"TextRun SCXO123170864\" lang=\"EN-US\" xml:lang=\"EN-US\"><span class=\"NormalTextRun SCXO123170864\">Transaction Log Backups<\/span><\/span><\/h3>\n<p><b>How to run a Transaction Log backup through the interface:<\/b><\/p>\n<ol>\n<li data-aria-posinset=\"1\" data-aria-level=\"1\">Right click on the database and go to &#8216;Tasks -&gt; Backup&#8217;<\/li>\n<li data-aria-posinset=\"2\" data-aria-level=\"1\">Select &#8216;transaction&#8217; in the backup type drop down menu<\/li>\n<li data-aria-posinset=\"3\" data-aria-level=\"1\">In the &#8216;Destination&#8217; box you select where you want to save the backup to. If you don&#8217;t want it in the default area, click &#8216;Remove&#8217; and then &#8216;Add&#8217; to select your location of choice.<\/li>\n<li data-aria-posinset=\"4\" data-aria-level=\"1\">Click OK to run the Full backup<\/li>\n<\/ol>\n<p><b>How to run a Transaction Log backup using T-SQL:<\/b><\/p>\n<p style=\"text-align: left;\"><em>BACKUP LOG [AdventureWorks2012]\u202f\u00a0<\/em><br \/>\n<em>\u202f\u202f\u202f TO DISK = &#8216;E:\\msft_sql_70_462\\13-Backup\\AdventureWorks2012_LOG.trn&#8217;\u00a0<\/em><br \/>\n<em>\u202f\u202f\u202f WITH NAME = &#8216;AdventureWorks2012-Transaction Log Backup&#8217;\u00a0<\/em><br \/>\n<em>GO\u00a0<\/em><\/p>\n<p><b>Log Backup Options<\/b><\/p>\n<p>Truncate the Transaction Log: this means that it will leave anything that is active in the\u00a0Transaction\u00a0log, but anything that has been committed or written to disk will be truncated<br \/>\nBackup the Tail log: This will backup the active portion of the log<\/p>\n<p>&nbsp;<\/p>\n<h2><b>Automating The Backup Strategy<\/b><\/h2>\n<h3>Using T-SQL:<\/h3>\n<p>You can do this using T-SQL and then setting up an SQL Agent job to run the code on a schedule.\u00a0The following code will backup the AdventureWorks2012 database to the file path\u00a0E:\\Backups using the naming convention &#8216;AdventureWorks2012-DATEOFBACKUP_Full.bak<\/p>\n<p><i>DECLARE @filePath\u00a0VARCHAR(256)\u202f\u202f\u202f\u202f &#8212; root path for backup files<\/i><br \/>\n<i>DECLARE @fileDate\u00a0VARCHAR(20)\u202f\u202f\u202f\u202f &#8212; file date of backup<\/i><br \/>\n<i>DECLARE @fileName\u00a0VARCHAR(256)\u202f\u202f\u202f\u202f &#8212; full path with name and date<\/i><\/p>\n<p><i>\u202f<\/i>\u00a0<i>SET @filePath\u00a0= &#8216;E:\\Backups&#8217;\u202f <\/i><br \/>\n<i>SELECT @fileDate\u00a0= CONVERT(VARCHAR(20), GETDATE(), 112)<\/i><\/p>\n<p><i>SET @fileName\u00a0= @filePath\u00a0+ &#8216;AdventureWorks2012&#8217; + &#8216;_&#8217; + @fileDate\u00a0+ &#8216;_FULL.bak&#8217;\u202f\u202f<\/i><\/p>\n<p><i>BACKUP DATABASE [AdventureWorks2012]\u202f<\/i><br \/>\n<i>\u202f\u202f\u202f TO DISK = @fileName<\/i><br \/>\n<i>\u202f\u202f\u202f WITH NAME = &#8216;AdventureWorks2012-Full Database Backup&#8217;<\/i><\/p>\n<p><i>GO<\/i><\/p>\n<p>&nbsp;<\/p>\n<h3><b>Using The\u00a0Interface:<\/b><\/h3>\n<ol>\n<li data-aria-posinset=\"1\" data-aria-level=\"1\">Expand &#8216;Management&#8217;<\/li>\n<li data-aria-posinset=\"2\" data-aria-level=\"1\">Right click on &#8216;Maintenance Plans&#8217; and select &#8216;New Maintenance Plan Wizard&#8217;<\/li>\n<li data-aria-posinset=\"3\" data-aria-level=\"1\">Give the Plan\u202f a name and select &#8216;Separate Schedules For Each Task&#8217;<\/li>\n<li data-aria-posinset=\"3\" data-aria-level=\"1\">If you have more than one instance you will need to select the instances you want to apply the plan to<\/li>\n<li data-aria-posinset=\"3\" data-aria-level=\"1\">In the Maintenance Tasks Window select the following:<br \/>\n&#8211; Backup DAtabase (FULL)<br \/>\n&#8211; Backup Database (Differential)<br \/>\n&#8211; Backup Database (Transaction Log)<\/li>\n<li data-aria-posinset=\"3\" data-aria-level=\"1\">In the Full Backup task window select the Database you want to backup<\/li>\n<li data-aria-posinset=\"3\" data-aria-level=\"1\">Select location to save the backups. You can also put a tick in the box to &#8220;Create a sub-directory for each database&#8221; so it keeps all the backups in different folders<\/li>\n<li data-aria-posinset=\"3\" data-aria-level=\"1\">Repeat these steps for the Differential and Transaction Log backup pages<\/li>\n<li data-aria-posinset=\"3\" data-aria-level=\"1\">On the next page you can select to write a report on the maintenance plan each time it runs, and\/or send an email with the results each time it runs. (<a href=\"http:\/\/www.spktechfit.com\/?p=7\">See section on setting up Database email<\/a>)<\/li>\n<li data-aria-posinset=\"3\" data-aria-level=\"1\">Click Next and then Finish to create the Maintenance Plan<\/li>\n<\/ol>\n<p><b>Now that we have created the Maintenance Plan we need to set it to run on a schedule.<\/b><\/p>\n<p><b>Setting the Schedule for this Maintenance Plan<\/b><\/p>\n<ol>\n<li data-aria-posinset=\"1\" data-aria-level=\"1\">Go to Maintenance Plans and double click the Plan you just created<\/li>\n<li data-aria-posinset=\"2\" data-aria-level=\"1\">You will see 3 subplans, 1 each for Full, Differential, Transaction Log backups. Click on the calendar icon to set the schedule.<br \/>\nIts probably worth changing the name of the Subplan to something more relevant like &#8216;FULL, Differential or Log&#8217;<br \/>\nFor the Full backup task we will set it to run once a week on Sunday at 12am<\/li>\n<li data-aria-posinset=\"4\" data-aria-level=\"1\">Then do the same thing with the Differential subplan, setting the schedule for daily at\u202f Midnight<\/li>\n<li data-aria-posinset=\"4\" data-aria-level=\"1\">Then for the Transaction Log sub plan set the schedule to run daily and set the Daily Frequency to every 30 mins<\/li>\n<li data-aria-posinset=\"4\" data-aria-level=\"1\">Save the Maintenance Plan and it is now ready to go<\/li>\n<\/ol>\n<h2><b>Copy Only Backups<\/b><\/h2>\n<p>As stated earlier, differential backups use the most recent full backup as their base., I.e.: they backup any changes made since the last full backup. Lets say someone does a full backup as a test, then deletes that backup. Now the differentials are backing up since that last test backup was done, but you don\u2019t have the full backup anymore and therefore the sequence is broken and you lose data. If you try to restore this Diff backup to the Full backup you have you will get an error.<br \/>\nCopy only backups don\u2019t affect the sequence.\u202f The differential backups still work from the last full backup, not the copy only backup.<\/p>\n<h3><b>To run a copy only backup using T-SQL:<\/b><\/h3>\n<p><i>BACKUP DATABASE [AdventureWorks2012]<\/i><br \/>\n<i>\u202f\u202f\u202f TO DISK = &#8216;E:SQLBackup\\AdventureWorks_Copy.bak&#8217;\u202f<\/i><br \/>\n<i>\u202f\u202f\u202f WITH NAME = &#8216;AdventureWorks2012&#8217;,\u00a0<\/i><i>COPY_ONLY<\/i><i>\u202f<\/i><br \/>\n<i>GO<\/i><\/p>\n<p>It\u2019s the &#8216;COPY_ONLY&#8217; flag that makes it a copy only backup.<\/p>\n<h3><b>To run a copy only backup using the interface:<\/b><\/h3>\n<ol>\n<li data-aria-posinset=\"1\" data-aria-level=\"1\">Right click on the database and go to &#8216;Tasks -&gt; Backup&#8217;<\/li>\n<li data-aria-posinset=\"2\" data-aria-level=\"1\">Select &#8216;Full&#8217; in the backup type drop down menu<\/li>\n<li data-aria-posinset=\"3\" data-aria-level=\"1\">Under Backup Type, select &#8216;Copy Only Backup&#8217;<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Backups are crucial. You need to have the right backups in place to have your data covered in the case of any disasters. In this post I go through the different backup types and some of the different strategies for implementing them.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2,3],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.spktechfit.com\/index.php?rest_route=\/wp\/v2\/posts\/43"}],"collection":[{"href":"https:\/\/www.spktechfit.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.spktechfit.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.spktechfit.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.spktechfit.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=43"}],"version-history":[{"count":8,"href":"https:\/\/www.spktechfit.com\/index.php?rest_route=\/wp\/v2\/posts\/43\/revisions"}],"predecessor-version":[{"id":53,"href":"https:\/\/www.spktechfit.com\/index.php?rest_route=\/wp\/v2\/posts\/43\/revisions\/53"}],"wp:attachment":[{"href":"https:\/\/www.spktechfit.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=43"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.spktechfit.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=43"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.spktechfit.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}