Skip to content

Instantly share code, notes, and snippets.

@jlainezs
Last active April 24, 2023 07:11
Show Gist options
  • Save jlainezs/bfd59980ad3eac5fc16477dd2310152d to your computer and use it in GitHub Desktop.
Save jlainezs/bfd59980ad3eac5fc16477dd2310152d to your computer and use it in GitHub Desktop.
Automated MySQL backup with tables filter.
#!/usr/bin/env bash
# Backup up database tables
# Requires a credentials files on
# ./mysqlcredentials.cnf
#
# CAUTION
# If you have running Google's sqlproxy you could end messing with the remote mysql!
CURRENT_TIME=$(date "+%Y%m%d-%H%M%S")
DB_TO_DUMP=mydbtodump
DB_HOST=127.0.0.1
DUMP_PATH=mydbdump_directory
DUMP_FILENAME="$DUMP_PATH/local-$DB_TO_DUMP-dump-$CURRENT_TIME.sql"
CREDENTIALS_FILE=mysqlcredentials.cnf
SQL="SET group_concat_max_len = 10240;"
SQL="${SQL} SELECT GROUP_CONCAT(table_name separator ' ')"
SQL="${SQL} FROM information_schema.tables WHERE table_schema='${DB_TO_DUMP}'"
SQL="${SQL} AND (table_name like '%YOUR_TABLES_FILTER_HERE%')"
TABLES=`mysql --defaults-extra-file=${CREDENTIALS_FILE} -AN -e"${SQL}"`
mysqldump --defaults-extra-file=${CREDENTIALS_FILE} ${DB_TO_DUMP} ${TABLES} > $DUMP_FILENAME
[client]
user = myuser
password = mypass
# For localhost sometimes is required to use 127.0.0.1 instead of localhost
host = myhost_ip
port = myinstance_port
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment