Backup github and restore when github down

I need to backup github repo and restore it to another git server (eg: gitlab/bitbucket etc, with all those issue/pull request/tag/branches restore)

Assumption:

  • The backup will not be sync to another repo. It will only be restore to another git repo when github is down
  • During restore time to another git server, GITHUB service is completely down and not accessible.
  • Not depending on any other paid backup solution service

I am currently using git clone --bare to do backup but I am unsure if it is dependent on GITHUB service availability when restoring to gitlab/bitbucket etc

Or if there is a better way to do backup (Eg: git clone --mirror, git bundle)

To back up a GitHub repository and restore it to another Git server (like GitLab or Bitbucket) with all associated metadata (issues, pull requests, tags, branches, etc.), here’s a comprehensive approach:

1. Backup the Repository

a. Clone the Repository with --mirror

  • The git clone --mirror command is more appropriate for creating a backup than --bare. The --mirror option ensures that you get all refs (branches, tags, etc.) and remotes.
  • This command also backs up all the configuration settings and hooks associated with the repository.
git clone --mirror https://github.com/username/repository-name.git
  • This will create a directory named repository-name.git that contains all the repository data, including refs and hooks.

b. Backing Up Issues and Pull Requests

  • Git itself does not store issues, pull requests, and other GitHub-specific data like comments and metadata.
  • Use the GitHub API to download these issues and pull requests as JSON files:
# For issues
curl -H "Authorization: token YOUR_GITHUB_TOKEN" \
"https://api.github.com/repos/username/repository-name/issues?state=all" \
-o issues.json

# For pull requests
curl -H "Authorization: token YOUR_GITHUB_TOKEN" \
"https://api.github.com/repos/username/repository-name/pulls?state=all" \
-o pull_requests.json
  • This method assumes that GitHub is accessible when you make the backup. You’ll need to ensure that you regularly back up this data.

c. Backup Tags and Releases

  • Tags are already included in the --mirror clone. Releases, however, are stored as GitHub metadata, and you’ll need to back them up using the GitHub API:
curl -H "Authorization: token YOUR_GITHUB_TOKEN" \
"https://api.github.com/repos/username/repository-name/releases" \
-o releases.json

2. Restoring the Repository

a. Restoring to a New Git Server

  • To restore the repository to another Git server like GitLab or Bitbucket, you can push the mirrored repository

cd repository-name.git
git push --mirror https://gitlab.com/username/new-repository-name.git

  • This will push all refs (branches, tags, etc.) and the repository history to the new server.

b. Restoring Issues, Pull Requests, and Releases

  • For issues, pull requests, and releases, you’ll need to use the API of the target server (GitLab, Bitbucket, etc.) to recreate them from the JSON files you backed up.
  • For GitLab, for example, you can use their API to create issues and merge requests.

3. Regular Backup Schedule

  • Set up a cron job or a scheduled task to regularly perform the backup steps to ensure that you always have an up-to-date backup.

4. Considerations

  • Automation: You can automate the backup and restore process using scripts to ensure consistency and reduce manual effort.
  • Service-Specific Data: Some GitHub-specific features might not have direct equivalents in GitLab or Bitbucket (e.g., GitHub Actions). You’ll need to manually handle such cases.
  • Testing: Regularly test the restore process to ensure that you can recover everything as expected.

By using this approach, you’ll be able to back up your GitHub repository and restore it to another Git server without relying on GitHub’s availability during the restoration process.