Last active
May 11, 2018 19:46
-
-
Save rsmartins78/1c626f7fa767a6804e1fe7aee35454e6 to your computer and use it in GitHub Desktop.
Revisions
-
rsmartins78 revised this gist
May 11, 2018 . 1 changed file with 26 additions and 28 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -45,7 +45,7 @@ function backup_externo() { echo "Para realizar o backup externo é necessário criar um arquivo ~/.ssh/config" echo "Com os parametros de acesso SSH ao destino" echo "" echo "Use a opção -h para ajuda" exit 2 fi @@ -72,21 +72,17 @@ function start() { #Definindo opção de limpar origem = false deleta=false #COLETANDO A ID DO CONTAINER DO GITLAB-SERVER CONTAINERID=$(docker ps | grep -i gitlab-server | awk '{print $1'}) if [ -z $CONTAINERID ]; then echo "ID do Container não identificada" echo "Verifique se o mesmo existe ou está sendo executado" exit 2 fi echo "A ID do Container é ${CONTAINERID}" #VERIFICA SE DESTINO CONTEM CARACTERE '@' TDESTINO=$( echo ${DESTINO} | sed 's/@//g' ) @@ -102,7 +98,7 @@ function start() { echo "Origem: ${ORIGEM}" echo "Destino: ${TDESTINO}" echo "" echo "Use a opção -h para ajuda" exit 1 elif [ ${local} = true ]; then echo "Escolhida a opção de backup local" @@ -129,6 +125,16 @@ function start() { } function error_local() { echo "Favor informar o tipo de backup !" echo "-l para local ou -e para externo (via SSH)" echo "" echo "Local? ${local}" echo "Externo? ${externo}" echo "Use a opção -h para ajuda" exit 1 } #Abaixo é coletado os parâmetros solicitados, junto com seus respectivos argumentos. while getopts :hrleo:d: OPTION; do case "${OPTION}" in @@ -167,25 +173,17 @@ while getopts :hrleo:d: OPTION; do done # Abaixo está sendo realizada uma validação se os parâmetros obrigatórios foram passado antes de iniciar aplicação. if [ $local = true ] && [ $externo = true ]; then error_local elif [ $local = false ] && [ $externo = false ]; then error_local fi if [ -n $ORIGEM ] && [ -n $DESTINO ]; then start else echo "Favor informar Origem e Destino" echo "Origem ${ORIGEM}" echo "Destino ${DESTINO}" fi -
rsmartins78 revised this gist
May 11, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -61,7 +61,7 @@ function limpa_origem() { function testa_ssh() { SSH=$( echo ${DESTINO} | cut -d: -f1 ) TSSH=$( ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no $SSH > /dev/null ) if [ $TSSH != true ]; then echo "Falha na conexão SSH com ${SSH}" exit 2 -
rsmartins78 revised this gist
May 11, 2018 . 1 changed file with 62 additions and 36 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -10,10 +10,15 @@ ## OBRIGADO PELO APOIO ## ############################################################################# local=false externo=false function usage() { echo "Script para backup do Gitlab em Container" echo "Como usar:" echo "$0 -l ou -e -o path/de/origem -d path/de/destino" echo "-l para backup local" echo "-e para backup externo" echo "-h exibe este menu" echo "-r remove todos os arquivos da pasta de origem" echo "USE A OPÇÃO -r COM CAUTELA, ELA APAGA TUDO DA ORIGEM DEFINIDA" @@ -54,6 +59,15 @@ function limpa_origem() { rm -rf ${ORIGEM}/* } function testa_ssh() { SSH=$( echo ${DESTINO} | cut -d: -f1 ) TSSH=$( ssh -o ConnectTimeout=10 $SSH > /dev/null ) if [ $TSSH != true ]; then echo "Falha na conexão SSH com ${SSH}" exit 2 fi } function start() { #Definindo opção de limpar origem = false @@ -75,42 +89,48 @@ function start() { echo "" #VERIFICA SE DESTINO CONTEM CARACTERE '@' TDESTINO=$( echo ${DESTINO} | sed 's/@//g' ) #Verifica o target, e se as opções origem e destino foram setadas, em caso de externo, valida se contem um @. if [ ${externo} = true ] && [ ${DESTINO} == ${TDESTINO} ]; then #Caso as validações acima retornem falso, irá printar como usar. echo "Target, Origem ou Destino Inválido !!" echo "Em caso de target externo, o destino deve conter o formato de uma conexão SSH" echo "DEBUG ABAIXO" echo "Local? ${local}" echo "Externo? ${externo}" echo "Origem: ${ORIGEM}" echo "Destino: ${TDESTINO}" echo "" usage exit 1 elif [ ${local} = true ]; then echo "Escolhida a opção de backup local" #Executa função de backup local caso verdadeiro. backup_local #Caso seja escolhida opção -r, será executada função para limpar origem. if [ $deleta = true ]; then echo "Eliminando conteúdo da pasta de origem: ${ORIGEM}" limpa_origem fi exit 0 else echo "Escolhida a opção de backup externo (via SSH)" #Executa função de backup externo caso verdadeiro. testa_ssh backup_externo #Caso seja escolhida opção -r, será executada função para limpar origem. if [ $deleta = true ]; then echo "Eliminando conteúdo da pasta de origem: ${ORIGEM}" limpa_origem fi exit 0 fi } #Abaixo é coletado os parâmetros solicitados, junto com seus respectivos argumentos. while getopts :hrleo:d: OPTION; do case "${OPTION}" in o) # Gravando origem na variável. @@ -124,9 +144,13 @@ while getopts :hrt:o:d: OPTION; do # Informando que a origem deverá ser limpa. deleta=true ;; l) # Definindo target local=true ;; e) # Definindo target externo=true ;; h) # Exibindo menu de ajuda. @@ -143,23 +167,25 @@ while getopts :hrt:o:d: OPTION; do done # Abaixo está sendo realizada uma validação se os parâmetros obrigatórios foram passado antes de iniciar aplicação. if [ $local = true ] && [ $externo = false ] || [ $local = false ] && [ $externo = true ]; then if [ -z $ORIGEM ]; then echo "Favor definir a origem" echo "" usage exit 1 elif [ -z $DESTINO ]; then echo "Favor definir o destino" echo "" usage exit 1 else # Programa é iniciado start fi else echo "Favor informar o tipo de backup !" echo "-l para local ou -e para externo (via SSH)" echo "" usage exit 1 fi -
rsmartins78 revised this gist
May 11, 2018 . 1 changed file with 7 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -2,11 +2,10 @@ ############################################################################# ## Nome: Gitlab-Backup ## ## Função: SCRIPT PARA AUTOMAÇÃO DE BACKUP DE ## ## UM GITLAB SERVER EXECUTADO EM DOCKER. ## ## PODE SER UTILIZADO PARA USO EM CRONTAB, POIS COLETA A ID CONTAINER. ## ## Linguagem: Shell Script ## ## ## ## OBRIGADO PELO APOIO ## ############################################################################# @@ -44,7 +43,7 @@ function backup_externo() { usage exit 2 fi echo "Realizando Backup" docker exec -t ${CONTAINERID} bash -c 'gitlab-rake gitlab:backup:create' echo "Copiando ao Destino ${DESTINO}" @@ -79,7 +78,7 @@ function start() { TDESTINO=$( echo $DESTINO | sed 's/@//g' ) #Verifica o target, e se as opções origem e destino foram setadas, em caso de externo, valida se contem um @. if [ ${TARGET} == 'externo' ] && [ -n ${ORIGEM} ] && [ ${DESTINO} -eq ${TDESTINO} ]; then echo "Escolhida a opção de backup externo (via SSH)" #Executa função de backup externo caso verdadeiro. backup_externo @@ -89,7 +88,7 @@ function start() { limpa_origem fi exit 0 elif [ ${TARGET} == 'local' ] && [ -n ${ORIGEM} ] && [ -n ${DESTINO} ] ; then echo "Escolhida a opção de backup local" #Executa função de backup local caso verdadeiro. backup_local @@ -102,6 +101,7 @@ function start() { else #Caso as validações acima retornem falso, irá printar como usar. echo "Target, Origem ou Destino Inválido !!" echo "Em caso de target externo, o destino deve conter o formato de uma conexão SSH" echo "" usage exit 1 -
rsmartins78 revised this gist
May 11, 2018 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -71,7 +71,6 @@ function start() { echo "Iniciando backup !!" echo "" echo "A ID do Container é ${CONTAINERID}" echo "A Origem é: ${ORIGEM}" echo "O Destino é: ${DESTINO}" echo "" -
rsmartins78 created this gist
May 11, 2018 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,166 @@ #!/bin/bash ############################################################################# ## Nome: Gitlab-Backup ## ## Função: SCRIPT PARA AUTOMAÇÃO DE BACKUP DE UM GITLAB SERVER EXECUTADO ## ## EM DOCKER. PODE SER UTILIZADO PARA USO EM CRONTAB, POIS COLETA A ID ## ## DO CONTAINER DE FORMA DINÂMICA ## ## Linguagem: Shell Script ## ## Criado por: RAFAEL MARTINS <rsmartins78@hotmail.com> ## ## ## ## OBRIGADO PELO APOIO ## ############################################################################# function usage() { echo "Script para backup do Gitlab em Container" echo "Como usar:" echo "$0 -t <local|externo> -o path/de/origem -d path/de/destino" echo "-h exibe este menu" echo "-r remove todos os arquivos da pasta de origem" echo "USE A OPÇÃO -r COM CAUTELA, ELA APAGA TUDO DA ORIGEM DEFINIDA" echo "" echo "Exemplos:" echo "$0 -t local -o /devops/gitlab/backup -d /backup/" echo "$0 -t externo -o /devops/gitlab/backup -d root@10.10.10.10:/backup" echo "" echo "ATENÇÃO" echo "CASO O PARAMETRO ESCOLHIDO PARA -t SEJA 'externo', FAVOR CRIAR ~/.ssh/config," echo "especificando endereço do host e path da chave." echo "Mais informações em https://www.cyberciti.biz/faq/force-ssh-client-to-use-given-private-key-identity-file/" } function backup_local() { echo "Realizando Backup" docker exec -t ${CONTAINERID} bash -c 'gitlab-rake gitlab:backup:create' echo "Copiando ao Destino ${DESTINO}" rsync -azvh --progress ${ORIGEM} ${DESTINO} } function backup_externo() { if [ ! -f ${HOME}/.ssh/config ]; then echo "Para realizar o backup externo é necessário criar um arquivo ~/.ssh/config" echo "Com os parametros de acesso SSH ao destino" echo "" usage exit 2 fi echo "Realizando Backup" docker exec -t ${CONTAINERID} bash -c 'gitlab-rake gitlab:backup:create' echo "Copiando ao Destino ${DESTINO}" rsync -Cravzpte ssh --progress ${ORIGEM} ${DESTINO} } function limpa_origem() { rm -rf ${ORIGEM}/* } function start() { #Definindo opção de limpar origem = false deleta=false CONTAINERID=$(docker ps | grep -i gitlab-server | awk '{print $1'}) if [ -z $CONTAINERID ]; then echo "ID do Container não identificada" echo "Verifique se o mesmo existe ou está sendo executado" exit 2 fi echo "" echo "Iniciando backup !!" echo "" echo "A ID do Container é ${CONTAINERID}" echo "O comando à ser executado será: ${BACKUPCOMMAND}" echo "A Origem é: ${ORIGEM}" echo "O Destino é: ${DESTINO}" echo "" #VERIFICA SE DESTINO CONTEM CARACTERE '@' TDESTINO=$( echo $DESTINO | sed 's/@//g' ) #Verifica o target, e se as opções origem e destino foram setadas, em caso de externo, valida se contem um @. if [ ${TARGET} = 'externo' ] && [ -n ${ORIGEM} ] && [ ${DESTINO} -eq ${TDESTINO} ]; then echo "Escolhida a opção de backup externo (via SSH)" #Executa função de backup externo caso verdadeiro. backup_externo #Caso seja escolhida opção -r, será executada função para limpar origem. if [ $deleta = true ]; then echo "Eliminando conteúdo da pasta de origem: ${ORIGEM}" limpa_origem fi exit 0 elif [ ${TARGET} = 'local' ] && [ -n ${ORIGEM} ] && [ -n ${DESTINO} ] ; then echo "Escolhida a opção de backup local" #Executa função de backup local caso verdadeiro. backup_local #Caso seja escolhida opção -r, será executada função para limpar origem. if [ $deleta = true ]; then echo "Eliminando conteúdo da pasta de origem: ${ORIGEM}" limpa_origem fi exit 0 else #Caso as validações acima retornem falso, irá printar como usar. echo "Target, Origem ou Destino Inválido !!" echo "" usage exit 1 fi } #Abaixo é coletado os parâmetros solicitados, junto com seus respectivos argumentos. while getopts :hrt:o:d: OPTION; do case "${OPTION}" in o) # Gravando origem na variável. ORIGEM="${OPTARG}" ;; d) # Gravando destino na variável. DESTINO="${OPTARG}" ;; r) # Informando que a origem deverá ser limpa. deleta=true ;; t) # Definindo target TARGET="${OPTARG}" ;; h) # Exibindo menu de ajuda. usage exit 1 ;; *) # No caso de opção inválida, é emitido alerta. echo "Opção inválida !" echo "Verifique como usar com -h" exit 1 ;; esac done # Abaixo está sendo realizada uma validação se os parâmetros obrigatórios foram passado antes de iniciar aplicação. if [ -z $TARGET ]; then echo "Favor definir o target" echo "" usage exit 1 elif [ -z $ORIGEM ]; then echo "Favor definir a origem" echo "" usage exit 1 elif [ -z $DESTINO ]; then echo "Favor definir o destino" echo "" usage exit 1 else # Programa é iniciado start fi ## FIM DO CÓDIGO