refcodes-batch.1.0.2.source-code.replicate-db-to-db-mirror-over-ssh-client-to-ssh-server.job Maven / Gradle / Ivy
#!/bin/bash
if [[ $HELP = y ]] ; then
echo "The switch \"-j $JOB\" replicates a MySQL DB from this (local) " >&2
echo "host via SSH to the DB mirror. This (local) DB is exported to a file to the " >&2
echo "mirror (remote) host and imported to it's DB mirror. " >&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 \"$DB_NAME@\" to dump file \"$DUMP_FILE\" ..." >&2
logSeparator
fi
mysqldump -u $DB_USER_NAME -p$DB_USER_PASSWORD --opt $DB_NAME > $DUMP_FILE
exitOnError "Unable dump DB \"$DB_NAME\" to dump file \"$DUMP_FILE\" !!!"
if [[ $VERBOSE = y ]] ; then
echo "Importing DB \"$DB_NAME\" to DB mirror \"$DB_MIRROR_NAME@$SSH_SERVER_HOSTNAME\" from dump file \"$DUMP_FILE\" ..." >&2
logSeparator
fi
ssh $SSH_SERVER_USER_NAME@$SSH_SERVER_HOSTNAME -i $SSH_CLIENT_KEY_FILE mysql -u$DB_MIRROR_USER_NAME -p$DB_MIRROR_USER_PASSWORD $DB_MIRROR_NAME < $DUMP_FILE
exitOnError "Unable to import DB \"DB_NAME\" from dump file \"$DUMP_FILE\" to DB mirror \"$DB_MIRROR_NAME@$SSH_SERVER_HOSTNAME\" !!!"
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 \"$DB_NAME\" to DB mirror \"$DB_MIRROR_NAME@$SSH_SERVER_HOSTNAME\" !"