Skip to content

Instantly share code, notes, and snippets.

@peterjc
Created August 17, 2018 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save peterjc/2f1bf0633afbbbb93e074625758fa9a7 to your computer and use it in GitHub Desktop.
Save peterjc/2f1bf0633afbbbb93e074625758fa9a7 to your computer and use it in GitHub Desktop.
Script to simplify setting up git repository for cron-based mirroring of a GitHub fork
#!/bin/bash
set -euo pipefail
# See https://blastedbio.blogspot.co.uk/2016/05/sync-github-mirror-with-cron.html and
# https://gist.github.com/peterjc/eccac1942a9709993040425d33680352 for mirroring script
#
# Usage:
#
# 1. Fork upstream repo under HuttonICS, disable wiki, projects, issues etc. Protect master branch.
# 2. Run:
#
# ./mirror_setup.sh repo-name https://github.com/upstream-owner/repo-name.git
#
# 3. Copy and paste repo-name_key.pub into GitHub fork settings as deploy key with write permissions
# 4. Add crontab entry
#
#
#The script does this:
#
# 1. ssh-keygen -t rsa -b 4096 -C "repo-name key" -f repo-name_key -N ""
# 2. Clone upstream repo using HTTPS, cd repo-name
# 3. git remote add mirror *HuttonForkUsingGit*
# 4. git fetch mirror
name="$1"
upstream="$2"
if [ ! -f "${name}_key" ]; then
echo "Generating ${name} SSH key"
ssh-keygen -t rsa -b 4096 -C "huttonics/${name} deployment (Peter's iMac)" -f ${name}_key -N ""
fi
if [ ! -d "${name}/.git" ]; then
echo "Cloning upstream ${name} repository ${upstream}"
git clone "$upstream" "$name"
cd $name
git remote add mirror git@github.com:HuttonICS/${name}.git
cd ..
fi
echo "======================================================="
echo
echo "For the GitHub deployment key:"
echo
cat ${name}_key.pub
echo
echo "Paste this into https://github.com/HuttonICS/${name}/settings/keys"
echo
echo "======================================================="
echo
echo "For the cron tab:"
echo "~/cron/mirror_git ~/cron/${name} ~/cron/${name}_key ~/cron/${name}.log"
@peterjc
Copy link
Author

peterjc commented Jun 25, 2021

This has been superseded by https://gist.github.com/peterjc/899a6d24badd47d6305ddc5e299fd4bd which uses the GitHub API with a personal access token allowing it find repositories and add the deployment keys automatically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment