File "backup_incremental.sh"
Full path: /www/wwwroot/fabriciovc.eti.br/downloads/backup/backup_incremental.sh
File size: 5.54 KiB (5672 bytes)
MIME-type: text/x-shellscript
Charset: utf-8
#!/usr/bin/env bash
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# NOTA DE 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. #
# #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# #
# Criado em 22/11/2008 - Ultima alteracao em 12/12/2008 #
# Desenvolvido por Fabricio Vaccari Constanski #
# Contato pelo Email fabriciovc[em]fabriciovc.eti.br #
# Funcao: Script que realiza backup incremental utilizando sistema de copia #
# com links, reduzindo assim o espaco em disco utilizado para a #
# realizacao do backup e possibilitando o aumento de dias de #
# armazenamento dos arquivos. #
# #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#- 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 --------------------------------------------------------------------
BACKUP="/srv"
BACKUP01="${BACKUP}/backup01"
BACKUP02="${BACKUP}/backup02"
BACKUP03="${BACKUP}/backup03"
#- OPCOES DE BACKUP -----------------------------------------------------------
DATE_BKP=`date +%Y%m%d-%H`
DATE_AGO=`date --date "3 days ago" +%Y%m%d`
#- FUNCAO QUE CHECA COMANDOS --------------------------------------------------
_CHECKOK () {
if [ $? -eq 0 ]; then
echo -e "\\033[1;32m CONCLUIDO \\033[0;39m"
else
echo -e "\\033[1;31m FALHOU \\033[0;39m"
fi
}
#- FUNCAO DE BACKUP -----------------------------------------------------------
_BACKUP () {
echo; echo "Backup de ${HOST_NAME} iniciado em `date +%d/%m/%Y` as `date +%H:%M:%S`"
if [ -d ${CURRENT_BACKUP} ]; then
if [ -d ${CURRENT_BACKUP}.copia ]; then
echo -n "Diretorio ${CURRENT_BACKUP}.copia encontrado, excluindo..."
_CHECKOK
${RM} -rf ${CURRENT_BACKUP}.copia
fi
echo -n "Diretorio ${CURRENT_BACKUP} encontrado, renomeando para ${CURRENT_BACKUP}.copia..."
_CHECKOK
${MV} ${CURRENT_BACKUP} ${CURRENT_BACKUP}.copia
fi
if [ ! -d ${BACKUP_DIR} ]; then
${MKDIR} -p ${BACKUP_DIR}
fi
if [ -e ${BACKUP_DIR}/last ]; then
LAST_BACKUP=`cat ${BACKUP_DIR}/last`
if [ -d ${LAST_BACKUP} ]; then
echo -n "Ultimo backup encontrando, linkando arquivos..."
${CP} -la ${LAST_BACKUP} ${CURRENT_BACKUP}
_CHECKOK
fi
else
echo -n "Nenhum backup anterior encontrado, criando diretorio para backup atual..."
${MKDIR} -p ${CURRENT_BACKUP}
_CHECKOK
fi
for DIRS in ${DIRS_TO_COPY}; do
echo -n "Iniciando backup de ${SERVER_IP}:${DIRS}..."
${RSYNC} ${RSYNC_OPT} ${SERVER_IP}:${DIRS} ${CURRENT_BACKUP}
_CHECKOK
done
echo ${CURRENT_BACKUP} > ${BACKUP_DIR}/last
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}-*
_CHECKOK
else
echo -n "Nenhum backup anterior para ser removido..."
_CHECKOK
fi
echo "Backup de ${HOST_NAME} finalizado em `date +%d/%m/%Y` as `date +%H:%M:%S`"
}
#- MAQUINAS REMOTAS PARA BACKUP -----------------------------------------------
_ARQUITETO () {
HOST_NAME='ARQUITETO'
SERVER_IP='10.0.0.233'
BACKUP_DIR="${BACKUP01}/${HOST_NAME}"
CURRENT_BACKUP="${BACKUP_DIR}/${DATE_BKP}"
DIRS_TO_COPY="/boot /etc /var/www /var/lib/mysql /var/cache/bind"
_BACKUP
unset HOST_NAME SERVER_IP BACKUP_DIR CURRENT_BACKUP DIRS_TO_COPY
}
_MORPHEUS () {
HOST_NAME='MORPHEUS'
SERVER_IP='10.0.0.241'
BACKUP_DIR="${BACKUP01}/${HOST_NAME}"
CURRENT_BACKUP="${BACKUP_DIR}/${DATE_BKP}"
DIRS_TO_COPY="/boot /etc"
_BACKUP
unset HOST_NAME SERVER_IP BACKUP_DIR CURRENT_BACKUP DIRS_TO_COPY
}
_NEO () {
HOST_NAME='NEO'
SERVER_IP='172.16.1.230'
BACKUP_DIR="${BACKUP01}/${HOST_NAME}"
CURRENT_BACKUP="${BACKUP_DIR}/${DATE_BKP}"
DIRS_TO_COPY="/boot /etc /srv/ldap-base /var/lib/ldap /srv/usuarios /srv/samba/netlogon /srv/samba/profiles"
_BACKUP
unset HOST_NAME SERVER_IP BACKUP_DIR CURRENT_BACKUP DIRS_TO_COPY
}
_NIOBE () {
HOST_NAME='NIOBE'
SERVER_IP='10.0.0.242'
BACKUP_DIR="${BACKUP01}/${HOST_NAME}"
CURRENT_BACKUP="${BACKUP_DIR}/${DATE_BKP}"
DIRS_TO_COPY="/boot /etc"
_BACKUP
unset HOST_NAME SERVER_IP BACKUP_DIR CURRENT_BACKUP DIRS_TO_COPY
}
_SENTINELA () {
HOST_NAME='SENTINELA'
SERVER_IP='10.0.0.254'
BACKUP_DIR="${BACKUP01}/${HOST_NAME}"
CURRENT_BACKUP="${BACKUP_DIR}/${DATE_BKP}"
DIRS_TO_COPY="/boot /etc /var/www /var/cache/bind"
_BACKUP
unset HOST_NAME SERVER_IP BACKUP_DIR CURRENT_BACKUP DIRS_TO_COPY
}
#- BACKUPS A SEREM REALIZADOS -------------------------------------------------
_ARQUITETO
_MORPHEUS
_NEO
_NIOBE
_SENTINELA
#------------------------------------------------------------------------------