| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- #!/bin/bash
- USER=`whoami`
- PWFile="/home/$USER/PW/mysql_Gitea_ED.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/Gitea_ED/Script_Gitea_ED/_LOG"
- LOG1=/ED/Gitea_ED/Script_Gitea_ED/_LOG/Gitea_EDbackup-mysql-Auto.log
-
- Gitea_ED_Bak_Folder="/ED/Gitea_ED/Backup_file"
- Gitea_ED_Bak_File_Name=$Gitea_ED_Bak_Folder/backup-mysql-Gitea_ED.gz
- Gitea_ED_Bak_File_Name_TS=$Gitea_ED_Bak_Folder/backup-mysql-Gitea_ED-$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_Gitea_ED_BAK_Folder="$BAK_Disk_A_Hostname_Folder/Gitea_ED/Backup_file"
- BAK_Disk_A_Gitea_ED_Bak_File_Name_TS="$BAK_Disk_A_Gitea_ED_BAK_Folder/backup-mysql-Gitea_ED-$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_Gitea_ED_BAK_Folder="$BAK_Disk_B_Hostname_Folder/Gitea_ED/Backup_file"
- BAK_Disk_B_Gitea_ED_Bak_File_Name_TS="$BAK_Disk_B_Gitea_ED_BAK_Folder/backup-mysql-Gitea_ED-$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_joe_"
- echo -e "ID: ed"
- 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
- sudo 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_Gitea_ED_Bak_Folder (){
- echo -e "\033[32mCheck the local original backup folder exist: \033[35m($Gitea_ED_Bak_Folder) \033[0m"
- ls $Gitea_ED_Bak_Folder > /dev/null 2>&1
- if [[ $? == 0 ]]; then
- echo -e " >> \033[34mPass. Backup file saving folder: $Gitea_ED_Bak_Folder exist, go next.\033[0m" | tee -a $LOG1
- else
- echo -e " >> \033[33mWarning! There is no Backup file saving folder: $Gitea_ED_Bak_Folder, next will create a new one automatically.\033[0m" | tee -a $LOG1
- sudo mkdir -p $Gitea_ED_Bak_Folder
- sudo chown $USER:$USER $Gitea_ED_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
- /opt/lampp/bin/mysqldump -u $DATABASEUser -p$DATABASEPassword $DATABASEName | gzip > $Gitea_ED_Bak_File_Name
- # mysqldump -u $DATABASEUser -p$DATABASEPassword $DATABASEName | gzip > $Gitea_ED_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 /opt/lampp/bin/mysqldump -u ed -p db_gitea | gzip > $Gitea_ED_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[32m* mysqldump: Got error: 1045: Access denied for user 'ed'@'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 $Gitea_ED_Bak_File_Name $Gitea_ED_Bak_File_Name_TS
- if [[ $? == 0 ]]; then
- echo -e " >> \033[34mPass. Make a copy OK - \033[35m($Gitea_ED_Bak_File_Name_TS) \033[0m" | tee -a $LOG1
- else
- echo -e " >> \033[41;37mFail. Make a copy failed. Exit\033[0m - \033[35m($Gitea_ED_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
- sudo 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
- sudo 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
- sudo 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
- sudo 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_Gitea_ED_BAK_Folder (){
- echo -e "\033[32mCheck the BAK_Disk_A Gitea_ED backup folder status: \033[35m($BAK_Disk_A_Gitea_ED_BAK_Folder) \033[0m"
- ls $BAK_Disk_A_Gitea_ED_BAK_Folder > /dev/null 2>&1
- if [[ $? == 0 ]]; then
- echo -e " >> \033[34mPass. BAK_Disk's Backup file saving folder: $BAK_Disk_A_Gitea_ED_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_Gitea_ED_BAK_Folder, next will create a new one automatically.\033[0m" | tee -a $LOG1
- sudo mkdir -p $BAK_Disk_A_Gitea_ED_BAK_Folder
- sudo chown $USER:$USER $BAK_Disk_A_Gitea_ED_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_Gitea_ED_BAK_Folder (){
- echo -e "\033[32mCheck the BAK_Disk_B Gitea_ED backup folder status: \033[35m($BAK_Disk_B_Gitea_ED_BAK_Folder) \033[0m"
- ls $BAK_Disk_B_Gitea_ED_BAK_Folder > /dev/null 2>&1
- if [[ $? == 0 ]]; then
- echo -e " >> \033[34mPass. BAK_Disk's Backup file saving folder: $BAK_Disk_B_Gitea_ED_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_Gitea_ED_BAK_Folder, next will create a new one automatically.\033[0m" | tee -a $LOG1
- sudo mkdir -p $BAK_Disk_B_Gitea_ED_BAK_Folder
- sudo chown $USER:$USER $BAK_Disk_B_Gitea_ED_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 $Gitea_ED_Bak_File_Name_TS $BAK_Disk_A_Gitea_ED_Bak_File_Name_TS
- if [[ $? == 0 ]]; then
- echo -e " >> \033[34mPass. Make a copy OK - \033[35m($BAK_Disk_A_Gitea_ED_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_Gitea_ED_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# sudo 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 $Gitea_ED_Bak_File_Name_TS $BAK_Disk_B_Gitea_ED_Bak_File_Name_TS
- if [[ $? == 0 ]]; then
- echo -e " >> \033[34mPass. Make a copy OK - \033[35m($BAK_Disk_B_Gitea_ED_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_Gitea_ED_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# sudo 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_Gitea_ED_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_Gitea_ED_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_Gitea_ED_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
|