- 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.
- It will initialize a local repository first.
- Get updates by running
git pull <trace-branch>
- 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.
-
Default config file should be named as
repo.json
and put indata
folder (already ignored in.gitignore
).. ├── data │ ├── repo.json │ └── github-access-token ├── mirror-sync.sh ├── readme.md └── template.json
-
Github access token should be put in
github-access-token
file underdata
folder, otherwise it's very likely to reach the api's rate limit. -
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
-
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
-
The content of the configuration file is a json array.
[ { "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>" }], "downloadRelease": "<Download latetst github release, boolean>", "releaseStoragePath": "<Override default storage location for downloaded artifacts>", "excludeKeywords": "<Keywords to exclude the artifacts, split with ','>" } ]