Skip to content

Instantly share code, notes, and snippets.

@aendra-rininsland
Created October 8, 2014 00:23
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 aendra-rininsland/f47f8ec015f158829c8a to your computer and use it in GitHub Desktop.
Save aendra-rininsland/f47f8ec015f158829c8a to your computer and use it in GitHub Desktop.
Dump databases for RDS while excluding others in MySQL v5.definitely-not-most-recent
#!/bin/bash
### dump_mysql_for_rds.sh
## 2014 Ændrew Rininsland (@aendrew)
# usage: ./dump_mysql_for_rds.sh <root_user> <password> <path_to_mysql_socket> <dbs,to,exclude> <filename for dump>
ARG_DBUSER=$1
ARG_DBPASS=$2
ARG_DBSOCKET=$3
ARG_EXCLUDE=$4
ARG_DUMPFILE=$5
# Setup exclude list
EXCLUDE="mysql\|test\|information_schema"
for i in $(echo $ARG_EXCLUDE | tr "," "\|")
do
EXCLUDE="$EXCLUDE$i"
done
# Fetch list of databases, minus excluded
DATABASE_LIST=$(mysql -u $ARG_DBUSER -p$ARG_DBPASS --socket=$ARG_DBSOCKET -NBe 'show schemas' | grep -wv $EXCLUDE)
echo "Excluding: $EXCLUDE"
echo "Including: $DATABASE_LIST"
echo "Dumping to file: $ARG_DUMPFILE"
# Dump to dumpfile
RESULT=$(mysqldump -u $ARG_DBUSER -p$ARG_DBPASS --socket=$ARG_DBSOCKET --databases $DATABASE_LIST > $ARG_DUMPFILE)
## TODO (feel free to fork):
# 1. Add more arguments to import directly to RDS
# 2. Allow either port or socket
# 3. Make exclude DB list optional
# 4. Add stanza to stripe "DEFINER" statements from dump.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment