Search results matching tags 'tools' and 'datetime'http://sqlblog.com/search/SearchResults.aspx?o=DateDescending&tag=tools,datetime&orTags=0Search results matching tags 'tools' and 'datetime'en-USCommunityServer 2.1 SP2 (Build: 61129.1)Backup and the evil RETAINDAYS optionhttp://sqlblog.com/blogs/tibor_karaszi/archive/2012/07/08/backup-and-the-evil-retaindays-option.aspxSun, 08 Jul 2012 15:25:00 GMT21093a07-8b3d-42db-8cbf-3350fcbf5496:44226TiborKaraszi<p>"So what bad has this option done?", you probably as yourself. Well, not much, but I find it evil because it confuses people, especially those new to SQL Server. I have many times seen people specifying something like 3, and expect SQL Server to keep the three most recent backups in the backup file and overwrite everything which is older than that. Well, that is <strong>not</strong> what the option does. </p>
<p>But before we go into details, let's look at an example backup command which is using this option:</p><p>
<code style="font-size:12px;"><span style="color:blue;">BACKUP DATABASE </span><span style="color:black;">sqlmaint </span><span style="color:blue;">TO DISK = </span><span style="color:red;">'R:\sqlmaint.bak' </span><span style="color:blue;">WITH </span><span style="color:black;">RETAINDAYS </span><span style="color:blue;">= </span><span style="color:black;">3</span></code>
</p><p>The RETAINDAYS is also exposed in the backup dialog in SSMS: "<em>Backup set will expire: After x days</em>".</p><p>It is also exposed in Maintenance Plans, the backup task. The option is named "<em>Backup set will expire: After x days</em>". It is only enabled if you select the "<em>Back up databases across one or more files</em>" option, which is not the default option. This makes sense.<br>The default option is "<em>Create a backup files for every database</em>", which means that every time a backup is performed, a new file is created consisting of&nbsp;the database name, date and time. Since we will see that this option is only relevant when we do append, it makes sense in the RETAINDAYS not being enabled for this choice.</p><p>So what does this option do? All it does is make SQL Server&nbsp;return an error message of you try to do a backup using the INIT option (which means overwrite) before the date and time has occurred. In other words, it tries to help you in not overwriting a backup file, using the INIT option, before it is time. You can still overwrite&nbsp;earlier by either using the stronger FORMAT option instead of INIT; or by simply deleting the backup file. Btw, the RETAINDAYS parameter has a cousin named EXPIREDATE, which does the same thing but you specify a datetime value instead of number of days.</p><p><strong>Backup generations<br></strong>So, we have seen that RETAINDAYS do not in any way provide any automatic backup generation handling. There is no such functionality built-in in the BACKUP command. This means that when you are looking for functionality such as "keep backup files three days, and remove older than that", you need to look outside the BACKUP command. I.e., some script or tool of some sort. </p><p>I have an example (without the "delete old files" part) <a href="http://www.karaszi.com/SQLServer/util_backup_script_like_MP.asp">here</a>, which I mostly created as a starting point for those who want to roll their own and want to have some example to start with. Many of you are probably using Maintenance plans (the "Create a backup files for every database" option in the backup task, along with Maintenance Cleanup task). Another popular script/tool for this is Ola Hallengren's Maintenance Solution, which you find at <a href="http://ola.hallengren.com/">http://ola.hallengren.com/</a>.</p>