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"