社内で扱っている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しておかないとバックアップ対象にならないのがネックです。
もっといい方法があればぜひ教えて下さい。