Files
handy_scripts/restore-mysql-daily

250 lines
8.5 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
### 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-03 10:03:30 -04:00
2022-11-04 11:10:44 -04:00
while [[ $# -gt 0 ]]; do
case $1 in
-d|--dry-run)
DRY_RUN=true
CMD=echo
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-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 ]"
echo -e "\t -d| --dry-run no permanent changes, show plan [ flag ]"
2022-11-04 17:18:05 -04:00
echo -e "\t -a| --archive directory containing archive directories [ string ]"
2022-11-04 17:11:21 -04:00
echo -e "\t-sd| --subdir directory containing actual compressed sql files [ string ]"
echo -e "\t-sk| --skip databases to not restore (e.g. mysql would replace users) [ array ] mysql"
echo -e "\t -f| --full restore all tables not excluded (no comparison) [ array ] off"
echo -e "\t -p| --pos datbase from which master log position will be used [ string ]"
2022-11-05 08:11:16 -04:00
echo -e "\t -c| --cfg path of config file for mysql credentials [ string ] .my.cnf"
2022-11-04 17:11:21 -04:00
echo -e "\t-ns| --nostart do not restart replication [ flag ] off"
echo -e "\t-np| --noprompt do not prompt user (Y/n) [ flag ] off"
echo -e "\t-mh| --masterhost replication master host address [ string ]"
echo -e "\t-mu| --masteruser user name for replication [ string ]"
echo -e "\t-mp| --masterpass password for replication [ string ]"
2022-11-04 17:18:05 -04:00
echo -e "\nEXAMPLE: $0 -p main_db -sd backup_data -a /home/backups/mysql/daily -sk 'mysql performance_stats' --dry-run"
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
ARCHPATH="${ARCHPATH%/}/"
SUBDIR="${SUBDIR%/}/"
echo "restore with options:"
2022-11-04 17:11:21 -04:00
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"
echo -e "\treplication master = $MASTER_HOST"
echo -e "\treplication user = $MASTER_USER"
echo -e "\treplication password = $([[ $MASTER_PASSWORD ]] && echo true || echo false )"
2022-11-04 11:10:44 -04:00
echo -e "\n"
if [[ -f "$CFG" ]]; then
2022-11-04 13:53:47 -04:00
if [[ ! $(grep user $CFG) ]]; then
2022-11-04 11:10:44 -04:00
echo "ERROR: 'user' field not found in '$CFG'"
exit 1
fi
2022-11-04 13:53:47 -04:00
if [[ ! $(grep password $CFG) ]]; then
2022-11-04 11:10:44 -04:00
echo "ERROR: 'password' field not found in '$CFG'"
exit 1
fi
else
echo "ERROR: configuration file '$CFG' not found"
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
echo -e "\naborting"
exit 1
fi
2022-11-03 11:16:07 -04:00
fi
2022-11-04 11:10:44 -04:00
echo -e "\nrestoring"
START=$(date +%s)
2022-11-03 13:13:52 -04:00
echo -e "\n--START: $(date +%F\ %T)\nrestoring most recent backups from $ARCHPATH"
echo -e "\tstopping replication\n";
2022-11-04 11:10:44 -04:00
$CMD mysql -e "STOP SLAVE;"
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-05 08:16:56 -04:00
echo -e "compartive restoration selected:\n\tfiles from:\t'$NEWDATE'\n\tcompared to:\t'$OLDDATE'"
2022-11-03 11:16:07 -04:00
else
2022-11-04 11:10:44 -04:00
echo "full 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-05 08:27:27 -04:00
echo -e "\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)
echo -e "\n$(date +%F\ %T) | $FILE:"
2022-11-05 08:27:27 -04:00
if [[ "${SETPOSDB}" =~ ${FILE%%.*} ]]; then echo -e "\t-- recognized '$FILE' for master position and log file"
SETPOSFILE=$NEWPATH$FILE
2022-11-05 08:16:56 -04:00
SETPOSSTMT=$(zcat $SETPOSFILE | head -40 | grep MASTER | sed 's/-- //')
2022-11-05 08:27:27 -04:00
echo -e "\t$SETPOSSTMT"
2022-11-05 08:16:56 -04:00
fi
2022-11-05 08:11:16 -04:00
2022-11-03 16:18:40 -04:00
if [[ "${SKIPDBS[*]}" =~ ${FILE%%.*} ]]; then echo -e "\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-04 13:53:47 -04:00
echo -e "\t$OLDDATE:$FILE:$OLDSIZE != $NEWDATE:$FILE:$NEWSIZE"
2022-11-03 13:13:52 -04:00
DB=$(echo $FILE | cut -f1 -d'.')
2022-11-03 11:16:07 -04:00
echo -e "\tDROPPING/CREATING '$DB'
2022-11-04 11:10:44 -04:00
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
2022-11-03 10:03:30 -04:00
fi
2022-11-03 13:13:52 -04:00
ENDLOOP=$(date +%s)
EXCTIME=$(expr $ENDLOOP - $STARTLOOP)
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
echo -e "\napplying configuration for replication\n\thost: $MASTER_HOST\n\tuser: $MASTER_USER"
2022-11-04 17:11:21 -04:00
$CMD mysql -e "CHANGE MASTER TO master_host='$MASTER_HOST',master_user='$MASTER_USER',master_password='$MASTER_PASSWORD';"
else
2022-11-05 08:29:15 -04:00
echo -e "\nreplication credentials not configured"
2022-11-04 17:11:21 -04:00
fi
2022-11-05 08:27:27 -04:00
if [[ $SETPOSSTMT ]]; then
$CMD mysql -e "$SETPOSSTMT"
2022-11-04 17:11:21 -04:00
else
DONOTSTART=true
2022-11-05 08:27:27 -04:00
echo "\nmaster position database undefined, listing suggestions"
2022-11-04 17:11:21 -04:00
for FILE in $NEWPATH*.gz; do
echo $(basename $FILE)
echo -e "\t$(zcat $FILE | head -40 | grep MASTER)"
done
2022-11-04 11:10:44 -04:00
fi
if [[ $NOPROMPT == false ]]; then
read -n 1 -p "restart replication? [Y/n] " reply;
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-05 08:29:15 -04:00
echo -e "\nrestarting replication"
2022-11-04 17:11:21 -04:00
$CMD mysql -e "START SLAVE"
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-05 08:29:15 -04:00
printf '\ntotal runtime: %02dh:%02dm:%02ds\n' $((EXCTIME/3600)) $((EXCTIME%3600/60)) $((EXCTIME%60))
echo "\n--END: $(date +%F\ %T)\n\n"