Created
January 20, 2017 00:20
-
-
Save Adadov/fdeea09f1f4ea99d5a03683a148a4e78 to your computer and use it in GitHub Desktop.
Script de backup pour MySQL. Date chaque sauvegarde et sépare les DB.
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
#!/usr/bin/bash | |
MYSQL_USER='' # Nom d'utilisateur pour MySQL | |
MYSQL_PWD='' # Password pour MySQL | |
DIST_USER='' # Nom d'utilisateur pour la connexion SSH au serveur de backup | |
DIST_SRV='' # Adresse ou non du serveur de backup | |
DIST_REP='' # Répertoire où envoyer le fichier sur le serveur de backup | |
RETDAY='7' # Nombre de jour avant suppression du fichier en local | |
LOG_FILE='/var/log/backup.log' # Fichier contenant les logs du backup | |
DATE=$(date +%Y%m%d) | |
HOST=$(hostname -s) | |
DATADIR='/var/lib/mysql' | |
BACKDIR='/srv/backup' | |
CGZIP='/bin/gzip' | |
CFIND='/bin/find' | |
RSYNC='/usr/bin/rsync -ve "ssh -i /root/.ssh/id_rsa_backup"' | |
############################################ | |
## RIEN A MODIFIER AU DELÀ DE CETTE LIGNE ## | |
############################################ | |
SCRIPTPATH=$(dirname $0) | |
printf "[%s] Lancement de %s\n" "$(date +'%d/%m/%Y %H:%M')" "$0" >> ${LOG_FILE} | |
for i in `${CFIND} ${DATADIR}/* -type d`; do | |
DB=$(basename $i) | |
grep ${DB} ${SCRIPTPATH}/backupMysql.exclude > /dev/null 2>&1 | |
if [ $? -eq 1 ]; then | |
LEN=$((25-${#DB})) | |
printf "Sauvegarde de %s" ${DB} >> ${LOG_FILE} | |
\mysqldump --login-path=backup --routines ${DB} | ${CGZIP} -9 > ${BACKDIR}/sql_${DB}_${DATE}.sql.gz | |
if [ $? -eq 0 ]; then | |
OUT="OK" | |
else | |
OUT="FAIL" | |
fi | |
printf "%${LEN}s\n" "[${OUT}]" >> ${LOG_FILE} | |
fi | |
done | |
DB=mysql | |
printf "Sauvegarde de %s" ${DB} >> ${LOG_FILE} | |
\mysqldump --login-path=backup --routines --events ${DB} | ${CGZIP} -9 > ${BACKDIR}/sql_${DB}_${DATE}.sql.gz | |
LEN=$((25-${#DB})) | |
if [ $? -eq 0 ]; then | |
OUT="OK" | |
else | |
OUT="FAIL" | |
fi | |
printf "%${LEN}s\n" "[${OUT}]" >> ${LOG_FILE} | |
if [ ! -z "${DIST_SRV}" ]; then | |
printf "[%s] Transfert vers le serveur %s\n" "$(date +'%d/%m/%Y %H:%M')" "${DIST_SRV}" >> ${LOG_FILE} 2>&1 | |
${RSYNC} ${BACKDIR}/*_${DATE}.sql.gz ${DIST_USER}@${DIST_SRV}:${DIST_REP}/ >> ${LOG_FILE} 2>&1 | |
fi | |
${CFIND} ${BACKDIR}/*.sql.gz -ctime +${RETDAY} -type f -delete |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment