|  | #!/bin/sh | 
        
          |  |  | 
        
          |  | ############################################################################ | 
        
          |  | ## 			So - there is one `backup.sh` for each site on the server.        ## | 
        
          |  | ##      The content is largely the same, just a change of variables to    ## | 
        
          |  | ##      ensure things are saved in the write places with the right names! ## | 
        
          |  | ############################################################################ | 
        
          |  |  | 
        
          |  | ## REQUESTS FOR SPECIFIC HELP | 
        
          |  | # This is all fine, but I want to find a way that a message will be sent to slack if one of the other commands (or the whole script) fails. | 
        
          |  | # EG - `tar` failed because filesystem run out of space; or `mysqldump` failed because wrong password? or simply 'couldn't find folder!?, etc! | 
        
          |  |  | 
        
          |  |  | 
        
          |  | ## Load in Functions File (See above file in GIST) | 
        
          |  | source "/root/backup_functions_frb/functions.sh" | 
        
          |  |  | 
        
          |  | # Color Variables | 
        
          |  | RED='\033[0;31m' | 
        
          |  | GREEN='\033[0;32m' | 
        
          |  | NC='\033[0m' # No Color | 
        
          |  |  | 
        
          |  | # 1. SET VARIABLES | 
        
          |  | SITE_NAME="www.mydomain.org" | 
        
          |  |  | 
        
          |  | DATABASE_NAME="freds_test_db" | 
        
          |  |  | 
        
          |  | # Notice this variable gets picked up in `functions.sh` | 
        
          |  | SLACK_NAME="BACKUP TEST USER" | 
        
          |  |  | 
        
          |  | # Get the path directory to 'here'. (No need to add in more changing variables so long as file/directory structure always the same) | 
        
          |  | PATH_DIRECTORY="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | 
        
          |  |  | 
        
          |  | # Although called "BACKUPTIME" this is effectivly the file name. | 
        
          |  | BACKUPTIME="$SITE_NAME-$(date +"%Y-%m-%d")" | 
        
          |  |  | 
        
          |  | # Each site has an 'auto_backups' folder as part of it's structure | 
        
          |  | BACKUP_DIRECTORY="$PATH_DIRECTORY/auto_backups" | 
        
          |  |  | 
        
          |  | # Tell Slack that we're starting | 
        
          |  | slackWrite "Starting backup script: 'backup.sh'" | 
        
          |  |  | 
        
          |  | # Tell shell output that we're starting | 
        
          |  | echo -e "${GREEN}Starting MySQL Backups...${NC}" | 
        
          |  | slackWrite "Starting MySQL Dump" | 
        
          |  |  | 
        
          |  | # 2. DO LATEST MYSQL | 
        
          |  | mysqldump $DATABASE_NAME > $PATH_DIRECTORY/latest-dump_$DATABASE_NAME.sql | 
        
          |  |  | 
        
          |  | echo -e "${GREEN}Placed the latest backup in the file system...${NC}" | 
        
          |  | slackWrite "Dumped latest MySQL in filesystem" | 
        
          |  |  | 
        
          |  | # 3. DO MYSQLDUMP | 
        
          |  | echo -e "${GREEN}Now backing up to backups folder... ${NC}" | 
        
          |  | mysqldump $DATABASE_NAME > $BACKUP_DIRECTORY/$BACKUPTIME.sql | 
        
          |  | echo -e "${GREEN}Starting GZIP${NC}" | 
        
          |  |  | 
        
          |  | gzip $BACKUP_DIRECTORY/$BACKUPTIME.sql | 
        
          |  |  | 
        
          |  | echo -e "${GREEN}Completed MySQL Backup${NC}" | 
        
          |  |  | 
        
          |  | # 4. DO FILE BACKUP | 
        
          |  | echo -e "${GREEN}Starting Filesystem Backup... sit back this takes a while...${NC}" | 
        
          |  | slackWrite "Starting Filesystem Backup..." | 
        
          |  |  | 
        
          |  | tar -zcvf $BACKUP_DIRECTORY/$BACKUPTIME.tar -C $PATH_DIRECTORY doc_root/ wp-config.php | 
        
          |  |  | 
        
          |  | echo -e "${GREEN}Starting GZIP${NC}" | 
        
          |  |  | 
        
          |  | gzip $BACKUP_DIRECTORY/$BACKUPTIME.tar | 
        
          |  |  | 
        
          |  | echo -e "${GREEN}Completed Filesystem backup Backup${NC}" | 
        
          |  | slackWrite "Completed Filesystem Backup" | 
        
          |  |  | 
        
          |  | # 5. FIND AND DELETE OLD FILES more than 7 days | 
        
          |  | echo -e "${GREEN}Checking if I need to delete any old backups...${NC}" | 
        
          |  | slackWrite "Checking if I need to delete any old backups..." | 
        
          |  |  | 
        
          |  | array=($(find $BACKUP_DIRECTORY -type f -mtime +6)) | 
        
          |  | for i in ${array[@]}; | 
        
          |  |  | 
        
          |  | do | 
        
          |  | name=$(basename "$i") | 
        
          |  | echo -e "${RED}Deleted $i backup ${NC}" | 
        
          |  | slackWrite "Deleted $name" | 
        
          |  |  | 
        
          |  | rm $i | 
        
          |  |  | 
        
          |  | done; | 
        
          |  |  | 
        
          |  | echo -e "${GREEN}All 100% Done${NC}" | 
        
          |  |  | 
        
          |  | slackWrite "<!here> Completed: 'backup.sh'" |