#!/bin/bash USER=`whoami` PWFile="/home/$USER/PW/mysql_ED8.info" DATABASEName="`cat $PWFile | grep Database_Name: | awk '{print $NF}'`" DATABASEUser="`cat $PWFile | grep ID: | awk '{print $NF}'`" DATABASEPassword="`cat $PWFile | grep Password: | awk '{print $NF}'`" TS=`date +"%Y%m%d-%H%M%S"` LOG_Folder="/ED/ED8/Script_ED8/_LOG" LOG1=/ED/ED8/Script_ED8/_LOG/ED8backup-mysql-Auto.log ED8_Bak_Folder="/ED/ED8/Backup_file" ED8_Bak_File_Name=$ED8_Bak_Folder/backup-mysql-ED8.gz ED8_Bak_File_Name_TS=$ED8_Bak_Folder/backup-mysql-ED8-$TS.gz BAK_Disk_A=/dev/sdb BAK_Disk_A_Mount_Point="/BAK_Disk_A" BAK_Disk_A_Hostname_Folder="$BAK_Disk_A_Mount_Point/`hostnamectl | grep "Static hostname" | awk '{print $NF}'`" BAK_Disk_A_ED8_BAK_Folder="$BAK_Disk_A_Hostname_Folder/ED8/Backup_file" BAK_Disk_A_ED8_Bak_File_Name_TS="$BAK_Disk_A_ED8_BAK_Folder/backup-mysql-ED8-$TS.gz" BAK_Disk_B=/dev/sda BAK_Disk_B_Mount_Point="/BAK_Disk_B" BAK_Disk_B_Hostname_Folder="$BAK_Disk_B_Mount_Point/`hostnamectl | grep "Static hostname" | awk '{print $NF}'`" BAK_Disk_B_ED8_BAK_Folder="$BAK_Disk_B_Hostname_Folder/ED8/Backup_file" BAK_Disk_B_ED8_Bak_File_Name_TS="$BAK_Disk_B_ED8_BAK_Folder/backup-mysql-ED8-$TS.gz" ##### FUNC CHK_PWFile (){ echo -e "\033[32mCheck the mysql password info file exist: \033[0m" ls $PWFile > /dev/null 2>&1 if [[ $? != 0 ]]; then echo -e " >> \033[31mFail. $PWFile didn't exist. Please create one. Exit\033[0m" | tee -a $LOG1 echo echo -e "$PWFile Content example: " echo -e "Database_Name: db_ed8_" echo -e "ID: root" echo -e "Password: +039*******" echo exit else echo -e " >> \033[34mPass. $PWFile exist.\033[0m" | tee -a $LOG1 echo -e -n " Check context information accuracy (DATABASEName)\t\t: " if [[ $DATABASEName == "" ]]; then echo -e "\033[31mFail. There is no correct info of DATABASEName in $PWFile. Exit\033[0m" echo exit else echo -e "\033[34mPass. \033[0m" fi echo -e -n " Check context information accuracy (DATABASEUser)\t\t: " if [[ $DATABASEUser == "" ]]; then echo -e "\033[31mFail. There is no correct info of DATABASEUser in $PWFile. Exit\033[0m" echo exit else echo -e "\033[34mPass. \033[0m" fi echo -e -n " Check context information accuracy (DATABASEPassword)\t: " if [[ $DATABASEPassword == "" ]]; then echo -e "\033[31mFail. There is no correct info of DATABASEPassword in $PWFile. Exit\033[0m" echo exit else echo -e "\033[34mPass. \033[0m" fi fi } CHK_LOG_Folder (){ echo -e "\033[32mCheck the auto backup log saving folder exist: \033[35m($LOG_Folder) \033[0m" ls $LOG_Folder > /dev/null 2>&1 if [[ $? != 0 ]]; then echo -e " >> \033[33mWarning! There is no folder \"$LOG_Folder\". Next will create a new one.\033[0m" | tee -a $LOG1 sudo mkdir -p $LOG_Folder chown $USER:$USER $LOG_Folder -R if [[ $? != 0 ]]; then echo -e " >> \033[41;37mCreate the folder failed. Exit\033[0m" echo exit fi else echo -e " >> \033[34mPass. There is a folder \"$LOG_Folder\". \033[0m" | tee -a $LOG1 fi } CHK_ED8_Bak_Folder (){ echo -e "\033[32mCheck the local original backup folder exist: \033[35m($ED8_Bak_Folder) \033[0m" ls $ED8_Bak_Folder > /dev/null 2>&1 if [[ $? == 0 ]]; then echo -e " >> \033[34mPass. Backup file saving folder: $ED8_Bak_Folder exist, go next.\033[0m" | tee -a $LOG1 else echo -e " >> \033[33mWarning! There is no Backup file saving folder: $ED8_Bak_Folder, next will create a new one automatically.\033[0m" | tee -a $LOG1 sudo mkdir -p $ED8_Bak_Folder chown $USER:$USER $ED8_Bak_Folder -R if [[ $? != 0 ]]; then echo -e " >> \033[41;37mCreate the folder failed. Exit\033[0m" echo exit fi fi } Mysql_Dump (){ echo -e "\033[32mStarting mysqldump...\033[0m" echo -e "\033[45;37m==========================================================================================\033[0m" | tee -a $LOG1 echo -e "\033[41;37m Important! Please check below messages for making sure mysqldump be performed exactly. \033[0m" | tee -a $LOG1 mysqldump -u $DATABASEUser -p$DATABASEPassword $DATABASEName | gzip > $ED8_Bak_File_Name # mysqldump -u $DATABASEUser -p$DATABASEPassword $DATABASEName | gzip > $ED8_Bak_File_Name if [[ $? == 0 ]]; then echo -e " >> \033[34mPass. mysql dump w/ gzip OK. \033[0m" | tee -a $LOG1 else echo -e " >> \033[41;37m Fail. mysql dump w/ gzip failed. Exit \033[0m" | tee -a $LOG1 echo -e " >> \033[31m If you got error message, please try to perform below command for debugging\033[0m" | tee -a $LOG1 echo -e " >> \033[31m # sudo mysqldump -u ed -p db_ed8_ | gzip > $ED8_Bak_File_Name \033[0m" | tee -a $LOG1 echo exit fi echo -e "\033[45;37m==========================================================================================\033[0m" | tee -a $LOG1 echo -e " Debug message: " echo -e " \033[31m* mysqldump: Got error: 1045: Access denied for user 'root'@'localhost' (using password: YES) when trying to connect \033[0m " echo -e " >> Please check the content of $PWFile" echo -e "\033[45;37m==========================================================================================\033[0m" echo -e "\033[32mStarting make a copy with timastamp filename...\033[0m" | tee -a $LOG1 cp $ED8_Bak_File_Name $ED8_Bak_File_Name_TS if [[ $? == 0 ]]; then echo -e " >> \033[34mPass. Make a copy OK - \033[35m($ED8_Bak_File_Name_TS) \033[0m" | tee -a $LOG1 else echo -e " >> \033[41;37mFail. Make a copy failed. Exit\033[0m - \033[35m($ED8_Bak_File_Name_TS) \033[0m" | tee -a $LOG1 echo exit fi } CHK_BAK_Disk_A (){ echo -e "\033[32mCheck the detection of Backup_Disk_A: \033[35m(lsscsi $BAK_Disk_A) \033[0m" lsscsi | grep $BAK_Disk_A > /dev/null 2>&1 if [[ $? == 0 ]]; then echo -e " >> \033[34mPass. The BAK_Disk_A \"$BAK_Disk_A\" be detected by lsscsi. \033[0m" | tee -a $LOG1 else echo -e " >> \033[41;37mFail. The BAK_Disk_A \"$BAK_Disk_A\" not be detected by lsscsi. Exit \033[0m" | tee -a $LOG1 echo exit fi } CHK_BAK_Disk_B (){ echo -e "\033[32mCheck the detection of Backup_Disk_A: \033[35m(lsscsi $BAK_Disk_B) \033[0m" lsscsi | grep $BAK_Disk_B > /dev/null 2>&1 if [[ $? == 0 ]]; then echo -e " >> \033[34mPass. The BAK_Disk_B \"$BAK_Disk_B\" be detected by lsscsi. \033[0m" | tee -a $LOG1 else echo -e " >> \033[41;37mFail. The BAK_Disk_B \"$BAK_Disk_B\" not be detected by lsscsi. Exit \033[0m" | tee -a $LOG1 echo exit fi } CHK_BAK_Disk_A_Mount_Point (){ echo -e "\033[32mCheck the BAK_Disk_A mount point folder exist: \033[35m($BAK_Disk_A_Mount_Point) \033[0m" ls $BAK_Disk_A_Mount_Point > /dev/null 2>&1 if [[ $? == 0 ]]; then echo -e " >> \033[34mPass. The BAK_Disk_A mount point exist \"$BAK_Disk_A_Mount_Point\"\033[0m" | tee -a $LOG1 else echo -e " >> \033[33mWarning! The BAK_Disk_A mount point doesn't exist \"$BAK_Disk_A_Mount_Point\"\033[0m, auto create a new one. " | tee -a $LOG1 sudo mkdir -p $BAK_Disk_A_Mount_Point chown $USER:$USER $BAK_Disk_A_Mount_Point -R if [[ $? != 0 ]]; then echo -e " >> \033[41;37mCreate the folder failed. Exit\033[0m" echo exit fi fi } CHK_BAK_Disk_B_Mount_Point (){ echo -e "\033[32mCheck the BAK_Disk_A mount point folder exist: \033[35m($BAK_Disk_B_Mount_Point) \033[0m" ls $BAK_Disk_B_Mount_Point > /dev/null 2>&1 if [[ $? == 0 ]]; then echo -e " >> \033[34mPass. The BAK_Disk_B mount point exist \"$BAK_Disk_B_Mount_Point\"\033[0m" | tee -a $LOG1 else echo -e " >> \033[33mWarning! The BAK_Disk_B mount point doesn't exist \"$BAK_Disk_B_Mount_Point\"\033[0m, auto create a new one. " | tee -a $LOG1 sudo mkdir -p $BAK_Disk_B_Mount_Point chown $USER:$USER $BAK_Disk_B_Mount_Point -R if [[ $? != 0 ]]; then echo -e " >> \033[41;37mCreate the folder failed. Exit\033[0m" echo exit fi fi } CHK_BAK_Disk_A_Mount_Status (){ echo -e "\033[32mCheck the BAK_Disk_A mount status: \033[0m" if [[ `df -h | grep $BAK_Disk_A | awk '{print $NF}'` != $BAK_Disk_A_Mount_Point ]]; then echo -e " >> \033[31mFail. The BAK_Disk_A ($BAK_Disk_A) not be mounted at \"$BAK_Disk_A_Mount_Point\". Exit. \033[0m" | tee -a $LOG1 df -h echo exit else echo -e " >> \033[34mPass. The BAK_Disk_A ($BAK_Disk_A) be mounted at \"$BAK_Disk_A_Mount_Point\"\033[0m" | tee -a $LOG1 fi } CHK_BAK_Disk_B_Mount_Status (){ echo -e "\033[32mCheck the BAK_Disk_B mount status: \033[0m" if [[ `df -h | grep $BAK_Disk_B | awk '{print $NF}'` != $BAK_Disk_B_Mount_Point ]]; then echo -e " >> \033[31mFail. The BAK_Disk_B ($BAK_Disk_B) not be mounted at \"$BAK_Disk_B_Mount_Point\". Exit. \033[0m" | tee -a $LOG1 df -h echo exit else echo -e " >> \033[34mPass. The BAK_Disk_B ($BAK_Disk_B) be mounted at \"$BAK_Disk_B_Mount_Point\"\033[0m" | tee -a $LOG1 fi } CHK_BAK_Disk_A_Hostname_Folder (){ echo -e "\033[32mCheck the BAK_Disk_A Hostname folder status: \033[35m($BAK_Disk_A_Hostname_Folder) \033[0m" ls $BAK_Disk_A_Hostname_Folder > /dev/null 2>&1 if [[ $? == 0 ]]; then echo -e " >> \033[34mPass. BAK_Disk_A Hostname folder: $BAK_Disk_A_Hostname_Folder exist, go next.\033[0m" | tee -a $LOG1 else echo -e " >> \033[33mWarning! BAK_Disk_A Hostname folder: $BAK_Disk_A_Hostname_Folder, next will create a new one automatically.\033[0m" | tee -a $LOG1 sduo mkdir -p $BAK_Disk_A_Hostname_Folder chown $USER:$USER $BAK_Disk_A_Hostname_Folder -R if [[ $? != 0 ]]; then echo -e " >> \033[41;37mCreate the folder failed. Exit\033[0m" echo exit fi fi } CHK_BAK_Disk_B_Hostname_Folder (){ echo -e "\033[32mCheck the BAK_Disk_B Hostname folder status: \033[35m($BAK_Disk_B_Hostname_Folder) \033[0m" ls $BAK_Disk_B_Hostname_Folder > /dev/null 2>&1 if [[ $? == 0 ]]; then echo -e " >> \033[34mPass. BAK_Disk_B Hostname folder: $BAK_Disk_B_Hostname_Folder exist, go next.\033[0m" | tee -a $LOG1 else echo -e " >> \033[31mWarning! BAK_Disk_B Hostname folder: $BAK_Disk_B_Hostname_Folder, next will create a new one automatically.\033[0m" | tee -a $LOG1 sudo mkdir -p $BAK_Disk_B_Hostname_Folder chown $USER:$USER $BAK_Disk_B_Hostname_Folder -R if [[ $? != 0 ]]; then echo -e " >> \033[41;37mCreate the folder failed. Exit\033[0m" echo exit fi fi } CHK_BAK_Disk_A_ED8_BAK_Folder (){ echo -e "\033[32mCheck the BAK_Disk_A ED8 backup folder status: \033[35m($BAK_Disk_A_ED8_BAK_Folder) \033[0m" ls $BAK_Disk_A_ED8_BAK_Folder > /dev/null 2>&1 if [[ $? == 0 ]]; then echo -e " >> \033[34mPass. BAK_Disk's Backup file saving folder: $BAK_Disk_A_ED8_BAK_Folder exist, go next.\033[0m" | tee -a $LOG1 else echo -e " >> \033[33mWarning! There is no BAK_Disk's Backup file saving folder: $BAK_Disk_A_ED8_BAK_Folder, next will create a new one automatically.\033[0m" | tee -a $LOG1 sudo mkdir -p $BAK_Disk_A_ED8_BAK_Folder chown $USER:$USER $BAK_Disk_A_ED8_BAK_Folder -R if [[ $? != 0 ]]; then echo -e " >> \033[41;37mCreate the folder failed. Exit\033[0m" echo exit fi fi } CHK_BAK_Disk_B_ED8_BAK_Folder (){ echo -e "\033[32mCheck the BAK_Disk_B ED8 backup folder status: \033[35m($BAK_Disk_B_ED8_BAK_Folder) \033[0m" ls $BAK_Disk_B_ED8_BAK_Folder > /dev/null 2>&1 if [[ $? == 0 ]]; then echo -e " >> \033[34mPass. BAK_Disk's Backup file saving folder: $BAK_Disk_B_ED8_BAK_Folder exist, go next.\033[0m" | tee -a $LOG1 else echo -e " >> \033[33mWarning! There is no BAK_Disk's Backup file saving folder: $BAK_Disk_B_ED8_BAK_Folder, next will create a new one automatically.\033[0m" | tee -a $LOG1 sudo mkdir -p $BAK_Disk_B_ED8_BAK_Folder chown $USER:$USER $BAK_Disk_B_ED8_BAK_Folder -R if [[ $? != 0 ]]; then echo -e " >> \033[41;37mCreate the folder failed. Exit\033[0m" echo exit fi fi } CP_TO_BAK_Disk_A (){ echo -e "\033[32mCopy the backup files to Backup_Disk_A...\033[0m" cp $ED8_Bak_File_Name_TS $BAK_Disk_A_ED8_Bak_File_Name_TS if [[ $? == 0 ]]; then echo -e " >> \033[34mPass. Make a copy OK - \033[35m($BAK_Disk_A_ED8_Bak_File_Name_TS) \033[0m" | tee -a $LOG1 else echo -e " >> \033[41;37mFail. Make a copy failed. Exit\033[0m - \033[35m($BAK_Disk_A_ED8_Bak_File_Name_TS) \033[0m" | tee -a $LOG1 echo echo -e " \033[35mIf you got (Permission denied) message, please perform below command and re-do again. \033[0m" echo -e " \033[35mIt happened at fist backup, caused from created disk mount point folder ($BAK_Disk_A_Mount_Point) with sudo \033[0m" echo -e " \033[41;37m# chown $USER:$USER $BAK_Disk_A_Mount_Point \033[0m" echo exit fi } CP_TO_BAK_Disk_B (){ echo -e "\033[32mCopy the backup files to Backup_Disk_B...\033[0m" cp $ED8_Bak_File_Name_TS $BAK_Disk_B_ED8_Bak_File_Name_TS if [[ $? == 0 ]]; then echo -e " >> \033[34mPass. Make a copy OK - \033[35m($BAK_Disk_B_ED8_Bak_File_Name_TS) \033[0m" | tee -a $LOG1 else echo -e " >> \033[41;37mFail. Make a copy failed. Exit\033[0m - \033[35m($BAK_Disk_B_ED8_Bak_File_Name_TS) \033[0m" | tee -a $LOG1 echo echo -e " \033[35mIf you got (Permission denied) message, please perform below command and re-do again. \033[0m" echo -e " \033[35mIt happened at fist backup, caused from created disk mount point folder ($BAK_Disk_B_Mount_Point) with sudo \033[0m" echo -e " \033[41;37m# chown $USER:$USER $BAK_Disk_B_Mount_Point \033[0m" echo exit fi } echo -e "===== `date +"%Y/%m/%d %H:%M:%S"` ===== \033[45;37m$0\033[0m =====" | tee -a $LOG1 CHK_PWFile CHK_LOG_Folder CHK_ED8_Bak_Folder Mysql_Dump CHK_BAK_Disk_A CHK_BAK_Disk_A_Mount_Point CHK_BAK_Disk_A_Mount_Status CHK_BAK_Disk_A_Hostname_Folder CHK_BAK_Disk_A_ED8_BAK_Folder CP_TO_BAK_Disk_A CHK_BAK_Disk_B CHK_BAK_Disk_B_Mount_Point CHK_BAK_Disk_B_Mount_Status CHK_BAK_Disk_B_Hostname_Folder CHK_BAK_Disk_B_ED8_BAK_Folder CP_TO_BAK_Disk_B echo echo -e "\033[33mIf you want to check the LOG, please perform below command: " echo -e " # cat $LOG1\033[0m" echo