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 \
"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
if [[ -z "${MYSQL_DBNAMES// }" ]]
@ -50,6 +50,13 @@ dump_databases_to_directory() {
#Create a list of Databases
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
for db in "${MYSQL_DBNAMES[@]}"
do
@ -62,6 +69,7 @@ dump_databases_to_directory() {
sed -i 's/$/;/' $TMP_DIR/${db}_grant.sql
else
log ERROR "Failed to create GRANT files for ${db}"
return 1
fi
done

View File

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