#!/bin/bash#set -x## (By|Maintained by) Juan Riano## Script name: clean_mysql_definers.sh## Usage: sudo $0 dbname.sql[.gz]# Usage: sudo $0 [-h|-help]## What does it do?# Takes a mysql db dump (even .gz) and updates all DEFINERs to be root###########################################################usage(){echo"Usage: $0 [-h|-help]"echo"Usage $0 dbname"echo"Takes a mysql.sql or mysql.sql.gz database dump and updates all DEFINERs to be root"}# Minimal parameters checkif ! ["$#"=1];then usage
exitfi# Show helpif[["${1}"=~ ^(help|-help|h|-h)$ ]];then usage
exit0fi###########################################################DB=$1DBDIR=$( dirname "${DB}")DBBASENAME=$( basename "${DB}")DBNAME=$(echo"${DBBASENAME%.*}")# ecluding the .gz if existingDBTYPE=$(echo"${DBBASENAME##*.}")# Extract if a .gzif["${DBTYPE}"=="gz"];thenecho"Unzipping ${DB}..." gunzip "${DB}"DB="${DBDIR}/${DBNAME}"echo"Unzipped, new file: ${DB}"fi# Find definers other than root and admin, and replace them by rootNEWDEFINER='DEFINER=`root`'DEFINERS=$( grep -oE "DEFINER=\`[a-zA-Z]+\`""${DB}"| grep -viE "DEFINER=\`(root|admin)\`"| sort | uniq )DEFINERCOUNT=0if ! [ -z "${DEFINERS}"];thenDEFINERCOUNT=$(echo"${DEFINERS}"| wc -l )# Put definers in a single line after trimming (sed breakes without trimming!)DEFINERS=$(echo"${DEFINERS}"| awk 1ORS=' '| awk '{$1=$1}1')fiif[["${DEFINERCOUNT}"="0"]];thenecho"No definers to fix"exit0fiecho"Cleaning definers, ${DEFINERCOUNT} found..."echo"${DEFINERS}"for d in "${DEFINERS}";doecho"Replacing ${d} -with- ${NEWDEFINER}" sed -i.bak "s/${d}/${NEWDEFINER}/g""$DB"# .bak is a hack to make it work in Macecho"Cleaned ${d}"done# Remove temp file and finishrm "${DB}.bak"echo"Job done"exit0
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.