#!/bin/bash MYSQL_OUTLOG="/root/mysql_innodb_restore.mysqloutput" ERROR_LOG="/root/mysql_innodb_restore.err" STAGING_DB="lw_innodb_restore" RESTORE_BASEDIR="/home/temp/mysql-restore-2015.07.20" DATABACKUP_BASEDIR="/home/temp/mysql_data_backups" MYSQL_DATADIR="/var/lib/mysql" echo -e "------Initiated at `date`------\n\n\n" mysql -e "CREATE DATABASE IF NOT EXISTS $STAGING_DB;" while read db; do user=$(grep "$db:" /var/cpanel/databases/dbindex.db | awk '{print $2}') restore_dir="$RESTORE_BASEDIR/$user/mysql" backup_dir="$DATABACKUP_BASEDIR/$db" echo -e "\n ---------------------------------------------------------------------------- " echo -e " DB: Processing $db (cPanel user $user)"; echo -e " ---------------------------------------------------------------------------- \n" if [ ! -e "$restore_dir/$db.sql" ]; then echo " ERROR: $restore_dir/$db.sql doesn't exist? Skipping" | tee -a $ERROR_LOG; continue; fi # Not a reliable way to check, not all dumps end like this. #if [[ ! $(tail -1 "$restore_dir/$db.sql") =~ "Dump completed" ]]; then # echo " ERROR: $restore_dir/$db.sql is incomplete! Skipping" | tee -a $ERROR_LOG; # continue; #fi if [ ! -d "$restore_dir/$db" ]; then echo -e " DIR: Creating directory for table dumps:\n\t $restore_dir/$db"; mkdir "$restore_dir/$db" fi if [ ! -d "$backup_dir" ]; then echo -e " DIR: Creating directory for data backups:\n\t $backup_dir"; mkdir $backup_dir fi echo -e "\n DB: Emptying and repopulating staging database from $restore_dir/$db.sql\n"; mysql -e "DROP DATABASE $STAGING_DB; CREATE DATABASE $STAGING_DB;" >> $MYSQL_OUTLOG 2>&1; mysql $STAGING_DB < "$restore_dir/$db.sql" >> $MYSQL_OUTLOG 2>&1; if [[ $? != 0 ]]; then echo -e "\n !!! ERROR: $restore_dir/$db.sql did not import successfully! Skipping" | tee -a $ERROR_LOG; continue; fi while read table; do mysql $STAGING_DB -e "SELECT COUNT(1) FROM $table;" >/dev/null; if [[ $? != 0 ]]; then echo " ERROR: $restore_dir/$db.sql is missing $table! Skipping" | tee -a $ERROR_LOG; continue 2; fi done < "/home/temp/mysql_tablelists/$db" while read table; do tabledump_file="$restore_dir/$db/$table.sql"; echo -e " TABLE $table: Dumping to:\n\t $tabledump_file"; mysqldump $STAGING_DB $table > $tabledump_file; if [[ $? != 0 ]]; then echo -e "\n !!! ERROR: $db.$table dump did not complete successfully !!!" | tee -a $ERROR_LOG; # dwells - operations on some tables may cause innodb to restart # allow time for that to finish echo " !!! SKIPPING RESTORE AND SLEEPING FOR 4 SECONDS !!! "; sleep 4; continue; fi echo -e " BACKUP: Backing up .frm file to:\n\t $backup_dir"; cp "$MYSQL_DATADIR/$db/$table.frm" "$backup_dir"; echo " TABLE $table: Discarding tablespace and dropping table"; mysql $db -e "ALTER TABLE $table DISCARD TABLESPACE; DROP TABLE $table;" >> $MYSQL_OUTLOG 2>&1; echo -e " TABLE $table: Moving .ibd file to:\n\t $backup_dir"; mv "$MYSQL_DATADIR/$db/$table.ibd" "$backup_dir/"; echo " TABLE $table: Restoring $db.$table from table dump"; mysql $db < $tabledump_file >> $MYSQL_OUTLOG 2>&1; done < "/home/temp/mysql_tablelists/$db" done < /root/dbs_to_restore echo -e "\n\n\n------Finished at `date`------"