Wednesday, January 9, 2013

My git setup at work - the synchronisation script


In my previous post, I detailed my git setup.
Although I can synchronize the backup manually, it is more convenient to have an automatic solution.

I decided for a scheduled task solution, based on a small powershell script. I could have chosen a post-commit hook.

I wanted to write the output of the git push command in windows event log. Thanks to powershell, it is straightforward. First, create a event source:
New-EventLog -LogName Application -Source "Git_Backup"
Note that you may need to launch this powershell command in an elevated powershell prompt.
Then, I created the following script in c:\repo_git\backup.ps1:
$gitDir = "c:\repo_git"
$ret = cmd /c "git --git-dir=$gitDir\.git --work-tree=$gitDirpush backup master 2>&1"

$id = $lastExitCode
$type = "Information"
if( $id -ne 0)
{
 $type = "Error"
}

Write-EventLog -LogName "Application" -Source "Git_Backup" -EntryType $type -Message $($ret -join [Environment]::NewLine) -EventId $id
The command line for the scheduled task will then be:
C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -NonInteractive -NoLogo -WindowStyle Hidden c:\repo_git\backup.ps1

No comments:

Post a Comment