A blog about SQL Server, SSIS, C# and whatever else I happen to be dealing with in my professional life.

Find ramblings

Thursday, March 6, 2014

Biml - Maintenance Cleanup Task

Biml - Maintenance Cleanup Task

The Maintenance Cleanup Task removes artifacts from maintenance plans, backups, reports, etc. As with other Maintenace Tasks, this requires an ADO.NET Connection Manager.

<Biml xmlns="http://schemas.varigence.com/biml.xsd">
    <Connections>
        <AdoNetConnection Name="CM_ADO_DB" ConnectionString="Data Source=localhost\dev2012;Integrated Security=SSPI;Connect Timeout=30;Database=msdb;" Provider="SQL"  />
    </Connections>

    <Packages>
        <Package ConstraintMode="Linear" Name="Task_MaintenanceCleanup">
            <!-- 
                Backup\InstanceName\DBName\DIFF 
                Backup\InstanceName\DBName\FULL 
                Backup\InstanceName\DBName\LOG 
            -->
            <Variables>
                <Variable Name="FileExtension" DataType="String">bacon</Variable>
                <Variable Name="FolderBase" DataType="String">J:\Backup</Variable>
                <Variable Name="InstanceName" DataType="String">WESTMARCH$DEV2012</Variable>
                <Variable Name="DatabaseName" DataType="String">Adventureworks2012</Variable>
                <Variable Name="FolderPath" DataType="String" EvaluateAsExpression="true">@[User::FolderBase] + "\\" + @[User::InstanceName] + "\\" + @[User::DatabaseName] </Variable>
            </Variables>
            <Tasks>
                <!--
                    http://stackoverflow.com/questions/11571002/can-someone-help-explain-some-of-the-new-ssis-2012-properties 
                -->
                <MaintenanceCleanup 
                    ConnectionName="CM_ADO_DB" 
                    Name="MC Clean backup files" 
                    DeleteFromAllBricks="true"
                    AgeBased="true"
                    FileTypeSelected="BackupFiles"
                    OlderThanTimeUnits="4"
                    OlderThanTimeUnitType="Weeks"
                    >
                    <!-- Delete all the bacon -->
                    <FolderAndExtension 
                        Folder="C:\DrivenByVariable" 
                        FileExtension="VariableToo"
                        IncludeFirstLevelSubfolders="true"
                    >
                    </FolderAndExtension>
                    <Expressions>
                        <Expression PropertyName="FolderPath">@[User::FolderPath]</Expression>
                        <Expression PropertyName="FileExtension">@[User::FileExtension]</Expression>
                    </Expressions>
                </MaintenanceCleanup>
            </Tasks>
        </Package>
    </Packages>
</Biml>

Result

A package is created with Variables to control the FileExtension and FolderPath that are removed. I'm basing the three Variables that build my FolderPath variable based on the default output of Ola's Maintenance plans.

MC Clean Backup Files

Great googly-moogly, someone's attempting to get rid of all the bacon! Well, at least any Backup files that are in the first-level subfolder, with an extension of bacon, that are older than 4 weeks.

Expressions

As you can see, I've applied an Expression to control both FileExtension and FolderPath based on our user Variables.

No comments: