#!/bin/bash BAK_Project=ED8 Script_SRC_BAK=${BAK_Project}backup-mysql-Auto.sh.SRC Script_TMP_BAK=${BAK_Project}backup-mysql-Auto.sh.TMP Script_REAL_BAK=${BAK_Project}backup-mysql-Auto.sh Script_SRC_Restore=${BAK_Project}restore_from_local-mysql.sh.SRC Script_TMP_Restore=${BAK_Project}restore_from_local-mysql.sh.TMP Script_REAL_Restore=${BAK_Project}restore_from_local-mysql.sh Script_SRC_Script=${BAK_Project}backup-Script_${BAK_Project}-Auto.sh.SRC Script_TMP_Script=${BAK_Project}backup-Script_${BAK_Project}-Auto.sh.TMP Script_REAL_Script=${BAK_Project}backup-Script_${BAK_Project}-Auto.sh Script_SRC_Source_Files=${BAK_Project}backup-Source_Files-Auto.sh.SRC Script_TMP_Source_Files=${BAK_Project}backup-Source_Files-Auto.sh.TMP Script_REAL_Source_Files=${BAK_Project}backup-Source_Files-Auto.sh Script_SRC_Remote_Backup=${BAK_Project}Remote_backup.sh.SRC Script_TMP_Remote_Backup=${BAK_Project}Remote_backup.sh.TMP Script_REAL_Remote_Backup=${BAK_Project}Remote_backup.sh USER=`whoami` PWFile="/home/$USER/PW/mysql_${BAK_Project}.info" WORK_Folder="/ED/${BAK_Project}/Script_${BAK_Project}" SOUCE_Folder="/ED/${BAK_Project}/Source_Files" Bak_Folder="/ED/${BAK_Project}/Backup_file" UTY_1=expect UTY_2=lsscsi UTY_3=gunzip UTY_4=mysql UTY_5=mysqldump CHK_QTY=9 cp $Script_SRC_BAK $Script_TMP_BAK; echo $? cp $Script_SRC_Restore $Script_TMP_Restore; echo $? cp $Script_SRC_Script $Script_TMP_Script; echo $? cp $Script_SRC_Source_Files $Script_TMP_Source_Files; echo $? cp $Script_SRC_Remote_Backup $Script_TMP_Remote_Backup; echo $? echo -e -n "You are \033[44;37m$USER\033[0m. Is it correct? (y|n) " read yn if [[ $yn == y ]]; then echo -e "Change necessary files owner to $USER" sudo chown $USER:$USER /ED/${BAK_Project} -R elif [[ $yn == n ]]; then echo "\033[31mYour input is $yn. Exit. " echo exit else echo -e "\033[41;37mWrong input ($yn). Exit. \033[0m" echo exit fi echo echo -e "\033[32mCheck list: \033[0m" echo -e " \033[33m01. Password file \t\t= $PWFile\033[0m" echo -e " \033[33m02. Work Folder \t\t= $WORK_Folder\033[0m" echo -e " \033[33m03. Source files folder \t= $SOUCE_Folder\033[0m" echo -e " \033[33m04. Local Backup Folder \t= $Bak_Folder\033[0m" echo -e " \033[33m05. Utility status \t\t= $UTY_1\033[0m" echo -e " \033[33m06. Utility status \t\t= $UTY_2\033[0m" echo -e " \033[33m07. Utility status \t\t= $UTY_3\033[0m" echo -e " \033[33m08. Utility status \t\t= $UTY_4\033[0m" echo -e " \033[33m09. Utility status \t\t= $UTY_5\033[0m" echo echo i=0 ##### Func SEL_BAK_Mode (){ echo -e "\033[33mPlease select which backup mode do you want to use? \033[0m" echo -e " \033[32mA: OS_HDD only \t\t\t(You only have one Storage in host) \033[0m" echo -e " \033[32mB: OS_HDD + BAK_Disk_A\t\t(You have two Storages installed in host at least) \033[0m" echo -e " \033[32mC: OS_HDD + BAK_Disk_A + BAK_Disk_B\t(You have three Storages installed in host at least) \033[0m" echo echo -e -n "\033[33mPlease input: (A|B|C) \033[0m" read MODE if [[ $MODE == "A" ]]; then echo -e " Your input is (A): OS_HDD only" elif [[ $MODE == "B" ]]; then echo -e " Your input is (B): OS_HDD + BAK_Disk_A - You have two Storages installed in host at least" QTY_Storage=`lsscsi | awk '{print $2}' | grep "disk" |wc -l` if [[ $QTY_Storage < 2 ]]; then echo -e " \033[41;37m>> You don't have enough Storages ($QTY_Storage) to do this. Exit \033[0m" echo lsscsi echo exit fi lsscsi echo echo -e -n "\033[33mAbove are all installed storages, which one is your BAK_Disk_A? (sda|sdb|sdc|sdx) \033[0m" read STORAGE_A lsscsi | grep $STORAGE_A > /dev/null 2>&1 if [[ $? != 0 ]]; then echo -e " \033[41;37mThere is no storage named /dev/$STORAGE_A in list. Exit \033[0m" echo exit fi sed -i "s/BAK_Disk_A=\/dev\/sdb/BAK_Disk_A=\/dev\/$STORAGE_A/g" $Script_TMP_BAK sed -i 's/#CHK_BAK_Disk_A/CHK_BAK_Disk_A/g' $Script_TMP sed -i 's/#CHK_BAK_Disk_A_Mount_Point/CHK_BAK_Disk_A_Mount_Point/g' $Script_TMP_BAK sed -i 's/#CHK_BAK_Disk_A_Mount_Status/CHK_BAK_Disk_A_Mount_Status/g' $Script_TMP_BAK sed -i 's/#CHK_BAK_Disk_A_Hostname_Folder/CHK_BAK_Disk_A_Hostname_Folder/g' $Script_TMP_BAK sed -i "s/#CHK_BAK_Disk_A_${BAK_Project}_BAK_Folder/CHK_BAK_Disk_A_${BAK_Project}_BAK_Folder/g" $Script_TMP_BAK sed -i 's/#CP_TO_BAK_Disk_A/CP_TO_BAK_Disk_A/g' $Script_TMP_BAK sed -i 's/#CP_TO_BAK_Disk_A/CP_TO_BAK_Disk_A/g' $Script_TMP_Script_ sed -i 's/#CP_TO_BAK_Disk_A/CP_TO_BAK_Disk_A/g' $Script_TMP_Source_Files echo -e " >> Set it done. " echo elif [[ $MODE == "C" ]]; then echo -e " Your input is (C): OS_HDD + BAK_Disk_A + BAK_Disk_B - You have three Storages installed in host at least" QTY_Storage=`lsscsi | awk '{print $2}' | grep "disk" | wc -l` if [[ $QTY_Storage < 3 ]]; then echo -e " \033[41;37m>> You don't have enough Storages ($QTY_Storage) to do this. Exit \033[0m" echo lsscsi echo exit fi lsscsi echo echo -e -n "\033[33mAbove are all installed storages, which one is your BAK_Disk_A? (sda|sdb|sdc|sdx) \033[0m" read STORAGE_A lsscsi | grep $STORAGE_A > /dev/null 2>&1 if [[ $? != 0 ]]; then echo -e " \033[41;37mThere is no storage named /dev/$STORAGE_A in list. Exit \033[0m" echo exit fi sed -i "s/BAK_Disk_A=\/dev\/sdb/BAK_Disk_A=\/dev\/$STORAGE_A/g" $Script_TMP_BAK sed -i 's/#CHK_BAK_Disk_A/CHK_BAK_Disk_A/g' $Script_TMP_BAK sed -i 's/#CHK_BAK_Disk_A_Mount_Point/CHK_BAK_Disk_A_Mount_Point/g' $Script_TMP_BAK sed -i 's/#CHK_BAK_Disk_A_Mount_Status/CHK_BAK_Disk_A_Mount_Status/g' $Script_TMP_BAK sed -i 's/#CHK_BAK_Disk_A_Hostname_Folder/CHK_BAK_Disk_A_Hostname_Folder/g' $Script_TMP_BAK sed -i "s/#CHK_BAK_Disk_A_${BAK_Project}_BAK_Folder/CHK_BAK_Disk_A_${BAK_Project}_BAK_Folder/g" $Script_TMP_BAK sed -i 's/#CP_TO_BAK_Disk_A/CP_TO_BAK_Disk_A/g' $Script_TMP_BAK sed -i 's/#CP_TO_BAK_Disk_A/CP_TO_BAK_Disk_A/g' $Script_TMP_Script sed -i 's/#CP_TO_BAK_Disk_A/CP_TO_BAK_Disk_A/g' $Script_TMP_Source_Files echo -e " >> Set it done. " echo echo -e -n "\033[33mWhich one is your BAK_Disk_B? (sda|sdb|sdc|sdx) \033[0m" read STORAGE_B if [[ $STORAGE_A == $STORAGE_B ]]; then echo -e "\033[41;37mYour BAK_Disk_A($STORAGE_A) and BAK_Disk_A($STORAGE_A) are the same. Wrong inout. Exit \033[0m" echo exit fi lsscsi | grep $STORAGE_B > /dev/null 2>&1 if [[ $? != 0 ]]; then echo -e " \033[41;37mThere is no storage named /dev/$STORAGE_B in list. Exit \033[0m" echo exit fi sed -i "s/BAK_Disk_B=\/dev\/sda/BAK_Disk_B=\/dev\/$STORAGE_B/g" $Script_TMP_BAK sed -i 's/#CHK_BAK_Disk_B/CHK_BAK_Disk_B/g' $Script_TMP_BAK sed -i 's/#CHK_BAK_Disk_B_Mount_Point/CHK_BAK_Disk_B_Mount_Point/g' $Script_TMP_BAK sed -i 's/#CHK_BAK_Disk_B_Mount_Status/CHK_BAK_Disk_B_Mount_Status/g' $Script_TMP_BAK sed -i 's/#CHK_BAK_Disk_B_Hostname_Folder/CHK_BAK_Disk_B_Hostname_Folder/g' $Script_TMP_BAK sed -i "s/#CHK_BAK_Disk_B_${BAK_Project}_BAK_Folder/CHK_BAK_Disk_B_${BAK_Project}_BAK_Folder/g" $Script_TMP_BAK sed -i 's/#CP_TO_BAK_Disk_B/CP_TO_BAK_Disk_B/g' $Script_TMP_BAK sed -i 's/#CP_TO_BAK_Disk_B/CP_TO_BAK_Disk_B/g' $Script_TMP_Script sed -i 's/#CP_TO_BAK_Disk_B/CP_TO_BAK_Disk_B/g' $Script_TMP_Source_Files echo -e " >> Set it done. " echo else echo -e " \033[41;37mWrong input ($MODE). Exit \033[0m" echo exit fi } CHK_PWFile (){ ls $PWFile > /dev/null 2>&1 if [[ $? != 0 ]]; then echo -e " \033[31m01. Fail. $PWFile doesn't exist. Please create one. Exit \033[0m" echo echo -e "$PWFile Content example: " echo -e "Database_Name: db_ed8_" echo -e "ID: ed" echo -e "Password: edat****" echo exit else echo -e " \033[34m01. Pass. $PWFile exist. \033[0m" 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}'`" echo -e -n " >> 01-1. 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[34mGood \033[0m" fi echo -e -n " >> 01-2. 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[34mGood \033[0m" fi echo -e -n " >> 01-3. 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[34mGood \033[0m" fi i=$((i+1)) fi } CHK_Work_F (){ if [[ `pwd` != $WORK_Folder ]]; then echo -e " \033[31m02. Fail. It's not work folder \"$WORK_Folder\". \033[0m" else echo -e " \033[34m02. Pass. Current Work Folder is $WORK_Folder \033[0m" i=$((i+1)) fi } CHK_SRC_F (){ ls $SOUCE_Folder > /dev/null 2>&1 if [[ $? != 0 ]]; then echo -e " \033[31m03. Fail. $SOUCE_Folder doesn't exist. \033[0m" else echo -e " \033[34m03. Pass. $SOUCE_Folder folder exist. \033[0m" i=$((i+1)) fi } CHK_BAK_F (){ ls $Bak_Folder > /dev/null 2>&1 if [[ $? != 0 ]]; then echo -e " \033[31m04. Fail. $Bak_Folder doesn't exist. Create a new one. \033[0m" sudo mkdir -p $Bak_Folder chown $USER:$USER $Bak_Folder -R else echo -e " \033[34m04. Pass. $Bak_Folder folder exist. \033[0m" i=$((i+1)) fi } CHK_UTY_1 (){ man $UTY_1 > /dev/null 2>&1 if [[ $? == 0 ]]; then echo -e " \033[34m05. Pass. The utility ($UTY_1) was installed. \033[0m" i=$((i+1)) else echo -e " \033[31m05. Fail. The utility ($UTY_1) not be installed. \033[0m" fi } CHK_UTY_2 (){ man $UTY_2 > /dev/null 2>&1 if [[ $? == 0 ]]; then echo -e " \033[34m06. Pass. The utility ($UTY_2) was installed. \033[0m" i=$((i+1)) else echo -e " \033[31m06. Fail. The utility ($UTY_2) not be installed. \033[0m" fi } CHK_UTY_3 (){ man $UTY_3 > /dev/null 2>&1 if [[ $? == 0 ]]; then echo -e " \033[34m07. Pass. The utility ($UTY_3) was installed. \033[0m" i=$((i+1)) else echo -e " \033[31mm07. Fail. The utility ($UTY_3) not be installed. \033[0m" fi } CHK_UTY_4 (){ man $UTY_4 > /dev/null 2>&1 if [[ $? == 0 ]]; then echo -e " \033[34m08. Pass. The utility ($UTY_4) was installed. Setting ${BAK_Project}restore_from_local-mysql.sh use it. \033[0m" sed -i 's/\/opt\/lampp\/bin\/mysql -u/mysql -u/g' $Script_TMP_Restore i=$((i+1)) else echo -e " \033[33m08. Warning! The utility ($UTY_4) not be installed by apt, but service might be provided by xampp. \033[0m" ls /opt/lampp/bin/mysql > /dev/null 2>&1 if [[ $? == 0 ]]; then echo -e " \033[35m>> Good. your mysql service is provided by xampp \033[0m" i=$((i+1)) else echo -e " \033[31m>> Fail. mysql service not be provided by xampp either. \033[0m" fi fi } CHK_UTY_5 (){ man $UTY_5 > /dev/null 2>&1 if [[ $? == 0 ]]; then echo -e " \033[34m09. Pass. The utility ($UTY_5) was installed. Setting ${BAK_Project}backup-mysql-Auto.sh use it. \033[0m" sed -i 's/\/opt\/lampp\/bin\/mysqldump -u/mysqldump -u/g' $Script_TMP_BAK i=$((i+1)) else echo -e " \033[33m09. Fail. The utility ($UTY_5) not be installed by apt. \033[0m" ls /opt/lampp/bin/mysqldump > /dev/null 2>&1 if [[ $? == 0 ]]; then echo -e " \033[35m>> Good. your mysqldump is provided by xampp. \033[0m" i=$((i+1)) else echo -e " \033[31m>> Fail. mysql service not be provided by xampp either. \033[0m" fi fi } SEL_REMOTE_BAK (){ echo -e "\033[32mThere two remote backup plan you can use.\033[0m (\033[35mPLAN#X\033[0m and \033[36mPLAN#Y\033[0m)" echo -e -n "\033[33m1. Do you want to enable \033[35mPlan#X\033[0m? (y|n) " read yn if [[ $yn == y ]]; then echo -e " \033[32m>> Your input is y. Enabling the PLAN#X... \033[0m" sed -i 's/#MKDIR_REMOTE_BAK_Folder_BAK_Disk_X/MKDIR_REMOTE_BAK_Folder_BAK_Disk_X/g' $Script_TMP_Remote_Backup sed -i 's/#SCP_TO_BAK_Disk_X/SCP_TO_BAK_Disk_X/g' $Script_TMP_Remote_Backup elif [[ $yn == n ]]; then echo -e " \033[35m>> Your input is n. Disabling the PLAN#Y... \033[0m" else echo -e " \033[31m>> Wrong inout($yn). Exit \033[0m" echo exit fi echo -e -n "\033[33m2. Do you want to enable \033[36mPlan#Y\033[0m? (y|n) " read yn if [[ $yn == y ]]; then echo -e " \033[32m>> Your input is y. Enabling the PLAN#Y... \033[0m" sed -i 's/#MKDIR_REMOTE_BAK_Folder_BAK_Disk_Y/MKDIR_REMOTE_BAK_Folder_BAK_Disk_Y/g' $Script_TMP_Remote_Backup sed -i 's/#SCP_TO_BAK_Disk_Y/SCP_TO_BAK_Disk_Y/g' $Script_TMP_Remote_Backup elif [[ $yn == n ]]; then echo -e " \033[35m>> Your input is n. Disabling the PLAN#Y... \033[0m" else echo -e " \033[31m>> Wrong inout($yn). Exit \033[0m" echo exit fi } SEL_BAK_Mode echo echo echo -e "\033[32mStart check list check... \033[0m" CHK_PWFile CHK_Work_F CHK_SRC_F CHK_BAK_F CHK_UTY_1 CHK_UTY_2 CHK_UTY_3 CHK_UTY_4 CHK_UTY_5 echo SEL_REMOTE_BAK cp $Script_TMP_BAK $Script_REAL_BAK cp $Script_TMP_Restore $Script_REAL_Restore cp $Script_TMP_Script $Script_REAL_Script cp $Script_TMP_Source_Files $Script_REAL_Source_Files cp $Script_TMP_Remote_Backup $Script_REAL_Remote_Backup rm -rf $Script_TMP_BAK $Script_TMP_Restore $Script_TMP_Script $Script_TMP_Source_Files $Script_TMP_Remote_Backup echo echo if [[ $i == $CHK_QTY ]]; then echo -e "\033[33mAll check list Passed. You can start your scripts. \033[0m" else echo -e "\033[41;37mFailed. There still be error items. Please check it. \033[0m" fi echo