#!/bin/bash BAK_Project=ED8 USER=`whoami` LOG_Folder="/ED/${BAK_Project}/Script_${BAK_Project}/_LOG" LOG1=/ED/${BAK_Project}/Script_${BAK_Project}/_LOG/${BAK_Project}backup-attachment-rsync-Auto.log Bak_Folder="/ED/${BAK_Project}/Backup_file" ATTACH_FILE_Folder="/var/www/html/ed8/data" CHK_LOG_Folder (){ echo -e "\033[32mCheck the auto backup log saving folder exist: \033[35m($LOG_Folder) \033[0m" | tee -a $LOG1 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" | tee -a $LOG1; echo; exit; fi else echo -e " >> \033[34mPass. There is a folder \"$LOG_Folder\". \033[0m" | tee -a $LOG1 fi } CHK_Bak_Folder (){ echo -e "\033[32mCheck the local original backup folder exist: \033[35m($Bak_Folder) \033[0m" | tee -a $LOG1 ls $Bak_Folder > /dev/null 2>&1 if [[ $? == 0 ]]; then echo -e " >> \033[34mPass. Backup file saving folder: $Bak_Folder exist, go next.\033[0m" | tee -a $LOG1 else echo -e " >> \033[33mWarning! There is no Backup file saving folder: $Bak_Folder, next will create a new one automatically.\033[0m" | tee -a $LOG1 sudo mkdir -p $Bak_Folder sudo chown $USER:$USER $Bak_Folder -R if [[ $? != 0 ]]; then echo -e " >> \033[41;37mCreate the folder failed. Exit\033[0m" | tee -a $LOG1; echo; exit; fi fi } RSYNC_ATTACHMENT (){ echo -e "\033[32mStarting rsync... \033[0m" | tee -a $LOG1 rsync -a $ATTACH_FILE_Folder $Bak_Folder/ if [[ $? == 0 ]]; then echo -e " >> \033[34mPass. rsync attach file directory ($ATTACH_FILE_Folder).\033[0m" | tee -a $LOG1; else echo -e " >> \033[31mFail. rsync attach file directory ($ATTACH_FILE_Folder) failed.\033[0m" | tee -a $LOG1 fi; echo } ##### Main echo -e "===== \033[43;30m$(date +"%Y/%m/%d %H:%M:%S")\033[0m ===== \033[36m$0\033[0m =====" | tee -a $LOG1 CHK_LOG_Folder CHK_Bak_Folder RSYNC_ATTACHMENT