Last active
January 24, 2022 09:56
-
-
Save jsianes/5875195 to your computer and use it in GitHub Desktop.
This shell-script creates a local MySQL backup file, maintaining a 10 days rotation period (by default. Can be configured on parameters). Controls if there is enough space before doing it and overlap executions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# This script make a local MySQL DB backup | |
# Developed for RHEL5 by Javier Sianes - [email protected] | |
# | |
# Start of Parameters | |
############################################## | |
USUARIO="username" | |
PASSWORD="password" | |
IP="127.0.0.1" | |
FACTOR_ESPACIO_NECESARIO="1" | |
DIRECTORIO_DESTINO="/opt/backup/BBDD" | |
PERIODO_DE_RETENCION_EN_DIAS="10" | |
MYSQL_DIRECTORY="/var/lib/mysql" | |
############################################## | |
# End of parameters | |
FECHA=`date +"%Y%m%d"` | |
FICHERO_BACKUP="${DIRECTORIO_DESTINO}/MySQL-BBDD-${FECHA}.sql" | |
FICHERO_LOCK="/tmp/.backup-BBDD.lock" | |
INPUT_DEVICE=`df ${DIRECTORIO_DESTINO} | grep ^/ | cut -d \ -f 1` | |
flock() { | |
rm -f ${FICHERO_LOCK} >/dev/null 2>&1 | |
exit 3 | |
} | |
#Si existe el fichero de bloqueo, no se hace backup | |
if [ -f ${FICHERO_LOCK} ] | |
then | |
exit 2 | |
fi | |
trap flock SIGINT SIGQUIT | |
if [ -d ${DIRECTORIO_DESTINO} ] | |
then | |
#Revisando si hay espacio libre para generar el backup | |
ESPACIO_LIBRE=`df -m ${INPUT_DEVICE} | tail -1 | awk '{ print $3 }'` | |
ESPACIO_OCUPADO_BBDD=`du -ms ${MYSQL_DIRECTORY} | awk '{ print $1 }'` | |
ESPACIO_NECESARIO=`echo "scale=0; (${ESPACIO_OCUPADO_BBDD}*${FACTOR_ESPACIO_NECESARIO}/1)+1" | bc -q` | |
ESPACIO_TRAS_BACKUP=`echo "scale=0; (${ESPACIO_LIBRE}-${ESPACIO_NECESARIO})/1" | bc -q` | |
if [ ${ESPACIO_TRAS_BACKUP} -lt 200 ] | |
then | |
echo "Error, la BBDD ocupa ${ESPACIO_OCUPADO_BBDD}MB, se estima que seran necesarios ${ESPACIO_NECESARIO}MB para realizar el backup pero el espacio libre disponible es de tan solo ${ESPACIO_LIBRE}MB. Imposible realizar el backup en esta situacion sin riesgo de saturar del disco principal de sistema." | |
exit 1 | |
fi | |
touch ${FICHERO_LOCK} | |
#Comienza a exportar todas las BBDD al fichero SQL | |
echo -n "Comenzando backup (${FECHA}) ... " | |
mysqldump --all-databases --add-drop-table --create-options --host=${IP} --user=${USUARIO} --password=${PASSWORD} > ${FICHERO_BACKUP} | |
echo "OK" | |
echo -n "Comprimiendo la exportacion generada ... " | |
gzip -9 ${FICHERO_BACKUP} | |
echo "OK" | |
#Por ultimo, se eliminan los ficheros viejos | |
echo -n "Eliminando los ficheros de backup con mas de ${PERIODO_DE_RETENCION_EN_DIAS} dias ... " | |
find ${DIRECTORIO_DESTINO} -iname "MySQL-BBDD-*.sql.gz" -mtime +${PERIODO_DE_RETENCION_EN_DIAS} -print | | |
while read FICHERO | |
do | |
rm -f "${FICHERO}" | |
done | |
echo "OK" | |
rm -f ${FICHERO_LOCK} >/dev/null 2>&1 | |
exit 0 | |
else | |
echo "Error, no existe el directorio destino de backups." | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment