From a23a60921ab7546a1475cedd10a4d03148ef142b Mon Sep 17 00:00:00 2001 From: "Huang, Sophie (sh879n)" Date: Fri, 3 Jul 2020 17:49:41 +0000 Subject: [PATCH] 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 --- mariadb/templates/bin/_backup_mariadb.sh.tpl | 10 +++++- mariadb/templates/bin/_restore_mariadb.sh.tpl | 36 ++++++++++--------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/mariadb/templates/bin/_backup_mariadb.sh.tpl b/mariadb/templates/bin/_backup_mariadb.sh.tpl index 00517de17..9945609de 100644 --- a/mariadb/templates/bin/_backup_mariadb.sh.tpl +++ b/mariadb/templates/bin/_backup_mariadb.sh.tpl @@ -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 diff --git a/mariadb/templates/bin/_restore_mariadb.sh.tpl b/mariadb/templates/bin/_restore_mariadb.sh.tpl index 1e8841189..d9c421969 100755 --- a/mariadb/templates/bin/_restore_mariadb.sh.tpl +++ b/mariadb/templates/bin/_restore_mariadb.sh.tpl @@ -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"