Skip to content

Instantly share code, notes, and snippets.

@fredbradley
Last active August 11, 2017 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredbradley/2f348250225799a413479f6631019166 to your computer and use it in GitHub Desktop.
Save fredbradley/2f348250225799a413479f6631019166 to your computer and use it in GitHub Desktop.
I'd like a bit of help refactoring a couple of bash scripts

Backup Script

What is this?

These two files are what I use to create regular backups on our websites. They create a sql dump of the database, and then a tarball of the directory structure.

Specific Help required

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!

I'd really appreciate any pointers, or help in simplifing the script too!

Get in touch

#!/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'"
#!/bin/sh
# A function that will post a CURL request to Slack
function slacker {
# Usage: slacker <channel> <username> <message>
webhookuri='< OMITTED FORM PUBLIC GIST >'
# Get Channel Name
channel=$1
if [[ $channel == "" ]]
then
echo "No channel specified"
exit 1
fi
shift
# Get Username you want to be displayed as
username=$1
if [[ $username == "" ]]
then
echo "No Username specified"
exit 1
fi
shift
# Get Text
text=$*
if [[ $text == "" ]]
then
echo "No text specified"
exit 1
fi
escapedText=$(echo $text | sed 's/"/\"/g' | sed "s/'/\'/g" )
json="{\"username\": \"$username\", \"channel\": \"#$channel\", \"text\": \"$escapedText\"}"
curl --silent --data "payload=$json" "$webhookuri"
#TODO: Is there a way to stop the script (when run in terminal) to surpress the "ok" resposne that this CURL recieves?
# (I thought that's what `--silent` did!)
}
# A shorthand helper function that users the main `slacker` function written above.
# $SLACK_NAME is a variable pulled in from `backup.sh`.
function slackWrite {
# Usage: slackWrite <message>
text=$*
slacker website-app-logs "$SLACK_NAME" $text
}
# Function deprecated due to a superior method used above.
# Will be removed in a future version.
function quickslack {
# Usage: quickslack <message>
text=$*
slacker website-project cranlive $text
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment