---
title: GitHub Backup
date: 2025-04-14T16:23:53Z
modified: 2025-04-14T16:23:56Z
permalink: "https://techarticles.co.uk/github-backup/"
type: post
status: publish
excerpt: ""
wpid: 4746
categories:
  - Automation
  - Coding
  - Windows
timestamp: 2025-04-14T16:23:56Z
tags:
  - Automation
  - Coding
  - Windows
---

I have a reasonably large github account. I now have over 200 repos across private and public, and one of the items I’ve been meaning to do is to back up these repos more efficiently.

I already keep local copies of those I am actively tinkering with and can pull them down at a moment’s notice, but a more robust set-and-forget backup method was needed. After all, I should keep a copy of my repos just in case of a disaster.

So I looked into SAAS options, and given that most of my repos are open source or just plugins I have modified for various reasons, i.e faster activations, allowing activation from wp-config, etc. I wanted a simple, free method.

This is complicated because I am a Windows user, and we all know this makes this sort of thing harder. Most people expect development on a Mac, so I decided I would use the Git CLI. I already had it installed, but it’s really easy to do if you have Winget installed


```
winget install --id GitHub.cli
```

I am already authenticated as I use get on the command line and via Sublime Merge often, but then the next thing to do would be to authorise login using the following command.


```
gh auth login
```

Just follow the instructions for a browser authentication or an SSH authentication.

So, on my backup partition,, I set up a GitHub backup folder and wrote a batch script to allow me to automatically backup via clone all my repo’s

```
<span class="line"><span style="color: #F97583">@echo</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">off</span></span>
<span class="line"><span style="color: #F97583">setlocal</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">enabledelayedexpansion</span></span>
<span class="line"><span style="color: #F97583">set</span><span style="color: #E1E4E8"> BACKUP_DIR</span><span style="color: #F97583">=</span><span style="color: #E1E4E8">D:\GitHub Backup</span></span>
<span class="line"><span style="color: #F97583">mkdir</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">"</span><span style="color: #E1E4E8">%BACKUP_DIR%</span><span style="color: #9ECBFF">"</span><span style="color: #E1E4E8"> </span><span style="color: #79B8FF">2</span><span style="color: #F97583">></span><span style="color: #79B8FF">nul</span></span>
<span class="line"><span style="color: #F97583">cd</span><span style="color: #E1E4E8"> /d </span><span style="color: #9ECBFF">"</span><span style="color: #E1E4E8">%BACKUP_DIR%</span><span style="color: #9ECBFF">"</span></span>
<span class="line"></span>
<span class="line"><span style="color: #F97583">for</span><span style="color: #E1E4E8"> /f </span><span style="color: #9ECBFF">"tokens=1,2 delims=/"</span><span style="color: #E1E4E8"> </span><span style="color: #FFAB70">%%a</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">in</span><span style="color: #E1E4E8"> ('gh repo list --limit </span><span style="color: #79B8FF">1000</span><span style="color: #E1E4E8"> --json nameWithOwner --jq </span><span style="color: #9ECBFF">".[]|.nameWithOwner"</span><span style="color: #E1E4E8">') </span><span style="color: #F97583">do</span><span style="color: #E1E4E8"> (</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">set</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">"</span><span style="color: #E1E4E8">REPO_NAME</span><span style="color: #F97583">=</span><span style="color: #79B8FF">%%b</span><span style="color: #9ECBFF">"</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">if</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">exist</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">"</span><span style="color: #E1E4E8">!REPO_NAME!</span><span style="color: #9ECBFF">"</span><span style="color: #E1E4E8"> (</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">echo</span><span style="color: #E1E4E8"> Updating repository: !REPO_NAME!</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">cd</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">"</span><span style="color: #E1E4E8">!REPO_NAME!</span><span style="color: #9ECBFF">"</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">&&</span><span style="color: #E1E4E8"> git pull </span><span style="color: #F97583">&&</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">cd</span><span style="color: #E1E4E8"> ..</span></span>
<span class="line"><span style="color: #E1E4E8">    ) </span><span style="color: #F97583">else</span><span style="color: #E1E4E8"> (</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #F97583">echo</span><span style="color: #E1E4E8"> Cloning repository: !REPO_NAME!</span></span>
<span class="line"><span style="color: #E1E4E8">        git clone </span><span style="color: #9ECBFF">"https://github.com/</span><span style="color: #79B8FF">%%a</span><span style="color: #9ECBFF">/</span><span style="color: #79B8FF">%%b</span><span style="color: #9ECBFF">.git"</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">"</span><span style="color: #E1E4E8">!REPO_NAME!</span><span style="color: #9ECBFF">"</span></span>
<span class="line"><span style="color: #E1E4E8">    )</span></span>
<span class="line"><span style="color: #E1E4E8">)</span></span>
<span class="line"></span>
<span class="line"><span style="color: #F97583">echo</span><span style="color: #E1E4E8"> Backup complete.</span></span>
<span class="line"><span style="color: #F97583">pause</span></span>
<span class="line"></span>
```BAT (Batchfile)

Let it run overnight and about four gigabytes later my backup is done, I now need to run it once a day and it will pull me the latest update and its even better because [syncbackpro](https://ta0.uk/syncbackpro) now automatically backs this up to my archive drive too so I have two local copies one on NVME one on Hard Drive and one on GitHub.

This now runs automatically and is synced automatically too, another job checked off the list.