Introduction
In a project where people work together in a team it is advisable to use Team Foundation Server for tracking and storing your Visual Studio project. When I start a project or working on my own on a project, I normally make a backup of the Visual Studio project every day or couple of days. In this blogpost I describe a way to automate the backup of a Visual Studio project by zipping it, tagging it, storing it in a backup folder, send a email when it succeeded or when it failed.The Script
Below an example of the Powershell script that executes a couple of steps:- If the same zipped backup file exists remove it.
- Try to compress the project.
- Copy the file to the backup folder.
- Send an email when it succeeds or when it fails.
#########################################
# Author : Hennie de Nooijer
# Description : Script copies the folder, make a zip with a date and time tag.
# Date : 20170430
#########################################
$source = "F:\Project"
$dDateTime =
$strDate = Get-Date -format yyyyMMdd
$strTime = Get-Date -format HHmmss
$destination = "F:\Backup\Project_" + $strDate + "_" + $strTime + ".zip"
If(Test-path $destination) {Remove-item $destination}
Add-Type -assembly "system.io.compression.filesystem"
Try
{
[io.compression.zipfile]::CreateFromDirectory($Source, $destination)
Send-MailMessage -From <putyouremailhere>
-To <putyouremailhere>
-Subject "Project Backup Succeeded"
-SmtpServer <SMTPServerHere>
}
Catch
{
Remove-Item $destination
$ErrorMessage = $_.Exception.Message
Send-MailMessage -From <putyouremailhere>
-To <putyouremailhere>
-Subject "Project Backup Failed"
-SmtpServer <SMTPServerHere>
-Body "The error message was $ErrorMessage"
Break
}
This is the result if you execute the script a couple of times (without _).
Schedule a windows task
In order to automate the backup of the project, a task in windows is needed. To do that, the Computer Management tool is used. There you can setup windows tasks. Below, the steps are explained step by step.Give the task a appropriate name and press Next.
Select the frequency of the windows task. In this example we choose Daily. Press Next.
Next step is the time of execution. Select the Start moment and by how many days the task should be recurring. Press Next.
Choose start a program and press Next.
The following step is an important step. Here you enter the command that executes the script. The command is Powershell and the parameters -file "D:\ps1\BackupProject.ps1". Press Next.
Here is the script again.
powershell -file "D:\ps1\BackupProject.ps1"
A warning appears and you can press on Yes.
Finally the task is ready and now press Next.
The task below is shown and is ready or usage.
With the context menu it is possible to run the task on an ad hoc manner..
Conclusion
This blogpost describes a Powershell script that automates zips and backups of a project.Greetz,
Hennie