LoginSignup
4
4

More than 5 years have passed since last update.

全てのGItプロジェクトのバックアップ

Posted at

社内で扱っているGitのプロジェクトが増えてきて、バックアップ取らないとまずいなーと思っていたので、やってみました。

下記のスクリプトで、全プロジェクトの全リモートブランチのバックアップが可能です。

#!/bin/sh

REPO_DIR="/backup/git/repositories"
SOUR_DIR="/backup/git/source"

for filepath in ${REPO_DIR}/*
do
    cd ${filepath}
    project_name="$(echo -n "$filepath" | cut -d/ -f5)"

    # master branch backup
    git checkout master
    git pull origin master

    if [ ! -e $SOUR_DIR/$project_name/master ]; then
        mkdir -p $SOUR_DIR/$project_name/master
    fi

    cp -frp $REPO_DIR/$project_name/* $SOUR_DIR/$project_name/master

    # other branch backup
    for remote_branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master`;
    do
        branch_name="$(echo -n "$remote_branch" | cut -d/ -f3)"
        git checkout $branch_name
        git pull origin $branch_name

        if [ ! -e $SOUR_DIR/$project_name/$branch_name ]; then
            mkdir -p $SOUR_DIR/$project_name/$branch_name
        fi

        cp -frp $REPO_DIR/$project_name/* $SOUR_DIR/$project_name/$branch_name
    done

    git checkout master

    echo "${filepath} Repository Complete!"
done

一応問題なく回っていますが、この方法だと、先に指定のディレクトリに全リポジトリをcloneしておかないとバックアップ対象にならないのがネックです。

もっといい方法があればぜひ教えて下さい。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4