Showing posts with label git. Show all posts
Showing posts with label git. Show all posts

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

Monday, January 7, 2013

My git setup at work


Although I do not code any more recently, I keep on the good habit of versioning.
Bascially, I have a local git repository on my desktop and all the files I work with (text, keynotes, excel, scripts...) are located inside.

I do changes and commit them as I would do with code. With git, it's simple and efficient. Git logs are also a good way to know what I worked on on a given date, very convenient for reporting.

The only remaining problem was related to backup. Indeed, if my laptop disk crashes, I would lose everything. I'm not allowed to set up a git repository at work, but our IT provides everybody with a shared private folder. The advantage of this shared folder is that IT performs backup every night.

The first thing I did, was just to move my directory on the shared folder. It worked great...until I wanted to access my files from a location without the network! So, the repository on the shared folder was not a solution.

I decided to create a backup git repository on the shared folder. This way, I would still work on the laptop and I would have a backup on the shared drive.

Here is the set of commands to do this. I'll suppose that the shared folder has been mounted on drive x:\ and that my repository is located in c:\repo_git :

First, we create the backup repository:
cd x:\
mkdir repo_git_backup
cd repo_git_backup
git init . --bare
bare option means that the repository will only contain git database files. You cannot perform any checkout on the repository. It's OK for my use case, because I just need a backup.

We can register our new repository, we will name it backup:
cd c:\repo_git
git remote add backup file://x:/repo_git_backup
Now everything is in place. In order to start a backup, just execute:
git push backup master
In case your git repository contains a lot of file or has a long history, it may take some time. Afterwards, it should be must faster.

Let's say that my disk crashes, I will lose all the data in c:\repo_git . How to I retrieve the files from the backup? Just create a folder and execute:
git clone file://x:/git_repo_backup
Everything will be back to the latest "git push".

In order to push automatically, I decided to create a scheduled task running the following powershell script, I'll describe it in a next post.