File "backup_remoto.sh"
Full path: /www/wwwroot/fabriciovc.eti.br/downloads/backup/backup_remoto.sh
File size: 4.15 KiB (4248 bytes)
MIME-type: text/x-shellscript
Charset: utf-8
#!/usr/bin/env bash
#- LICENCA --------------------------------------------------------------------
# Este trabalho esta licenciado sob uma Licenca Creative Commons
# Atribuicao-Compartilhamento pela mesma Licenca 2.5 Brasil. Para ver uma
# copia desta licenca, visite http://creativecommons.org/licenses/by-sa/2.5/br/
# ou envie uma carta para Creative Commons, 171 Second Street, Suite 300,
# San Francisco, California 94105, USA.
#------------------------------------------------------------------------------
#- COMANDOS -------------------------------------------------------------------
CP=`which cp`
CUT=`which cut`
FIND=`which find`
GREP=`which grep`
HEAD=`which head`
LS=`which ls`
MKDIR=`which mkdir`
MV=`which mv`
RM=`which rm`
RSYNC=`which rsync`
RSYNC_OPT='--delete -a'
#- DESTINO DOS DADOS ----------------------------------------------------------
BACKUP01="/srv/backup01"
BACKUP02="/srv/backup02"
BACKUP03="/srv/backup03"
BACKUP04="/srv/backup04"
BACKUP05="/srv/backup05"
#- OPCOES DE DATA -------------------------------------------------------------
DATE_BKP=`date +%Y%m%d`
DATE_AGO=`date --date "5 days ago" +%Y%m%d`
#- FUNCAO DE STATUS -----------------------------------------------------------
_STATUS () {
# Definindo funcoes:
COL=80;WCOL=50
SET_COL="echo -en \\033[${COL}G"
NORMAL="echo -en \\033[0;39m"
SUCCESS="echo -en \\033[1;32m"
FAILURE="echo -en \\033[1;31m"
if [ $? -eq 0 ]; then
#$SET_COL
echo -n "["
#$SUCCESS
echo -n "CONCLUIDO"
#$NORMAL
echo "]"
else
#$SET_COL
echo -n "["
#$FAILURE
echo -n "FALHOU"
#$NORMAL
echo "]"
fi
}
#- FUNCAO DE BACKUP -----------------------------------------------------------
_BACKUP () {
echo; echo "Backup de ${HOST_NAME} iniciado em `date +%d/%m/%Y` as `date +%H:%M:%S` "
# Verifica se o diretorio com a data atual ja existe.
if [ -d ${CURRENT_BACKUP} ]; then
if [ -d ${CURRENT_BACKUP}.old ]; then
echo -n "Diretorio ${CURRENT_BACKUP}.old encontrado, excluindo... "
${RM} -rf ${CURRENT_BACKUP}.old
_STATUS
fi
echo "Diretorio ${CURRENT_BACKUP} encontrado,"
echo -n " renomeando para ${CURRENT_BACKUP}.old..."
${MV} ${CURRENT_BACKUP} ${CURRENT_BACKUP}.old
_STATUS
if [ -d ${CURRENT_BACKUP}.old ]; then
echo ${CURRENT_BACKUP}.old > ${BACKUP_DIR}/last
fi
fi
# Apaga backups antigos caso existam. A quantidade de dias deve ser setada em DATE_AGO.
if [ -d ${BACKUP_DIR} ]; then
DIR=`${LS} ${BACKUP_DIR} | ${CUT} -d '-' -f 1 | ${GREP} ${DATE_AGO} | ${HEAD} -n1`
if [ "${DIR}" = "${DATE_AGO}" ]; then
echo -n "Removendo todos os backups do dia ${DATE_AGO}... "
${RM} -rf ${BACKUP_DIR}/${DATE_AGO}
_STATUS
else
echo -n "Nenhum backup anterior para ser removido... "
_STATUS
fi
else
${MKDIR} -p ${BACKUP_DIR}
fi
# Realiza o backup
if [ -f ${BACKUP_DIR}/last ]; then
LAST_BACKUP=`cat ${BACKUP_DIR}/last`
for DIRS in ${DIRS_TO_COPY}; do
echo -n "Realizando backup de ${SERVER_IP}:${DIRS}... "
${RSYNC} -e 'ssh -p 22022' ${RSYNC_OPT} --link-dest=${LAST_BACKUP} ${SERVER_IP}:${DIRS} ${CURRENT_BACKUP}
_STATUS
done
else
for DIRS in ${DIRS_TO_COPY}; do
echo -n "Realizando backup de ${SERVER_IP}:${DIRS}... "
#${RSYNC} ${RSYNC_OPT} ${SERVER_IP}:${DIRS} ${CURRENT_BACKUP}
${RSYNC} -e 'ssh -p 22022' ${RSYNC_OPT} ${SERVER_IP}:${DIRS} ${CURRENT_BACKUP}
_STATUS
# Atualiza o arquivo last com o nome do ultimo backup realizado
echo ${CURRENT_BACKUP} > ${BACKUP_DIR}/last
done
fi
# Finaliza o backup
echo "Backup de ${HOST_NAME} finalizado em `date +%d/%m/%Y` as `date +%H:%M:%S` "
unset HOST_NAME SERVER_IP BACKUP_DIR CURRENT_BACKUP DIRS_TO_COPY
}
#- MAQUINAS REMOTAS PARA BACKUP -----------------------------------------------
_SERVIDOR01 () {
HOST_NAME='SERVIDOR01'
SERVER_IP='200.200.200.200'
BACKUP_DIR="${BACKUP01}/${HOST_NAME}"
CURRENT_BACKUP="${BACKUP_DIR}/${DATE_BKP}"
DIRS_TO_COPY="/etc /srv /var/lib/mysql"
_BACKUP
}
_SERVIDOR02 () {
HOST_NAME='SERVIDOR02'
SERVER_IP='201.201.201.201'
BACKUP_DIR="${BACKUP02}/${HOST_NAME}"
CURRENT_BACKUP="${BACKUP_DIR}/${DATE_BKP}"
DIRS_TO_COPY="/etc /srv /var/lib/mysql"
_BACKUP
}
#- BACKUPS A SEREM REALIZADOS -------------------------------------------------
_SERVIDOR01
_SERVIDOR02
echo
`which df` -hT