Created
October 1, 2012 12:58
-
-
Save isidromerayo/3811661 to your computer and use it in GitHub Desktop.
restore mysql all tables from file per table
This file contains hidden or 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 | |
# comands | |
ECHO=/bin/echo | |
DATE=/bin/date | |
DF=/bin/df | |
EXPORT=/usr/bin/mysqldump | |
#validate number params | |
if [ $# -ne 3 ]; then | |
$ECHO "" | |
$ECHO "" | |
$ECHO "El número de parámetros debe ser 3" | |
$ECHO "Los parámetros deben ser [aplicacion] [bbdd] [servidor]" | |
$ECHO "" | |
$ECHO "" | |
exit; | |
fi | |
# Recogida de parametros | |
APLI=$1 | |
DB=$2 | |
SERVER=$3 | |
# connect to | |
USER="-u USER_BACKUP_ROLE" | |
PASSWORD="-pPWD_USER_BACKUP" | |
HOST="-h $SERVER" | |
# files and others | |
FICHERO_LOG="/tmp/backup_$APLI.log" | |
BACKUP_DIR="/backups_mysql/$APLI" | |
RESTORESCRIPT="$BACKUP_DIR/__restoreData.sql" | |
OPCIONS="--opt" | |
OPTIONS_TABLE="--add-drop-table" | |
DATE=`/bin/date '+%y%m%d_%H%M%S'` | |
LIS_DEST_MAIL="[email protected]" | |
# functions | |
function prepare_backup_to_restore() { | |
first=$(ls -t $BACKUP_DIR | head -1) | |
cd ${BACKUP_DIR} | |
tar xvfz $first | |
$ECHO "descomprimiendo ficheros..." | |
$ECHO ${BACKUP_DIR} | |
} | |
function restore_database_file_per_table() { | |
$ECHO "==========================================" | |
$ECHO ${DB} - Restaurando TODAS las tablas | |
$ECHO "==========================================" | |
cd ${BACKUP_DIR} | |
mysql $USER $PASSWORD $HOST $DB < $RESTORESCRIPT | |
} | |
function remove_temporal_files() { | |
$ECHO removing temporary files... | |
rm -f ${BACKUP_DIR}/*.sql > /dev/null 2>&1 | |
} | |
function init_process_report() { | |
>$FICHERO_LOG | |
local HORA_REAL=`date +%T" - "%A" "%d" de "%B" de "%Y` | |
$ECHO "-------------------------------------------------------------------------------------------" >> $FICHERO_LOG | |
$ECHO $HORA_REAL "- Inicio del proceso de RESTAURAR de la BB.DD. MySQL $APLI " >> $FICHERO_LOG | |
$ECHO "-------------------------------------------------------------------------------------------" >> $FICHERO_LOG | |
} | |
function finish_process_report() { | |
local HORA_REAL=`date +%T` | |
$ECHO "-------------------------------------------------------------------------------------------" >> $FICHERO_LOG | |
$ECHO $HORA_REAL "- Estado del filesystem del dir. $DIR_BACKUP despues de la limpieza" >> $FICHERO_LOG | |
$ECHO "-------------------------------------------------------------------------------------------" >> $FICHERO_LOG | |
$DF -h $DIR_BACKUP >> $FICHERO_LOG | |
$ECHO "===========================================================================================" >> $FICHERO_LOG | |
} | |
# | |
# 3,2,1,zerooooooo!! | |
# | |
$ECHO "start... " | |
init_process_report | |
# | |
prepare_backup_to_restore | |
restore_database_file_per_table | |
# | |
remove_temporal_files | |
finish_process_report | |
mailx -s "Restaurar MySQL $BBDD desde ficheros del dir. $BACKUP_DIR en el host $SERVER" $LIS_DEST_MAIL < $FICHERO_LOG | |
$ECHO "...finish " |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment