[Bash] Mysql Backup v 1.2

Este script realiza copias de la base de datos indicada

#!/bin/bash
####################SECCION DE CONFIGURACION###################3
DB_NAME=db_name
DB_USER=db_user
DB_PASS=db_pass
DB_HOST=db_host
ADMIN_EMAIL= tu.direccion@de.correo.com
DOMINIO=tu.dominio.com
MAIL_DEBUG=1 
BACKUP_DIR=/ruta/donde/se/almacenaran/los/backups
############################################################

LAST_FILE=`ls -1t $BACKUP_DIR | head -n1`

#fichero temporal
mysqldump --user=${DB_USER} --password=${DB_PASS} --host=${DB_HOST} ${DB_NAME}  | bzip2 -c > /tmp/db-$(date +%Y%m%d).sql.bz2

if [ "$?" -ne "0" ]; then
        if [ $MAIL_DEBUG = 0 ]; then
		echo -e "\nmysqldump failed!"
		exit 1
	else
		echo "Ha habido un problema al ejecutar el backup de la base de datos. El comando mysqldump ha fallado" | mail -s "Backup de $DOMINIO" $ADMIN_EMAIL

        	exit 1
	fi
fi

tar -cjf /tmp/db-$(date +%Y%m%d).tar.bz2 /tmp/db-$(date +%Y%m%d).sql.bz2
if [ "$?" -ne "0" ]; then
	if [ $MAIL_DEBUG = 0 ]; then	
		echo -ne "tarball creation failed!\\n"
       		exit 1
	else 
		echo "Ha habido un problema al comprimir el backup de la base de datos. El comando tar ha fallado" | mail -s "Backup de $DOMINIO" $ADMIN_EMAIL
	fi
fi

TEMP_FILE=/tmp/db-$(date +%Y%m%d).tar.bz2
######################comprobacion de tamaño#######################################

if [ `du -h $TEMP_FILE | cut -f1` == `du -h $BACKUP_DIR/$LAST_FILE | cut -f1` ]; then 
	if [ $MAIL_DEBUG = 0 ]; then
		echo " No se hace nada, no ha habido cambios"
	else
		echo "No se ha completado el backup, ya que no ha habido cambios" | mail -s "Backup de $DOMINIO" $ADMIN_EMAIL
	fi	
else
	if [ $MAIL_DEBUG = 0 ]; then
		echo "Ha habido cambios, procedemos a almacenar el backup"
	fi	
mv /tmp/db-$(date +%Y%m%d).tar.bz2 $BACKUP_DIR
echo "" | mutt -a  $BACKUP_DIR/db-$(date +%Y%m%d).tar.bz2 -s "Backup de $DOMINIO" $ADMIN_EMAIL
fi

contadortotal=0
for e in $( ls ./ | nl | cut -f2 )
do
let contadortotal=$contadortotal+1
done
let contadortotal=$contadortotal-30

contadorparcial=0

for e in $( ls -tr ./ | nl | cut -f2 )
do
let contadorparcial=$contadorparcial+1
if [ $contadorparcial -lt $contadortotal ] ; then
rm $e
fi
done

3 comments for “[Bash] Mysql Backup v 1.2

  1. 16/11/2012 at 18:30

    And I thought I was the senislbe one. Thanks for setting me straight.

  2. 10/08/2012 at 10:11

    Posts like this brighten up my day. Thanks for tanikg the time.

    • 12/09/2012 at 5:19

      The following aparpes function helps in determining if a variable is set or not.isset(){ [ ${!1-X} == ${!1-Y} ]}[sara@mypc ~]# isset MYVAR || echo notsetnotset[sara@mypc ~]# MYVAR=X[sara@mypc ~]# isset MYVAR || echo notset[sara@mypc ~]# MYVAR=[sara@mypc ~]# isset MYVAR || echo notset[sara@mypc ~]# unset MYVAR[sara@mypc ~]# isset MYVAR || echo notsetnotset[sara@mypc ~]# isset BASH_VERSION[2] || echo notset[sara@mypc ~]# isset BASH_VERSION[20] || echo notsetnotset[sara@mypc ~]#

Deja un comentario