Last active
April 24, 2023 07:11
-
-
Save jlainezs/bfd59980ad3eac5fc16477dd2310152d to your computer and use it in GitHub Desktop.
Automated MySQL backup with tables filter.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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