A simple automation tool that keeps a directory full of git mirrors synchronized with their remote repositories.
Git-Mirror automates the process of maintaining multiple git repository mirrors by automatically fetching updates from remote repositories. Perfect for backup purposes, continuous integration pipelines, or maintaining local copies of important repositories.
- Automated Updates: Keeps all git mirrors in a directory synchronized with their remotes
- Cron Integration: Designed to work seamlessly with cron for scheduled updates
- Simple Setup: Minimal configuration required
- Logging Support: Easy integration with system logging
- Lightweight: Uses standard git commands with minimal overhead
- git - For performing git operations
- cron - For automated scheduled updates (optional but recommended)
- Unix/Linux environment - Bash-compatible shell
- Clone this repository or download the
git-mirrorscript - Make the script executable:
chmod +x git-mirror
- Place the script in your PATH (e.g.,
/usr/local/bin/or~/bin/)
To start mirroring a new git repository:
# Create a directory for your mirrors
mkdir ~/gitmirrors
cd ~/gitmirrors
# Clone the repository as a mirror
git clone --mirror [email protected]:username/repository-name.gitExample:
cd ~/gitmirrors
git clone --mirror [email protected]:torvalds/linux.git
git clone --mirror https://github.com/git/git.gitUpdate all mirrors in a directory:
git-mirror ~/gitmirrors/Set up automatic daily updates by adding this to your crontab (crontab -e):
# Update git mirrors daily at 4:32 AM
32 4 * * * /path/to/git-mirror ~/gitmirrors/ >>/var/log/git-mirror.log 2>&1Alternative scheduling examples:
# Every 6 hours
0 */6 * * * /path/to/git-mirror ~/gitmirrors/ >>/var/log/git-mirror.log 2>&1
# Every Monday at 2 AM
0 2 * * 1 /path/to/git-mirror ~/gitmirrors/ >>/var/log/git-mirror.log 2>&1Your mirror directory should look like this:
~/gitmirrors/
├── repository1.git/
├── repository2.git/
├── another-repo.git/
└── ...
Each subdirectory should be a bare git repository created with git clone --mirror.
The script supports standard output redirection for logging:
# Log to file
git-mirror ~/gitmirrors/ >> /var/log/git-mirror.log 2>&1
# Log with timestamps
git-mirror ~/gitmirrors/ 2>&1 | while IFS= read -r line; do echo "$(date): $line"; done >> /var/log/git-mirror.log- The script scans the specified directory for git repositories
- For each repository found, it runs
git remote updateto fetch all remote branches and tags - Updates are logged to stdout/stderr for monitoring
- Failed updates are reported but don't stop the processing of other repositories
Permission denied errors:
- Ensure your SSH keys are properly configured for git operations
- Check that the script has execute permissions
Repository not updating:
- Verify the remote URLs are still valid:
git remote -v - Check network connectivity to the remote repositories
- Ensure the mirror was created with
--mirrorflag
Cron job not running:
- Check cron service is running:
systemctl status cron - Verify crontab syntax:
crontab -l - Check system logs:
/var/log/syslogor/var/log/cron
To add a new remote to an existing mirror:
cd ~/gitmirrors/repository.git
git remote add new-remote https://github.com/user/repo.gitTo verify mirror status:
cd ~/gitmirrors/repository.git
git remote -v
git branch -r- Backup: Maintain local backups of important repositories
- CI/CD: Keep local mirrors for faster builds and deployments
- Network-limited environments: Reduce bandwidth usage by updating mirrors once
- Development: Maintain stable local copies of dependencies
- Archival: Preserve repositories that might be removed from hosting services
Contributions are welcome! Please feel free to submit issues or pull requests.
This project is available under standard open source terms. Please check the repository for specific license information.
Note: This tool is designed for mirroring public repositories or repositories you have access to. Ensure you comply with the terms of service of the git hosting platforms you're mirroring from.