All Downloads are FREE. Search and download functionalities are using the official Maven repository.

refcodes-batch.1.0.2.source-code.replicate-db-mirror-to-db-over-ssh-server-to-ssh-client.job Maven / Gradle / Ivy

There is a newer version: 1.0.5
Show newest version
#!/bin/bash

if [[ $HELP = y ]] ; then
	echo "The switch \"-j $JOB\" replicates a MySQL DB mirror from a mirror               " >&2
	echo "(remote) host via SSH to the (local) DB. The mirror DB is exported to a file on " >&2
	echo "this host and imported to this (local) DB.                                      " >&2
	logSeparator
fi

. $SCRIPT_DIR/validate-db-config.job
. $SCRIPT_DIR/validate-db-mirror-config.job
. $SCRIPT_DIR/validate-ssh-minimal-config.job

if [[ $HELP = y ]] ; then
	exit $EXIT_SUCCESS
fi

exitOnConfigOff "DB"
exitOnConfigOff "DB_MIRROR"
exitOnConfigOff "SSH_MINIMAL"

SSH_CLIENT_KEY_FILE=$SSH_CLIENT_KEY_DIR/$SSH_KEY_FILE_NAME
DUMP_DIR="$HOME"
DUMP_FILE="$DUMP_DIR/$DB_NAME.sql"

if [[ $VERBOSE = y ]] ; then
	echo "Assuming the path \"$SSH_CLIENT_KEY_FILE\" to exist ..." >&2
	echo "Assuming the path \"$DUMP_DIR\" to exist ..." >&2
	logSeparator
fi

exitOnMissingVariablePath "SSH_CLIENT_KEY_FILE"
exitOnMissingVariablePath "DUMP_DIR"

if [ -e "$DUMP_FILE" ] ; then
	echo "Removing \"$DUMP_FILE\" ..." >&2
	logSeparator
	rm $DUMP_FILE
	exitOnError "Unable to remove \"$DUMP_FILE\" !!!"	
fi

if [[ $VERBOSE = y ]] ; then
	echo "Dumping DB mirror \"$DB_MIRROR_NAME@$SSH_SERVER_HOSTNAME\" to dump file \"$DUMP_FILE\" ..." >&2
	logSeparator
fi

ssh $SSH_SERVER_USER_NAME@$SSH_SERVER_HOSTNAME -i $SSH_CLIENT_KEY_FILE mysqldump -u $DB_MIRROR_USER_NAME -p$DB_MIRROR_USER_PASSWORD --opt $DB_MIRROR_NAME > $DUMP_FILE
exitOnError "Unable dump DB mirror \"$DB_MIRROR_NAME@$SSH_SERVER_HOSTNAME\" to dump file \"$DUMP_FILE\" !!!"

if [[ $VERBOSE = y ]] ; then
	echo "Importing DB mirror \"$DB_MIRROR_NAME@$SSH_SERVER_HOSTNAME\" to DB \"$DB_NAME\" from dump file \"$DUMP_FILE\" ..." >&2
	logSeparator
fi

mysql -u$DB_USER_NAME -p$DB_USER_PASSWORD $DB_NAME < $DUMP_FILE
exitOnError "Unable to import DB mirror \"$DB_MIRROR_NAME@$SSH_SERVER_HOSTNAME\" from dump file \"$DUMP_FILE\" to DB \"DB_NAME\" !!!"
if [[ $VERBOSE = y ]] ; then
	echo "Cleaning up dump file \"$DUMP_FILE\" ..." >&2
	logSeparator
fi

rm $DUMP_FILE
exitOnError "Unable to remove \"$DUMP_FILE\" !!!"

showSuccess "Replicated DB mirror \"$DB_MIRROR_NAME@$SSH_SERVER_HOSTNAME\" to DB \"$DB_NAME\" !"




© 2015 - 2024 Weber Informatics LLC | Privacy Policy