Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Import a dump folder to a database
- # Will remove the table, view and trigger just before importing each table
- # Where is located your dumps.
- # The folder must contain one file per table and must use the .sql.gz extension
- DUMP_FOLDER="$1"
- # MySQL information
- MY_HOST="$2"
- MY_USER="$3"
- MY_DB="$4"
- # For security reasons, you should not specify this in the command line (if not specified, the script will prompt for it)
- MY_PASS="$5"
- # binaries
- AWK=$(which awk)
- GREP=$(which grep)
- MYSQL=$(which mysql)
- WC=$(which wc)
- ZCAT=$(which zcat)
- if [ -z "${MY_PASS}" ]
- then
- read -p "MySQL password: " -s MY_PASS
- fi
- MYSQL_OPT="-h ${MY_HOST} -u ${MY_USER} -p$MY_PASS ${MY_DB}"
- # get max table length (string)
- MAX_TABLE_LEN=$(${MYSQL} ${MYSQL_OPT} -e "SHOW TABLES" | ${AWK} "{ print $1 }" | ${GREP} -v '^Tables' | ${WC} -L)
- for file in `ls -1 ${DUMP_FOLDER}/*.sql.gz`
- do
- table=$(basename $file)
- table=${table%.sql.gz}
- printf "Table %-*s " ${MAX_TABLE_LEN} $table
- echo -n "DROP ... "
- ${MYSQL} ${MYSQL_OPT} -e "SET FOREIGN_KEY_CHECKS = 0; \
- DROP TABLE IF EXISTS $table; \
- DROP VIEW IF EXISTS $table; \
- SET FOREIGN_KEY_CHECKS = 1"
- echo -n "TRIGGERS ... "
- TRIGGERS=$(${MYSQL} ${MYSQL_OPT} -e "SHOW TRIGGERS IN ${MY_DB} WHERE \`Table\` = '${table}'\G" | ${GREP} "Trigger: " | ${AWK} '{ print $2 }')
- if [ -n "${TRIGGERS}" ]
- then
- for trigger in ${TRIGGERS}
- do
- ${MYSQL} ${MYSQL_OPT} -e "DROP TRIGGER IF EXISTS $trigger"
- done
- fi
- echo -n "IMPORT ... "
- ${ZCAT} $file | ${MYSQL} ${MYSQL_OPT}
- echo "DONE"
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement