☕️ Let's automate a backup process of your GitHub accounts, organizations & repositories
Tags: github, tutorial, beginners, docker
Published: 2020-07-21 14:42:39

## Introduction

Good day/night, DEV people! 😉 I declare openly: [GitHub](https://github.com/koddr) is my favorite professional social network to communicate and share my experience with other developers around the World.

But sometimes, when servers crash (_which have increased dramatically in the last six months_), I would like to have backups of all my projects on my work computer.

> I'm talking about a **full backup** with all history, commits, pull-requests and releases of GitHub repository... not a simple `git clone` command!

— _So, what do I need to start backing up my GitHub account?_

Let's work it out together! 👍

<a name="toc"></a>
### 📝 Table of contents

- [The problem](#ch-1)
- [Solution](#ch-2)

<a name="ch-1"></a>
## The problem

If you've never seen [GitHub Status](https://www.githubstatus.com/) page, I'm very happy for you! Because, it means **I caught Internal Server Error (500)**, while working with GitHub... 😨

> All the useful information, like "_what's broken_" or "_when it's fixed_", will be on this page. Also, you can subscribe to official [Twitter](https://twitter.com/githubstatus) account.

![github status page](https://dev-to-uploads.s3.amazonaws.com/i/8epofuri2bo7njjv0syt.png)

Yes, we spend thousands of hours (_a la money_) to write, support and improve projects. But let's not forget that the project **doesn't** really belong to us 100 percent, if it was uploaded to _commercial_ service.

> 💭 GitHub is a commercial service for working with `git` (which also belongs to [Microsoft Corporation](https://news.microsoft.com/announcement/microsoft-acquires-github/)), even if it has free plans.

Any downtime or rollback of such commercial service, can play a bad joke on you. Therefore, it's a **good reason** to have a full backup of all your projects. And on the whole, the backup hasn't bothered anyone yet!

[↑ Table of contents](#toc)

<a name="ch-2"></a>
## Solution

- Install Python (`3.5` and later) and Ansible (`2.9` and later):

```bash
brew install python3 ansible
```

> ☝️ This is example for `macOS` using the [Brew](https://brew.sh/) package manager. How to install Python and Ansible on your system, please ask Google.

- Download (or `git clone`) the **Useful Ansible playbooks** collection and go into its folder:

```bash
git clone https://github.com/truewebartisans/useful-playbooks.git

cd useful-playbooks
```

{% github truewebartisans/useful-playbooks no-readme %}

- Go to your [GitHub settings](https://github.com/settings/tokens) and generate a new **personal access token** with needed scopes.

![github personal access token](https://dev-to-uploads.s3.amazonaws.com/i/7osxx23vzf8vidvfb21p.png)

- Create `.env` file with the following structure:

```ini
# GitHub users or organizations login names, 
# separated with commas (without spaces).
USERS=user123,org123

# GitHub personal access token.
GITHUB_TOKEN=0000000000000000000000000

# Max count of a saved backup files.
MAX_BACKUPS=10

# Time to delay.
# - d: days
# - h: hours
# - m: minutes
# - s: seconds
DELAY_TIME=1d

# Timezone for DELAY_TIME.
# See: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
TIME_ZONE=America/Chicago
```

> ☝️ This is **required**, because the Docker image, what we're using in this playbook ([koddr/github-backup-automation](https://hub.docker.com/r/koddr/github-backup-automation)), rest on these environment variables.

- Create a new virtual server with a pre-installed Docker (on [DigitalOcean](https://m.do.co/c/b41859fa9b6e), for example, or on your favorite cloud provider. The minimum droplet's plan for _$5/month_, should be more than enough).

- And, finally, run Ansible playbook:

```bash
ansible-playbook \
                  github_backup-playbook.yml \
                  --user <USER> \
                  --extra-vars "host=<HOST>"
```

> `<USER>` remote user's username (for example, `root`)
> `<HOST>` hostname in your inventory (from `/etc/ansible/hosts` file)

All saved backups will be located at `/var/github-backup` folder of your remote server. That's it! 🎉

[↑ Table of contents](#toc)

<a name=""></a>
## Photos by

- GitHub Status page https://www.githubstatus.com

<a name=""></a>
## P.S.

If you want more articles (like this) on this blog, then post a comment below and subscribe to me. Thanks! 😻

And of course, you can help me make developers' lives even better! Just connect to one of my projects as a contributor. It's easy!

My projects that need your help (and stars) 👇

- 🔥 [gowebly][gowebly_url]: A next-generation CLI tool for easily build amazing web applications with Go on the backend, using htmx & hyperscript and the most popular atomic/utility-first CSS frameworks on the frontend.
- ✨ [create-go-app][cgapp_url]: Create a new production-ready project with Go backend, frontend and deploy automation by running one CLI command.
- 🏃 [yatr][yatr_url]: Yet Another Task Runner allows you to organize and automate your routine operations that you normally do in Makefile (or else) for each project.
- 📚 [gosl][gosl_url]: The Go Snippet Library provides snippets collection for working with routine operations in your Go programs with a super user-friendly API and the most efficient performance.
- 🏄‍♂️ [csv2api][csv2api_url]: The parser reads the CSV file with the raw data, filters the records, identifies fields to be changed, and sends a request to update the data to the specified endpoint of your REST API.
- 🚴 [json2csv][json2csv_url]: The parser can read given folder with JSON files, filtering and qualifying input data with intent & stop words dictionaries and save results to CSV files by given chunk size.

<!-- Footer links -->

[gowebly_url]: https://github.com/gowebly
[cgapp_url]: https://github.com/create-go-app
[yatr_url]: https://github.com/koddr/yatr
[gosl_url]: https://github.com/koddr/gosl
[csv2api_url]: https://github.com/koddr/csv2api
[json2csv_url]: https://github.com/koddr/json2csv