Skip to content

Instantly share code, notes, and snippets.

@eddturtle
Last active June 10, 2016 09:59
Show Gist options
  • Save eddturtle/b6a1e3fa34c7099e9941cfd659e77cfb to your computer and use it in GitHub Desktop.
Save eddturtle/b6a1e3fa34c7099e9941cfd659e77cfb to your computer and use it in GitHub Desktop.
Backup a MySql database, with download speeds and hipchat notification (more info @ http://bit.ly/1RZ1the)
#!/bin/bash
# DB Backup Script
# Backup a MySql Database Script using mysqldump, pv, gzip and curl.
# Will dump the entire contents of a database into a gzipped file.
# Options:
# Database credentials
DBUSER=""
DBPASS=""
DBHOST=""
DBNAME=""
# Note: no need for trailing slash
SAVE_PATH="/home/$USER"
# Note: can't have space after the plus
# format's like: 1970-01-01-13:00.sql.gz
date=$(date +"%Y-%m-%d-%H:%M")
# Check MySqlDump is actually Installed. (HowTo: http://bit.ly/1PObIdX)
hash mysqldump 2>/dev/null || { echo >&2 "The 'mysqldump' command is required. Aborting."; exit 1; }
# The Important Step:
# Using the single-transaction flag is faster and doesn't seem to affect live usage too much.
# The dump command gets passed into pv which echos to screen the amount of data flowing
# through the pipe (useful to know it's working).
echo "-- Downloading... --"
mysqldump \
--user=$DBUSER \
--password=$DBPASS \
--host=$DBHOST \
--single-transaction \
--skip-lock-tables \
$DBNAME | pv | gzip > $SAVE_PATH/$date.sql.gz
# (Optional) Notify HipChat room of completion, Based on: http://bit.ly/1UGfcLB
# Currently prints a random zen message, like "Mind your words, they are important."
ROOM_ID=""
AUTH_TOKEN=""
OWNER_ID=""
MESSAGE=$(curl -s https://api.github.com/zen)
COLOUR=purple
curl --header "content-type: application/json" \
--header "Authorization: Bearer $AUTH_TOKEN" \
-X POST \
-d "{\"name\":\"dev\",\"privacy\":\"private\",\"is_archived\":false,\"is_guest_accessible\":false,\"topic\":\"cURL\",\"message\":\"$MESSAGE\",\"color\":\"$COLOUR\",\"owner\":{\"id\":$OWNER_ID}}" \
https://api.hipchat.com/v2/room/$ROOM_ID/notification
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment