Skip to content

Commit

Permalink
[bitnami/mysql] Fix mysqldump master database (#55126)
Browse files Browse the repository at this point in the history
* fix mysql dump database

Signed-off-by: Michel Silva <[email protected]>

* remove comment

Signed-off-by: Michel Silva <[email protected]>

* fix: linter issue SC2046

Signed-off-by: Alberto Otero Lorenzo <[email protected]>

---------

Signed-off-by: Michel Silva <[email protected]>
Signed-off-by: Alberto Otero Lorenzo <[email protected]>
Co-authored-by: Alberto Otero Lorenzo <[email protected]>
  • Loading branch information
michel-silva and aoterolorenzo authored Feb 26, 2024
1 parent e08d015 commit 4f52344
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 57 deletions.
68 changes: 40 additions & 28 deletions bitnami/mysql/8.0/debian-12/rootfs/opt/bitnami/scripts/libmysql.sh
Original file line number Diff line number Diff line change
Expand Up @@ -189,35 +189,45 @@ EOF
#########################
mysql_exec_initial_dump() {
info "MySQL dump master data start..."
mysql -h "$DB_MASTER_HOST" -P "$DB_MASTER_PORT_NUMBER" -u "$DB_MASTER_ROOT_USER" -p"$DB_MASTER_ROOT_PASSWORD" -e 'RESET MASTER;'

databases=("mysql")
if [ -n "$DB_DATABASE" ]; then
databases+=("$DB_DATABASE")
fi
for DB in "${databases[@]}"; do
info "Start dump process database $DB"
if [[ $DB = @(information_schema|performance_schema|sys) ]]; then
info "Skipping default table $DB to be imported"
continue
fi
DUMP_FILE="$DB_DATA_DIR/dump_$DB.sql"
if mysqldump --verbose -h "$DB_MASTER_HOST" -P "$DB_MASTER_PORT_NUMBER" -u "$DB_MASTER_ROOT_USER" -p"$DB_MASTER_ROOT_PASSWORD" "$DB" > "$DUMP_FILE"; then
info "Finish dump database $DB"
info "Ensure database exists $DB"
mysql -u "$DB_MASTER_ROOT_USER" <<EOF
create database if not exists $DB;
info "LOCK MASTER DATABASES FOR WRITE OPERATIONS..."
mysql -h "$DB_MASTER_HOST" -P "$DB_MASTER_PORT_NUMBER" -u "$DB_MASTER_ROOT_USER" -p"$DB_MASTER_ROOT_PASSWORD" -se 'FLUSH TABLES WITH READ LOCK;'

info "SHOW MASTER STATUS..."
read -r MYSQL_FILE MYSQL_POSITION <<< "$(mysql -h "$DB_MASTER_HOST" -P "$DB_MASTER_PORT_NUMBER" -u "$DB_MASTER_ROOT_USER" -p"$DB_MASTER_ROOT_PASSWORD" -se 'SHOW MASTER STATUS;' | awk 'NR==1 {print $1, $2}')"
info "File: $MYSQL_FILE and Position: $MYSQL_POSITION"

info "Start dump process databases"

FILE_LOCATION="$DB_DATA_DIR/dump_all_databases.sql"

mysqldump --verbose --all-databases -h "$DB_MASTER_HOST" -P "$DB_MASTER_PORT_NUMBER" -u "$DB_MASTER_ROOT_USER" -p"$DB_MASTER_ROOT_PASSWORD" > $FILE_LOCATION

info "Finish dump databases"

info "UNLOCK MASTER DATABASES FOR WRITE OPERATIONS..."
mysql -h "$DB_MASTER_HOST" -P "$DB_MASTER_PORT_NUMBER" -u "$DB_MASTER_ROOT_USER" -p"$DB_MASTER_ROOT_PASSWORD" -se 'UNLOCK TABLES;'

info "Start import dump databases"
mysql_execute < $FILE_LOCATION
info "Finish import dump databases"

mysql_execute "mysql" <<EOF
CHANGE MASTER TO MASTER_HOST='$DB_MASTER_HOST',
MASTER_PORT=$DB_MASTER_PORT_NUMBER,
MASTER_USER='$DB_REPLICATION_USER',
MASTER_PASSWORD='$DB_REPLICATION_PASSWORD',
MASTER_DELAY=$DB_MASTER_DELAY,
MASTER_LOG_FILE='$MYSQL_FILE',
MASTER_LOG_POS=$MYSQL_POSITION,
MASTER_CONNECT_RETRY=10;
EOF
info "Start import dump database $DB"
mysql_execute "$DB" < "$DUMP_FILE"
info "Finish import dump database $DB"
else
info "Error creating dump"
fi
info "Remove dump file"
rm -f "$DUMP_FILE"
info "Finish dump process database $DB"
done

info "Remove dump file"
rm -f $FILE_LOCATION

info "Finish dump process databases"

info "MySQL dump master data finish..."
}

Expand All @@ -240,7 +250,7 @@ mysql_configure_replication() {

if [[ "$DB_REPLICATION_SLAVE_DUMP" = "true" ]]; then
mysql_exec_initial_dump
fi
else

debug "Replication master ready!"
debug "Setting the master configuration"
Expand All @@ -252,6 +262,8 @@ MASTER_PASSWORD='$DB_REPLICATION_PASSWORD',
MASTER_DELAY=$DB_MASTER_DELAY,
MASTER_CONNECT_RETRY=10;
EOF

fi
elif [[ "$DB_REPLICATION_MODE" = "master" ]]; then
info "Configuring replication in master node"
if [[ -n "$DB_REPLICATION_USER" ]]; then
Expand Down
68 changes: 40 additions & 28 deletions bitnami/mysql/8.3/debian-12/rootfs/opt/bitnami/scripts/libmysql.sh
Original file line number Diff line number Diff line change
Expand Up @@ -189,35 +189,45 @@ EOF
#########################
mysql_exec_initial_dump() {
info "MySQL dump master data start..."
mysql -h "$DB_MASTER_HOST" -P "$DB_MASTER_PORT_NUMBER" -u "$DB_MASTER_ROOT_USER" -p"$DB_MASTER_ROOT_PASSWORD" -e 'RESET MASTER;'

databases=("mysql")
if [ -n "$DB_DATABASE" ]; then
databases+=("$DB_DATABASE")
fi
for DB in "${databases[@]}"; do
info "Start dump process database $DB"
if [[ $DB = @(information_schema|performance_schema|sys) ]]; then
info "Skipping default table $DB to be imported"
continue
fi
DUMP_FILE="$DB_DATA_DIR/dump_$DB.sql"
if mysqldump --verbose -h "$DB_MASTER_HOST" -P "$DB_MASTER_PORT_NUMBER" -u "$DB_MASTER_ROOT_USER" -p"$DB_MASTER_ROOT_PASSWORD" "$DB" > "$DUMP_FILE"; then
info "Finish dump database $DB"
info "Ensure database exists $DB"
mysql -u "$DB_MASTER_ROOT_USER" <<EOF
create database if not exists $DB;
info "LOCK MASTER DATABASES FOR WRITE OPERATIONS..."
mysql -h "$DB_MASTER_HOST" -P "$DB_MASTER_PORT_NUMBER" -u "$DB_MASTER_ROOT_USER" -p"$DB_MASTER_ROOT_PASSWORD" -se 'FLUSH TABLES WITH READ LOCK;'

info "SHOW MASTER STATUS..."
read -r MYSQL_FILE MYSQL_POSITION <<< "$(mysql -h "$DB_MASTER_HOST" -P "$DB_MASTER_PORT_NUMBER" -u "$DB_MASTER_ROOT_USER" -p"$DB_MASTER_ROOT_PASSWORD" -se 'SHOW MASTER STATUS;' | awk 'NR==1 {print $1, $2}')"
info "File: $MYSQL_FILE and Position: $MYSQL_POSITION"

info "Start dump process databases"

FILE_LOCATION="$DB_DATA_DIR/dump_all_databases.sql"

mysqldump --verbose --all-databases -h "$DB_MASTER_HOST" -P "$DB_MASTER_PORT_NUMBER" -u "$DB_MASTER_ROOT_USER" -p"$DB_MASTER_ROOT_PASSWORD" > $FILE_LOCATION

info "Finish dump databases"

info "UNLOCK MASTER DATABASES FOR WRITE OPERATIONS..."
mysql -h "$DB_MASTER_HOST" -P "$DB_MASTER_PORT_NUMBER" -u "$DB_MASTER_ROOT_USER" -p"$DB_MASTER_ROOT_PASSWORD" -se 'UNLOCK TABLES;'

info "Start import dump databases"
mysql_execute < $FILE_LOCATION
info "Finish import dump databases"

mysql_execute "mysql" <<EOF
CHANGE MASTER TO MASTER_HOST='$DB_MASTER_HOST',
MASTER_PORT=$DB_MASTER_PORT_NUMBER,
MASTER_USER='$DB_REPLICATION_USER',
MASTER_PASSWORD='$DB_REPLICATION_PASSWORD',
MASTER_DELAY=$DB_MASTER_DELAY,
MASTER_LOG_FILE='$MYSQL_FILE',
MASTER_LOG_POS=$MYSQL_POSITION,
MASTER_CONNECT_RETRY=10;
EOF
info "Start import dump database $DB"
mysql_execute "$DB" < "$DUMP_FILE"
info "Finish import dump database $DB"
else
info "Error creating dump"
fi
info "Remove dump file"
rm -f "$DUMP_FILE"
info "Finish dump process database $DB"
done

info "Remove dump file"
rm -f $FILE_LOCATION

info "Finish dump process databases"

info "MySQL dump master data finish..."
}

Expand All @@ -240,7 +250,7 @@ mysql_configure_replication() {

if [[ "$DB_REPLICATION_SLAVE_DUMP" = "true" ]]; then
mysql_exec_initial_dump
fi
else

debug "Replication master ready!"
debug "Setting the master configuration"
Expand All @@ -252,6 +262,8 @@ MASTER_PASSWORD='$DB_REPLICATION_PASSWORD',
MASTER_DELAY=$DB_MASTER_DELAY,
MASTER_CONNECT_RETRY=10;
EOF

fi
elif [[ "$DB_REPLICATION_MODE" = "master" ]]; then
info "Configuring replication in master node"
if [[ -n "$DB_REPLICATION_USER" ]]; then
Expand Down
1 change: 0 additions & 1 deletion bitnami/mysql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,6 @@ The above command scales up the number of slaves to `3`. You can scale down in t
If your master database is missing some binary files, the replication will break. It's possible to add `MYSQL_REPLICATION_SLAVE_DUMP=true` to make a dump on the master and import it on the slave.

> **Note**: The master database must be only used by this process until the end to avoid missing data.
> **Note**: This process will use "RESET MASTER" on the master database, removing all binary log files that are listed in the index file. It is recommended to make a backup of binary files to avoid possible data loss, in case something goes wrong.

### Configuration file

Expand Down

0 comments on commit 4f52344

Please sign in to comment.