MariaDB backup and restore with grants of all users

This patchset captures the grants of all the MariaDB users
in the backup tarball and restores the grants during the
all databases restore.
Percona tool pt-show-grants is installed to the image to
accomplish the task in this PS:
https://review.opendev.org/#/c/739149/

Change-Id: I26882956f96c961b6202b1004b8cf0faee6e73eb
This commit is contained in:
Huang, Sophie (sh879n) 2020-07-03 17:49:41 +00:00 committed by Tin Lam
parent 824f168efc
commit a23a60921a
2 changed files with 28 additions and 18 deletions

View File

@ -38,7 +38,7 @@ dump_databases_to_directory() {
MYSQL_DBNAMES=( $($MYSQL --silent --skip-column-names -e \ MYSQL_DBNAMES=( $($MYSQL --silent --skip-column-names -e \
"show databases;" | \ "show databases;" | \
egrep -vi 'information_schema|performance_schema') ) egrep -vi 'information_schema|performance_schema|mysql') )
#check if there is a database to backup, otherwise exit #check if there is a database to backup, otherwise exit
if [[ -z "${MYSQL_DBNAMES// }" ]] if [[ -z "${MYSQL_DBNAMES// }" ]]
@ -50,6 +50,13 @@ dump_databases_to_directory() {
#Create a list of Databases #Create a list of Databases
printf "%s\n" "${MYSQL_DBNAMES[@]}" > $TMP_DIR/db.list printf "%s\n" "${MYSQL_DBNAMES[@]}" > $TMP_DIR/db.list
#Retrieve and create the GRANT file for all the users
if ! pt-show-grants --defaults-file=/etc/mysql/admin_user.cnf \
2>>"$LOG_FILE" > "$TMP_DIR"/grants.sql; then
log ERROR "Failed to create GRANT for all the users"
return 1
fi
#Retrieve and create the GRANT files per DB #Retrieve and create the GRANT files per DB
for db in "${MYSQL_DBNAMES[@]}" for db in "${MYSQL_DBNAMES[@]}"
do do
@ -62,6 +69,7 @@ dump_databases_to_directory() {
sed -i 's/$/;/' $TMP_DIR/${db}_grant.sql sed -i 's/$/;/' $TMP_DIR/${db}_grant.sql
else else
log ERROR "Failed to create GRANT files for ${db}" log ERROR "Failed to create GRANT files for ${db}"
return 1
fi fi
done done

View File

@ -221,6 +221,10 @@ restore_single_db() {
$MYSQL < ${TMP_DIR}/${SINGLE_DB_NAME}_grant.sql 2>>$RESTORE_LOG $MYSQL < ${TMP_DIR}/${SINGLE_DB_NAME}_grant.sql 2>>$RESTORE_LOG
if [[ "$?" -eq 0 ]] if [[ "$?" -eq 0 ]]
then then
if ! $MYSQL --execute="FLUSH PRIVILEGES;"; then
echo "Failed to flush privileges for $SINGLE_DB_NAME."
return 1
fi
echo "Database $SINGLE_DB_NAME Permission Restore successful." echo "Database $SINGLE_DB_NAME Permission Restore successful."
else else
cat $RESTORE_LOG cat $RESTORE_LOG
@ -254,27 +258,25 @@ restore_all_dbs() {
echo "Databases $( echo $DBS | tr -d '\n') Restore failed." echo "Databases $( echo $DBS | tr -d '\n') Restore failed."
return 1 return 1
fi fi
if [ -n "$DBS" ] if [[ -f ${TMP_DIR}/grants.sql ]]
then then
for db in $DBS $MYSQL < ${TMP_DIR}/grants.sql 2>$RESTORE_LOG
do
if [ -f ${TMP_DIR}/${db}_grant.sql ]
then
$MYSQL < ${TMP_DIR}/${db}_grant.sql 2>>$RESTORE_LOG
if [[ "$?" -eq 0 ]] if [[ "$?" -eq 0 ]]
then then
echo "Database $db Permission Restore successful." if ! $MYSQL --execute="FLUSH PRIVILEGES;"; then
echo "Failed to flush privileges."
return 1
fi
echo "Databases Permission Restore successful."
else else
cat $RESTORE_LOG cat $RESTORE_LOG
echo "Database $db Permission Restore failed." echo "Databases Permission Restore failed."
return 1 return 1
fi fi
else else
echo "There is no permission file available for $db" echo "There is no permission file available"
return 1 return 1
fi fi
done
fi
else else
echo "There is no database file available to restore from" echo "There is no database file available to restore from"
return 1 return 1