#!/bin/bash ARCHPATH='/path/to/backup/daily/' SUBDIR='backup_data' SKIPDBS=('mysql') FULLRESTORE=false SETPOSDB=db_for_master_log_position CFG='~/.my.cnf' DRY_RUN=false NOPROMPT=false CMD='' #--- while [[ $# -gt 0 ]]; do case $1 in -d|--dry-run) DRY_RUN=true CMD=echo $PIPE shift 1 ;; -a|--arch) ARCHPATH=$2 shift 2 ;; -s|--subdir) SUBDIR=$2 shift 2 ;; -k|--skip) SKIPDBS=($2) shift 2 ;; -f|--full) FULLRESTORE=true shift 1 ;; -p|--pos) SETPOSDB=$2 shift 2 ;; -c|--cfg) CFG=$2 shift 2 ;; -n|--noprompt ) NOPROMPT=true shift 1 ;; *) echo 'error in command line parsing' >&2 exit 1 esac done ARCHPATH="${ARCHPATH%/}/" SUBDIR="${SUBDIR%/}/" echo "restore with options:" echo -e "\tarchive path = $ARCHPATH" echo -e "\tsubdirectory = $SUBDIR" echo -e "\tDBs to skip = ${SKIPDBS[@]}" echo -e "\tfull restore = $FULLRESTORE" echo -e "\tDB for position = $SETPOSDB" echo -e "\tconfig file = $CFG" echo -e "\tdry run = $DRY_RUN" echo -e "\n" if [[ -f "$CFG" ]]; then if [[ ! $(grep user ~/.my.cnf) ]]; then echo "ERROR: 'user' field not found in '$CFG'" exit 1 fi if [[ ! $(grep password ~/.my.cnf) ]]; then echo "ERROR: 'password' field not found in '$CFG'" exit 1 fi else echo "ERROR: configuration file '$CFG' not found" exit 1 fi if [[ $NOPROMPT == false ]]; then read -n 1 -p "restore? [Y/n] " reply; if [[ $reply == [Nn] ]]; then echo -e "\naborting" exit 1 fi fi echo -e "\nrestoring" START=$(date +%s) echo -e "\n--START: $(date +%F\ %T)\nrestoring most recent backups from $ARCHPATH" echo -e "\tstopping replication\n"; $CMD mysql -e "STOP SLAVE;" NEWDATE=$(ls -1r "$ARCHPATH" | head -1) NEWPATH="$ARCHPATH$NEWDATE$SUBDIR" if [[ $FULLRESTORE == false ]]; then OLDDATE=$(ls -1rt "$ARCHPATH" | head -1) OLDPATH="$ARCHPATH$OLDATE$SUBDIR" echo "compartive restoration selected: files from '$NEWDATE' will be compared to '$OLDDATE'" else echo "full restorion selected: comparisons will not be made, all non-excluded databases will be restored" fi for FILE in $NEWPATH/*.gz; do FILE=$(basename $FILE) STARTLOOP=$(date +%s) echo -e "\n$(date +%F\ %T) | $FILE:" if [[ "${SETPOSDB}" =~ ${FILE%%.*} ]]; then echo -e "\t-- recognized $FILE for master position and log file";SETPOSFILE=$(realpath $FILE); fi; if [[ "${SKIPDBS[*]}" =~ ${FILE%%.*} ]]; then echo -e "\t-- skipping $FILE";continue; fi; if [[ ! -z $OLDDATE ]]; then OLDSIZE=$(stat -c %s $OLDPATH$FILE) else OLDSIZE=0 fi NEWSIZE=$(stat -c %s $NEWPATH/$FILE) if [[ $OLDSIZE != $NEWSIZE ]]; then DB=$(echo $FILE | cut -f1 -d'.') echo -e "\tDROPPING/CREATING '$DB' pv $NEWPATH$FILE | gunzip | mysql $DB" $CMD mysql -e "DROP DATABASE IF EXISTS $DB" $CMD mysql -e "CREATE DATABASE $DB" if [[ -z $CMD ]]; then pv $NEWPATH$FILE | gunzip | mysql $DB; fi fi ENDLOOP=$(date +%s) EXCTIME=$(expr $ENDLOOP - $STARTLOOP) printf '\t%02dh:%02dm:%02ds\n' $((EXCTIME/3600)) $((EXCTIME%3600/60)) $((EXCTIME%60)) done if [[ -z $SETPOSFILE ]]; then SETPOSSTMT=$(zcat $SETPOSFILE | head -40 | grep MASTER | sed 's/-- //') echo "\n$SETPOSSTMT\n" $CMD mysql -e "$SETPOS" fi if [[ $NOPROMPT == false ]]; then read -n 1 -p "restart replication? [Y/n] " reply; if [[ $reply == [Nn] ]]; then echo -e "\naborting" exit 1 fi fi echo -e "restarting replication" $CMD mysql -e "START SLAVE" END=$(date +%s) EXCTIME=$(expr $END - $START) printf 'total runtime: %02dh:%02dm:%02ds\n' $((EXCTIME/3600)) $((EXCTIME%3600/60)) $((EXCTIME%60)) echo "--END: $(date +%F\ %T)\n\n"