Last active
September 10, 2021 09:21
-
-
Save lefred/96ff5fc6732bad53b3293a85619d3e45 to your computer and use it in GitHub Desktop.
streaming binlogs from MDS to Object Storage
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
#!/bin/bash | |
if [ $# -eq 0 ] | |
then | |
echo "A configuration file is required" | |
exit 1 | |
fi | |
CONF_FILE=$1 | |
if [ ! -f "$CONF_FILE" ] | |
then | |
echo "Configuration file [$CONF_FILE] doesn't exist" | |
exit 2 | |
fi | |
# default config | |
MYSQLCONFIGED=/bin/mysql_config_editor | |
MYSQLBINLOG=/bin/mysqlbinlog | |
RESPAWN=10 # time to wait before reconnecting after failure | |
source $CONF_FILE | |
# check for some files | |
for i in $MYSQLCONFIGED $MYSQLBINLOG | |
do | |
which $i >/dev/null 2>&1 | |
if [[ $? -ne 0 ]] | |
then | |
echo "$i is missing, please install" | |
exit 6 | |
fi | |
done | |
# check is object storage is mounted | |
mount | grep $OBJECT_STORAGE_MOUNT | grep s3fs >/dev/null | |
if [[ $? -ne 0 ]] | |
then | |
echo "$OBJECT_STORAGE_MOUNT is not mounted or it's not a Object Storage mount point" | |
exit 3 | |
fi | |
if [ ! -d "$OBJECT_STORAGE_MOUNT/$BINLOGS_DIR" ] | |
then | |
echo "[$OBJECT_STORAGE_MOUNT/$BINLOGS_DIR] doesn't exist... creating it" | |
mkdir $OBJECT_STORAGE_MOUNT/$BINLOGS_DIR | |
if [[ $? -ne 0 ]] | |
then | |
echo "Error creating [$OBJECT_STORAGE_MOUNT/$BINLOGS_DIR], aborting !" | |
exit 4 | |
fi | |
fi | |
# check is credentials are available | |
$MYSQLCONFIGED print --login-path=$MYSQL_LOGIN_PATH | grep $MYSQL_LOGIN_PATH >/dev/null | |
if [[ $? -ne 0 ]] | |
then | |
echo "please use $MYSQLCONFIGED to add the credentials for login-path=$MYSQL_LOGIN_PATH" | |
exit 7 | |
fi | |
cd $OBJECT_STORAGE_MOUNT/$BINLOGS_DIR | |
echo "Streaming binary logs to $OBJECT_STORAGE_MOUNT/$BINLOGS_DIR" | |
while true | |
do | |
LASTFILE=`ls -1 $OBJECT_STORAGE_MOUNT/$BINLOGS_DIR|grep -v orig|tail -n 1` | |
TIMESTAMP=`date +%s` | |
if [ -z "$LASTFILE" ] | |
then | |
if [ $# -eq 2 ] | |
then | |
LASTFILE=$2 | |
else | |
LASTFILE=$(mysql --login-path=$MYSQL_LOGIN_PATH -BN -e "select SUBSTRING_INDEX(FILE_NAME,'/', -1) binlog from performance_schema.file_instances WHERE EVENT_NAME='wait/io/file/sql/binlog' ORDER BY 1 limit 1;") | |
fi | |
else | |
FILESIZE=$(stat -c%s "$LASTFILE") | |
if [ $FILESIZE -gt 0 ]; then | |
echo "Backing up last binlog" | |
mv $LASTFILE $LASTFILE.orig$TIMESTAMP | |
fi | |
touch $LASTFILE | |
fi | |
echo "Starting live binlog streaming from $LASTFILE" | |
$MYSQLBINLOG --login-path=$MYSQL_LOGIN_PATH --raw --read-from-remote-server --stop-never $LASTFILE | |
echo "mysqlbinlog exited with $? trying to reconnect in $RESPAWN seconds." | |
sleep $RESPAWN | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment