Files
handy_scripts/restore-mysql-daily

279 lines
9.7 KiB
Plaintext
Raw Normal View History

2022-11-03 10:03:30 -04:00
#!/bin/bash
2022-11-04 20:22:07 -04:00
#--- --- --- --- --- --- --- --- --- --- --- --- --- --- ---#
## script was written with daily, dated, backups from holland backup #
## mysqldump per table in directories with date/time like
## /backups/daily/20221104_65127/backup_data/database-name.sql.gz
##
### path to archive directory. e.g., /home/backupusr/backups/daily/
2022-11-03 16:18:40 -04:00
ARCHPATH='/path/to/backup/daily/'
2022-11-04 20:22:07 -04:00
### sudirectory from date. e.g., /backup_data/
2022-11-03 16:18:40 -04:00
SUBDIR='backup_data'
2022-11-04 20:22:07 -04:00
### databases to skip. e.g., mysql
2022-11-04 11:10:44 -04:00
SKIPDBS=('mysql')
2022-11-04 20:22:07 -04:00
### restore only changed databases or restore all
2022-11-04 11:10:44 -04:00
FULLRESTORE=false
2022-11-04 20:22:07 -04:00
### database to use for master log position
2022-11-03 11:29:03 -04:00
SETPOSDB=db_for_master_log_position
2022-11-04 20:22:07 -04:00
### config file for mysql credentials
2022-11-05 08:11:16 -04:00
CFG='.my.cnf'
2022-11-04 20:22:07 -04:00
### dry-run will show actions for testing
2022-11-04 11:10:44 -04:00
DRY_RUN=false
2022-11-04 20:22:07 -04:00
### noprompt for skipping user input
2022-11-04 11:10:44 -04:00
NOPROMPT=false
2022-11-04 20:22:07 -04:00
### do not start will not restart replication when complete
2022-11-04 17:11:21 -04:00
DONOTSTART=false
2022-11-04 20:22:07 -04:00
2022-11-08 07:46:27 -05:00
### file to log output
LOG_FILE="_$(basename $0).$(date +%F_%T).log"
2022-11-04 20:22:07 -04:00
### values for configuring replication master
2022-11-04 20:23:35 -04:00
### empty if replication already configured
2022-11-04 17:11:21 -04:00
MASTER_HOST=''
MASTER_USER=''
MASTER_PASSWORD=''
2022-11-04 20:22:07 -04:00
2022-11-08 07:46:27 -05:00
2022-11-04 20:22:07 -04:00
# #
#--- --- --- --- --- --- --- --- --- --- --- --- --- --- ---#
2022-11-03 10:03:30 -04:00
2022-11-08 07:46:27 -05:00
2022-11-04 11:10:44 -04:00
while [[ $# -gt 0 ]]; do
case $1 in
-d|--dry-run)
2022-11-08 07:46:27 -05:00
DRY_RUN=true
2022-11-04 11:10:44 -04:00
shift 1
;;
2022-11-04 17:18:05 -04:00
-a|--archive)
2022-11-04 11:10:44 -04:00
ARCHPATH=$2
shift 2
;;
2022-11-04 17:11:21 -04:00
-sd|--subdir)
2022-11-04 11:10:44 -04:00
SUBDIR=$2
shift 2
;;
2022-11-04 17:11:21 -04:00
-sk|--skip)
2022-11-04 11:10:44 -04:00
SKIPDBS=($2)
shift 2
;;
-f|--full)
FULLRESTORE=true
shift 1
;;
-p|--pos)
SETPOSDB=$2
shift 2
;;
-c|--cfg)
CFG=$2
shift 2
;;
2022-11-08 07:46:27 -05:00
-l|--log)
LOG_FILE=$2
shift 2
;;
-nl|--nolog)
LOG_FILE=''
shift 2
;;
2022-11-04 17:11:21 -04:00
-ns|--nostart)
DONOTSTART=true
shift 1
;;
-np|--noprompt)
2022-11-04 11:10:44 -04:00
NOPROMPT=true
shift 1
;;
2022-11-04 17:11:21 -04:00
-mh|--masterhost)
MASTER_HOST=$2
shift 2
;;
2022-11-05 07:49:33 -04:00
-mu|--masteruser)
2022-11-04 17:11:21 -04:00
MASTER_USER=$2
shift 2
;;
2022-11-05 07:49:33 -04:00
-mp|--masterpass)
2022-11-04 17:11:21 -04:00
MASTER_PASSWORD=$2
shift 2
;;
2022-11-04 13:53:47 -04:00
-h|--help)
2022-11-04 20:22:07 -04:00
echo -e "\n\n\t _____________________________/‾‾‾ MySQL restoration script options ‾‾‾\____________________________________\n"
2022-11-04 17:11:21 -04:00
echo -e "\t | arg | | type | default |\n"
echo -e "\t ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾"
echo -e "\t -h| --help display this menu [ flag ]"
2022-11-08 07:46:27 -05:00
echo -e "\t -d| --dry-run no permanent changes, show plan [ flag ] $DRY_RUN"
echo -e "\t -a| --archive directory containing archive directories [ string ] $ARCHPATH"
echo -e "\t-sd| --subdir directory containing actual compressed sql files [ string ] $SUBDIR"
echo -e "\t-sk| --skip databases to not restore (e.g. mysql would replace users) [ array ] $SKIPDBS"
echo -e "\t -f| --full restore all tables not excluded (no comparison) [ array ] $FULLRESTORE"
echo -e "\t -p| --pos datbase from which master log position will be used [ string ] $SETPOSFILE"
echo -e "\t -c| --cfg path of config file for mysql credentials [ string ] $CFG"
echo -e "\t -l| --log path of log file [ string ] $LOG_FILE"
echo -e "\t-nl| --nolog disable logging [ string ] $([[ $LOG_FILE ]] && echo false || echo true)"
echo -e "\t-ns| --nostart do not restart replication [ flag ] $DONOTSTART"
echo -e "\t-np| --noprompt do not prompt user (Y/n) [ flag ] $NOPROMPT"
echo -e "\t-mh| --masterhost replication master host address [ string ] $MASTER_HOST"
echo -e "\t-mu| --masteruser user name for replication [ string ] $MASTER_USER"
echo -e "\t-mp| --masterpass password for replication [ string ] $([[ $MASTER_PASSWORD ]] && echo true || echo false)"
echo -e "\nEXAMPLE: $0 -p main_db -sd backup_data -a /home/backups/mysql/daily -sk 'mysql performance_stats' --dry-run -np"
2022-11-04 13:53:47 -04:00
exit
;;
*)
echo 'error in command line parsing' >&2
echo "try: '$0 --help"
2022-11-04 11:10:44 -04:00
exit 1
esac
done
2022-11-08 07:46:27 -05:00
function hecko() {
if [[ $LOG_FILE ]]; then echo -e "$1" | tee -a $LOG_FILE
else echo -e "$1"; fi
}
function xct() {
if [[ $DRY_RUN == true ]]; then hecko "\t$(tput setaf 3)EXEC PLAN: $(tput sgr 0)$1"
else hecko "\t$(tput setaf 5)EXECUTE: $(tput sgr 0)$1"; eval $1; fi
}
2022-11-04 11:10:44 -04:00
ARCHPATH="${ARCHPATH%/}/"
SUBDIR="${SUBDIR%/}/"
2022-11-08 07:46:27 -05:00
hecko "\n\n\t________/‾‾‾ MySQL restoration script options ‾‾‾\___________________\n"
hecko "\tarchive path = $ARCHPATH"
hecko "\tsubdirectory = $SUBDIR"
hecko "\tDBs to skip = ${SKIPDBS[@]}"
hecko "\tfull restore = $FULLRESTORE"
hecko "\tDB for position = $SETPOSDB"
hecko "\tconfig file = $CFG"
hecko "\tdry run = $DRY_RUN"
hecko "\n"
hecko "\treplication master = $MASTER_HOST"
hecko "\treplication user = $MASTER_USER"
hecko "\treplication password = $([[ $MASTER_PASSWORD ]] && echo true || echo false)"
hecko "\n"
hecko "\tlog file = $([[ $LOG_FILE ]] && echo $LOG_FILE || echo false)"
hecko "\t_____________________________________________________________________________\n"
2022-11-04 11:10:44 -04:00
if [[ -f "$CFG" ]]; then
2022-11-04 13:53:47 -04:00
if [[ ! $(grep user $CFG) ]]; then
2022-11-08 07:46:27 -05:00
hecko "ERROR: 'user' field not found in '$CFG'"
2022-11-04 11:10:44 -04:00
exit 1
fi
2022-11-04 13:53:47 -04:00
if [[ ! $(grep password $CFG) ]]; then
2022-11-08 07:46:27 -05:00
hecko "ERROR: 'password' field not found in '$CFG'"
2022-11-04 11:10:44 -04:00
exit 1
fi
else
2022-11-08 07:46:27 -05:00
hecko "ERROR: configuration file '$CFG' not found"
2022-11-04 11:10:44 -04:00
exit 1
2022-11-03 11:16:07 -04:00
fi
2022-11-04 11:10:44 -04:00
if [[ $NOPROMPT == false ]]; then
read -n 1 -p "restore? [Y/n] " reply;
if [[ $reply == [Nn] ]]; then
2022-11-08 07:46:27 -05:00
hecko "\naborting"
2022-11-04 11:10:44 -04:00
exit 1
fi
2022-11-03 11:16:07 -04:00
fi
2022-11-08 07:46:27 -05:00
hecko "\nrestoring"
2022-11-04 11:10:44 -04:00
START=$(date +%s)
2022-11-08 07:46:27 -05:00
hecko "\n--START: $(date +%F\ %T)\nrestoring most recent backups from $ARCHPATH"
hecko "\tstopping replication";
xct "mysql -e 'STOP SLAVE;'"
2022-11-04 11:10:44 -04:00
NEWDATE=$(ls -1r "$ARCHPATH" | head -1)
2022-11-04 13:53:47 -04:00
NEWPATH="$ARCHPATH$NEWDATE/$SUBDIR"
2022-11-03 10:03:30 -04:00
if [[ $FULLRESTORE == false ]]; then
2022-11-04 11:10:44 -04:00
OLDDATE=$(ls -1rt "$ARCHPATH" | head -1)
2022-11-04 13:53:47 -04:00
OLDPATH="$ARCHPATH$OLDDATE/$SUBDIR"
2022-11-08 07:46:27 -05:00
hecko "\n\ncompartive restoration selected:\n\tfiles from:\t'$NEWDATE'\n\tcompared to:\t'$OLDDATE'"
2022-11-03 11:16:07 -04:00
else
2022-11-08 07:46:27 -05:00
hecko "\n\nfull restorion selected: comparisons will not be made, all non-excluded databases will be restored"
2022-11-03 10:03:30 -04:00
fi
2022-11-08 07:46:27 -05:00
hecko "\nfinding files in '$NEWPATH*.gz'"
2022-11-04 13:53:47 -04:00
for FILE in $NEWPATH*.gz; do
2022-11-05 08:18:35 -04:00
STARTLOOP=$(date +%s)
FILE=$(basename $FILE)
2022-11-08 07:46:27 -05:00
hecko "\n$(date +%F\ %T) | $FILE:"
2022-11-05 08:18:35 -04:00
2022-11-08 07:46:27 -05:00
if [[ "${SETPOSDB}" =~ ${FILE%%.*} ]]; then hecko "\t-- recognized '$FILE' for master position and log file"
2022-11-05 08:27:27 -04:00
SETPOSFILE=$NEWPATH$FILE
2022-11-05 08:16:56 -04:00
SETPOSSTMT=$(zcat $SETPOSFILE | head -40 | grep MASTER | sed 's/-- //')
2022-11-08 07:46:27 -05:00
hecko "\t$SETPOSSTMT"
2022-11-05 08:16:56 -04:00
fi
2022-11-05 08:11:16 -04:00
2022-11-08 07:46:27 -05:00
if [[ "${SKIPDBS[*]}" =~ ${FILE%%.*} ]]; then hecko "\t-- skipping $FILE";continue; fi;
2022-11-03 14:10:42 -04:00
2022-11-04 13:53:47 -04:00
if [[ FULLRESTORE == false ]]; then
2022-11-04 11:10:44 -04:00
OLDSIZE=$(stat -c %s $OLDPATH$FILE)
2022-11-03 10:03:30 -04:00
else
OLDSIZE=0
fi
2022-11-04 13:53:47 -04:00
NEWSIZE=$(stat -c %s $NEWPATH$FILE)
2022-11-03 10:03:30 -04:00
if [[ $OLDSIZE != $NEWSIZE ]]; then
2022-11-08 07:46:27 -05:00
hecko "\t$OLDDATE:$FILE:$OLDSIZE != $NEWDATE:$FILE:$NEWSIZE"
2022-11-03 13:13:52 -04:00
DB=$(echo $FILE | cut -f1 -d'.')
2022-11-08 07:46:27 -05:00
hecko "\tDROPPING/CREATING '$DB'
2022-11-04 11:10:44 -04:00
pv $NEWPATH$FILE | gunzip | mysql $DB"
2022-11-08 07:46:27 -05:00
xct "mysql -e 'DROP DATABASE IF EXISTS $DB'"
xct "mysql -e 'CREATE DATABASE $DB'"
xct "pv $NEWPATH$FILE | gunzip | mysql $DB;"
2022-11-03 10:03:30 -04:00
fi
2022-11-03 13:13:52 -04:00
ENDLOOP=$(date +%s)
EXCTIME=$(expr $ENDLOOP - $STARTLOOP)
2022-11-08 07:46:27 -05:00
hecko $(printf '\t%02dh:%02dm:%02ds\n' $((EXCTIME/3600)) $((EXCTIME%3600/60)) $((EXCTIME%60)))
2022-11-03 10:03:30 -04:00
done
2022-11-05 08:27:27 -04:00
if [[ $MASTER_HOST && $MASTER_USER && $MASTER_PASSWORD ]]; then
2022-11-08 07:46:27 -05:00
hecko "\napplying configuration for replication\n\thost: $MASTER_HOST\n\tuser: $MASTER_USER"
2022-11-08 11:52:14 -05:00
xct 'mysql -e "CHANGE MASTER TO master_host=$MASTER_HOST,master_user=$MASTER_USER,master_password=$MASTER_PASSWORD;"'
2022-11-04 17:11:21 -04:00
else
2022-11-08 07:46:27 -05:00
hecko "\nwarning: replication credentials not present; this might be bad if replication host is not already configured"
2022-11-04 17:11:21 -04:00
fi
2022-11-05 08:27:27 -04:00
if [[ $SETPOSSTMT ]]; then
2022-11-08 07:46:27 -05:00
xct "mysql -e '$SETPOSSTMT'"
2022-11-04 17:11:21 -04:00
else
DONOTSTART=true
2022-11-08 07:46:27 -05:00
hecko "\nmaster position database undefined, replication will not be started\n\tlisting suggestions:"
2022-11-04 17:11:21 -04:00
for FILE in $NEWPATH*.gz; do
2022-11-08 07:46:27 -05:00
hecko $(basename $FILE)
hecko "\t$(zcat $FILE | head -40 | grep MASTER)"
2022-11-04 17:11:21 -04:00
done
2022-11-04 11:10:44 -04:00
fi
if [[ $NOPROMPT == false ]]; then
2022-11-08 07:46:27 -05:00
read -n1 -p -t600 "restart replication? [Y/n] " reply;
2022-11-04 11:10:44 -04:00
if [[ $reply == [Nn] ]]; then
2022-11-04 17:11:21 -04:00
DONOTSTART=true
2022-11-04 11:10:44 -04:00
fi
2022-11-03 10:03:30 -04:00
fi
2022-11-04 17:11:21 -04:00
if [[ $DONOTSTART != true ]]; then
2022-11-08 07:46:27 -05:00
hecko "\nrestarting replication"
xct "mysql -e 'START SLAVE'"
2022-11-04 17:11:21 -04:00
fi
2022-11-03 14:10:42 -04:00
2022-11-03 10:03:30 -04:00
END=$(date +%s)
EXCTIME=$(expr $END - $START)
2022-11-08 07:46:27 -05:00
hecko $(printf '\t%02dh:%02dm:%02ds\n' $((EXCTIME/3600)) $((EXCTIME%3600/60)) $((EXCTIME%60)))
hecko "\n--END: $(date +%F\ %T)\n\n"
exit 0