MSBuild task to automagically backup your Sitecore webroot Folder before VS Publish

MSBuild tasks are very interesting and can be effective if you put them to use in the right situation. Think of this scenario:

How many times have you run a Visual Studio filesytem publish to later realise your publish has blown up the Sitecore instance? You then tell yourself, "I should have backed up the webroot folder". Obviously there are many ways to restore your instance,

1. Open the scwdp file, pull all folders/files and copy over to webroot

2. Install everything from beginning then do VS studio publish

3. Ask your co-developer to add the webroot folder to a shared drive and copy-over? 

Any other crazy ideas? Just add-on!

Then there is this preventive approach: What if there was a way to backup your webroot folder automatically before you press the publish button?

This is one of the scenarios you can put the MSBuild task to use:

Just add this AfterBuild task to your webroot .csproj:

<Target Name="AfterBuild">

<Message Text="Checking backup Publish folder - $(PublishUrl) at c:\sctemp" Importance="High" />

<Exec Command="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NonInteractive -executionpolicy Unrestricted -command &quot;&amp; {$root=Split-Path -Path $(PublishUrl) -Leaf; if (-not(Test-Path -Path c:\sctemp\$(root))) {Copy-Item -Path $(PublishUrl) -Destination c:\sctemp -Recurse }} &quot;" LogStandardErrorAsError="True" ContinueOnError="False" WorkingDirectory="$(MSBuildProjectDirectory)" />

</Target>


All it does is, based on the publish folder path, gets the content of webroot then dumps all files/folders to a sctemp folder after creating the sctemp folder.

In further builds, if the webroot folder exists within c:\sctemp folder, the script doesn't disturb the same. Isn't that cool?

First time run :

                                  Screen shot just for build time  and generated backup folder

Further runs:

                                                    Screen shot just for build time 

Obviously, you must remember to add this script to AfterBuild task of your website root .csproj file. One way to not miss this process is confluence as part of project setup instructions! 

You can also tweak it by adding a timestamp/Guid to the backup folder. Careful! This runs on every build click, creates a guid-based folder, copies the backup. Hence, affects build time + fills up disk space so, remember to clear folders and maybe remove the script once your local environment has stabilised:

<Target Name="AfterBuild">

<Message Text="Checking backup Publish folder - $(PublishUrl) at c:\sctemp" Importance="High" />

<Exec Command="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NonInteractive -executionpolicy Unrestricted -command &quot;&amp; {$ts=New-Guid;$dest='c:\sctemp\' + $ts; Copy-Item -Path $(PublishUrl) -Destination $dest -Recurse } &quot;" LogStandardErrorAsError="True" ContinueOnError="False" WorkingDirectory="$(MSBuildProjectDirectory)" />

</Target>

Screen shot just for build time and Guid folders

Possibilities are limitless with msbuild tasks!

In case if we just look at backup as an option for webroot folder, we could include this as part of gulp task since we could introduce a switch then.

Comments

Popular Posts