Skip to content

Instantly share code, notes, and snippets.

@valvallow
Created May 18, 2016 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valvallow/30813929e4941050baa136843a232aa5 to your computer and use it in GitHub Desktop.
Save valvallow/30813929e4941050baa136843a232aa5 to your computer and use it in GitHub Desktop.
#!/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
"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment