{"id":29,"date":"2012-06-13T16:29:10","date_gmt":"2012-06-13T22:29:10","guid":{"rendered":"http:\/\/s1095387061.online-home.ca\/?p=29"},"modified":"2013-02-21T16:04:22","modified_gmt":"2013-02-21T22:04:22","slug":"backup-default-sql-server-trace-files","status":"publish","type":"post","link":"https:\/\/databaserefresh.com\/blog\/backup-default-sql-server-trace-files\/","title":{"rendered":"Backup Default SQL Server Trace files"},"content":{"rendered":"<p>Default Trace first has been introduced in SQL Server 2005 and could be really useful for Database Administrators in troubleshooting.<\/p>\n<p>There is really great article about what Default Trace is and what you can do with it if you want to learn more:<\/p>\n<p><a href=\"http:\/\/www.simple-talk.com\/sql\/performance\/the-default-trace-in-sql-server---the-power-of-performance-and-security-auditing\/\">http:\/\/www.simple-talk.com\/sql\/performance\/the-default-trace-in-sql-server&#8212;the-power-of-performance-and-security-auditing\/<\/a><\/p>\n<p>Today I would like to share with you my solution on how to set up an automated trace files\u2019 backups.<\/p>\n<p>How many times you thought: \u201cIf I set this audit last month I would know who changed the server configuration\u201d. Unfortunately sometimes we are too busy with our day to day work or do not think that we will ever need to check who changed that specific option on SQL Server and when it was changed. One of the reasons could be that nobody has access to the SQL Server except you. Or you know what you do and documented every one of your steps. But what about those new applications that require sysadmin privileges on SQL Server to install applications and databases? You ask vendor what are the requirements and their response is: \u201cSQL Server 2005 or higher\u201d. And then you discover that installation process actually changed your SQL Server configuration to enable CLR.<\/p>\n<p>This is not real life example, but very close. It took me a couple of hours to go through the tape backups to find out when I changed the server configuration, because I could not remember changing it at all! So, it WAS an application installation.<\/p>\n<p>The first problem when you go through the tape backups is that it is very time consuming. The tape could be offsite; you need to involve backup guys. The second problem is that the trace file we are looking for may not even be on the tape, especially if you backup trace files only once a day. The number of trace files is limited to five files and the new file is generated when file\u2019s size reaches 20 MB. At the same time the oldest file is deleted.<\/p>\n<p>There are lots of solutions out there to have specific records imported to a database from the trace file. One of the latest articles by Aaron Bertrand could be found here:<\/p>\n<p><a href=\"http:\/\/www.mssqltips.com\/sqlservertip\/2695\/maintaining-sql-server-default-trace-historical-events-for-analysis-and-reporting\/?utm_source=dailynewsletter&amp;utm_medium=email&amp;utm_content=headline&amp;utm_campaign=201257\">http:\/\/www.mssqltips.com\/sqlservertip\/2695\/maintaining-sql-server-default-trace-historical-events-for-analysis-and-reporting\/?utm_source=dailynewsletter&amp;utm_medium=email&amp;utm_content=headline&amp;utm_campaign=201257<\/a><\/p>\n<p>But this does not quite work for me. I do not know what I may need next time, so capturing just specific records do not really work for me. Considering that I use DDL triggers for audit and that I need to check old trace files probably once a year, I do not want to import all records to the database, because this is another database to support and this includes backups, maintenance etc.<\/p>\n<p>To save myself time when I needed to check something the next time I came up with solution to backup trace files. This solution will allowed me to make sure that ALL trace files are on the tape backup as well as allow me to will keep an archive of the trace files on the server for the quick access. The solution has been designed and tested for SQL Server 2008 R2.<\/p>\n<p>We want to have all archived Default Trace files on the same server, but in different folder. The files that are archived would also be compressed (zipped).<\/p>\n<p>A little challenge here is that we cannot backup the latest trace file because it will be still in use. We can will only backup \u201cclosed\u201d traces.<\/p>\n<p><strong>Components\/Requirements:<\/strong><\/p>\n<ol>\n<li>Archive folder on the server (make sure that SQL Server account or account that will be used to run the archiving job has rights to create and delete files in this folder). To make the scripting more generic I have created the folder under the \u201cLog\u201d folder (location of the Default  Trace on your SQL Server instance).<\/li>\n<li>WMI SQL Server Alert.<\/li>\n<li>SQL Server Job to respond to alert (modify job to backup files).<\/li>\n<li>SQL Server Agent \u201cReplace tokens for all job responses to alerts\u201d option is enabled.<\/li>\n<li>Database mail set up.<\/li>\n<li>Operator exists to get test e-mails.<\/li>\n<li>Gzip utility to \u201czip\u201d archived files.<\/li>\n<\/ol>\n<p><span style=\"text-decoration: underline;\">Note<\/span>:  5 and 6 only required for the testing. You may skip testing part as this is just to demonstrate files rollover process and response.<\/p>\n<p> <strong>Implementation Steps (including testing):<\/strong><\/p>\n<ul>\n<li>This solution requires SQL Server Agent option set to \u201cReplace tokens for all job responses to alerts\u201d which may not be desirable if you are running a very secure environment.<\/li>\n<\/ul>\n<p style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">EXEC<\/span> msdb.dbo.<span style=\"color: #800000;\">sp_set_sqlagent_properties<\/span>@alert_replace_runtime_tokens=1<\/p>\n<p style=\"padding-left: 60px;\"><strong>OR<\/strong> set it up using SQL Server Management Studio through the SQL Server Agent Properties:<\/p>\n<p style=\"padding-left: 60px;\"><a href=\"http:\/\/s1095387061.online-home.ca\/wp-content\/uploads\/2012\/06\/bp2_img1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-33\" title=\"bp2_img1\" src=\"http:\/\/s1095387061.online-home.ca\/wp-content\/uploads\/2012\/06\/bp2_img1-300x268.jpg\" alt=\"\" width=\"300\" height=\"268\" \/><\/a><\/p>\n<p style=\"padding-left: 60px;\">Note that if SYSTEM account has been removed from logins or SYSADMIN server role you may have issues with WMI alerts.<\/p>\n<ul>\n<li>First we need to find location of the Default  Trace on your SQL Server instance:\n <\/li>\n<\/ul>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">SELECT<\/span> <span style=\"color: #ff00ff;\">LEFT<\/span>(<span style=\"color: #0000ff;\">path<\/span>, <span style=\"color: #ff00ff;\">LEN<\/span>(<span style=\"color: #0000ff;\">path<\/span>)-<span style=\"color: #ff00ff;\">CHARINDEX<\/span>(<span style=\"color: #ff0000;\">'\\'<\/span>, <span style=\"color: #ff00ff;\">REVERSE<\/span>(<span style=\"color: #0000ff;\">path<\/span>)))  <span style=\"color: #0000ff;\">FROM<\/span> <span style=\"color: #008000;\">sys.traces<\/span> <span style=\"color: #0000ff;\">WHERE<\/span> id = 1<\/pre>\n<p style=\"padding-left: 60px;\"> <a href=\"http:\/\/s1095387061.online-home.ca\/wp-content\/uploads\/2012\/06\/bp2_img2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-34\" title=\"bp2_img2\" src=\"http:\/\/s1095387061.online-home.ca\/wp-content\/uploads\/2012\/06\/bp2_img2.jpg\" alt=\"\" width=\"322\" height=\"79\" \/><\/a><\/p>\n<ul>\n<li> Now \u2013 create subfolder for the archived trace files (it could be any other folder that you want to backup to the tapes):<\/li>\n<\/ul>\n<p style=\"padding-left: 60px;\"> <a href=\"http:\/\/s1095387061.online-home.ca\/wp-content\/uploads\/2012\/06\/bp2_img3.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-35\" title=\"bp2_img3\" src=\"http:\/\/s1095387061.online-home.ca\/wp-content\/uploads\/2012\/06\/bp2_img3.jpg\" alt=\"\" width=\"572\" height=\"75\" \/><\/a><\/p>\n<ul>\n<li>Create job to test\/confirm that WMI event (trace file rollover) triggers e-mail and that \u201cReplace tokens\u201d option has been configured properly and works. You will need to replace <em>@notify_email_operator_name<\/em> parameter\u2019s value and <em>@recipients<\/em> e-mail in the script below. Also replace <em>@profile_name<\/em> with your DB Mail profile. Remember, that we are just testing WMI now. Do not test this in your production environment!<\/li>\n<\/ul>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">USE<\/span> [msdb]<\/pre>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">GO<\/span><\/pre>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\"> BEGIN TRANSACTION<\/span><\/pre>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">DECLARE<\/span> @ReturnCode <span style=\"color: #0000ff;\">INT<\/span><\/pre>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">SELECT<\/span> @ReturnCode = 0<\/pre>\n<pre style=\"padding-left: 60px;\"> <span style=\"color: #0000ff;\">IF<\/span> <span style=\"color: #808080;\">NOT EXISTS<\/span> (<span style=\"color: #0000ff;\">SELECT<\/span> name <span style=\"color: #0000ff;\">FROM<\/span> msdb.dbo.syscategories <span style=\"color: #0000ff;\">WHERE<\/span> name=<span style=\"color: #ff0000;\">N'DBA'<\/span> <span style=\"color: #808080;\">AND<\/span> category_class=1)<\/pre>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">BEGIN<\/span><\/pre>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">EXEC<\/span> @ReturnCode = msdb.dbo.<span style=\"color: #800000;\">sp_add_category<\/span> @class=<span style=\"color: #ff0000;\">N'JOB'<\/span>, @type=<span style=\"color: #ff0000;\">N'LOCAL'<\/span>, @name=<span style=\"color: #ff0000;\">N'DBA'<\/span><\/pre>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">IF<\/span> (<span style=\"color: #ff00ff;\">@@ERROR<\/span> &lt;&gt; 0 <span style=\"color: #808080;\">OR<\/span> @ReturnCode &lt;&gt; 0) <span style=\"color: #0000ff;\">GOTO<\/span> QuitWithRollback<\/pre>\n<pre style=\"padding-left: 60px;\"> <span style=\"color: #0000ff;\">END<\/span><\/pre>\n<pre style=\"padding-left: 60px;\"> <span style=\"color: #0000ff;\">DECLARE<\/span> @jobId <span style=\"color: #0000ff;\">BINARY<\/span>(16)<\/pre>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">EXEC<\/span> @ReturnCode =  msdb.dbo.<span style=\"color: #800000;\">sp_add_job<\/span> @job_name=<span style=\"color: #ff0000;\">N'WMI Response - Default Trace File Rollover'<\/span>,<\/pre>\n<pre style=\"padding-left: 60px;\">            @enabled=1,<\/pre>\n<pre style=\"padding-left: 60px;\">            @notify_level_eventlog=0,<\/pre>\n<pre style=\"padding-left: 60px;\">            @notify_level_email=2,<\/pre>\n<pre style=\"padding-left: 60px;\">            @notify_level_netsend=0,<\/pre>\n<pre style=\"padding-left: 60px;\">            @notify_level_page=0,<\/pre>\n<pre style=\"padding-left: 60px;\">            @delete_level=0,<\/pre>\n<pre style=\"padding-left: 60px;\">            @description=<span style=\"color: #ff0000;\">N'Sends notifications to DBA when Default Trace File Rollover event occures'<\/span>,<\/pre>\n<pre style=\"padding-left: 60px;\">            @category_name=<span style=\"color: #ff0000;\">N'DBA'<\/span>,<\/pre>\n<pre style=\"padding-left: 60px;\">            @owner_login_name=<span style=\"color: #ff0000;\">N'sa'<\/span>,<\/pre>\n<pre style=\"padding-left: 60px;\">            @notify_email_operator_name=<span style=\"color: #ff0000;\">N'DBA_Operator'<\/span>, @job_id = @jobId <span style=\"color: #0000ff;\">OUTPUT<\/span><\/pre>\n<pre style=\"padding-left: 150px;\"><span style=\"color: #008000;\">-- replace N'DBA_Operator' with operator you created<\/span><\/pre>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">IF<\/span> (<span style=\"color: #ff00ff;\">@@ERROR<\/span> &lt;&gt; 0 <span style=\"color: #808080;\">OR<\/span> @ReturnCode &lt;&gt; 0) <span style=\"color: #0000ff;\">GOTO<\/span> QuitWithRollback<\/pre>\n<pre style=\"padding-left: 60px;\"> <span style=\"color: #0000ff;\">EXEC<\/span> @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=<span style=\"color: #ff0000;\">N'Sends e-mail in response to alert'<\/span>,<\/pre>\n<pre style=\"padding-left: 60px;\">            @step_id=1,<\/pre>\n<pre style=\"padding-left: 60px;\">            @cmdexec_success_code=0,<\/pre>\n<pre style=\"padding-left: 60px;\">            @on_success_action=1,<\/pre>\n<pre style=\"padding-left: 60px;\">            @on_success_step_id=0,<\/pre>\n<pre style=\"padding-left: 60px;\">            @on_fail_action=2,<\/pre>\n<pre style=\"padding-left: 60px;\">            @on_fail_step_id=0,<\/pre>\n<pre style=\"padding-left: 60px;\">            @retry_attempts=0,<\/pre>\n<pre style=\"padding-left: 60px;\">            @retry_interval=0,<\/pre>\n<pre style=\"padding-left: 60px;\">            @os_run_priority=0, @subsystem=<span style=\"color: #ff0000;\">N'TSQL'<\/span>,<\/pre>\n<pre style=\"padding-left: 60px;\">            @command=<span style=\"color: #ff0000;\">N'EXEC msdb.dbo.sp_send_dbmail<\/span><\/pre>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #ff0000;\">    @profile_name = ''DBServerAlerts'',<\/span><\/pre>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #ff0000;\">    @recipients = ''firstName.LastName@maildomain.com'',<\/span><\/pre>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #ff0000;\">    @body =   ''File Name: $(ESCAPE_SQUOTE(WMI(FileName)));<\/span><\/pre>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #ff0000;\">                        Start Time: $(ESCAPE_SQUOTE(WMI(StartTime)));<\/span><\/pre>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #ff0000;\">                        Server Name: $(ESCAPE_SQUOTE(WMI(ServerName)));<\/span><\/pre>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #ff0000;\">                        Instance Name: $(ESCAPE_SQUOTE(WMI(SQLInstance)))'',<\/span><\/pre>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #ff0000;\">    @subject = ''Default Trace File Rollover event - $(ESCAPE_SQUOTE(WMI(SQLInstance)))'' ;'<\/span><\/pre>\n<pre style=\"padding-left: 60px;\">,<\/pre>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #008000;\">-- replace firstName.LastName@maildomain.com with e-mail you will be using for the testing;<\/span><\/pre>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #008000;\">-- replace @profile_name with your DB Mail profile<\/span><\/pre>\n<pre style=\"padding-left: 60px;\">            @database_name=<span style=\"color: #ff0000;\">N'master'<\/span>,<\/pre>\n<pre style=\"padding-left: 60px;\">            @flags=4<\/pre>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">IF<\/span> (<span style=\"color: #ff00ff;\">@@ERROR<\/span> &lt;&gt; 0 <span style=\"color: #808080;\">OR<\/span> @ReturnCode &lt;&gt; 0) <span style=\"color: #0000ff;\">GOTO<\/span> QuitWithRollback<\/pre>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">EXEC<\/span> @ReturnCode = msdb.dbo.<span style=\"color: #800000;\">sp_update_job<\/span> @job_id = @jobId, @start_step_id = 1<\/pre>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">IF<\/span> (<span style=\"color: #ff00ff;\">@@ERROR<\/span> &lt;&gt; 0 <span style=\"color: #808080;\">OR<\/span> @ReturnCode &lt;&gt; 0) <span style=\"color: #0000ff;\">GOTO<\/span> QuitWithRollback<\/pre>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">EXEC<\/span> @ReturnCode = msdb.dbo.<span style=\"color: #800000;\">sp_add_jobserver<\/span> @job_id = @jobId, @server_name = <span style=\"color: #ff0000;\">N'(local)'<\/span><\/pre>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">IF<\/span> (<span style=\"color: #ff00ff;\">@@ERROR<\/span> &lt;&gt; 0 <span style=\"color: #808080;\">OR<\/span> @ReturnCode &lt;&gt; 0) <span style=\"color: #0000ff;\">GOTO<\/span> QuitWithRollback<\/pre>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">COMMIT TRANSACTION<\/span><\/pre>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">GOTO<\/span> EndSave<\/pre>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">QuitWithRollback:<\/span><\/pre>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">    IF<\/span> (<span style=\"color: #ff00ff;\">@@TRANCOUNT<\/span> &gt; 0) <span style=\"color: #0000ff;\">ROLLBACK TRANSACTION<\/span><\/pre>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">EndSave:<\/span><\/pre>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\"> <\/span><span style=\"color: #0000ff;\">GO<\/span><\/pre>\n<ul>\n<li>Create alert:<\/li>\n<\/ul>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">EXEC<\/span> msdb.dbo.<span style=\"color: #800000;\">sp_add_alert<\/span> @name=<span style=\"color: #ff0000;\">N'WMI - Default Trace File Rollover'<\/span>,<\/pre>\n<pre style=\"padding-left: 60px;\">   @message_id=0,<\/pre>\n<pre style=\"padding-left: 60px;\">   @severity=0,<\/pre>\n<pre style=\"padding-left: 60px;\">   @enabled=1,<\/pre>\n<pre style=\"padding-left: 60px;\">   @delay_between_responses=0,<\/pre>\n<pre style=\"padding-left: 60px;\">   @include_event_description_in=1,<\/pre>\n<pre style=\"padding-left: 60px;\">   @category_name=<span style=\"color: #ff0000;\">N'[Uncategorized]'<\/span>,<\/pre>\n<pre style=\"padding-left: 60px;\">   @wmi_namespace=<span style=\"color: #ff0000;\">N'\\\\.\\root\\Microsoft\\SqlServer\\ServerEvents\\MSSQLSERVER'<\/span>,<\/pre>\n<pre style=\"padding-left: 60px;\">   @wmi_query=<span style=\"color: #ff0000;\">N'select *  from TRACE_FILE_CLOSE'<\/span>,<\/pre>\n<pre style=\"padding-left: 60px;\">   @job_name=<span style=\"color: #ff0000;\">N'WMI Response - Default Trace File Rollover'<\/span> -<span style=\"color: #008000;\">- name of the job created in step 4<\/span><\/pre>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">GO<\/span><\/pre>\n<p style=\"padding-left: 60px;\"> <strong>Note<\/strong> that <em>@wmi_namespace<\/em> will be different for the named instance (example: \\\\.\\root\\Microsoft\\SqlServer\\ServerEvents\\<em>INSTANCENAME<\/em>).<\/p>\n<ul>\n<li>Initial testing.<\/li>\n<\/ul>\n<p style=\"padding-left: 60px;\">It may take some time to get response from this alert when you test this. This will depend on how busy your system is (how often trace files reach 20 MB size). It may take actually several hours on your test SQL Server. To speed up trace files generation I run this script when I do my testing (it runs about 2 minutes on my test server):<\/p>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">CREATE TABLE<\/span> tempdb.dbo.ThisIsLongNameToFillOutTraceFile<\/pre>\n<pre style=\"padding-left: 60px;\">       (<\/pre>\n<pre style=\"padding-left: 60px;\">       ThisIsLongNameToFillOutTraceFile_c1 <span style=\"color: #0000ff;\">uniqueidentifier<\/span> <span style=\"color: #808080;\">NOT NULL <\/span> <span style=\"color: #0000ff;\">CONSTRAINT<\/span> column_a_un1 <span style=\"color: #0000ff;\">UNIQUE<\/span>,<\/pre>\n<pre style=\"padding-left: 60px;\">       ThisIsLongNameToFillOutTraceFile_c2 <span style=\"color: #0000ff;\">uniqueidentifier<\/span> <span style=\"color: #808080;\">NOT NULL <\/span> <span style=\"color: #0000ff;\">CONSTRAINT<\/span> column_a_un2 <span style=\"color: #0000ff;\">UNIQUE<\/span>,<\/pre>\n<pre style=\"padding-left: 60px;\">       ThisIsLongNameToFillOutTraceFile_c3 <span style=\"color: #0000ff;\">uniqueidentifier<\/span> <span style=\"color: #808080;\">NOT NULL <\/span> <span style=\"color: #0000ff;\">CONSTRAINT<\/span> column_a_un3 <span style=\"color: #0000ff;\">UNIQUE<\/span>,<\/pre>\n<pre style=\"padding-left: 60px;\">       ThisIsLongNameToFillOutTraceFile_c4 <span style=\"color: #0000ff;\">uniqueidentifier<\/span> <span style=\"color: #808080;\">NOT NULL <\/span> <span style=\"color: #0000ff;\">CONSTRAINT<\/span> column_a_un4 <span style=\"color: #0000ff;\">UNIQUE<\/span>,<\/pre>\n<pre style=\"padding-left: 60px;\">       ThisIsLongNameToFillOutTraceFile_c11 <span style=\"color: #0000ff;\">uniqueidentifier<\/span> <span style=\"color: #808080;\">NOT NULL <\/span> <span style=\"color: #0000ff;\">CONSTRAINT<\/span> column_a_un5 <span style=\"color: #0000ff;\">UNIQUE<\/span>,<\/pre>\n<pre style=\"padding-left: 60px;\">       ThisIsLongNameToFillOutTraceFile_c12 <span style=\"color: #0000ff;\">uniqueidentifier<\/span> <span style=\"color: #808080;\">NOT NULL <\/span> <span style=\"color: #0000ff;\">CONSTRAINT<\/span> column_a_un6 <span style=\"color: #0000ff;\">UNIQUE<\/span>,<\/pre>\n<pre style=\"padding-left: 60px;\">       ThisIsLongNameToFillOutTraceFile_c13 <span style=\"color: #0000ff;\">uniqueidentifier<\/span> <span style=\"color: #808080;\">NOT NULL <\/span> <span style=\"color: #0000ff;\">CONSTRAINT<\/span> column_a_un7 <span style=\"color: #0000ff;\">UNIQUE<\/span>,<\/pre>\n<pre style=\"padding-left: 60px;\">       ThisIsLongNameToFillOutTraceFile_c14 <span style=\"color: #0000ff;\">uniqueidentifier<\/span> <span style=\"color: #808080;\">NOT NULL <\/span> <span style=\"color: #0000ff;\">CONSTRAINT<\/span> column_a_un8 <span style=\"color: #0000ff;\">UNIQUE<\/span>,<\/pre>\n<pre style=\"padding-left: 60px;\">       ThisIsLongNameToFillOutTraceFile_c111 <span style=\"color: #0000ff;\">uniqueidentifier<\/span> <span style=\"color: #808080;\">NOT NULL<\/span>  <span style=\"color: #0000ff;\">CONSTRAINT<\/span> column_a_un9 <span style=\"color: #0000ff;\">UNIQUE<\/span>,<\/pre>\n<pre style=\"padding-left: 60px;\">       ThisIsLongNameToFillOutTraceFile_c112 <span style=\"color: #0000ff;\">uniqueidentifier<\/span> <span style=\"color: #808080;\">NOT NULL <\/span> <span style=\"color: #0000ff;\">CONSTRAINT<\/span> column_a_un10 <span style=\"color: #0000ff;\">UNIQUE<\/span>,<\/pre>\n<pre style=\"padding-left: 60px;\">       ThisIsLongNameToFillOutTraceFile_c113 <span style=\"color: #0000ff;\">uniqueidentifier<\/span> <span style=\"color: #808080;\">NOT NULL <\/span> <span style=\"color: #0000ff;\">CONSTRAINT<\/span> column_a_un11 <span style=\"color: #0000ff;\">UNIQUE<\/span>,<\/pre>\n<pre style=\"padding-left: 60px;\">       ThisIsLongNameToFillOutTraceFile_c114 <span style=\"color: #0000ff;\">uniqueidentifier<\/span> <span style=\"color: #808080;\">NOT NULL <\/span> <span style=\"color: #0000ff;\">CONSTRAINT<\/span> column_a_un12 <span style=\"color: #0000ff;\">UNIQUE<\/span>,<\/pre>\n<pre style=\"padding-left: 60px;\">       ThisIsLongNameToFillOutTraceFile_c211 <span style=\"color: #0000ff;\">uniqueidentifier<\/span> <span style=\"color: #808080;\">NOT NULL <\/span> <span style=\"color: #0000ff;\">CONSTRAINT<\/span> column_a_un13 <span style=\"color: #0000ff;\">UNIQUE<\/span>,<\/pre>\n<pre style=\"padding-left: 60px;\">       ThisIsLongNameToFillOutTraceFile_c212 <span style=\"color: #0000ff;\">uniqueidentifier<\/span> <span style=\"color: #808080;\">NOT NULL <\/span> <span style=\"color: #0000ff;\">CONSTRAINT<\/span> column_a_un14 <span style=\"color: #0000ff;\">UNIQUE<\/span>,<\/pre>\n<pre style=\"padding-left: 60px;\">       ThisIsLongNameToFillOutTraceFile_c213 <span style=\"color: #0000ff;\">uniqueidentifier<\/span> <span style=\"color: #808080;\">NOT NULL <\/span> <span style=\"color: #0000ff;\">CONSTRAINT<\/span> column_a_un15 <span style=\"color: #0000ff;\">UNIQUE<\/span>,<\/pre>\n<pre style=\"padding-left: 60px;\">       ThisIsLongNameToFillOutTraceFile_c214 <span style=\"color: #0000ff;\">uniqueidentifier<\/span> <span style=\"color: #808080;\">NOT NULL <\/span> <span style=\"color: #0000ff;\">CONSTRAINT<\/span> column_a_un16 <span style=\"color: #0000ff;\">UNIQUE<\/span><\/pre>\n<pre style=\"padding-left: 60px;\">       )  <span style=\"color: #0000ff;\">ON<\/span> [PRIMARY]<\/pre>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">DROP TABLE <\/span>dbo.ThisIsLongNameToFillOutTraceFile<\/pre>\n<pre style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">GO <\/span>3000<\/pre>\n<p style=\"padding-left: 60px;\">Here is example of e-mail that you will get when trace file rollover will occur:<\/p>\n<p style=\"padding-left: 60px;\"> <a href=\"http:\/\/s1095387061.online-home.ca\/wp-content\/uploads\/2012\/06\/bp2_img4.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-36\" title=\"bp2_img4\" src=\"http:\/\/s1095387061.online-home.ca\/wp-content\/uploads\/2012\/06\/bp2_img4-300x129.jpg\" alt=\"\" width=\"300\" height=\"129\" \/><\/a> <\/p>\n<p style=\"padding-left: 60px;\">You will see on the server the new file (log_2088.trc) created at the same timetime:<\/p>\n<p style=\"padding-left: 60px;\"> <a href=\"http:\/\/s1095387061.online-home.ca\/wp-content\/uploads\/2012\/06\/bp2_img5.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-37\" title=\"bp2_img5\" src=\"http:\/\/s1095387061.online-home.ca\/wp-content\/uploads\/2012\/06\/bp2_img5.jpg\" alt=\"\" width=\"382\" height=\"113\" \/><\/a><\/p>\n<ul>\n<li>Now, when we validated that WMI alerts work and you have server configured properly we can replace the job with the final version:<\/li>\n<\/ul>\n<p style=\"padding-left: 60px;\"> <a href=\"http:\/\/s1095387061.online-home.ca\/wp-content\/uploads\/2012\/06\/bp2_img6.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-38\" title=\"bp2_img6\" src=\"http:\/\/s1095387061.online-home.ca\/wp-content\/uploads\/2012\/06\/bp2_img6-300x269.jpg\" alt=\"\" width=\"300\" height=\"269\" \/><\/a><\/p>\n<p style=\"padding-left: 60px;\">There will be two steps \u2013 to copy closed trace file to the archive folder and to zip the copied file:<\/p>\n<p style=\"padding-left: 60px;\"> <a href=\"http:\/\/s1095387061.online-home.ca\/wp-content\/uploads\/2012\/06\/bp2_img7.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-39\" title=\"bp2_img7\" src=\"http:\/\/s1095387061.online-home.ca\/wp-content\/uploads\/2012\/06\/bp2_img7-300x75.jpg\" alt=\"\" width=\"300\" height=\"75\" \/><\/a><\/p>\n<p style=\"padding-left: 60px;\">Step 1:<\/p>\n<p style=\"padding-left: 60px;\"> <a href=\"http:\/\/s1095387061.online-home.ca\/wp-content\/uploads\/2012\/06\/bp2_img8.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-40\" title=\"bp2_img8\" src=\"http:\/\/s1095387061.online-home.ca\/wp-content\/uploads\/2012\/06\/bp2_img8-300x142.jpg\" alt=\"\" width=\"300\" height=\"142\" \/><\/a><\/p>\n<p style=\"padding-left: 60px;\">Command (in one line):<\/p>\n<pre style=\"padding-left: 60px;\"><strong>for \/f \"tokens=1,2,3,4,5 delims=\\ \" %a in (\"$(ESCAPE_SQUOTE(WMI(FileName)))\") do <\/strong><\/pre>\n<pre style=\"padding-left: 60px;\"><strong>xcopy $(ESCAPE_SQUOTE(WMI(FileName))) %a\\%b\\%c\\%d\\defaultTraceArchive<\/strong><\/pre>\n<p style=\"padding-left: 60px;\">Step 2:<\/p>\n<p style=\"padding-left: 60px;\"> <a href=\"http:\/\/s1095387061.online-home.ca\/wp-content\/uploads\/2012\/06\/bp2_img9.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-41\" title=\"bp2_img9\" src=\"http:\/\/s1095387061.online-home.ca\/wp-content\/uploads\/2012\/06\/bp2_img9-300x132.jpg\" alt=\"\" width=\"300\" height=\"132\" \/><\/a> <\/p>\n<p style=\"padding-left: 60px;\">Command (in one line):<\/p>\n<pre style=\"padding-left: 60px;\"><strong>for \/f \"tokens=1,2,3,4,5 delims=\\ \" %a in (\"$(ESCAPE_SQUOTE(WMI(FileName)))\") do <\/strong><\/pre>\n<pre style=\"padding-left: 60px;\"><strong>cmd \/c \"%a\\%b\\%c\\%d\\defaultTraceArchive\\gzip.exe -9 %a\\%b\\%c\\%d\\defaultTraceArchive\\*.trc\"<\/strong><\/pre>\n<p>&nbsp;<\/p>\n<p>Now we have everything ready for the automated backup of the Default Trace files. <\/p>\n<p>&nbsp;<\/p>\n<p><strong>Final script:<\/strong><\/p>\n<p> <a href=\"http:\/\/s1095387061.online-home.ca\/2012\/06\/13\/backup-default-sql-server-trace-files\/dbrefresh_backupdefaulttracefiles\/\" rel=\"attachment wp-att-52\">DBRefresh_BackupDefaultTraceFiles.sql<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Default Trace first has been introduced in SQL Server 2005 and could be really useful for Database Administrators in troubleshooting. There is really great article about what Default Trace is and what you can do with it if you want &hellip; <a href=\"https:\/\/databaserefresh.com\/blog\/backup-default-sql-server-trace-files\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[9,8,7,3],"tags":[20,16,21],"class_list":["post-29","post","type-post","status-publish","format-standard","hentry","category-sql_auto","category-sql_back_recovery","category-sql_maint","category-sql_server","tag-backup","tag-sql-server","tag-trace-file"],"_links":{"self":[{"href":"https:\/\/databaserefresh.com\/blog\/wp-json\/wp\/v2\/posts\/29","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/databaserefresh.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/databaserefresh.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/databaserefresh.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/databaserefresh.com\/blog\/wp-json\/wp\/v2\/comments?post=29"}],"version-history":[{"count":33,"href":"https:\/\/databaserefresh.com\/blog\/wp-json\/wp\/v2\/posts\/29\/revisions"}],"predecessor-version":[{"id":31,"href":"https:\/\/databaserefresh.com\/blog\/wp-json\/wp\/v2\/posts\/29\/revisions\/31"}],"wp:attachment":[{"href":"https:\/\/databaserefresh.com\/blog\/wp-json\/wp\/v2\/media?parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/databaserefresh.com\/blog\/wp-json\/wp\/v2\/categories?post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/databaserefresh.com\/blog\/wp-json\/wp\/v2\/tags?post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}