diff --git a/restore-mysql-daily b/restore-mysql-daily index 0118f61..4027551 100644 --- a/restore-mysql-daily +++ b/restore-mysql-daily @@ -7,8 +7,12 @@ SETPOSDB=db_for_master_log_position CFG='~/.my.cnf' DRY_RUN=false NOPROMPT=false +DONOTSTART=false CMD='' -#--- +MASTER_HOST='' +MASTER_USER='' +MASTER_PASSWORD='' +#--- --- --- --- --- --- ---# while [[ $# -gt 0 ]]; do case $1 in @@ -21,11 +25,11 @@ while [[ $# -gt 0 ]]; do ARCHPATH=$2 shift 2 ;; - -s|--subdir) + -sd|--subdir) SUBDIR=$2 shift 2 ;; - -k|--skip) + -sk|--skip) SKIPDBS=($2) shift 2 ;; @@ -41,23 +45,43 @@ while [[ $# -gt 0 ]]; do CFG=$2 shift 2 ;; - -n|--noprompt) + -ns|--nostart) + DONOTSTART=true + shift 1 + ;; + -np|--noprompt) NOPROMPT=true shift 1 ;; + -mh|--masterhost) + MASTER_HOST=$2 + shift 2 + ;; + -sd|--subdir) + MASTER_USER=$2 + shift 2 + ;; + -sd|--subdir) + MASTER_PASSWORD=$2 + shift 2 + ;; -h|--help) - echo -e "\n\n\t______________________________ MySQL restoration script options ____________________________________\n" - 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 ]" - echo -e "\t-a| --arch directory containing archive directories [ string ]" - echo -e "\t-s| --subdir directory containing actual compressed sql files [ string ]" - echo -e "\t-k| --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 ]" - echo -e "\t-c| --cfg path of config file for mysql credentials [ string ] ~/.my.cnf" - echo -e "\t-n| --noprompt do not prompt user (Y/n) [ flag ] off" + echo -e "\n\n\t ______________________________ MySQL restoration script options ____________________________________\n" + 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 ]" + echo -e "\t -a| --arch directory containing archive directories [ string ]" + 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 ]" + echo -e "\t -c| --cfg path of config file for mysql credentials [ string ] ~/.my.cnf" + 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 ]" echo -e "\nEXAMPLE: $0 -p main_db -s backup_data -a /home/backups/mysql/daily -k 'mysql performance_stats' --dry-run" exit ;; @@ -71,16 +95,20 @@ 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 "\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 )" +echo -e "\n" + if [[ -f "$CFG" ]]; then if [[ ! $(grep user $CFG) ]]; then @@ -123,7 +151,7 @@ else echo "full restorion selected: comparisons will not be made, all non-excluded databases will be restored" fi -echo "finding files '$NEWPATH*.gz'" +echo "\nfinding files in '$NEWPATH*.gz'" for FILE in $NEWPATH*.gz; do FILE=$(basename $FILE) STARTLOOP=$(date +%s) @@ -154,22 +182,37 @@ for FILE in $NEWPATH*.gz; do printf '\t%02dh:%02dm:%02ds\n' $((EXCTIME/3600)) $((EXCTIME%3600/60)) $((EXCTIME%60)) done -if [[ -z $SETPOSFILE ]]; then +if [[ MASTER_HOST && MASTER_USER && MASTER_PASSWORD ]]; then + echo "applying configuration for replication from $MASTER_HOST" + $CMD mysql -e "CHANGE MASTER TO master_host='$MASTER_HOST',master_user='$MASTER_USER',master_password='$MASTER_PASSWORD';" +else + echo "replication credentials not configured" +fi + +if [[ $SETPOSFILE ]]; then SETPOSSTMT=$(zcat $SETPOSFILE | head -40 | grep MASTER | sed 's/-- //') - echo "\n$SETPOSSTMT\n" + echo -e "\n$SETPOSSTMT\n" $CMD mysql -e "$SETPOS" +else + DONOTSTART=true + echo "master position database undefined, listing suggestions" + for FILE in $NEWPATH*.gz; do + echo $(basename $FILE) + echo -e "\t$(zcat $FILE | head -40 | grep MASTER)" + done fi if [[ $NOPROMPT == false ]]; then read -n 1 -p "restart replication? [Y/n] " reply; if [[ $reply == [Nn] ]]; then - echo -e "\naborting" - exit 1 + DONOTSTART=true fi fi -echo -e "restarting replication" -$CMD mysql -e "START SLAVE" +if [[ $DONOTSTART != true ]]; then + echo -e "restarting replication" + $CMD mysql -e "START SLAVE" +fi END=$(date +%s) EXCTIME=$(expr $END - $START)