镜像 github 项目到本地 gitlab 环境

mirror 脚本:

#!/bin/bash -x
# runsisi AT hust.edu.cn

export LANG=C
export LC_ALL=C

# mirror.conf
MIRROR_CONF="mirror.conf"
export HTTPS_PROXY=http://proxy123.example.com:12345
export HTTP_PROXY=$HTTPS_PROXY
export NO_PROXY=src.example.com

# parse a string like this:
# src [localdir] mirror [refspec], e.g.,
# https://github.com/ceph/ceph.git ceph http://src.example.com/runsisi/ceph.git --tags master firefly giant hammer
# or
# https://github.com/ceph/ceph.git ceph-ci http://src.example.com/runsisi/ceph.git --tags
# or
# https://gitbhu.com/ceph/ceph.git http://src.example.com/runsisi/ceph.git

do_parse() {
    local line_="$1"
    local i_=0

    line_=$(echo -e $line_ | sed -e 's/^\s*//' -e 's/\s*$//')
    line_=$(echo -e $line_ | sed 's/\s\+/ /g')
    [ ${line_:0:1} = "#" ] && return 1
    ((++i_))
    src=$(echo -e $line_ | cut -d' ' -f $i_)
    ((++i_))
    dir=$(echo -e $line_ | cut -d' ' -f $i_)
    if echo -e $dir | grep -q "^\(https\?\|ftp\|ssh\|file\)://"; then
        ((--i_))
        dir=$(echo -e "$src" | grep -o "[^/]*$")
    fi
    ((++i_))
    mirror=$(echo $line_ | cut -d' ' -f $i_)
    ((++i_))
    refspec=$(echo $line_ | cut -d' ' -f $i_-)
}

do_mirror() {
    src=""
    dir=""
    mirror=""
    refsepc=""

    local dir_=""

    if ! do_parse "$1"; then
        # do nothing
        return 0
    fi

    echo -e "\n- [mirror]: $1"

    # TODO: validate parameters

    export https_proxy="$HTTPS_PROXY"
    export http_proxy="$HTTP_PROXY"
    export no_proxy="$NO_PROXY"

    # if dir does not exist, then clone it
    if [ ! -d "$dir" ]; then
        git clone --mirror $src $dir
        (
        cd "$dir"
        git remote set-url --push origin "$mirror"
        if [ -n "$refspec" ]; then
            git config remote.origin.mirror false
        fi
        )
    else # if dir does exist, then update it
        (
        cd "$dir"
        git remote update
        )
    fi

    # push to mirror
    (
    cd "$dir"

/usr/bin/expect <<-EOF
    # do not quote $refspec
    spawn git push --force origin $refspec
    expect "Username"
    send "runsisi\r"
    expect "Password"
    send "password-abc\r"
    expect eof
EOF
    )

    # sleep for a while
    sleep 5
}

./clean

export -f do_parse
export -f do_mirror

xargs -a "$MIRROR_CONF" -I line bash -e -c 'do_mirror "$@"' _ line

unset -f do_parse
unset -f do_mirror

echo -e "\n-- OK --"

clean 脚本:

#! /bin/bash

for i in $(ls); do
(
    if [ -d $i ]; then
        cd $i
        rm -f gc.log
        git prune
    fi
)
done

mirror.conf 配置文件:

### ceph core modules ###

# ceph/ceph -> runsisi/ceph
https://github.com/ceph/ceph.git runsisi-ceph.git http://src.example.com/runsisi/ceph.git master jewel luminous mimic

# ceph/ceph -> runsisi/ceph-dev
https://github.com/ceph/ceph.git runsisi-dev.git http://src.example.com/runsisi/ceph-dev.git --tags master hammer jewel luminous

### submodules ###

# ceph/ceph-object-corpus
https://github.com/ceph/ceph-object-corpus.git http://src.example.com/ceph/ceph-object-corpus.git

# ceph/rocksdb
https://github.com/ceph/rocksdb.git http://src.example.com/ceph/rocksdb.git

# ceph/rocksdb -> runsisi/rocksdb
https://github.com/ceph/rocksdb.git runsisi-rocksdb.git http://src.example.com/runsisi/rocksdb.git --tags master luminous

# ceph/ceph-erasure-code-corpus
https://github.com/ceph/ceph-erasure-code-corpus.git http://src.example.com/ceph/ceph-erasure-code-corpus.git

# ceph/dpdk
https://github.com/ceph/dpdk.git http://src.example.com/ceph/dpdk.git

# spdk/dpdk
https://github.com/spdk/dpdk.git spdk-dpdk.git http://src.example.com/spdk/dpdk.git

# ceph/spdk
https://github.com/ceph/spdk.git http://src.example.com/ceph/spdk.git

# ceph/googletest
https://github.com/ceph/googletest.git http://src.example.com/ceph/googletest.git

# google/googletest
https://github.com/google/googletest.git google-googletest.git http://src.example.com/google/googletest.git

### others ###

# saltstack/salt
https://github.com/saltstack/salt.git http://src.example.com/runsisi/salt.git --tags develop 2017.7 2017.7.1

# open-iscsi/tcmu-runner
https://github.com/open-iscsi/tcmu-runner.git http://src.example.com/runsisi/tcmu-runner.git --tags master

将 mirror 脚本,clean 脚本,以及 mirror.conf 配置文件放在同一个文件夹下,然后通过 cron 定时运行:

0   0 * * * cd /home/runsisi/mirrors && ./mirror > /dev/null 2>&1

最后修改于 2019-04-15