Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash -e
- # Set permissions: `chmod 755 ./mysql_copy.sh`
- # Run: `./mysql_copy.sh`
- ######### CONFIGURE HERE ######################################################
- # insert your source database credentials
- SRC_DB_HOST=<SOURCE HOST HERE>
- SRC_DB_USER=<SOURCE USER HERE>
- SRC_DB_PASS=<SOURCE PASS HERE
- # insert your destination database credentials
- DEST_DB_HOST=<DESTINATION HOST HERE>
- DEST_DB_USER=<DESTINATION USER HERE>
- DEST_DB_PASS=<DESTINATION PASS HERE>
- # add the databases you want to ignore here space-separated
- # leave the information_schema, performance_schema and mysql
- IGNORE_DBS=( mysql performance_schema information_schema sys YOUR_DB_HERE ANOTHER_DB_HERE )
- ########## END CONFIGURATION ##################################################
- SRC_DB_OPTS="-q --host $SRC_DB_HOST --user $SRC_DB_USER -p$SRC_DB_PASS"
- DEST_DB_OPTS="-q --host $DEST_DB_HOST --user $DEST_DB_USER -p$DEST_DB_PASS"
- GRAY='\033[1;30m'
- GREEN='\033[0;32m'
- WHITE='\033[1;37m'
- NC='\033[0m'
- echo -e "${GRAY}"
- importdb() {
- db=$1
- }
- # Loop through source databases
- mysql -e "show databases;" $SRC_DB_OPTS | while read db; do
- if [ "$db" != "Database" ] && [[ ! "${IGNORE_DBS[*]}" =~ "$db" ]]; then
- # check for existence in destination
- set +e
- mysql $DEST_DB_OPTS $db -e "select 1;" >/dev/null 2>&1
- DB_EXISTS=$?
- set -e
- # Check to see if the database exists
- if [ $DB_EXISTS -eq 0 ]; then
- echo -e "${GRAY}Skipping database '$db', already exists in destination${NC}"
- continue
- fi
- # Prompt for input
- echo -en "${WHITE}Import database '$db' (y/n)? "
- read confirm </dev/tty
- echo -e "${NC}"
- # Verify confirmed
- set +e
- echo $confirm | grep "^[Y|y]" >/dev/null 2>&1
- confirmed=$?
- set -e
- if [ $confirmed -eq 0 ]; then
- echo -e "${GREEN}Importing database '$db' ..."
- echo -e "${GRAY}"
- mysqldump ${SRC_DB_OPTS} $db | mysql ${DEST_DB_OPTS}
- echo -e "${NC}"
- else
- echo -e "${GRAY}Skipping database '$db'${NC}"
- fi
- else
- echo -e "${GRAY}Ignoring database '$db', in ignore list${NC}"
- fi
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement