How to Back up Your GitHub Repositories
Many developers use GitHub to share their code. It is convenient, offers a lot of features and works without any noticeable problems for years. Did you ever thought on creating a backup for your repositories?
Git helps us to ignore that task a bit. With every git clone we create a full copy of the whole repository, what by all means is a backup. Why should we then do some extra work to get another backup? It is true that Git creates a copy. But do you have a current copy of all your repositories on GitHub somewhere easy to find? Not only the few repositories you worked on in the last month, but everything you have on GitHub up to its newest commit. Probably not.
Create bare repositories
Git allows us to clone bare repositories, that are repositories with everything from the .git folder but no code files checked out. That makes them a great solution for backups, and clearly marks them as a place where we should not change anything.
We can create a bare repository with this command:
git clone --mirror [email protected]:jgraber/Blog_Snippets.git
The "new" --mirroroption is a shorthand that does the --bare option and adds all the remotes in one operation.
I prefer to use the SSH method to clone the repository, then that way I can use a SSH key to log in and do not need to fiddle with passwords in each repository.
Inside the Blog_Snippets.git folder we get the content that is usually inside the .git folder:
$ ll
total 0
drwxrwxrwx 1 jg jg 4096 Jun 28 11:08 ./
drwxrwxrwx 1 jg jg 4096 Jun 28 11:08 ../
-rwxrwxrwx 1 jg jg 21 Jun 28 11:08 HEAD*
-rwxrwxrwx 1 jg jg 201 Jun 28 11:08 config*
-rwxrwxrwx 1 jg jg 73 Jun 28 11:08 description*
drwxrwxrwx 1 jg jg 4096 Jun 28 11:08 hooks/
drwxrwxrwx 1 jg jg 4096 Jun 28 11:08 info/
drwxrwxrwx 1 jg jg 4096 Jun 28 11:08 objects/
-rwxrwxrwx 1 jg jg 294 Jun 28 11:08 packed-refs*
drwxrwxrwx 1 jg jg 4096 Jun 28 11:08 refs/
Update the backups
The bare repositories do not update themselves. If we keep them as they are, we get a backup from the time we cloned them. That is better than nothing, but not good enough.
Instead, we can create a little script that runs through the folder in which we put all our repositories. There it does a git fetch to get all the changes into our local repository:
We can run the script whenever we want to make a backup or put it into a tool like Cron:
If you need help in figuring out the time when your script should run, go to crontab.guru that will help you with a textual aide to find the right numbers:

Conclusion
Git and GitHub are great tools. But as with everything else, when it is important, we should make a backup. As we all know from painful experiences, when the data is lost, it is gone for good. Often it does not matter how much effort we put in; we are not getting it back. Therefore, make backups as long as the data is there – even when you use Git.