Backup de arquivos com rsync

Publicado por Breno Morato Mesquita Sabella (última atualização em 29/05/2013)

[ Hits: 13.554 ]

Homepage: http://bsabella.com.br

Download 5808.backup_com_rsync.sh




Srs.

Segue script para efetuar backup utilizando rsync e wake-on-LAN.

- Este script liga os servidores, caso estejam desligados.
- Efetua sync dos arquivos.
- Desliga os servidores listados, após execução do sync.
- Utiliza chave RSA para efetuar conexão sem solicitar senha.

  



Esconder código-fonte

#!/bin/bash
##############################################################
# Sistema de Backup dos Servidores Locais                    #
# Autor............: Breno Sabella                           #
# Data Criacao.....: 15/05/2013                              #
# Data Modificado..: 23/05/2013                              #
# Versao...........: 1.0.0                                   #
# Objetivo                                                   #
#   Efetuar backup dos diretorios dos seguintes servidores   #
#   A, B e C                                   #
#   - Liga os servidores remotamente                         #
#   - Efetua backup utilizando rsync                         #
#   - Desliga os servidores                                  #
##############################################################

##############################################################
# VARIAVEIS DO SISTEMA                                       #
##############################################################
DIR_BACKUP="/mnt/backup/"
IP_SERVER_LAN=("192.168.0.1:a" "192.168.0.2:b")
EMAIL_PARA="breno.sabella@teste.com.br"
CONTADOR=0
LOG="/scripts/log/backup.log"
DATA_ATUAL=`date`
DATA_BACKUP=`date +%Y%m%d`
USUARIO="root"
RSYNC_BIN="/usr/bin/rsync"
RSYNC_OPTS=" -avzP --delete"
COMPACTADO="/mnt/backup/compactado/"
MACADDRESS_SERVER_LAN=("00:1E:4F:37:ED:40" "00:22:19:5B:9B:DA")
BROADCAST="192.168.0.255"

##############################################################
# PROCEDIMENTOS - LIGANDO OS SERVIDORES                      #
##############################################################
echo "" > $LOG
echo "" > $LOG
echo "Ligando os servidores em --" $DATA_ATUAL >> $LOG
echo "" >> $LOG

# PERCORRE O VETOR LIGANDO OS SERVIDORES
while [ ${CONTADOR} != ${#MACADDRESS_SERVER_LAN[@]} ]
do
        echo "Ligando Servidor --" ${MACADDRESS_SERVER_LAN[$CONTADOR]} >> $LOG
        /usr/bin/wakeonlan -i ${BROADCAST} ${MACADDRESS_SERVER_LAN[$CONTADOR]}
        let "CONTADOR = CONTADOR + 1"
done

echo "" > $LOG
echo "Finalizado os processos em --" $DATA_ATUAL >> $LOG
echo "" >> $LOG

sleep 300 # 5MIN PARA LIGAR OS SERVIDORES

##############################################################
# PROCEDIMENTOS - BACKUP DOS SERVIDORES                      #
##############################################################

echo "Iniciando o backup em -- " $DATA_ATUAL >> $LOG
echo "" >> $LOG

# PERCORRE O VETOR FAZENDO BACKUP DOS SERVIDORES

CONTADOR=0 # LIMPANDO A VARIAVEL CONTADOR

while [ $CONTADOR != ${#IP_SERVER_LAN[@]} ]
do
        echo "Inicio dos processos no servidor -- " ${IP_SERVER_LAN[$CONTADOR]} >> $LOG
        IP_SERVER=$(echo ${IP_SERVER_LAN[$CONTADOR]} | cut -f1 -d:)
        DIR_DESTINO=$(echo ${IP_SERVER_LAN[$CONTADOR]} | cut -f2 -d:)
        echo "Sincronizando diretó /etc" >> $LOG
        $RSYNC_BIN $RSYNC_OPTS -e 'ssh -i /root/.ssh/key_c' $USUARIO@$IP_SERVER:/etc $DIR_BACKUP$DIR_DESTINO
        echo "Sincronizando diretó /scripts" >> $LOG
        $RSYNC_BIN $RSYNC_OPTS -e 'ssh -i /root/.ssh/key_c' $USUARIO@$IP_SERVER:/scripts $DIR_BACKUP$DIR_DESTINO
        echo "Sincronizando diretorio /home" >> $LOG
        $RSYNC_BIN $RSYNC_OPTS -e 'ssh -i /root/.ssh/key_c' $USUARIO@$IP_SERVER:/home $DIR_BACKUP$DIR_DESTINO
        if [ $DIR_DESTINO == "netuno" ]; then
            #echo "Sincronizando diretorio www" >> $LOG
            #$RSYNC_BIN $RSYNC_OPTS -e 'ssh -i /root/.ssh/key_c' $USUARIO@$IP_SERVER:/var/www $DIR_BACKUP$DIR_DESTINO
            echo "Sincronizando diretorio /mnt/home" >> $LOG
            $RSYNC_BIN $RSYNC_OPTS -e 'ssh -i /root/.ssh/key_c' $USUARIO@$IP_SERVER:/mnt/home $DIR_BACKUP$DIR_DESTINO
            echo "Sincronizando diretorio /mnt/home" >> $LOG
            $RSYNC_BIN $RSYNC_OPTS -e 'ssh -i /root/.ssh/key_c' $USUARIO@$IP_SERVER:/var/www $DIR_BACKUP$DIR_DESTINO
        fi
        ssh -i /root/.ssh/key_c $USUARIO@$IP_SERVER sudo poweroff
        let "CONTADOR = CONTADOR + 1"
done

echo "" >> $LOG
echo "Finalizando o backup em -- " $DATA_ATUAL >> $LOG
echo "" >> $LOG
##############################################################
# ENVIANDO EMAIL BACKUP DO SISTEMA                           #
##############################################################
cat $LOG | mail -s "Backup do Sistema - Interno" $EMAIL_PARA

Scripts recomendados

Fazendo backup e compactando

Backup de MySql para outro servidor FTP!

Backups de servidor CentOS com cPanel/WHM para servidor VPS Windows Server 2008

Script de backup MySQL [melhorado]

Backup do PostgreSQL 8.3 e aviso por e-mail


  

Comentários

Nenhum comentário foi encontrado.


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts