Backup Public GitHub Gists

To manage my code snippets I’m using GitHubGist connected with Gistbox.

Sadly none of this services providing a backup nor a download function for the gist files. That’s why I came upwith the idea to download them with PowerShell script.

For first my script only can download public gists, because I don’t know how to implement an authentication, luckily each of my gists is public. I recommend you to do the same, it’s the idea of OpenSource.

So here’s the script:

[System.Reflection.Assembly]::LoadWithPartialName("System.Web.Extensions")

function Get-GitHubGists{

    param(
        [string]$UserUrl
    )

    # set up request
    $WebRequest = [System.Net.WebRequest]::Create($UserUrl)
    $WebRequest.Method ="GET"
    $WebRequest.ContentLength = 0

    # get response
    $WebRequest = $WebRequest.GetResponse()
    $WebRequest = new-object System.IO.StreamReader($WebRequest.GetResponseStream())
    $WebResponse = $WebRequest.ReadToEnd()

    # convert json
    $JavaScriptSerializer = New-Object System.Web.Script.Serialization.JavaScriptSerializer
    $GitHubGists = $JavaScriptSerializer.DeserializeObject($WebResponse)

    # download github gists
    $GitHubGists | %{

        if(Test-Path $_.id){

            cd ($_.id)
            Write-Host "Update GitHubGist: $($_.description)"
            git pull
            cd ..

        }else{

            Write-Host "Cloning GitHubGist: $($_.description)"
            git clone ($_.git_pull_url)
        }
    }
}

Get-GitHubGists -UserUrl https://api.github.com/users/janikvonrotz/gists?page=1
Get-GitHubGists -UserUrl https://api.github.com/users/janikvonrotz/gists?page=2
Get-GitHubGists -UserUrl https://api.github.com/users/janikvonrotz/gists?page=3

Latest version: https://gist.github.com/6622205

Leave a Reply