2
0
mirror of https://github.com/deajan/osync synced 2024-11-19 03:25:37 +00:00
osync/upgrade-v1.0x-v1.3x.sh

606 lines
17 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
2015-09-26 11:30:55 +00:00
PROGRAM="osync instance upgrade script"
SUBPROGRAM="osync"
2020-01-10 10:07:25 +00:00
AUTHOR="(C) 2016-2020 by Orsiris de Jong"
2015-09-26 11:30:55 +00:00
CONTACT="http://www.netpower.fr/osync - ozy@netpower.fr"
2019-01-10 23:43:19 +00:00
OLD_PROGRAM_VERSION="v1.0x-v1.2x"
NEW_PROGRAM_VERSION="v1.3x"
CONFIG_FILE_REVISION=1.3.0
2020-01-22 17:49:20 +00:00
PROGRAM_BUILD=2020012201
2016-04-13 10:26:20 +00:00
## type -p does not work on platforms other than linux (bash). If if does not work, always assume output is not a zero exitcode
if ! type "$BASH" > /dev/null; then
2016-12-11 19:54:40 +00:00
echo "Please run this script only with bash shell. Tested on bash >= 3.2"
exit 127
2016-04-13 10:26:20 +00:00
fi
2015-09-26 11:30:55 +00:00
2016-10-17 15:40:37 +00:00
# Defines all keywords / value sets in osync configuration files
# bash does not support two dimensional arrays, so we declare two arrays:
# ${KEYWORDS[index]}=${VALUES[index]}
KEYWORDS=(
INSTANCE_ID
INITIATOR_SYNC_DIR
TARGET_SYNC_DIR
SSH_RSA_PRIVATE_KEY
SSH_PASSWORD_FILE
2017-02-08 12:35:53 +00:00
_REMOTE_TOKEN
2016-10-17 15:40:37 +00:00
CREATE_DIRS
LOGFILE
MINIMUM_SPACE
BANDWIDTH
SUDO_EXEC
RSYNC_EXECUTABLE
RSYNC_REMOTE_PATH
RSYNC_PATTERN_FIRST
RSYNC_INCLUDE_PATTERN
RSYNC_EXCLUDE_PATTERN
RSYNC_INCLUDE_FROM
RSYNC_EXCLUDE_FROM
PATH_SEPARATOR_CHAR
SSH_COMPRESSION
SSH_IGNORE_KNOWN_HOSTS
2019-07-05 20:37:12 +00:00
SSH_CONTROLMASTER
2016-10-17 15:40:37 +00:00
REMOTE_HOST_PING
REMOTE_3RD_PARTY_HOSTS
RSYNC_OPTIONAL_ARGS
2016-10-17 15:40:37 +00:00
PRESERVE_PERMISSIONS
PRESERVE_OWNER
PRESERVE_GROUP
PRESERVE_EXECUTABILITY
PRESERVE_ACL
PRESERVE_XATTR
COPY_SYMLINKS
KEEP_DIRLINKS
PRESERVE_HARDLINKS
CHECKSUM
RSYNC_COMPRESS
SOFT_MAX_EXEC_TIME
HARD_MAX_EXEC_TIME
KEEP_LOGGING
MIN_WAIT
MAX_WAIT
2017-06-06 14:23:04 +00:00
LOG_CONFLICTS
ALERT_CONFLICTS
2016-10-17 15:40:37 +00:00
CONFLICT_BACKUP
CONFLICT_BACKUP_MULTIPLE
CONFLICT_BACKUP_DAYS
CONFLICT_PREVALANCE
SOFT_DELETE
SOFT_DELETE_DAYS
2016-10-21 10:38:03 +00:00
SKIP_DELETION
2020-01-22 17:49:20 +00:00
SYNC_TYPE
2016-10-17 15:40:37 +00:00
RESUME_SYNC
RESUME_TRY
FORCE_STRANGER_LOCK_RESUME
PARTIAL
DELTA_COPIES
DESTINATION_MAILS
MAIL_BODY_CHARSET
2016-10-17 15:40:37 +00:00
SENDER_MAIL
SMTP_SERVER
SMTP_PORT
SMTP_ENCRYPTION
SMTP_USER
SMTP_PASSWORD
LOCAL_RUN_BEFORE_CMD
LOCAL_RUN_AFTER_CMD
REMOTE_RUN_BEFORE_CMD
REMOTE_RUN_AFTER_CMD
MAX_EXEC_TIME_PER_CMD_BEFORE
MAX_EXEC_TIME_PER_CMD_AFTER
STOP_ON_CMD_ERROR
RUN_AFTER_CMD_ON_ERROR
)
VALUES=(
sync-test
''
''
${HOME}/backupuser/.ssh/id_rsa
''
2017-02-08 12:35:53 +00:00
SomeAlphaNumericToken9
2019-01-10 23:43:19 +00:00
false
2016-10-17 15:40:37 +00:00
''
10240
0
2019-01-10 23:43:19 +00:00
false
2016-10-17 15:40:37 +00:00
rsync
''
include
''
''
''
''
\;
2019-01-10 23:43:19 +00:00
true
false
false
2019-07-05 20:37:12 +00:00
false
2016-10-23 17:16:33 +00:00
'www.kernel.org www.google.com'
''
2019-01-10 23:43:19 +00:00
true
true
true
true
false
false
false
false
false
false
true
2016-10-17 15:40:37 +00:00
7200
10600
1801
60
7200
false
2019-01-10 23:43:19 +00:00
false
true
false
2016-10-17 15:40:37 +00:00
30
initiator
2019-01-10 23:43:19 +00:00
true
2016-10-17 15:40:37 +00:00
30
2016-10-21 10:38:03 +00:00
''
2020-01-22 17:49:20 +00:00
''
2019-01-10 23:43:19 +00:00
true
2016-10-17 15:40:37 +00:00
2
2019-01-10 23:43:19 +00:00
false
false
true
2016-10-17 15:40:37 +00:00
''
''
2017-02-06 15:39:27 +00:00
alert@your.system.tld
2016-10-17 15:40:37 +00:00
smtp.your.isp.tld
25
none
''
''
''
''
''
''
0
0
2019-01-10 23:43:19 +00:00
true
false
2016-10-17 15:40:37 +00:00
)
2015-09-26 11:30:55 +00:00
function Init {
OSYNC_DIR=".osync_workdir"
STATE_DIR="state"
TREE_CURRENT_FILENAME="-tree-current-$SYNC_ID"
TREE_AFTER_FILENAME="-tree-after-$SYNC_ID"
DELETED_LIST_FILENAME="-deleted-list-$SYNC_ID"
FAILED_DELETE_LIST_FILENAME="-failed-delete-$SYNC_ID"
if [ "${SLAVE_SYNC_DIR:0:6}" == "ssh://" ]; then
2019-05-21 12:30:26 +00:00
# Might also exist from old config file as REMOTE_OPERATION=yes
REMOTE_OPERATION=true
2015-09-26 11:43:25 +00:00
# remove leadng 'ssh://'
uri=${SLAVE_SYNC_DIR#ssh://*}
if [[ "$uri" == *"@"* ]]; then
# remove everything after '@'
REMOTE_USER=${uri%@*}
else
REMOTE_USER=$LOCAL_USER
fi
if [ "$SSH_RSA_PRIVATE_KEY" == "" ]; then
SSH_RSA_PRIVATE_KEY=~/.ssh/id_rsa
fi
# remove everything before '@'
_hosturiandpath=${uri#*@}
# remove everything after first '/'
_hosturi=${_hosturiandpath%%/*}
if [[ "$_hosturi" == *":"* ]]; then
REMOTE_PORT=${_hosturi##*:}
else
REMOTE_PORT=22
fi
REMOTE_HOST=${_hosturi%%:*}
2015-09-26 11:30:55 +00:00
# remove everything before first '/'
2015-09-26 11:43:25 +00:00
SLAVE_SYNC_DIR=${_hosturiandpath#*/}
fi
2015-09-26 11:30:55 +00:00
SSH_CMD="$(type -p ssh) $SSH_COMP -i $SSH_RSA_PRIVATE_KEY $REMOTE_USER@$REMOTE_HOST -p $REMOTE_PORT"
}
function Usage {
echo "$PROGRAM $PROGRAM_BUILD"
2015-09-26 11:43:25 +00:00
echo $AUTHOR
echo $CONTACT
echo ""
echo "This script migrates osync $OLD_PROGRAM_VERSION setups to $NEW_PROGRAM_VERSION by updating state filenames and config files."
2015-09-26 11:30:55 +00:00
echo ""
echo "Usage: $0 /path/to/config_file.conf"
echo "Usage: $0 --master=/path/to/master/replica --slave=/path/to/local/slave/replica --sync-id=existing_id"
echo "Usage: $0 --master=/path/to/master/replica --slave=ssh://[backupuser]@remotehost.com[:portnumber]//path/to/slave/replica --sync-id=existing_id --rsakey=/path/to/rsa/key"
2015-09-26 11:30:55 +00:00
echo ""
2016-04-06 19:09:08 +00:00
echo "If config file is provided, the config file itself and both replicas from config file will be updated. Please make sure the config file is writable."
echo "If no config file provided, assume you run the update script just like any other quicksync task."
2015-09-26 11:30:55 +00:00
echo "If sync-id is not specified, it will assume handling a quicksync task."
2015-09-26 11:43:25 +00:00
exit 128
2015-09-26 11:30:55 +00:00
}
function LoadConfigFile {
2015-09-26 11:43:25 +00:00
local config_file="${1}"
if [ ! -f "$config_file" ]; then
echo "Cannot load configuration file [$config_file]. Sync cannot start."
exit 1
elif [[ "$1" != *".conf" ]]; then
echo "Wrong configuration file supplied [$config_file]. Sync cannot start."
exit 1
else
2016-12-11 20:27:30 +00:00
egrep '^#|^[^ ]*=[^;&]*' "$config_file" > "./$SUBPROGRAM.${FUNCNAME[0]}.$$"
2015-09-26 11:43:25 +00:00
# Shellcheck source=./sync.conf
2016-12-11 20:27:30 +00:00
source "./$SUBPROGRAM.${FUNCNAME[0]}.$$"
rm -f "./$SUBPROGRAM.${FUNCNAME[0]}.$$"
2015-09-26 11:43:25 +00:00
fi
2015-09-26 11:30:55 +00:00
}
function _RenameStateFilesLocal {
local state_dir="${1}" # Absolute path to statedir
local rewrite=false
echo "Rewriting state files in [$state_dir]."
# Make sure there is no ending slash
state_dir="${state_dir%/}/"
2019-12-25 17:39:53 +00:00
if [ -f "${state_dir}master${TREE_CURRENT_FILENAME}" ]; then
mv -f "${state_dir}master${TREE_CURRENT_FILENAME}" "${state_dir}initiator${TREE_CURRENT_FILENAME}"
2015-09-26 11:30:55 +00:00
if [ $? != 0 ]; then
2019-12-25 17:39:53 +00:00
echo "Error while rewriting ${state_dir}master${TREE_CURRENT_FILENAME}"
2015-09-26 11:30:55 +00:00
else
rewrite=true
fi
fi
2019-12-25 17:39:53 +00:00
if [ -f "${state_dir}master${TREE_AFTER_FILENAME}" ]; then
mv -f "${state_dir}master${TREE_AFTER_FILENAME}" "${state_dir}initiator${TREE_AFTER_FILENAME}"
2015-09-26 11:30:55 +00:00
if [ $? != 0 ]; then
2019-12-25 17:39:53 +00:00
echo "Error while rewriting ${state_dir}master${TREE_AFTER_FILENAME}"
2015-09-26 11:30:55 +00:00
else
rewrite=true
fi
fi
2019-12-25 17:39:53 +00:00
if [ -f "${state_dir}master${DELETED_LIST_FILENAME}" ]; then
mv -f "${state_dir}master${DELETED_LIST_FILENAME}" "${state_dir}initiator${DELETED_LIST_FILENAME}"
2015-09-26 11:30:55 +00:00
if [ $? != 0 ]; then
2019-12-25 17:39:53 +00:00
echo "Error while rewriting ${state_dir}master${DELETED_LIST_FILENAME}"
2015-09-26 11:30:55 +00:00
else
rewrite=true
fi
rewrite=true
fi
2019-12-25 17:39:53 +00:00
if [ -f "${state_dir}master${FAILED_DELETE_LIST_FILENAME}" ]; then
mv -f "${state_dir}master${FAILED_DELETE_LIST_FILENAME}" "${state_dir}initiator${FAILED_DELETE_LIST_FILENAME}"
2015-09-26 11:30:55 +00:00
if [ $? != 0 ]; then
2019-12-25 17:39:53 +00:00
echo "Error while rewriting ${state_dir}master${FAILED_DELETE_LIST_FILENAME}"
2015-09-26 11:30:55 +00:00
else
rewrite=true
fi
fi
2019-12-25 17:39:53 +00:00
if [ -f "${state_dir}master${TREE_CURRENT_FILENAME}-dry" ]; then
mv -f "${state_dir}master${TREE_CURRENT_FILENAME}-dry" "${state_dir}initiator${TREE_CURRENT_FILENAME}-dry"
2015-09-26 11:30:55 +00:00
if [ $? != 0 ]; then
2019-12-25 17:39:53 +00:00
echo "Error while rewriting ${state_dir}master${TREE_CURRENT_FILENAME}-dry"
2015-09-26 11:30:55 +00:00
else
rewrite=true
fi
fi
2019-12-25 17:39:53 +00:00
if [ -f "${state_dir}master${TREE_AFTER_FILENAME}-dry" ]; then
mv -f "${state_dir}master${TREE_AFTER_FILENAME}-dry" "${state_dir}initiator${TREE_AFTER_FILENAME}-dry"
2015-09-26 11:30:55 +00:00
if [ $? != 0 ]; then
2019-12-25 17:39:53 +00:00
echo "Error while rewriting ${state_dir}master${TREE_AFTER_FILENAME}-dry"
2015-09-26 11:30:55 +00:00
else
rewrite=true
fi
fi
2019-12-25 17:39:53 +00:00
if [ -f "${state_dir}master${DELETED_LIST_FILENAME}-dry" ]; then
mv -f "${state_dir}master${DELETED_LIST_FILENAME}-dry" "${state_dir}initiator${DELETED_LIST_FILENAME}-dry"
2015-09-26 11:30:55 +00:00
if [ $? != 0 ]; then
2019-12-25 17:39:53 +00:00
echo "Error while rewriting ${state_dir}master${DELETED_LIST_FILENAME}-dry"
2015-09-26 11:30:55 +00:00
else
rewrite=true
fi
fi
2019-12-25 17:39:53 +00:00
if [ -f "${state_dir}master${FAILED_DELETE_LIST_FILENAME}-dry" ]; then
mv -f "${state_dir}master${FAILED_DELETE_LIST_FILENAME}-dry" "${state_dir}initiator${FAILED_DELETE_LIST_FILENAME}-dry"
2015-09-26 11:30:55 +00:00
if [ $? != 0 ]; then
2019-12-25 17:39:53 +00:00
echo "Error while rewriting ${state_dir}master${FAILED_DELETE_LIST_FILENAME}-dry"
2015-09-26 11:30:55 +00:00
else
rewrite=true
fi
fi
2019-12-25 17:39:53 +00:00
if [ -f "${state_dir}slave${TREE_CURRENT_FILENAME}" ]; then
mv -f "${state_dir}slave${TREE_CURRENT_FILENAME}" "${state_dir}target${TREE_CURRENT_FILENAME}"
2015-09-26 11:30:55 +00:00
if [ $? != 0 ]; then
2019-12-25 17:39:53 +00:00
echo "Error while rewriting ${state_dir}slave${TREE_CURRENT_FILENAME}"
2015-09-26 11:30:55 +00:00
else
rewrite=true
fi
fi
2019-12-25 17:39:53 +00:00
if [ -f "${state_dir}slave${TREE_AFTER_FILENAME}" ]; then
mv -f "${state_dir}slave${TREE_AFTER_FILENAME}" "${state_dir}target${TREE_AFTER_FILENAME}"
2015-09-26 11:30:55 +00:00
if [ $? != 0 ]; then
2019-12-25 17:39:53 +00:00
echo "Error while rewriting ${state_dir}slave${TREE_AFTER_FILENAME}"
2015-09-26 11:30:55 +00:00
else
rewrite=true
fi
fi
2019-12-25 17:39:53 +00:00
if [ -f "${state_dir}slave${DELETED_LIST_FILENAME}" ]; then
mv -f "${state_dir}slave${DELETED_LIST_FILENAME}" "${state_dir}target${DELETED_LIST_FILENAME}"
2015-09-26 11:30:55 +00:00
if [ $? != 0 ]; then
2019-12-25 17:39:53 +00:00
echo "Error while rewriting ${state_dir}slave${DELETED_LIST_FILENAME}"
2015-09-26 11:30:55 +00:00
else
rewrite=true
fi
fi
2019-12-25 17:39:53 +00:00
if [ -f "${state_dir}slave${FAILED_DELETE_LIST_FILENAME}" ]; then
mv -f "${state_dir}slave${FAILED_DELETE_LIST_FILENAME}" "${state_dir}target${FAILED_DELETE_LIST_FILENAME}"
2015-09-26 11:30:55 +00:00
if [ $? != 0 ]; then
2019-12-25 17:39:53 +00:00
echo "Error while rewriting ${state_dir}slave${FAILED_DELETE_LIST_FILENAME}"
2015-09-26 11:30:55 +00:00
else
rewrite=true
fi
fi
2019-12-25 17:39:53 +00:00
if [ -f "${state_dir}slave${TREE_CURRENT_FILENAME}-dry" ]; then
mv -f "${state_dir}slave${TREE_CURRENT_FILENAME}-dry" "${state_dir}target${TREE_CURRENT_FILENAME}-dry"
2015-09-26 11:30:55 +00:00
if [ $? != 0 ]; then
2019-12-25 17:39:53 +00:00
echo "Error while rewriting ${state_dir}slave${TREE_CURRENT_FILENAME}-dry"
2015-09-26 11:30:55 +00:00
else
rewrite=true
fi
fi
2019-12-25 17:39:53 +00:00
if [ -f "${state_dir}slave${TREE_AFTER_FILENAME}-dry" ]; then
mv -f "${state_dir}slave${TREE_AFTER_FILENAME}-dry" "${state_dir}target${TREE_AFTER_FILENAME}-dry"
2015-09-26 11:30:55 +00:00
if [ $? != 0 ]; then
2019-12-25 17:39:53 +00:00
echo "Error while rewriting ${state_dir}slave${TREE_AFTER_FILENAME}-dry"
2015-09-26 11:30:55 +00:00
else
rewrite=true
fi
fi
2019-12-25 17:39:53 +00:00
if [ -f "${state_dir}slave${DELETED_LIST_FILENAME}-dry" ]; then
mv -f "${state_dir}slave${DELETED_LIST_FILENAME}-dry" "${state_dir}target${DELETED_LIST_FILENAME}-dry"
2015-09-26 11:30:55 +00:00
if [ $? != 0 ]; then
2019-12-25 17:39:53 +00:00
echo "Error while rewriting ${state_dir}slave${DELETED_LIST_FILENAME}-dry"
2015-09-26 11:30:55 +00:00
else
rewrite=true
fi
fi
2019-12-25 17:39:53 +00:00
if [ -f "${state_dir}slave${FAILED_DELETE_LIST_FILENAME}-dry" ]; then
mv -f "${state_dir}slave${FAILED_DELETE_LIST_FILENAME}-dry" "${state_dir}target${FAILED_DELETE_LIST_FILENAME}-dry"
2015-09-26 11:30:55 +00:00
if [ $? != 0 ]; then
2019-12-25 17:39:53 +00:00
echo "Error while rewriting ${state_dir}slave${FAILED_DELETE_LIST_FILENAME}-dry"
2015-09-26 11:30:55 +00:00
else
rewrite=true
fi
fi
2015-09-26 11:43:25 +00:00
if [ $rewrite == true ]; then
echo "State dir rewritten."
else
echo "Nothing rewritten in state dir."
fi
2015-09-26 11:30:55 +00:00
}
function _RenameStateFilesRemote {
echo "Connecting remotely to rewrite state files in [$1]."
2015-09-26 11:43:25 +00:00
$SSH_CMD state_dir="${1}" DELETED_LIST_FILENAME="$DELETED_LIST_FILENAME" FAILED_DELETE_LIST_FILENAME="$FAILED_DELETE_LIST_FILENAME" 'bash -s' << 'ENDSSH'
2015-09-26 11:30:55 +00:00
# Make sure there is no ending slash
state_dir="${state_dir%/}/"
2015-09-26 11:43:25 +00:00
rewrite=false
2015-09-26 11:30:55 +00:00
2019-12-25 17:39:53 +00:00
if [ -f "${state_dir}master${DELETED_LIST_FILENAME}" ]; then
mv -f "${state_dir}master${DELETED_LIST_FILENAME}" "${state_dir}initiator${DELETED_LIST_FILENAME}"
2015-09-26 11:30:55 +00:00
if [ $? != 0 ]; then
echo "Error while rewriting "$state_dir"master"$DELETED_LIST_FILENAME
else
rewrite=true
fi
fi
2019-12-25 17:39:53 +00:00
if [ -f "${state_dir}master${FAILED_DELETE_LIST_FILENAME}" ]; then
mv -f "${state_dir}master${FAILED_DELETE_LIST_FILENAME}" "${state_dir}initiator${FAILED_DELETE_LIST_FILENAME}"
2015-09-26 11:30:55 +00:00
if [ $? != 0 ]; then
echo "Error while rewriting "$state_dir"master"$FAILED_DELETE_LIST_FILENAME
else
rewrite=true
fi
fi
2019-12-25 17:39:53 +00:00
if [ -f "${state_dir}master${FAILED_DELETE_LIST_FILENAME}-dry" ]; then
mv -f "${state_dir}master${FAILED_DELETE_LIST_FILENAME}-dry" "${state_dir}initiator${FAILED_DELETE_LIST_FILENAME}-dry"
2015-09-26 11:30:55 +00:00
if [ $? != 0 ]; then
echo "Error while rewriting "$state_dir"master"$FAILED_DELETE_LIST_FILENAME"-dry"
else
rewrite=true
fi
fi
2015-09-26 11:43:25 +00:00
if [ $rewrite == true ]; then
echo "State dir rewritten."
else
echo "Nothing rewritten in state dir."
fi
2015-09-26 11:30:55 +00:00
ENDSSH
}
function RenameStateFiles {
_RenameStateFilesLocal "$MASTER_SYNC_DIR/$OSYNC_DIR/$STATE_DIR"
2019-05-21 12:30:26 +00:00
if [ "$REMOTE_OPERATION" != "yes" ] || "$REMOTE_OPERATION" == true ]; then
2015-09-26 11:30:55 +00:00
_RenameStateFilesLocal "$SLAVE_SYNC_DIR/$OSYNC_DIR/$STATE_DIR"
else
_RenameStateFilesRemote "$SLAVE_SYNC_DIR/$OSYNC_DIR/$STATE_DIR"
fi
2015-09-26 11:43:25 +00:00
}
2015-09-26 11:30:55 +00:00
2019-05-21 10:51:29 +00:00
function CheckAndBackup {
2015-09-26 11:30:55 +00:00
local config_file="${1}"
2016-12-11 20:27:30 +00:00
if ! grep "MASTER_SYNC_DIR=" "$config_file" > /dev/null && ! grep "INITIATOR_SYNC_DIR=" "$config_file" > /dev/null; then
2016-08-09 14:06:39 +00:00
echo "Config file [$config_file] does not seem to be an osync v1.0x or v1.1x file."
exit 1
2015-09-26 11:30:55 +00:00
fi
2016-12-11 19:54:40 +00:00
echo "Backing up [$config_file] as [$config_file.save]"
cp -p "$config_file" "$config_file.save"
if [ $? != 0 ]; then
echo "Cannot backup config file."
exit 1
fi
2019-05-21 10:51:29 +00:00
}
function RewriteOldConfigFiles {
local config_file="${1}"
2015-09-26 11:30:55 +00:00
echo "Rewriting config file $config_file"
#TODO: exclude occurences between doublequotes
2016-04-13 10:26:20 +00:00
sed -i'.tmp' 's/^MASTER_SYNC_DIR/INITIATOR_SYNC_DIR/g' "$config_file"
sed -i'.tmp' 's/^SLAVE_SYNC_DIR/TARGET_SYNC_DIR/g' "$config_file"
sed -i'.tmp' 's/^CONFLICT_PREVALANCE=master/CONFLICT_PREVALANCE=initiator/g' "$config_file"
sed -i'.tmp' 's/^CONFLICT_PREVALANCE=slave/CONFLICT_PREVALANCE=target/g' "$config_file"
sed -i'.tmp' 's/^SYNC_ID=/INSTANCE_ID=/g' "$config_file"
2016-11-19 12:15:57 +00:00
sed -i'.tmp' 's/^REMOTE_SYNC=.*//g' "$config_file"
2016-10-17 15:40:37 +00:00
rm -f "$config_file.tmp"
}
2016-02-08 10:41:22 +00:00
2019-01-10 23:43:19 +00:00
function AddMissingConfigOptionsAndFixBooleans {
2016-12-11 19:54:40 +00:00
local config_file="${1}"
local counter=0
while [ $counter -lt ${#KEYWORDS[@]} ]; do
if ! grep "^${KEYWORDS[$counter]}=" > /dev/null "$config_file"; then
echo "${KEYWORDS[$counter]} not found"
if [ $counter -gt 0 ]; then
2019-05-21 12:30:26 +00:00
if [ "${VALUES[$counter]}" == true ] || [ "${VALUES[$counter]}" == false ]; then
2019-01-10 23:43:19 +00:00
sed -i'.tmp' '/^'${KEYWORDS[$((counter-1))]}'=*/a\'$'\n'${KEYWORDS[$counter]}'='"${VALUES[$counter]}"'\'$'\n''' "$config_file"
else
sed -i'.tmp' '/^'${KEYWORDS[$((counter-1))]}'=*/a\'$'\n'${KEYWORDS[$counter]}'="'"${VALUES[$counter]}"'"\'$'\n''' "$config_file"
fi
2016-12-11 19:54:40 +00:00
if [ $? -ne 0 ]; then
echo "Cannot add missing ${[KEYWORDS[$counter]}."
exit 1
fi
else
2019-05-21 12:30:26 +00:00
if [ "${VALUES[$counter]}" == true ] || [ "${VALUES[$counter]}" == false ]; then
2019-01-10 23:43:19 +00:00
sed -i'.tmp' '/[GENERAL\]$//a\'$'\n'${KEYWORDS[$counter]}'='"${VALUES[$counter]}"'\'$'\n''' "$config_file"
else
sed -i'.tmp' '/[GENERAL\]$//a\'$'\n'${KEYWORDS[$counter]}'="'"${VALUES[$counter]}"'"\'$'\n''' "$config_file"
fi
2016-12-11 19:54:40 +00:00
fi
echo "Added missing ${KEYWORDS[$counter]} config option with default option [${VALUES[$counter]}]"
2019-01-10 23:43:19 +00:00
else
# Not the most elegant but the quickest way :)
2019-07-05 21:26:50 +00:00
if grep "^${KEYWORDS[$counter]}=yes$" > /dev/null "$config_file"; then
2019-01-10 23:43:19 +00:00
sed -i'.tmp' 's/^'${KEYWORDS[$counter]}'=.*/'${KEYWORDS[$counter]}'=true/g' "$config_file"
if [ $? -ne 0 ]; then
echo "Cannot rewrite ${[KEYWORDS[$counter]} boolean to true."
exit 1
fi
2019-07-05 21:26:50 +00:00
elif grep "^${KEYWORDS[$counter]}=no$" > /dev/null "$config_file"; then
2019-05-21 11:54:12 +00:00
sed -i'.tmp' 's/^'${KEYWORDS[$counter]}'=.*/'${KEYWORDS[$counter]}'=false/g' "$config_file"
2019-01-10 23:43:19 +00:00
if [ $? -ne 0 ]; then
2019-05-19 10:56:41 +00:00
echo "Cannot rewrite ${[KEYWORDS[$counter]} boolean to false."
2019-01-10 23:43:19 +00:00
exit 1
fi
fi
2016-12-11 19:54:40 +00:00
fi
counter=$((counter+1))
done
2016-10-17 15:40:37 +00:00
}
2019-01-10 23:43:19 +00:00
function RewriteSections {
local config_file="${1}"
sed -i'.tmp' 's/## ---------- GENERAL OPTIONS/[GENERAL]/g' "$config_file"
sed -i'.tmp' 's/## ---------- REMOTE OPTIONS/[REMOTE_OPTIONS]/g' "$config_file"
sed -i'.tmp' 's/## ---------- REMOTE SYNC OPTIONS/[REMOTE_OPTIONS]/g' "$config_file"
sed -i'.tmp' 's/## ---------- MISC OPTIONS/[MISC_OPTIONS]/g' "$config_file"
sed -i'.tmp' 's/## ---------- BACKUP AND DELETION OPTIONS/[BACKUP_DELETE_OPTIONS]/g' "$config_file"
sed -i'.tmp' 's/## ---------- BACKUP AND TRASH OPTIONS/[BACKUP_DELETE_OPTIONS]/g' "$config_file"
sed -i'.tmp' 's/## ---------- RESUME OPTIONS/[RESUME_OPTIONS]/g' "$config_file"
sed -i'.tmp' 's/## ---------- ALERT OPTIONS/[ALERT_OPTIONS]/g' "$config_file"
sed -i'.tmp' 's/## ---------- EXECUTION HOOKS/[EXECUTION_HOOKS]/g' "$config_file"
}
2016-10-17 15:40:37 +00:00
function UpdateConfigHeader {
2016-12-11 19:54:40 +00:00
local config_file="${1}"
2019-05-19 10:56:41 +00:00
if ! grep "^CONFIG_FILE_REVISION=" > /dev/null "$config_file"; then
2019-05-21 12:09:30 +00:00
if grep "\[GENERAL\]" > /dev/null "$config_file"; then
sed -i'.tmp' '/^\[GENERAL\]$/a\'$'\n'CONFIG_FILE_REVISION=$CONFIG_FILE_REVISION$'\n''' "$config_file"
else
sed -i'.tmp' '/.*onfig file rev.*/a\'$'\n'CONFIG_FILE_REVISION=$CONFIG_FILE_REVISION$'\n''' "$config_file"
fi
2019-05-19 10:56:41 +00:00
# "onfig file rev" to deal with earlier variants of the file where c was lower or uppercase
sed -i'.tmp' 's/.*onfig file rev.*//' "$config_file"
fi
2015-09-26 11:30:55 +00:00
}
2016-12-11 20:27:30 +00:00
_QUICK_SYNC=0
2015-09-26 11:30:55 +00:00
for i in "$@"
do
2015-09-26 11:43:25 +00:00
case $i in
--master=*)
MASTER_SYNC_DIR=${i##*=}
2019-05-20 09:37:37 +00:00
_QUICK_SYNC=$((_QUICK_SYNC + 1))
2015-09-26 11:43:25 +00:00
;;
--slave=*)
SLAVE_SYNC_DIR=${i##*=}
2019-05-20 09:37:37 +00:00
_QUICK_SYNC=$((_QUICK_SYNC + 1))
2015-09-26 11:43:25 +00:00
;;
--rsakey=*)
SSH_RSA_PRIVATE_KEY=${i##*=}
;;
--sync-id=*)
SYNC_ID=${i##*=}
;;
2015-09-26 11:30:55 +00:00
esac
done
2016-12-11 20:27:30 +00:00
if [ $_QUICK_SYNC -eq 2 ]; then
2015-09-26 11:30:55 +00:00
Init
2016-12-11 20:27:30 +00:00
RenameStateFiles "$MASTER_SYNC_DIR"
RenameStateFiles "$SLAVE_SYNC_DIR"
2015-09-26 11:30:55 +00:00
elif [ "$1" != "" ] && [ -f "$1" ] && [ -w "$1" ]; then
CONF_FILE="$1"
# Make sure there is no ending slash
CONF_FILE="${CONF_FILE%/}"
LoadConfigFile "$CONF_FILE"
Init
2019-05-21 10:51:29 +00:00
CheckAndBackup "$CONF_FILE"
RewriteSections "$CONF_FILE"
2016-12-11 19:54:40 +00:00
RewriteOldConfigFiles "$CONF_FILE"
2019-01-10 23:43:19 +00:00
AddMissingConfigOptionsAndFixBooleans "$CONF_FILE"
2016-12-11 19:54:40 +00:00
UpdateConfigHeader "$CONF_FILE"
if [ -d "$MASTER_SYNC_DIR" ]; then
RenameStateFiles "$MASTER_SYNC_DIR"
fi
if [ -d "$SLAVE_SYNC_DIR" ]; then
RenameStateFiles "$SLAVE_SYNC_DIR"
fi
2019-07-05 21:26:50 +00:00
rm -f "$CONF_FILE.tmp"
2019-10-21 11:55:48 +00:00
echo "Configuration file upgrade finished."
2015-09-26 11:30:55 +00:00
else
Usage
fi