Minor enhancements

pull/41/merge
deajan 9 years ago
parent 8a088d92cc
commit f4116ba69c

@ -4,7 +4,7 @@ PROGRAM="Osync instance upagrade script" # Rsync based two way sync engine with
AUTHOR="(L) 2015 by Orsiris \"Ozy\" de Jong" AUTHOR="(L) 2015 by Orsiris \"Ozy\" de Jong"
CONTACT="http://www.netpower.fr/osync - ozy@netpower.fr" CONTACT="http://www.netpower.fr/osync - ozy@netpower.fr"
PROGRAM_VERSION="1.0x to v1.1" PROGRAM_VERSION="1.0x to v1.1"
PROGRAM_BUILD=2015092501 PROGRAM_BUILD=2015092601
function Init { function Init {
OSYNC_DIR=".osync_workdir" OSYNC_DIR=".osync_workdir"
@ -17,45 +17,45 @@ function Init {
FAILED_DELETE_LIST_FILENAME="-failed-delete-$SYNC_ID" FAILED_DELETE_LIST_FILENAME="-failed-delete-$SYNC_ID"
if [ "${SLAVE_SYNC_DIR:0:6}" == "ssh://" ]; then if [ "${SLAVE_SYNC_DIR:0:6}" == "ssh://" ]; then
REMOTE_SYNC="yes" REMOTE_SYNC="yes"
# remove leadng 'ssh://' # remove leadng 'ssh://'
uri=${SLAVE_SYNC_DIR#ssh://*} uri=${SLAVE_SYNC_DIR#ssh://*}
if [[ "$uri" == *"@"* ]]; then if [[ "$uri" == *"@"* ]]; then
# remove everything after '@' # remove everything after '@'
REMOTE_USER=${uri%@*} REMOTE_USER=${uri%@*}
else else
REMOTE_USER=$LOCAL_USER REMOTE_USER=$LOCAL_USER
fi fi
if [ "$SSH_RSA_PRIVATE_KEY" == "" ]; then if [ "$SSH_RSA_PRIVATE_KEY" == "" ]; then
SSH_RSA_PRIVATE_KEY=~/.ssh/id_rsa SSH_RSA_PRIVATE_KEY=~/.ssh/id_rsa
fi fi
# remove everything before '@' # remove everything before '@'
_hosturiandpath=${uri#*@} _hosturiandpath=${uri#*@}
# remove everything after first '/' # remove everything after first '/'
_hosturi=${_hosturiandpath%%/*} _hosturi=${_hosturiandpath%%/*}
if [[ "$_hosturi" == *":"* ]]; then if [[ "$_hosturi" == *":"* ]]; then
REMOTE_PORT=${_hosturi##*:} REMOTE_PORT=${_hosturi##*:}
else else
REMOTE_PORT=22 REMOTE_PORT=22
fi fi
REMOTE_HOST=${_hosturi%%:*} REMOTE_HOST=${_hosturi%%:*}
# remove everything before first '/' # remove everything before first '/'
SLAVE_SYNC_DIR=${_hosturiandpath#*/} SLAVE_SYNC_DIR=${_hosturiandpath#*/}
fi fi
SSH_CMD="$(type -p ssh) $SSH_COMP -i $SSH_RSA_PRIVATE_KEY $REMOTE_USER@$REMOTE_HOST -p $REMOTE_PORT" SSH_CMD="$(type -p ssh) $SSH_COMP -i $SSH_RSA_PRIVATE_KEY $REMOTE_USER@$REMOTE_HOST -p $REMOTE_PORT"
} }
function Usage { function Usage {
echo "$PROGRAM $PROGRAM_VERSION $PROGRAM_BUILD" echo "$PROGRAM $PROGRAM_VERSION $PROGRAM_BUILD"
echo $AUTHOR echo $AUTHOR
echo $CONTACT echo $CONTACT
echo "" echo ""
echo "This script migrates osync v1.0x setups to v1.1 by updating state filenames and config files." echo "This script migrates osync v1.0x setups to v1.1 by updating state filenames and config files."
echo "" echo ""
echo "Usage: upgrade.sh /path/to/config_file.conf" echo "Usage: upgrade.sh /path/to/config_file.conf"
echo "Usage: upgrade.sh --master=/path/to/master/replica --slave=/path/to/local/slave/replica --sync-id=existing_id" echo "Usage: upgrade.sh --master=/path/to/master/replica --slave=/path/to/local/slave/replica --sync-id=existing_id"
@ -64,39 +64,40 @@ function Usage {
echo "If config file is provided, the config file itself and both replicas from config file will be updated." echo "If config file is provided, the config file itself and both replicas from config file will be updated."
echo "If no config file provided, assume you run the update script just like any other quicksync task." echo "If no config file provided, assume you run the update script just like any other quicksync task."
echo "If sync-id is not specified, it will assume handling a quicksync task." echo "If sync-id is not specified, it will assume handling a quicksync task."
exit 128 exit 128
} }
function CheckEnvironment { function CheckEnvironment {
if [ "$REMOTE_SYNC" == "yes" ]; then if [ "$REMOTE_SYNC" == "yes" ]; then
if ! type -p ssh > /dev/null 2>&1 if ! type -p ssh > /dev/null 2>&1
then then
Logger "ssh not present. Cannot start sync." "CRITICAL" Logger "ssh not present. Cannot start sync." "CRITICAL"
return 1 return 1
fi fi
fi fi
if ! type -p rsync > /dev/null 2>&1 if ! type -p rsync > /dev/null 2>&1
then then
Logger "rsync not present. Sync cannot start." "CRITICAL" Logger "rsync not present. Sync cannot start." "CRITICAL"
return 1 return 1
fi fi
} }
function LoadConfigFile { function LoadConfigFile {
local config_file="${1}" local config_file="${1}"
if [ ! -f "$config_file" ]; then if [ ! -f "$config_file" ]; then
echo "Cannot load configuration file [$config_file]. Sync cannot start." echo "Cannot load configuration file [$config_file]. Sync cannot start."
exit 1 exit 1
elif [[ "$1" != *".conf" ]]; then elif [[ "$1" != *".conf" ]]; then
echo "Wrong configuration file supplied [$config_file]. Sync cannot start." echo "Wrong configuration file supplied [$config_file]. Sync cannot start."
exit 1 exit 1
else else
egrep '^#|^[^ ]*=[^;&]*' "$config_file" > "./osync.$FUNCNAME.$SCRIPT_PID" egrep '^#|^[^ ]*=[^;&]*' "$config_file" > "./osync.$FUNCNAME.$$"
# Shellcheck source=./sync.conf # Shellcheck source=./sync.conf
source "./osync.$FUNCNAME.$SCRIPT_PID" source "./osync.$FUNCNAME.$$"
fi rm -f "./osync.$FUNCNAME.$$"
fi
} }
function _RenameStateFilesLocal { function _RenameStateFilesLocal {
@ -240,16 +241,23 @@ function _RenameStateFilesLocal {
rewrite=true rewrite=true
fi fi
fi fi
if [ $rewrite == true ]; then
echo "State dir rewritten."
else
echo "Nothing rewritten in state dir."
fi
} }
function _RenameStateFilesRemote { function _RenameStateFilesRemote {
echo "Connecting remotely to rewrite state files in [$1]." echo "Connecting remotely to rewrite state files in [$1]."
$SSH_CMD state_dir="${1}" DELETED_LIST_FILENAME="$DELETED_LIST_FILENAME" FAILED_DELETE_LIST_FILENAME="$FAILED_DELETE_LIST_FILENAME" 'bash -x -s' << 'ENDSSH' $SSH_CMD state_dir="${1}" DELETED_LIST_FILENAME="$DELETED_LIST_FILENAME" FAILED_DELETE_LIST_FILENAME="$FAILED_DELETE_LIST_FILENAME" 'bash -s' << 'ENDSSH'
# Make sure there is no ending slash # Make sure there is no ending slash
state_dir="${state_dir%/}/" state_dir="${state_dir%/}/"
rewrite=false
if [ -f "$state_dir""master"$DELETED_LIST_FILENAME ]; then if [ -f "$state_dir""master"$DELETED_LIST_FILENAME ]; then
mv -f "$state_dir""master"$DELETED_LIST_FILENAME "$state_dir""initiator"$DELETED_LIST_FILENAME mv -f "$state_dir""master"$DELETED_LIST_FILENAME "$state_dir""initiator"$DELETED_LIST_FILENAME
@ -275,6 +283,12 @@ $SSH_CMD state_dir="${1}" DELETED_LIST_FILENAME="$DELETED_LIST_FILENAME" FAILED_
rewrite=true rewrite=true
fi fi
fi fi
if [ $rewrite == true ]; then
echo "State dir rewritten."
else
echo "Nothing rewritten in state dir."
fi
ENDSSH ENDSSH
} }
@ -285,7 +299,7 @@ function RenameStateFiles {
else else
_RenameStateFilesRemote "$SLAVE_SYNC_DIR/$OSYNC_DIR/$STATE_DIR" _RenameStateFilesRemote "$SLAVE_SYNC_DIR/$OSYNC_DIR/$STATE_DIR"
fi fi
} }
function RewriteConfigFiles { function RewriteConfigFiles {
local config_file="${1}" local config_file="${1}"
@ -309,22 +323,22 @@ _QUICKSYNC=0
for i in "$@" for i in "$@"
do do
case $i in case $i in
--master=*) --master=*)
no_maxtime=1 no_maxtime=1
MASTER_SYNC_DIR=${i##*=} MASTER_SYNC_DIR=${i##*=}
QUICK_SYNC=$(($_QUICKSYNC + 1)) QUICK_SYNC=$(($_QUICKSYNC + 1))
;; ;;
--slave=*) --slave=*)
SLAVE_SYNC_DIR=${i##*=} SLAVE_SYNC_DIR=${i##*=}
QUICK_SYNC=$(($_QUICKSYNC + 1)) QUICK_SYNC=$(($_QUICKSYNC + 1))
;; ;;
--rsakey=*) --rsakey=*)
SSH_RSA_PRIVATE_KEY=${i##*=} SSH_RSA_PRIVATE_KEY=${i##*=}
;; ;;
--sync-id=*) --sync-id=*)
SYNC_ID=${i##*=} SYNC_ID=${i##*=}
;; ;;
esac esac
done done

Loading…
Cancel
Save