Skip to content

Instantly share code, notes, and snippets.

@ancho85
Created July 18, 2016 13:42
Show Gist options
  • Save ancho85/94477eb80cb6a760b77e4c8e9627ccab to your computer and use it in GitHub Desktop.
Save ancho85/94477eb80cb6a760b77e4c8e9627ccab to your computer and use it in GitHub Desktop.
backuper
#!/bin/bash
BACKUPDIR="/opt/backup" #CARPETA DESTINO EN EQUIPO LOCAL
LOGFILE=$BACKUPDIR/backup.log # Destino del log
USERNAME=""
PASSWORD=""
BD1=""
BD2=""
#DISCOLOCAL="/dev/sda2"
DISCOLOCAL="/dev/sda"
#DISCOMYSQL="/dev/sdb1"
DISCOMYSQL="/home"
#DISCOBACKUP="/dev/sdc1"
DISCOBACKUP="/"
REMITENTE=""
DESTINATARIO=""
DESTINATARIO1=""
DESTINATARIO2=""
DESTINATARIO3=""
ERRORMSG=""
ASUNTO1="Backup_Culminado"
ASUNTO2="Poco_Espacio_En_Disco"
FSBACKUPDIR="/opt/backup/backup"
BINARYLOGPATH="/home/mysql"
function setErrorAndLog {
ERRORMSG=$1
echo $1 >> $LOGFILE
}
##Verificacion de existencia de directorio en File Server
FSBACKUPDIREXIST=true #external path, may not be available
if ! [ -d $FSBACKUPDIR ]; then
FSBACKUPDIREXIST=false
setErrorAndLog "No existe el directorio de backups secundario $FSBACKUPDIR. Verificar punto de montaje"
elif ! [ -w $FSBACKUPDIR ]; then
setErrorAndLog "No se puede escribir en directorio de backups secundario $FSBACKUPDIR. Verificar permisos"
fi
##check if binary logs is activated
BINLOGENABLED=false
TESTBINLOG=$(mysql -u$USERNAME -p$PASSWORD -s -e "SELECT @@log_bin" | head -n 2)
if [ "$TESTBINLOG" -eq 1 ]; then
BINLOGENABLED=true
fi
##Recorrido de bases de datos y creacion de backups
databases=($BD1 $BD2)
for DBNAME in ${databases[*]}
do
HORA=$(date +"%F-%H-%M")
FILENAME="$DBNAME--$HORA.sql"
echo "..::Backup Iniciado $DBNAME $HORA::.." >> $LOGFILE
#method1 #2 procesos concurrentes, cada uno con otros 2 procesos concurrentes (mysqldump y gzip)
#CMD1="mysqldump -u$USERNAME -p$PASSWORD $DBNAME --compress --single-transaction --quick --ignore-table=$DBNAME.EventLog | gzip > $BACKUPDIR/$FILENAME.gz"
#CMD2="mysqldump -u$USERNAME -p$PASSWORD $DBNAME --compress --single-transaction --quick --ignore-table=$DBNAME.EventLog | gzip > $FSBACKUPDIR/$FILENAME.gz"
#eval "$CMD1 | $CMD2" #2 procesos concurrentes, cada uno con otros 2 procesos concurrentes (mysqldump y gzip)
#method2 #1 proceso dump 2 procesos de compresion
EXTRAPARAMS="--compact --hex-blob"
if $BINLOGENABLED; then EXTRAPARAMS="$EXTRAPARAMS --master-data=2"; fi
CMD="mysqldump -u$USERNAME -p$PASSWORD $DBNAME --single-transaction --quick --ignore-table=$DBNAME.EventLog $EXTRAPARAMS 2>> $LOGFILE \
| tee >(gzip -9 -c > $BACKUPDIR/$FILENAME.gz) "
if $FSBACKUPDIREXIST; then
CMD="$CMD | gzip -9 -c > $FSBACKUPDIR/$FILENAME.gz"
else
CMD="$CMD > /dev/null "
fi
eval "$CMD; typeset -a OUTPUT=(\${PIPESTATUS[@]})" #SETTING OUTPUT WITHIN THE EVAL OF THE COMMAND
## Verificacion de salida del dump y comprobacion de integridad
if [ ${OUTPUT[0]} -ne 0 ]; then
setErrorAndLog "Error al crear backup para $DBNAME"
else
echo "Verificando integridad de backups" >> $LOGFILE
TESTBKP1=$(gzip -t $BACKUPDIR/$FILENAME.gz)
TESTBKP2=0
if $FSBACKUPDIREXIST; then
TESTBKP2=$(gzip -t $FSBACKUPDIR/$FILENAME.gz)
fi
if ! [[ $TESTBKP1 -eq 0 ]]; then
setErrorAndLog "Fallo de integridad en $BACKUPDIR $FILENAME.gz"
elif ! [[ $TESTBKP2 -eq 0 ]]; then
setErrorAndLog "Fallo de integridad en $FSBACKUPDIR $FILENAME.gz"
else
echo "Verificacion exitosa" >> $LOGFILE
fi
fi
echo "Backups realizados en $BACKUPDIR y en $FSBACKUPDIR" >> $LOGFILE
done
## old files removal after 60 days
backupsdirectory=($BACKUPDIR $FSBACKUPDIR)
for BKPDIR in ${backupsdirectory[*]}
do
if [ -d $BKPDIR ]; then
echo "Borrando backups mayores a 60 dias en $BKPDIR" >> $LOGFILE
find $BKPDIR -type f -mtime +60 -exec rm {} \;
if [ $? -ne 0 ]; then
setErrorAndLog "Error al borrar backups de $BKPDIR"
fi
fi
done
DF1=`df -h $DISCOLOCAL | tail -1 | cut -c41-42`
DF2=`df -h $DISCOMYSQL | tail -1 | cut -c41-42`
DF3=`df -h $DISCOBACKUP | tail -1 | cut -c41-42`
## Comprobacion de espacion en disco Local
if [ $DF1 -gt 80 ]; then
setErrorAndLog "LLego a maximo del disco LOCAL $DF1% "
echo " Verificar Espacio en disco Local llego a $DF1% " | mail -s $ASUNTO2 -r $REMITENTE $DESTINATARIO
else
echo "Disco local esta en tamanho normal $DF1%" >> $LOGFILE
fi
##Comprobacion de disco MySQL
if [ $DF2 -gt 90 ]; then
setErrorAndLog "LLego a maximo del disco MYSQL $DF2% "
echo " Verificar Espacio en disco MYSQL llego a $DF2% " | mail -s $ASUNTO2 -r $REMITENTE $DESTINATARIO
else
echo "Disco MYSQL esta en tamanho normal $DF2%" >> $LOGFILE
fi
##Comprobacion de disco BACKUP
if [ $DF3 -gt 90 ]; then
setErrorAndLog "LLego a maximo del disco BACKUP $DF3% "
echo " Verificar Espacio en disco Backup llego a $DF3% " | mail -s $ASUNTO2 -r $REMITENTE $DESTINATARIO
else
echo "Disco Backup esta en tamanho normal $DF3%" >> $LOGFILE
fi
#echo " Backup Culminado" | mail -s $ASUNTO1 -r $REMITENTE -a $LOGFILE $DESTINATARIO
if ! [ -z "$ERRORMSG" ]; then
echo $ERRORMSG | mail -s "Gestion: Errores al crear backups" -r $REMITENTE -a $LOGFILE $DESTINATARIO1,$DESTINATARIO2,$DESTINATARIO3
fi
## binlogs
BKPSIZE=$(wc -c "$BACKUPDIR/$FILENAME.gz" | cut -f 1 -d ' ')
if $BINLOGENABLED && [ $BKPSIZE -gt 20 ]; then #if size <= 20, the backup has failed
#with --master-data=2 as mysqldump param
ZIPFIRSTLINE=$(zless $BACKUPDIR/$FILENAME.gz | head -n 1)
MASTER_LOG_POS=$(echo $ZIPFIRSTLINE | awk '{split($6,a,";"); print a[1]}' | cut -f2 -d=)
MASTER_LOG_FILE=$(echo $ZIPFIRSTLINE | awk '{split($5,a,","); print a[1]}' | cut -f2 -d "=" -d "'")
if ! [ -d $BACKUPDIR/recovery ]; then mkdir $BACKUPDIR/recovery; fi
printf "mysqlbinlog --offset=$MASTER_LOG_POS $BINARYLOGPATH/$MASTER_LOG_FILE > ultimoscambios.sql\nmysql < ultimoscambios.sql" > $BACKUPDIR/recovery/$HORA.txt
#need to check the index file for all logs generated after current backup generated
OTHERLOGS=$(mysql -u$USERNAME -p$PASSWORD -s -e "SHOW BINARY LOGS" | awk '{print $1}')
for file in $OTHERLOGS
do
fileEXT=${file#*.}
if [ "$fileEXT" -gt ${MASTER_LOG_FILE#*.} ] ; then #Compare extensions.
printf "\nmysqlbinlog $BINARYLOGPATH/$file > ultimoscambios$fileEXT.sql\nmysql < ultimoscambios$fileEXT.sql" >> $BACKUPDIR/recovery/$HORA.txt
fi
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment