Skip to content

tsaavik/Git-Mirror

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 

Repository files navigation

Git-Mirror

A simple automation tool that keeps a directory full of git mirrors synchronized with their remote repositories.

Overview

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.

Features

  • 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

Requirements

  • git - For performing git operations
  • cron - For automated scheduled updates (optional but recommended)
  • Unix/Linux environment - Bash-compatible shell

Installation

  1. Clone this repository or download the git-mirror script
  2. Make the script executable:
    chmod +x git-mirror
  3. Place the script in your PATH (e.g., /usr/local/bin/ or ~/bin/)

Usage

Setting Up Repository Mirrors

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.git

Example:

cd ~/gitmirrors
git clone --mirror [email protected]:torvalds/linux.git
git clone --mirror https://github.com/git/git.git

Running the Mirror Update

Update all mirrors in a directory:

git-mirror ~/gitmirrors/

Automated Updates with Cron

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>&1

Alternative 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>&1

Directory Structure

Your 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.

Logging

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

How It Works

  1. The script scans the specified directory for git repositories
  2. For each repository found, it runs git remote update to fetch all remote branches and tags
  3. Updates are logged to stdout/stderr for monitoring
  4. Failed updates are reported but don't stop the processing of other repositories

Troubleshooting

Common Issues

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 --mirror flag

Cron job not running:

  • Check cron service is running: systemctl status cron
  • Verify crontab syntax: crontab -l
  • Check system logs: /var/log/syslog or /var/log/cron

Manual Repository Maintenance

To add a new remote to an existing mirror:

cd ~/gitmirrors/repository.git
git remote add new-remote https://github.com/user/repo.git

To verify mirror status:

cd ~/gitmirrors/repository.git
git remote -v
git branch -r

Use Cases

  • 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

Contributing

Contributions are welcome! Please feel free to submit issues or pull requests.

License

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.

About

Automatically keep a directory full of git mirrors up to date with their remotes

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages