Skip to content

Instantly share code, notes, and snippets.

@starx
Created September 19, 2017 09:31
Show Gist options
  • Save starx/33ef3d3e7d21ca1e336f66e3de2382de to your computer and use it in GitHub Desktop.
Save starx/33ef3d3e7d21ca1e336f66e3de2382de to your computer and use it in GitHub Desktop.
Bash to extract MySQL bin logs of a database between date range
#!/bin/bash
MYSQL_BIN_LOG_FOLDER='/var/lib/mysql'
MYSQL_BIN_LOG_FORMAT='.*mysql\-bin.*'
DATABASE_NAME=$1
START_TIMESTAMP=$2
END_TIMESTAMP=$3
echo $DATABASE_NAME
echo $START_TIMESTAMP
echo $END_TIMESTAMP
for FILE_NAME in $(find $MYSQL_BIN_LOG_FOLDER -maxdepth 1 -type f -regex $MYSQL_BIN_LOG_FORMAT -printf "%p\n" | sort -n); do
echo "mysqlbinlog -d ${DATABASE_NAME} --start-datetime=\"${START_TIMESTAMP}\" --stop-datetime=\"${END_TIMESTAMP}\" ${FILE_NAME}"
mysqlbinlog -d $DATABASE_NAME --start-datetime="${START_TIMESTAMP}" --stop-datetime="${END_TIMESTAMP}" $FILE_NAME
done
@starx
Copy link
Author

starx commented Sep 19, 2017

Example: extract_binlog db_name '2017-01-01 01:00:00' '2017-12-31 23:59:59'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment