2 Gitea Mirror
tengel edited this page 2024-09-05 08:27:27 -05:00

Mission

Mirror repositories from one Gitea instance to another, using a private SSH key from a 3rd workstation / server. This keeps the SSH private keypart secure on your own servers and allows for a password to be set on it as desired.

Setup

  1. Create a new SSH keypair to use as a Deploy Key on both Gitea instances:
    ssh-keygen -t ed25519 -a 100 -f reposync -C reposync
    
  2. Add the public keypart to the source repository as a Deploy Key without Write access
  3. Add the public keypart to the destination repository as a Deploy Key with Write access
  4. Disable Issues, Projects and Pull Requests on the destination repository (optional)

Gitea allows the same SSH keypair to be used as Deploy Keys for multiple repositories. If using a password on the SSH keypair, leverage ssh-agent / ssh-add as needed.

Build Repos

One-time need, prepare the initial repository sync setup and test the initial mirroring.

buildmirrors.sh

#!/usr/bin/env bash

WDIR="${HOME}/repos/mirror"
cd "${WDIR}" || exit 1

REPOS="repo1 repo2 repo3 repo4"
USER="frank"
EMAIL="frank@thetank"
SSH="ssh -a -o IdentitiesOnly=yes -i ~/.ssh/reposync -F /dev/null"
SRC="git@src.gitea.server:frankthetank"
DST="git@dst.gitea.server:frankthetank"

for repo in ${REPOS}; do
  GIT_SSH_COMMAND="${SSH}" git clone --mirror ${SRC}/${repo}.git
  if [[ -d ${repo}.git ]]; then
    pushd "$(pwd)" >/dev/null
    cd ${repo}.git
    git config core.sshCommand "${SSH}"
    git config user.name "${USER}"
    git config user.email "${EMAIL}"
    git remote set-url --push origin ${DST}/${repo}.git
    git fetch -p origin
    git push --mirror
    popd >/dev/null
    sleep 1
  fi
done

Mirror Repos

To be used to keep the repos mirrored over time, whether automated or manual.

repomirror.sh

#!/usr/bin/env bash

WDIR="${HOME}/repos/mirror"
cd "${WDIR}" || exit 1

for repo in *.git/; do
  if [[ -d "${repo}" && ! -L "${repo}" ]]; then
    pushd "$(pwd)" >/dev/null
    cd "${repo}"
    git fetch -q -p origin
    git push -q --mirror
    popd >/dev/null
    sleep 1
  fi
done