gitのhookでバックアップ用のremoteにpushする手も考えましたが、リポジトリが増えるごとにメンテがいる気がしたのでやめました。メンテは0にしたい。
会社のリポジトリはすべて社内サーバの特定ディレクトリ配下にあるので「ssh接続して検索、すでに知ってるリポジトリであればpull、新しく見つかったらcloneするマン」を用意すれば楽だなって。cronで実行すればほったらかしで済むし、メールで飛んでくるcronのログさえ見てればOKだし。
- gitサーバにssh接続してgitリポジトリをfind (サブディレクトリも)
- 対象リポジトリのリストを作成 (ファイルにする必要はないけど特に何も考えてない)
- 新しいリポジトリならディレクトリ階層も再現しつつclone、そうでなければpull
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
SSH_SERVER="git-server" # .ssh/config | |
SSH_PASSWORD="*****" | |
SSH_SERVER_REPOS_ROOT_PATH='/repos' | |
BACKUP_DIR="/home/valvallow/repos" | |
REPOS_LIST_FILE="/home/valvallow/repos.list" | |
GIT_COMMAND_PATH="/home/valvallow/opt/bin/git" | |
# ---------------------------- | |
# find repository | |
# ---------------------------- | |
REMOTE_COMMAND="cd ${SSH_SERVER_REPOS_ROOT_PATH}; find ./ -type d -name '*.git';" | |
REPOSITORY_LIST=` | |
expect -c " | |
log_file -noappend ${REPOS_LIST_FILE} | |
set timeout 120 | |
spawn ssh ${SSH_SERVER} \"${REMOTE_COMMAND}\" | |
expect \"Enter passphrase for key\" | |
send \"${SSH_PASSWORD}\n\" | |
expect eof | |
exit | |
" | |
` | |
REPOSITORY_LIST=$(tail -n +2 ${REPOS_LIST_FILE}) | |
echo '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^' | |
echo $REPOSITORY_LIST | |
echo '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^' | |
# ---------------------------- | |
# pull or clone | |
# ---------------------------- | |
if [ ! -e ${BACKUP_DIR} ]; | |
then | |
mkdir -p ${BACKUP_DIR} | |
fi | |
for REPOSITORY in `echo ${REPOSITORY_LIST} | tr '\n' ' ' | tr '\r' ''` | |
do | |
cd | |
cd ${BACKUP_DIR} | |
echo '======================================================' | |
echo $REPOSITORY; | |
echo '======================================================' | |
NEED_CLONE=0; | |
if [ ! -e ${REPOSITORY} ]; | |
then | |
NEED_CLONE=1; | |
mkdir -p ${REPOSITORY} | |
cd ${REPOSITORY} | |
else | |
cd ${REPOSITORY} | |
cd `basename ${REPOSITORY} | sed -e s_^\./__g -e s/.git//g` | |
if [ "$?" -ne 0 ]; | |
then | |
NEED_CLONE=1; | |
fi | |
fi | |
if [ ${NEED_CLONE} -eq 1 ]; | |
then | |
REPOSITORY_FULL_PATH="echo ${REPOSITORY} | sed s_^\._${SSH_SERVER_REPOS_ROOT_PATH}_g" | |
REMOTE_COMMAND="${GIT_COMMAND_PATH} clone ${SSH_SERVER}:${REPOSITORY_FULL_PATH}" | |
else | |
REMOTE_COMMAND="${GIT_COMMAND_PATH} pull origin master" | |
fi | |
expect -c " | |
set timeout 120 | |
spawn ${REMOTE_COMMAND} | |
expect \"Enter passphrase for key\" | |
send \"${SSH_PASSWORD}\n\" | |
expect eof | |
exit | |
" |