From 5bff5f1ad48cedc971ab59fd78ee5b09bfab5a7f Mon Sep 17 00:00:00 2001 From: lucvic Date: Wed, 14 Oct 2015 15:43:33 +0200 Subject: [PATCH] Locality abstraction for db-backup and db-restore --- cluster-manager.sh | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 77 insertions(+), 16 deletions(-) diff --git a/cluster-manager.sh b/cluster-manager.sh index f0beadf..71049da 100644 --- a/cluster-manager.sh +++ b/cluster-manager.sh @@ -2,7 +2,7 @@ # The MIT License (MIT) # -# Copyright (c) +# Copyright (c) 2015 ISISLab, Università degli Studi di Salerno # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -23,6 +23,62 @@ # THE SOFTWARE. # +# FUNCTIONS DEFINITIONS +# + +# host_exec_cmd +# +# Description: +# Calls a command on a host, abstracting the locality of the host. +# +# Parameters: +# $1 hostname where to perform the command +# $2 the command to execute +function host_exec_cmd { + if [ "$MYHOST" == "$1" ]; then + "$2" + else + ssh -t ${SSH_USER}${1}.routetopa.eu "$2" + fi +} + +# host_send_file +# +# Description: +# Send a local file to remote host. If the copy is issued on the local host +# itself, the command is ignored. +# +# Parameters: +# $1 hostname to copy the file to +# $2 local filename +# $3 remote localhost +function host_send_file { + if [ "$MYHOST" == "$1" ]; then + mv ${2} ${3} + else + scp ${2} ${SSH_USER}${1}.routetopa.eu:${2} + fi +} + +# host_recv_file +# +# Description: +# Receive a remote file to localhost host. If the copy is issued on the local host +# itself, the command is ignored. +# +# Parameters: +# $1 hostname to copy the file from +# $2 remote filename +# $3 local filename +function host_recv_file { + if [ "$MYHOST" == "$1" ]; then + mv ${2} ${3} + else + scp ${SSH_USER}${1}.routetopa.eu:${2} ./${3} + fi +} + +# # DEFAULT OPTIONS # @@ -35,6 +91,7 @@ MYSQL_PASS=is15rdc MYSQL_DB=oxwall HOSTLIST=( + 'spod' 'prato' 'dublin' 'issy' @@ -101,31 +158,35 @@ echo 'NOTE: THIS ACTION HAS NOT BEEN TESTED YET!! JUST ECHOING COMMANDS...' echo ssh -t ${SSH_USER}${HOST}.routetopa.eu "source $ARG_SCRIPT_NAME; rm $ARG_SCRIPT_NAME" done ;; - db-restore) + db-backup) ARG_BACKUP_NAME=$2 if [ -z $ARG_BACKUP_NAME ]; then - echo "ERROR: Please specify the name of the backup. e.g.: $0 backup.sql" - exit 1 - fi - if [ ! -f $ARG_BACKUP_NAME ]; then - echo "ERROR: File $ARG_BACKUP_NAME does not exists" - exit 1 + ARG_BACKUP_NAME="${MYSQL_DB}-$(date +%Y%m%d-%H%M%S).sql" + echo "No given backup filename. Using $ARG_BACKUP_NAME" fi for HOST in ${HOSTLIST[@]}; do - scp $ARG_BACKUP_NAME ${SSH_USER}${HOST}.routetopa.eu:$ARG_BACKUP_NAME - ssh -t ${SSH_USER}${HOST}.routetopa.eu "mysql -u${MYSQL_USER} -p${MYSQL_PASS} ${MYSQL_DB} < ${ARG_BACKUP_NAME}" + host_exec_cmd ${HOST} "mysqldump -u${MYSQL_USER} -p${MYSQL_PASS} ${MYSQL_DB} > ${ARG_BACKUP_NAME}" + host_recv_file ${HOST} ${ARG_BACKUP_NAME} ${HOST}-${ARG_BACKUP_NAME} done - db-backup) + ;; + db-restore) ARG_BACKUP_NAME=$2 - if [ -z $ARG_BACKUP_NAME ]; then - ARG_BACKUP_NAME="${MYSQL_DB}-$(date +%Y%m%d-%H%M%S).sql" - echo "No given backup filename. Using $ARG_BACKUP_NAME" + if [ -z ${ARG_BACKUP_NAME} ]; then + echo "ERROR: Please specify the name of the backup. e.g.: $0 backup.sql" + exit 1 fi for HOST in ${HOSTLIST[@]}; do - ssh -t ${SSH_USER}${HOST}.routetopa.eu "mysqldump -u${MYSQL_USER} -p${MYSQL_PASS} ${MYSQL_DB} > ${ARG_BACKUP_NAME}" - scp ${SSH_USER}${HOST}.routetopa.eu:${ARG_BACKUP_NAME} ./${HOST}-${ARG_BACKUP_NAME} + if [ ! -f "${HOST}-${ARG_BACKUP_NAME}" ]; then + echo "ERROR: File ${HOST}-${ARG_BACKUP_NAME} does not exists" + exit 1 + fi + done + for HOST in ${HOSTLIST[@]}; do + host_send_file ${HOST} "${HOST}-${ARG_BACKUP_NAME}" "${ARG_BACKUP_NAME}" + host_exec_cmd ${HOST} "mysql -u${MYSQL_USER} -p${MYSQL_PASS} ${MYSQL_DB} < ${ARG_BACKUP_NAME}" done ;; + db-sql) echo 'NOTE: THIS ACTION HAS NOT BEEN TESTED YET!! JUST ECHOING COMMANDS...' ARG_SCRIPT_NAME=$2 -- libgit2 0.21.4