How to Automatically Create Build Backups in Visual Studio

How to Automatically Create Build Backups in Visual Studio

hrfstech

If you are a one man development team, you probably don’t really have the need for a full blown version control system, yet creating source code backups for each released version is undoubtedly important.

By leveraging the power of post-build events and a simple batch script, you can easily add the ability to have Visual Studio automatically create a source code backup for each release code build.
How it works
Our solution is simple: whenever a successful build event occurs, we have a batch script run which creates a compressed archive (optionally tagged and timestamped) of all files in the respective Visual Studio project folder.

That’s it. All you have to do is follow the steps below.

Setting up automatic build backups
First you will need to download and extract the batch script file from the link at the bottom of the article. Additionally, you will need the 7-Zip command line tool (this is included with a the ‘full’ version of the Project Build Backup script, or you can download it separately). In our example, we extracted these files to the directory “C:\Tools”, but any location will work.

Open your Visual Studio Project properties, by double-clicking on My Project under the respective project.

image

In the project properties, go to the Compile section.

image

In the bottom right corner, click the Build Events button.

image

In our case, we want to make a backup after a successful compile action. Make sure you have the option to run the post-build event “On successful build” and then click the Edit Post-build button.

image

The command below creates a build backup only for the compile of the Release configuration (this is what IF condition checks for) as, realistically, we probably don’t want to make a backup of each Debug/testing build. Additionally, the current timestamp will be appended (/D switch) with the backup file being in 7z file format (/7z) as opposed to zip. By adding the /T “$(ConfigurationName)” as a parameter, we are appending the build type (Release in this case) to the name of the backup file.

IF “$(ConfigurationName)” == “Release” CALL C:\Tools\ProjectBuildBackup.bat “$(SolutionDir)” “$(ProjectDir)” “$(ProjectName)” /T “$(ConfigurationName)” /D /7z

Using the Macros button, you can have Visual Studio prefill project specific information so no hardcoding is required. You can adjust this command as needed (especially the location of the batch file), but the first three parameters will likely not need to be changed.

It is important to keep in mind that post-event operations run regardless of the project configuration selected. This is why we need to add the IF “$(ConfigurationName)” == “Release” statement – otherwise the backup action would occur on every successful build event.

image

Once you finish your command and apply it, the command string should appear in the Post-build events section.

Note that while the “CALL” command is not technically required, it is highly recommended, as if this is omitted then any events added after this may not execute.

image

Now whenever you run a compile/build with your project in the Release configuration, you will see the output from the build backup operation.

image

[…]

image

Each successful Release build creates a new timestamped archive with the solution folder in a subdirectory, “Builds” (which can be custom defined with the /O switch if needed).

image

The contents of each backup is the full Visual Studio project – source files, configuration settings, compiled binaries, and all – which makes this a true point in time backup.

image

Not a replacement for a full version control system
In closing, we just want to reiterate that this tool is not intended to replace a full blown version control system. It is simply a useful tool for developers to create snapshots of their project’s source code after each compilation.

In the event you ever have to go back and examine a prior version, having a ready-to-use (just extract to a new directory) project file for a point in time compilation can really come in handy.

Links
Download Project Build Backup Script

Download 7-Zip Command Line Tool (Note – the 7za utility is also bundled with a download from the Project Build Backup script)