/git-mirror-synchronizer

A shell script to help tracking git repository and sync the updates to mirror repository.

Primary LanguageShellMIT LicenseMIT

Git Mirror Synchronizer

  • This is a shell script trying to do the repetitive work of tracking a repository and sync it with another repo.
  • Principle: What this script does is pretty boring.
    1. It will initialize a local repository first.
    2. Get updates by running git pull <trace-branch>
    3. Push updates to another mirror repository listed configuration, or do nothing if there's no mirror repository to sync.

Scenario I've got a self-hosted git server and trying to backup some repositories from GitHub and keep them up-to-date automatically. A simple shell script triggered by crontab is enough to do the job at first. As the tracking list getting longer and I'd like this sync job to be neat and clean so here is this script. I would definitely be happy if this script also helps some of others save the troubles.

Usage

  • Default config file should be named as repo.json and put in data folder (already ignored in .gitignore).

    .
    ├── data
    │   └── repo.json
    ├── mirror-sync.sh
    ├── readme.md
    └── template.json
    
  • Initialize Mode: If a new git repository or mirror repository is added, run initialize mode to apply the setting. This operation is idempotence and can be run over and over again without messing up existed repository.

    $ ./mirror-sync.sh -m init
  • Synchronize Mode: Get update from upstream and sync mirror repositories.

    $ ./mirror-sync.sh -m sync
  • Setup crontab task with log output and run this script in daily basis.

    # recommended: use default config file
    0 1 * * * path-to-repo/mirror-sync.sh | tee -a /var/log/mirror-sync.log
    # specify a config file
    0 1 * * * path-to-repo/mirror-sync.sh -f path-to-data/repo.json | tee -a /var/log/mirror-sync.log

Dependency

  • jq is required to parse json configuration. Reference Here.

    # For Debian based distros like Ubuntu
    $ sudo apt-get install jq
    # For RHEL based distros 
    $ sudo dnf install jq

Config Format

  • The content of the configuration file is a json array.

    [
        {
        	"repoName":"<Repository name to display>",
        	"repoLocalPath":"<Local path to save the repository>",
        	"trackRemoteRepoName":"<Name of the upstream repo, origin if not altered>",
        	"trackRemoteRepoUrl":"<Url of the upstream repo>",
        	"mirror":[{
        		"repoName":"<Name of the mirror repo>",
        		"repoUrl":"<Url of the mirror repo>"
        	},{
        		"repoName":"<Name of the other mirror repo>",
        		"repoUrl":"<Url of the other mirror repo>"
        	}]
        }
    ]