2
0
mirror of https://github.com/deajan/osync synced 2024-11-11 07:10:40 +00:00
osync/install.sh

989 lines
27 KiB
Bash
Raw Normal View History

2014-05-26 22:50:46 +00:00
#!/usr/bin/env bash
2017-03-14 21:36:27 +00:00
## Installer script suitable for osync / obackup / pmocr
2015-11-12 00:37:43 +00:00
PROGRAM=osync
2017-03-14 21:36:27 +00:00
PROGRAM_VERSION=$(grep "PROGRAM_VERSION=" $PROGRAM.sh)
PROGRAM_VERSION=${PROGRAM_VERSION#*=}
2015-11-12 00:37:43 +00:00
PROGRAM_BINARY=$PROGRAM".sh"
PROGRAM_BATCH=$PROGRAM"-batch.sh"
2017-02-07 15:00:44 +00:00
SSH_FILTER="ssh_filter.sh"
2019-03-15 10:58:45 +00:00
SCRIPT_BUILD=2019022601
2018-09-30 12:07:09 +00:00
INSTANCE_ID="installer-$SCRIPT_BUILD"
2014-05-21 17:12:19 +00:00
2016-04-06 18:14:41 +00:00
## osync / obackup / pmocr / zsnap install script
2016-12-13 08:19:14 +00:00
## Tested on RHEL / CentOS 6 & 7, Fedora 23, Debian 7 & 8, Mint 17 and FreeBSD 8, 10 and 11
2014-05-21 17:12:19 +00:00
## Please adapt this to fit your distro needs
2018-10-02 08:52:25 +00:00
_OFUNCTIONS_VERSION=2.3.0-RC2
2019-03-15 11:10:27 +00:00
_OFUNCTIONS_BUILD=2019031502
2018-09-30 12:07:09 +00:00
_OFUNCTIONS_BOOTSTRAP=true
2018-10-02 08:52:25 +00:00
if ! type "$BASH" > /dev/null; then
echo "Please run this script only with bash shell. Tested on bash >= 3.2"
exit 127
fi
## Correct output of sort command (language agnostic sorting)
export LC_ALL=C
## Default umask for file creation
umask 0077
2016-11-11 23:07:50 +00:00
2018-10-02 08:52:25 +00:00
# Standard alert mail body
MAIL_ALERT_MSG="Execution of $PROGRAM instance $INSTANCE_ID on $(date) has warnings/errors."
# Environment variables that can be overriden by programs
_DRYRUN=false
2017-07-27 21:20:24 +00:00
_LOGGER_SILENT=false
2018-10-02 08:52:25 +00:00
_LOGGER_VERBOSE=false
_LOGGER_ERR_ONLY=false
_LOGGER_PREFIX="date"
if [ "$KEEP_LOGGING" == "" ]; then
KEEP_LOGGING=1801
fi
2017-07-27 21:20:24 +00:00
2018-10-02 08:52:25 +00:00
# Initial error status, logging 'WARN', 'ERROR' or 'CRITICAL' will enable alerts flags
ERROR_ALERT=false
WARN_ALERT=false
2017-07-27 21:20:24 +00:00
2019-02-26 11:07:57 +00:00
## allow debugging from command line with _DEBUG=true
if [ ! "$_DEBUG" == true ]; then
_DEBUG=false
2018-10-02 08:52:25 +00:00
_LOGGER_VERBOSE=false
else
trap 'TrapError ${LINENO} $?' ERR
_LOGGER_VERBOSE=true
fi
2016-04-06 18:14:41 +00:00
2018-10-02 08:52:25 +00:00
if [ "$SLEEP_TIME" == "" ]; then # Leave the possibity to set SLEEP_TIME as environment variable when runinng with bash -x in order to avoid spamming console
SLEEP_TIME=.05
2017-02-07 15:00:44 +00:00
fi
2016-04-06 18:14:41 +00:00
2018-10-02 08:52:25 +00:00
SCRIPT_PID=$$
2016-04-08 19:10:54 +00:00
2018-10-02 08:52:25 +00:00
LOCAL_USER=$(whoami)
LOCAL_HOST=$(hostname)
2015-09-28 12:43:42 +00:00
2018-10-02 08:52:25 +00:00
if [ "$PROGRAM" == "" ]; then
PROGRAM="ofunctions"
fi
## Default log file until config file is loaded
if [ -w /var/log ]; then
LOG_FILE="/var/log/$PROGRAM.log"
2016-05-26 09:06:38 +00:00
elif ([ "$HOME" != "" ] && [ -w "$HOME" ]); then
2018-10-02 08:52:25 +00:00
LOG_FILE="$HOME/$PROGRAM.log"
elif [ -w . ]; then
LOG_FILE="./$PROGRAM.log"
2016-05-26 09:06:38 +00:00
else
2018-10-02 08:52:25 +00:00
LOG_FILE="/tmp/$PROGRAM.log"
fi
## Default directory where to store temporary run files
2018-10-17 21:53:13 +00:00
2018-10-02 08:52:25 +00:00
if [ -w /tmp ]; then
RUN_DIR=/tmp
elif [ -w /var/tmp ]; then
RUN_DIR=/var/tmp
else
RUN_DIR=.
2016-03-25 22:08:48 +00:00
fi
2018-10-17 21:53:13 +00:00
## Special note when remote target is on the same host as initiator (happens for unit tests): we'll have to differentiate RUN_DIR so remote CleanUp won't affect initiator.
if [ "$_REMOTE_EXECUTION" == true ]; then
mkdir -p "$RUN_DIR/$PROGRAM.remote"
RUN_DIR="$RUN_DIR/$PROGRAM.remote"
fi
2018-10-02 16:14:07 +00:00
# Get a random number on Windows BusyBox alike, also works on most Unixes that have dd, if dd is not found, then return $RANDOM
function PoorMansRandomGenerator {
local digits="${1}" # The number of digits to generate
local number
local isFirst=true
if type dd >/dev/null 2>&1; then
# Some read bytes can't be used, se we read twice the number of required bytes
dd if=/dev/urandom bs=$digits count=2 2> /dev/null | while read -r -n1 char; do
if [ $isFirst == false ] || [ $(printf "%d" "'$char") != "0" ]; then
number=$number$(printf "%d" "'$char")
isFirst=false
fi
if [ ${#number} -ge $digits ]; then
echo ${number:0:$digits}
break;
fi
done
elif [ "$RANDOM" -ne 0 ]; then
echo $RANDOM
else
Logger "Cannot generate random number." "ERROR"
fi
}
2018-10-02 08:52:25 +00:00
function PoorMansRandomGenerator {
local digits="${1}" # The number of digits to generate
local number
# Some read bytes can't be used, se we read twice the number of required bytes
dd if=/dev/urandom bs=$digits count=2 2> /dev/null | while read -r -n1 char; do
number=$number$(printf "%d" "'$char")
if [ ${#number} -ge $digits ]; then
echo ${number:0:$digits}
break;
fi
done
}
# Initial TSTMAP value before function declaration
2018-10-07 10:25:27 +00:00
TSTAMP=$(date '+%Y%m%dT%H%M%S').$(PoorMansRandomGenerator 5)
2018-10-02 08:52:25 +00:00
# Default alert attachment filename
ALERT_LOG_FILE="$RUN_DIR/$PROGRAM.$SCRIPT_PID.$TSTAMP.last.log"
# Set error exit code if a piped command fails
set -o pipefail
set -o errtrace
2018-06-25 22:23:01 +00:00
# Array to string converter, see http://stackoverflow.com/questions/1527049/bash-join-elements-of-an-array
# usage: joinString separaratorChar Array
function joinString {
local IFS="$1"; shift; echo "$*";
}
# Sub function of Logger
function _Logger {
local logValue="${1}" # Log to file
local stdValue="${2}" # Log to screeen
local toStdErr="${3:-false}" # Log to stderr instead of stdout
if [ "$logValue" != "" ]; then
echo -e "$logValue" >> "$LOG_FILE"
2016-12-13 08:19:14 +00:00
2018-06-25 22:23:01 +00:00
# Build current log file for alerts if we have a sufficient environment
2019-02-26 11:07:57 +00:00
if [ "$RUN_DIR/$PROGRAM" != "/" ]; then
2019-03-15 10:58:45 +00:00
echo -e "$logValue" >> "$RUN_DIR/$PROGRAM._Logger.$SCRIPT_PID.$TSTAMP.log"
2018-06-25 22:23:01 +00:00
fi
fi
if [ "$stdValue" != "" ] && [ "$_LOGGER_SILENT" != true ]; then
if [ $toStdErr == true ]; then
# Force stderr color in subshell
(>&2 echo -e "$stdValue")
else
echo -e "$stdValue"
fi
2016-12-13 08:19:14 +00:00
fi
2016-05-26 09:06:38 +00:00
}
2018-06-25 22:23:01 +00:00
# Remote logger similar to below Logger, without log to file and alert flags
function RemoteLogger {
local value="${1}" # Sentence to log (in double quotes)
local level="${2}" # Log level
local retval="${3:-undef}" # optional return value of command
2018-10-17 21:53:13 +00:00
local prefix
2018-06-25 22:23:01 +00:00
if [ "$_LOGGER_PREFIX" == "time" ]; then
prefix="TIME: $SECONDS - "
elif [ "$_LOGGER_PREFIX" == "date" ]; then
prefix="R $(date) - "
2015-11-12 00:37:43 +00:00
else
2018-06-25 22:23:01 +00:00
prefix=""
fi
if [ "$level" == "CRITICAL" ]; then
_Logger "" "$prefix\e[1;33;41m$value\e[0m" true
2019-02-26 11:07:57 +00:00
if [ $_DEBUG == true ]; then
2018-06-25 22:23:01 +00:00
_Logger -e "" "[$retval] in [$(joinString , ${FUNCNAME[@]})] SP=$SCRIPT_PID P=$$" true
fi
return
elif [ "$level" == "ERROR" ]; then
2018-08-08 09:31:58 +00:00
_Logger "" "$prefix\e[31m$value\e[0m" true
2019-02-26 11:07:57 +00:00
if [ $_DEBUG == true ]; then
2018-06-25 22:23:01 +00:00
_Logger -e "" "[$retval] in [$(joinString , ${FUNCNAME[@]})] SP=$SCRIPT_PID P=$$" true
fi
return
elif [ "$level" == "WARN" ]; then
_Logger "" "$prefix\e[33m$value\e[0m" true
2019-02-26 11:07:57 +00:00
if [ $_DEBUG == true ]; then
2018-06-25 22:23:01 +00:00
_Logger -e "" "[$retval] in [$(joinString , ${FUNCNAME[@]})] SP=$SCRIPT_PID P=$$" true
fi
return
elif [ "$level" == "NOTICE" ]; then
if [ $_LOGGER_ERR_ONLY != true ]; then
_Logger "" "$prefix$value"
fi
return
elif [ "$level" == "VERBOSE" ]; then
if [ $_LOGGER_VERBOSE == true ]; then
_Logger "" "$prefix$value"
fi
return
elif [ "$level" == "ALWAYS" ]; then
2018-08-08 09:31:58 +00:00
_Logger "" "$prefix$value"
2018-06-25 22:23:01 +00:00
return
elif [ "$level" == "DEBUG" ]; then
2019-02-26 11:07:57 +00:00
if [ "$_DEBUG" == true ]; then
2018-06-25 22:23:01 +00:00
_Logger "" "$prefix$value"
return
fi
else
_Logger "" "\e[41mLogger function called without proper loglevel [$level].\e[0m" true
_Logger "" "Value was: $prefix$value" true
2015-09-28 12:43:42 +00:00
fi
2016-05-26 09:06:38 +00:00
}
2018-06-25 22:23:01 +00:00
# General log function with log levels:
# Environment variables
# _LOGGER_SILENT: Disables any output to stdout & stderr
# _LOGGER_ERR_ONLY: Disables any output to stdout except for ALWAYS loglevel
# _LOGGER_VERBOSE: Allows VERBOSE loglevel messages to be sent to stdout
# Loglevels
# Except for VERBOSE, all loglevels are ALWAYS sent to log file
# CRITICAL, ERROR, WARN sent to stderr, color depending on level, level also logged
# NOTICE sent to stdout
2019-02-26 11:07:57 +00:00
# VERBOSE sent to stdout if _LOGGER_VERBOSE=true
# ALWAYS is sent to stdout unless _LOGGER_SILENT=true
# DEBUG & PARANOIA_DEBUG are only sent to stdout if _DEBUG=true
2018-06-25 22:23:01 +00:00
function Logger {
local value="${1}" # Sentence to log (in double quotes)
local level="${2}" # Log level
local retval="${3:-undef}" # optional return value of command
2018-10-17 21:53:13 +00:00
local prefix
2018-06-25 22:23:01 +00:00
if [ "$_LOGGER_PREFIX" == "time" ]; then
prefix="TIME: $SECONDS - "
elif [ "$_LOGGER_PREFIX" == "date" ]; then
2018-09-30 12:07:09 +00:00
prefix="$(date '+%Y-%m-%d %H:%M:%S') - "
2018-06-25 22:23:01 +00:00
else
prefix=""
fi
## Obfuscate _REMOTE_TOKEN in logs (for ssh_filter usage only in osync and obackup)
value="${value/env _REMOTE_TOKEN=$_REMOTE_TOKEN/__(o_O)__}"
value="${value/env _REMOTE_TOKEN=\$_REMOTE_TOKEN/__(o_O)__}"
if [ "$level" == "CRITICAL" ]; then
_Logger "$prefix($level):$value" "$prefix\e[1;33;41m$value\e[0m" true
ERROR_ALERT=true
# ERROR_ALERT / WARN_ALERT is not set in main when Logger is called from a subprocess. Need to keep this flag.
echo -e "[$retval] in [$(joinString , ${FUNCNAME[@]})] SP=$SCRIPT_PID P=$$\n$prefix($level):$value" >> "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.error.$SCRIPT_PID.$TSTAMP"
return
elif [ "$level" == "ERROR" ]; then
_Logger "$prefix($level):$value" "$prefix\e[91m$value\e[0m" true
ERROR_ALERT=true
echo -e "[$retval] in [$(joinString , ${FUNCNAME[@]})] SP=$SCRIPT_PID P=$$\n$prefix($level):$value" >> "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.error.$SCRIPT_PID.$TSTAMP"
return
elif [ "$level" == "WARN" ]; then
_Logger "$prefix($level):$value" "$prefix\e[33m$value\e[0m" true
WARN_ALERT=true
echo -e "[$retval] in [$(joinString , ${FUNCNAME[@]})] SP=$SCRIPT_PID P=$$\n$prefix($level):$value" >> "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.warn.$SCRIPT_PID.$TSTAMP"
return
elif [ "$level" == "NOTICE" ]; then
if [ "$_LOGGER_ERR_ONLY" != true ]; then
_Logger "$prefix$value" "$prefix$value"
fi
return
elif [ "$level" == "VERBOSE" ]; then
if [ $_LOGGER_VERBOSE == true ]; then
_Logger "$prefix($level):$value" "$prefix$value"
fi
return
elif [ "$level" == "ALWAYS" ]; then
_Logger "$prefix$value" "$prefix$value"
return
elif [ "$level" == "DEBUG" ]; then
2019-02-26 11:07:57 +00:00
if [ "$_DEBUG" == true ]; then
2018-06-25 22:23:01 +00:00
_Logger "$prefix$value" "$prefix$value"
return
fi
else
_Logger "\e[41mLogger function called without proper loglevel [$level].\e[0m" "\e[41mLogger function called without proper loglevel [$level].\e[0m" true
_Logger "Value was: $prefix$value" "Value was: $prefix$value" true
fi
}
2018-10-02 08:52:25 +00:00
2018-10-06 18:03:58 +00:00
# Function is busybox compatible since busybox ash does not understand direct regex, we use expr
function IsInteger {
local value="${1}"
if type expr > /dev/null 2>&1; then
2018-10-08 18:12:59 +00:00
expr "$value" : '^[0-9]\{1,\}$' > /dev/null 2>&1
2018-10-06 18:03:58 +00:00
if [ $? -eq 0 ]; then
echo 1
else
echo 0
fi
else
if [[ $value =~ ^[0-9]+$ ]]; then
echo 1
else
echo 0
fi
fi
}
2018-10-02 08:52:25 +00:00
# Portable child (and grandchild) kill function tester under Linux, BSD and MacOS X
function KillChilds {
local pid="${1}" # Parent pid to kill childs
local self="${2:-false}" # Should parent be killed too ?
# Paranoid checks, we can safely assume that $pid should not be 0 nor 1
if [ $(IsInteger "$pid") -eq 0 ] || [ "$pid" == "" ] || [ "$pid" == "0" ] || [ "$pid" == "1" ]; then
Logger "Bogus pid given [$pid]." "CRITICAL"
return 1
fi
if kill -0 "$pid" > /dev/null 2>&1; then
if children="$(pgrep -P "$pid")"; then
if [[ "$pid" == *"$children"* ]]; then
Logger "Bogus pgrep implementation." "CRITICAL"
children="${children/$pid/}"
fi
for child in $children; do
KillChilds "$child" true
done
fi
fi
# Try to kill nicely, if not, wait 15 seconds to let Trap actions happen before killing
if [ "$self" == true ]; then
# We need to check for pid again because it may have disappeared after recursive function call
if kill -0 "$pid" > /dev/null 2>&1; then
kill -s TERM "$pid"
Logger "Sent SIGTERM to process [$pid]." "DEBUG"
2019-03-15 10:58:45 +00:00
if [ $? -ne 0 ]; then
2018-10-02 08:52:25 +00:00
sleep 15
Logger "Sending SIGTERM to process [$pid] failed." "DEBUG"
kill -9 "$pid"
2019-03-15 10:58:45 +00:00
if [ $? -ne 0 ]; then
2018-10-02 08:52:25 +00:00
Logger "Sending SIGKILL to process [$pid] failed." "DEBUG"
return 1
fi # Simplify the return 0 logic here
else
return 0
fi
else
return 0
fi
else
return 0
fi
}
function KillAllChilds {
local pids="${1}" # List of parent pids to kill separated by semi-colon
local self="${2:-false}" # Should parent be killed too ?
local errorcount=0
IFS=';' read -a pidsArray <<< "$pids"
for pid in "${pidsArray[@]}"; do
KillChilds $pid $self
2019-03-15 10:58:45 +00:00
if [ $? -ne 0 ]; then
2018-10-02 08:52:25 +00:00
errorcount=$((errorcount+1))
fi
done
return $errorcount
}
function CleanUp {
2019-02-26 11:07:57 +00:00
if [ "$_DEBUG" != true ]; then
2018-10-02 08:52:25 +00:00
rm -f "$RUN_DIR/$PROGRAM."*".$SCRIPT_PID.$TSTAMP"
# Fix for sed -i requiring backup extension for BSD & Mac (see all sed -i statements)
rm -f "$RUN_DIR/$PROGRAM."*".$SCRIPT_PID.$TSTAMP.tmp"
fi
}
function GenericTrapQuit {
local exitcode=0
# Get ERROR / WARN alert flags from subprocesses that call Logger
if [ -f "$RUN_DIR/$PROGRAM.Logger.warn.$SCRIPT_PID.$TSTAMP" ]; then
WARN_ALERT=true
exitcode=2
fi
if [ -f "$RUN_DIR/$PROGRAM.Logger.error.$SCRIPT_PID.$TSTAMP" ]; then
ERROR_ALERT=true
exitcode=1
fi
CleanUp
exit $exitcode
}
# Get current install.sh path from http://stackoverflow.com/a/246128/2635443
SCRIPT_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
_LOGGER_SILENT=false
_STATS=1
ACTION="install"
FAKEROOT=""
function GetCommandlineArguments {
for i in "$@"; do
case $i in
--prefix=*)
FAKEROOT="${i##*=}"
;;
--silent)
_LOGGER_SILENT=true
;;
--no-stats)
_STATS=0
;;
--remove)
ACTION="uninstall"
;;
--help|-h|-?)
Usage
;;
*)
2019-02-26 11:07:57 +00:00
Logger "Unknown option '$i'" "ERROR"
2018-10-02 08:52:25 +00:00
Usage
exit
;;
esac
done
}
GetCommandlineArguments "$@"
CONF_DIR=$FAKEROOT/etc/$PROGRAM
BIN_DIR="$FAKEROOT/usr/local/bin"
SERVICE_DIR_INIT=$FAKEROOT/etc/init.d
# Should be /usr/lib/systemd/system, but /lib/systemd/system exists on debian & rhel / fedora
SERVICE_DIR_SYSTEMD_SYSTEM=$FAKEROOT/lib/systemd/system
SERVICE_DIR_SYSTEMD_USER=$FAKEROOT/etc/systemd/user
SERVICE_DIR_OPENRC=$FAKEROOT/etc/init.d
if [ "$PROGRAM" == "osync" ]; then
SERVICE_NAME="osync-srv"
elif [ "$PROGRAM" == "pmocr" ]; then
SERVICE_NAME="pmocr-srv"
fi
SERVICE_FILE_INIT="$SERVICE_NAME"
SERVICE_FILE_SYSTEMD_SYSTEM="$SERVICE_NAME@.service"
SERVICE_FILE_SYSTEMD_USER="$SERVICE_NAME@.service.user"
SERVICE_FILE_OPENRC="$SERVICE_NAME-openrc"
## Generic code
## Default log file
if [ -w "$FAKEROOT/var/log" ]; then
LOG_FILE="$FAKEROOT/var/log/$PROGRAM-install.log"
elif ([ "$HOME" != "" ] && [ -w "$HOME" ]); then
LOG_FILE="$HOME/$PROGRAM-install.log"
else
LOG_FILE="./$PROGRAM-install.log"
fi
2018-06-25 22:23:01 +00:00
## Modified version of https://gist.github.com/cdown/1163649
2016-12-13 08:19:14 +00:00
function UrlEncode {
local length="${#1}"
2018-08-08 09:31:58 +00:00
local i
2016-12-13 08:19:14 +00:00
local LANG=C
2018-06-25 22:23:01 +00:00
for i in $(seq 0 $((length-1))); do
2016-12-13 08:19:14 +00:00
local c="${1:i:1}"
case $c in
[a-zA-Z0-9.~_-])
printf "$c"
;;
*)
printf '%%%02X' "'$c"
;;
esac
done
2016-05-26 09:06:38 +00:00
}
2016-12-13 08:19:14 +00:00
function GetLocalOS {
2016-11-24 13:50:48 +00:00
local localOsVar
2017-02-08 07:39:59 +00:00
local localOsName
local localOsVer
2016-10-22 19:03:36 +00:00
2018-01-02 19:06:31 +00:00
# There is no good way to tell if currently running in BusyBox shell. Using sluggish way.
2016-12-13 08:19:14 +00:00
if ls --help 2>&1 | grep -i "BusyBox" > /dev/null; then
localOsVar="BusyBox"
2018-11-05 11:14:03 +00:00
elif set -o | grep "winxp" > /dev/null; then
localOsVar="BusyBox-w32"
2016-12-13 08:19:14 +00:00
else
# Detecting the special ubuntu userland in Windows 10 bash
if grep -i Microsoft /proc/sys/kernel/osrelease > /dev/null 2>&1; then
localOsVar="Microsoft"
else
2016-12-28 22:37:29 +00:00
localOsVar="$(uname -spior 2>&1)"
2019-03-15 10:58:45 +00:00
if [ $? -ne 0 ]; then
2016-12-13 08:19:14 +00:00
localOsVar="$(uname -v 2>&1)"
2019-03-15 10:58:45 +00:00
if [ $? -ne 0 ]; then
2016-12-13 08:19:14 +00:00
localOsVar="$(uname)"
fi
fi
fi
fi
2016-11-24 13:50:48 +00:00
case $localOsVar in
2016-12-13 08:19:14 +00:00
# Android uname contains both linux and android, keep it before linux entry
*"Android"*)
LOCAL_OS="Android"
;;
*"Linux"*)
LOCAL_OS="Linux"
;;
2016-05-26 09:06:38 +00:00
*"BSD"*)
2016-12-13 08:19:14 +00:00
LOCAL_OS="BSD"
;;
2017-03-23 22:03:27 +00:00
*"MINGW32"*|*"MINGW64"*|*"MSYS"*)
2016-12-13 08:19:14 +00:00
LOCAL_OS="msys"
;;
*"CYGWIN"*)
LOCAL_OS="Cygwin"
;;
2018-11-05 11:14:03 +00:00
*"Microsoft"*|*"MS/Windows"*)
2016-12-13 08:19:14 +00:00
LOCAL_OS="WinNT10"
2016-05-26 09:06:38 +00:00
;;
*"Darwin"*)
2016-12-13 08:19:14 +00:00
LOCAL_OS="MacOSX"
;;
*"BusyBox"*)
LOCAL_OS="BusyBox"
;;
*)
2019-02-26 11:07:57 +00:00
if [ "$IGNORE_OS_TYPE" == true ]; then
2016-12-13 08:19:14 +00:00
Logger "Running on unknown local OS [$localOsVar]." "WARN"
return
fi
if [ "$_OFUNCTIONS_VERSION" != "" ]; then
Logger "Running on >> $localOsVar << not supported. Please report to the author." "ERROR"
fi
exit 1
;;
esac
2016-12-28 22:37:29 +00:00
2017-02-08 07:39:59 +00:00
# Get linux versions
if [ -f "/etc/os-release" ]; then
2017-12-30 16:56:03 +00:00
localOsName=$(GetConfFileValue "/etc/os-release" "NAME" true)
localOsVer=$(GetConfFileValue "/etc/os-release" "VERSION" true)
2018-06-25 22:23:01 +00:00
elif [ "$LOCAL_OS" == "BusyBox" ]; then
localOsVer=`ls --help 2>&1 | head -1 | cut -f2 -d' '`
localOsName="BusyBox"
2017-02-08 07:39:59 +00:00
fi
2018-06-25 22:23:01 +00:00
# Get Host info for Windows
2018-11-05 11:14:03 +00:00
if [ "$LOCAL_OS" == "msys" ] || [ "$LOCAL_OS" == "BusyBox" ] || [ "$LOCAL_OS" == "Cygwin" ] || [ "$LOCAL_OS" == "WinNT10" ]; then
localOsVar="$localOsVar $(uname -a)"
2018-06-25 22:23:01 +00:00
if [ "$PROGRAMW6432" != "" ]; then
LOCAL_OS_BITNESS=64
LOCAL_OS_FAMILY="Windows"
elif [ "$PROGRAMFILES" != "" ]; then
LOCAL_OS_BITNESS=32
LOCAL_OS_FAMILY="Windows"
# Case where running on BusyBox but no program files defined
elif [ "$LOCAL_OS" == "BusyBox" ]; then
LOCAL_OS_FAMILY="Unix"
fi
# Get Host info for Unix
else
LOCAL_OS_FAMILY="Unix"
2018-11-05 11:14:03 +00:00
fi
if [ "$LOCAL_OS_FAMILY" == "Unix" ]; then
2018-06-25 22:23:01 +00:00
if uname -m | grep '64' > /dev/null 2>&1; then
LOCAL_OS_BITNESS=64
else
LOCAL_OS_BITNESS=32
fi
fi
LOCAL_OS_FULL="$localOsVar ($localOsName $localOsVer) $LOCAL_OS_BITNESS-bit $LOCAL_OS_FAMILY"
2017-04-26 19:03:25 +00:00
if [ "$_OFUNCTIONS_VERSION" != "" ]; then
Logger "Local OS: [$LOCAL_OS_FULL]." "DEBUG"
fi
2016-12-13 08:19:14 +00:00
}
2017-02-08 07:39:59 +00:00
function GetConfFileValue () {
2018-02-20 21:54:07 +00:00
local file="${1}"
local name="${2}"
2017-12-30 16:56:03 +00:00
local noError="${3:-false}"
2018-02-20 21:54:07 +00:00
local value
2017-02-08 07:39:59 +00:00
2018-02-20 21:54:07 +00:00
value=$(grep "^$name=" "$file")
2019-03-15 10:58:45 +00:00
if [ $? -eq 0 ]; then
2018-02-20 21:54:07 +00:00
value="${value##*=}"
echo "$value"
else
2017-12-30 16:56:03 +00:00
if [ $noError == true ]; then
2018-02-20 21:54:07 +00:00
Logger "Cannot get value for [$name] in config file [$file]." "NOTICE"
2017-12-30 16:56:03 +00:00
else
Logger "Cannot get value for [$name] in config file [$file]." "ERROR"
fi
2018-02-20 21:54:07 +00:00
fi
2017-02-08 07:39:59 +00:00
}
2017-12-30 16:56:03 +00:00
2016-12-13 08:19:14 +00:00
function SetLocalOSSettings {
USER=root
2016-12-28 22:37:29 +00:00
# LOCAL_OS and LOCAL_OS_FULL are global variables set at GetLocalOS
2016-12-13 08:19:14 +00:00
case $LOCAL_OS in
*"BSD"*)
GROUP=wheel
;;
*"MacOSX"*)
2016-05-26 09:06:38 +00:00
GROUP=admin
;;
2016-12-13 08:19:14 +00:00
*"msys"*|*"Cygwin"*)
2016-05-26 09:06:38 +00:00
USER=""
GROUP=""
;;
2016-08-29 15:53:23 +00:00
*)
GROUP=root
;;
2016-05-26 09:06:38 +00:00
esac
2017-02-08 15:28:18 +00:00
if [ "$LOCAL_OS" == "Android" ] || [ "$LOCAL_OS" == "BusyBox" ]; then
2019-02-26 11:07:57 +00:00
Logger "Cannot be installed on [$LOCAL_OS]. Please use $PROGRAM.sh directly." "CRITICAL"
2016-12-13 08:19:14 +00:00
exit 1
fi
if ([ "$USER" != "" ] && [ "$(whoami)" != "$USER" ] && [ "$FAKEROOT" == "" ]); then
2019-02-26 11:07:57 +00:00
Logger "Must be run as $USER." "CRITICAL"
2016-05-26 09:06:38 +00:00
exit 1
2016-03-25 22:08:48 +00:00
fi
2015-11-12 00:37:43 +00:00
2016-12-28 22:37:29 +00:00
OS=$(UrlEncode "$LOCAL_OS_FULL")
2016-05-26 09:06:38 +00:00
}
2016-05-25 15:35:25 +00:00
2016-05-26 09:06:38 +00:00
function GetInit {
2018-06-25 22:23:01 +00:00
if [ -f /sbin/openrc-run ]; then
init="openrc"
2019-02-26 11:07:57 +00:00
Logger "Detected openrc." "NOTICE"
2018-06-25 22:23:01 +00:00
elif [ -f /sbin/init ]; then
2016-05-26 09:06:38 +00:00
if file /sbin/init | grep systemd > /dev/null; then
init="systemd"
2019-02-26 11:07:57 +00:00
Logger "Detected systemd." "NOTICE"
2016-05-26 09:06:38 +00:00
else
init="initV"
2019-02-26 11:07:57 +00:00
Logger "Detected initV." "NOTICE"
2016-05-26 09:06:38 +00:00
fi
2016-04-08 19:10:54 +00:00
else
2019-02-26 11:07:57 +00:00
Logger "Can't detect initV, systemd or openRC. Service files won't be installed. You can still run $PROGRAM manually or via cron." "WARN"
2016-05-26 09:06:38 +00:00
init="none"
2016-04-08 19:10:54 +00:00
fi
2016-05-26 09:06:38 +00:00
}
2017-02-08 07:39:59 +00:00
function CreateDir {
local dir="${1}"
2018-10-06 18:03:58 +00:00
local dirMask="${2}"
local dirUser="${3}"
local dirGroup="${4}"
2017-02-08 07:39:59 +00:00
if [ ! -d "$dir" ]; then
2018-10-06 18:03:58 +00:00
(
if [ $(IsInteger $dirMask) -eq 1 ]; then
umask $dirMask
fi
2017-07-27 21:20:24 +00:00
mkdir -p "$dir"
2018-10-06 18:03:58 +00:00
)
2016-05-26 09:06:38 +00:00
if [ $? == 0 ]; then
2019-02-26 11:07:57 +00:00
Logger "Created directory [$dir]." "NOTICE"
2016-05-26 09:06:38 +00:00
else
2019-02-26 11:07:57 +00:00
Logger "Cannot create directory [$dir]." "CRITICAL"
2016-05-26 09:06:38 +00:00
exit 1
fi
2016-04-08 19:10:54 +00:00
fi
2018-10-06 18:03:58 +00:00
if [ "$dirUser" != "" ]; then
userGroup="$dirUser"
if [ "$dirGroup" != "" ]; then
userGroup="$userGroup"":$dirGroup"
fi
chown "$userGroup" "$dir"
if [ $? != 0 ]; then
2019-02-26 11:07:57 +00:00
Logger "Could not set directory ownership on [$dir] to [$userGroup]." "CRITICAL"
2018-10-06 18:03:58 +00:00
exit 1
else
2019-02-26 11:07:57 +00:00
Logger "Set file ownership on [$dir] to [$userGroup]." "NOTICE"
2018-10-06 18:03:58 +00:00
fi
fi
2016-05-26 09:06:38 +00:00
}
2016-04-08 19:10:54 +00:00
2017-02-07 15:00:44 +00:00
function CopyFile {
local sourcePath="${1}"
local destPath="${2}"
2018-06-25 22:23:01 +00:00
local sourceFileName="${3}"
local destFileName="${4}"
local fileMod="${5}"
local fileUser="${6}"
local fileGroup="${7}"
local overwrite="${8:-false}"
2017-02-07 15:00:44 +00:00
local userGroup=""
local oldFileName
2018-06-25 22:23:01 +00:00
if [ "$destFileName" == "" ]; then
destFileName="$sourceFileName"
2016-09-08 20:50:13 +00:00
fi
2018-06-25 22:23:01 +00:00
if [ -f "$destPath/$destFileName" ] && [ $overwrite == false ]; then
destfileName="$sourceFileName.new"
2019-02-26 11:07:57 +00:00
Logger "Copying [$sourceFileName] to [$destPath/$destFilename]." "NOTICE"
2018-06-25 22:23:01 +00:00
fi
cp "$sourcePath/$sourceFileName" "$destPath/$destFileName"
2016-04-08 19:10:54 +00:00
if [ $? != 0 ]; then
2019-02-26 11:07:57 +00:00
Logger "Cannot copy [$sourcePath/$sourceFileName] to [$destPath/$destFileName]. Make sure to run install script in the directory containing all other files." "CRITICAL"
Logger "Also make sure you have permissions to write to [$BIN_DIR]." "ERROR"
2016-05-26 09:06:38 +00:00
exit 1
2016-04-08 19:10:54 +00:00
else
2019-02-26 11:07:57 +00:00
Logger "Copied [$sourcePath/$sourceFileName] to [$destPath/$destFileName]." "NOTICE"
2018-10-06 18:03:58 +00:00
if [ "$(IsInteger $fileMod)" -eq 1 ]; then
2018-06-25 22:23:01 +00:00
chmod "$fileMod" "$destPath/$destFileName"
2017-02-07 15:00:44 +00:00
if [ $? != 0 ]; then
2019-02-26 11:07:57 +00:00
Logger "Cannot set file permissions of [$destPath/$destFileName] to [$fileMod]." "CRITICAL"
2017-02-07 15:00:44 +00:00
exit 1
else
2019-02-26 11:07:57 +00:00
Logger "Set file permissions to [$fileMod] on [$destPath/$destFileName]." "NOTICE"
2017-02-07 15:00:44 +00:00
fi
2018-10-06 18:03:58 +00:00
elif [ "$fileMod" != "" ]; then
2019-02-26 11:07:57 +00:00
Logger "Bogus filemod [$fileMod] for [$destPath] given." "WARN"
2016-05-26 09:06:38 +00:00
fi
2017-02-07 15:00:44 +00:00
if [ "$fileUser" != "" ]; then
userGroup="$fileUser"
if [ "$fileGroup" != "" ]; then
userGroup="$userGroup"":$fileGroup"
fi
2018-06-25 22:23:01 +00:00
chown "$userGroup" "$destPath/$destFileName"
2017-02-07 15:00:44 +00:00
if [ $? != 0 ]; then
2019-02-26 11:07:57 +00:00
Logger "Could not set file ownership on [$destPath/$destFileName] to [$userGroup]." "CRITICAL"
2017-02-07 15:00:44 +00:00
exit 1
else
2019-02-26 11:07:57 +00:00
Logger "Set file ownership on [$destPath/$destFileName] to [$userGroup]." "NOTICE"
2016-05-26 09:06:38 +00:00
fi
fi
fi
}
2017-02-07 15:00:44 +00:00
function CopyExampleFiles {
exampleFiles=()
exampleFiles[0]="sync.conf.example" # osync
exampleFiles[1]="host_backup.conf.example" # obackup
exampleFiles[2]="exclude.list.example" # osync & obackup
exampleFiles[3]="snapshot.conf.example" # zsnap
exampleFiles[4]="default.conf" # pmocr
for file in "${exampleFiles[@]}"; do
if [ -f "$SCRIPT_PATH/$file" ]; then
2018-06-25 22:23:01 +00:00
CopyFile "$SCRIPT_PATH" "$CONF_DIR" "$file" "$file" "" "" "" false
2016-05-26 09:06:38 +00:00
fi
2017-02-07 15:00:44 +00:00
done
}
function CopyProgram {
binFiles=()
binFiles[0]="$PROGRAM_BINARY"
2017-03-14 21:36:27 +00:00
if [ "$PROGRAM" == "osync" ] || [ "$PROGRAM" == "obackup" ]; then
binFiles[1]="$PROGRAM_BATCH"
binFiles[2]="$SSH_FILTER"
fi
2017-02-07 15:00:44 +00:00
local user=""
local group=""
if ([ "$USER" != "" ] && [ "$FAKEROOT" == "" ]); then
user="$USER"
fi
if ([ "$GROUP" != "" ] && [ "$FAKEROOT" == "" ]); then
group="$GROUP"
2016-05-26 09:06:38 +00:00
fi
2016-03-25 22:08:48 +00:00
2017-02-07 15:00:44 +00:00
for file in "${binFiles[@]}"; do
2018-06-25 22:23:01 +00:00
CopyFile "$SCRIPT_PATH" "$BIN_DIR" "$file" "$file" 755 "$user" "$group" true
2017-02-07 15:00:44 +00:00
done
}
function CopyServiceFiles {
if ([ "$init" == "systemd" ] && [ -f "$SCRIPT_PATH/$SERVICE_FILE_SYSTEMD_SYSTEM" ]); then
2017-07-27 21:20:24 +00:00
CreateDir "$SERVICE_DIR_SYSTEMD_SYSTEM"
2018-06-25 22:23:01 +00:00
CopyFile "$SCRIPT_PATH" "$SERVICE_DIR_SYSTEMD_SYSTEM" "$SERVICE_FILE_SYSTEMD_SYSTEM" "$SERVICE_FILE_SYSTEMD_SYSTEM" "" "" "" true
2017-07-27 21:20:24 +00:00
if [ -f "$SCRIPT_PATH/$SERVICE_FILE_SYSTEMD_USER" ]; then
CreateDir "$SERVICE_DIR_SYSTEMD_USER"
2018-06-25 22:23:01 +00:00
CopyFile "$SCRIPT_PATH" "$SERVICE_DIR_SYSTEMD_USER" "$SERVICE_FILE_SYSTEMD_USER" "$SERVICE_FILE_SYSTEMD_USER" "" "" "" true
2017-03-23 22:03:27 +00:00
fi
2019-02-26 11:07:57 +00:00
Logger "Created [$SERVICE_NAME] service in [$SERVICE_DIR_SYSTEMD_SYSTEM] and [$SERVICE_DIR_SYSTEMD_USER]." "NOTICE"
Logger "Can be activated with [systemctl start SERVICE_NAME@instance.conf] where instance.conf is the name of the config file in $CONF_DIR." "NOTICE"
Logger "Can be enabled on boot with [systemctl enable $SERVICE_NAME@instance.conf]." "NOTICE"
Logger "In userland, active with [systemctl --user start $SERVICE_NAME@instance.conf]." "NOTICE"
2017-02-08 07:39:59 +00:00
elif ([ "$init" == "initV" ] && [ -f "$SCRIPT_PATH/$SERVICE_FILE_INIT" ] && [ -d "$SERVICE_DIR_INIT" ]); then
2018-06-25 22:23:01 +00:00
#CreateDir "$SERVICE_DIR_INIT"
CopyFile "$SCRIPT_PATH" "$SERVICE_DIR_INIT" "$SERVICE_FILE_INIT" "$SERVICE_FILE_INIT" "755" "" "" true
2019-02-26 11:07:57 +00:00
Logger "Created [$SERVICE_NAME] service in [$SERVICE_DIR_INIT]." "NOTICE"
Logger "Can be activated with [service $SERVICE_FILE_INIT start]." "NOTICE"
Logger "Can be enabled on boot with [chkconfig $SERVICE_FILE_INIT on]." "NOTICE"
2018-08-08 09:31:58 +00:00
elif ([ "$init" == "openrc" ] && [ -f "$SCRIPT_PATH/$SERVICE_FILE_OPENRC" ] && [ -d "$SERVICE_DIR_OPENRC" ]); then
2018-06-25 22:23:01 +00:00
# Rename service to usual service file
CopyFile "$SCRIPT_PATH" "$SERVICE_DIR_OPENRC" "$SERVICE_FILE_OPENRC" "$SERVICE_FILE_INIT" "755" "" "" true
2019-02-26 11:07:57 +00:00
Logger "Created [$SERVICE_NAME] service in [$SERVICE_DIR_OPENRC]." "NOTICE"
Logger "Can be activated with [rc-update add $SERVICE_NAME.instance] where instance is a configuration file found in /etc/osync." "NOTICE"
2017-02-08 07:39:59 +00:00
else
2019-02-26 11:07:57 +00:00
Logger "Cannot properly find how to deal with init on this system. Skipping service file installation." "NOTICE"
2016-05-26 09:06:38 +00:00
fi
}
function Statistics {
2016-12-13 08:19:14 +00:00
if type wget > /dev/null; then
wget -qO- "$STATS_LINK" > /dev/null 2>&1
if [ $? == 0 ]; then
return 0
fi
2016-03-25 22:08:48 +00:00
fi
2016-12-13 08:19:14 +00:00
if type curl > /dev/null; then
curl "$STATS_LINK" -o /dev/null > /dev/null 2>&1
if [ $? == 0 ]; then
return 0
fi
2016-03-25 22:08:48 +00:00
fi
2019-02-26 11:07:57 +00:00
Logger "Neiter wget nor curl could be used for. Cannot run statistics. Use the provided link please." "WARN"
2016-12-13 08:19:14 +00:00
return 1
2016-03-25 22:08:48 +00:00
}
2017-02-07 15:00:44 +00:00
function RemoveFile {
local file="${1}"
if [ -f "$file" ]; then
rm -f "$file"
if [ $? != 0 ]; then
2019-02-26 11:07:57 +00:00
Logger "Could not remove file [$file]." "ERROR"
2017-02-07 15:00:44 +00:00
else
2019-02-26 11:07:57 +00:00
Logger "Removed file [$file]." "NOTICE"
2017-02-07 15:00:44 +00:00
fi
else
2019-02-26 11:07:57 +00:00
Logger "File [$file] not found. Skipping." "NOTICE"
2017-02-07 15:00:44 +00:00
fi
}
function RemoveAll {
RemoveFile "$BIN_DIR/$PROGRAM_BINARY"
2017-04-26 10:04:21 +00:00
if [ "$PROGRAM" == "osync" ] || [ "$PROGRAM" == "obackup" ]; then
RemoveFile "$BIN_DIR/$PROGRAM_BATCH"
fi
2017-02-10 10:53:41 +00:00
if [ ! -f "$BIN_DIR/osync.sh" ] && [ ! -f "$BIN_DIR/obackup.sh" ]; then # Check if any other program requiring ssh filter is present before removal
RemoveFile "$BIN_DIR/$SSH_FILTER"
else
2019-02-26 11:07:57 +00:00
Logger "Skipping removal of [$BIN_DIR/$SSH_FILTER] because other programs present that need it." "NOTICE"
2017-02-10 10:53:41 +00:00
fi
2017-02-07 15:00:44 +00:00
RemoveFile "$SERVICE_DIR_SYSTEMD_SYSTEM/$SERVICE_FILE_SYSTEMD_SYSTEM"
2017-07-27 21:20:24 +00:00
RemoveFile "$SERVICE_DIR_SYSTEMD_USER/$SERVICE_FILE_SYSTEMD_USER"
2017-02-07 15:00:44 +00:00
RemoveFile "$SERVICE_DIR_INIT/$SERVICE_FILE_INIT"
2019-02-26 11:07:57 +00:00
Logger "Skipping configuration files in [$CONF_DIR]. You may remove this directory manually." "NOTICE"
2017-02-07 15:00:44 +00:00
}
2016-05-26 09:09:56 +00:00
function Usage {
echo "Installs $PROGRAM into $BIN_DIR"
echo "options:"
echo "--silent Will log and bypass user interaction."
echo "--no-stats Used with --silent in order to refuse sending anonymous install stats."
2017-03-23 22:03:27 +00:00
echo "--remove Remove the program."
2017-07-27 21:20:24 +00:00
echo "--prefix=/path Use prefix to install path."
2016-05-26 09:09:56 +00:00
exit 127
}
2018-10-02 08:52:25 +00:00
function TrapQuit {
local exitcode=0
# Get ERROR / WARN alert flags from subprocesses that call Logger
if [ -f "$RUN_DIR/$PROGRAM.Logger.warn.$SCRIPT_PID.$TSTAMP" ]; then
WARN_ALERT=true
exitcode=2
fi
if [ -f "$RUN_DIR/$PROGRAM.Logger.error.$SCRIPT_PID.$TSTAMP" ]; then
ERROR_ALERT=true
exitcode=1
fi
CleanUp
exit $exitcode
}
2018-09-30 12:07:09 +00:00
############################## Script entry point
2018-10-02 08:52:25 +00:00
trap TrapQuit TERM EXIT HUP QUIT
2018-09-30 12:07:09 +00:00
if [ ! -w "$(dirname $LOG_FILE)" ]; then
echo "Cannot write to log [$(dirname $LOG_FILE)]."
else
Logger "Script begin, logging to [$LOG_FILE]." "DEBUG"
fi
2018-10-06 18:03:58 +00:00
# Set default umask
umask 0022
2018-09-30 12:07:09 +00:00
2016-12-13 08:19:14 +00:00
GetLocalOS
SetLocalOSSettings
2016-05-26 09:06:38 +00:00
GetInit
2017-02-07 15:00:44 +00:00
STATS_LINK="http://instcount.netpower.fr?program=$PROGRAM&version=$PROGRAM_VERSION&os=$OS&action=$ACTION"
if [ "$ACTION" == "uninstall" ]; then
RemoveAll
2019-02-26 11:07:57 +00:00
Logger "$PROGRAM uninstalled." "NOTICE"
2017-02-07 15:00:44 +00:00
else
2017-02-08 07:39:59 +00:00
CreateDir "$CONF_DIR"
CreateDir "$BIN_DIR"
2017-02-07 15:00:44 +00:00
CopyExampleFiles
CopyProgram
2017-04-26 10:04:21 +00:00
if [ "$PROGRAM" == "osync" ] || [ "$PROGRAM" == "pmocr" ]; then
CopyServiceFiles
fi
2019-02-26 11:07:57 +00:00
Logger "$PROGRAM installed. Use with $BIN_DIR/$PROGRAM_BINARY" "NOTICE"
if [ "$PROGRAM" == "osync" ] || [ "$PROGRAM" == "obackup" ]; then
2018-06-25 22:23:01 +00:00
echo ""
2019-02-26 11:07:57 +00:00
Logger "If connecting remotely, consider setup ssh filter to enhance security." "NOTICE"
2018-06-25 22:23:01 +00:00
echo ""
fi
2017-02-07 15:00:44 +00:00
fi
2016-05-26 09:06:38 +00:00
if [ $_STATS -eq 1 ]; then
2016-12-13 19:52:33 +00:00
if [ $_LOGGER_SILENT == true ]; then
2016-05-26 09:06:38 +00:00
Statistics
else
2019-02-26 11:07:57 +00:00
Logger "In order to make usage statistics, the script would like to connect to $STATS_LINK" "NOTICE"
2017-02-07 15:00:44 +00:00
read -r -p "No data except those in the url will be send. Allow [Y/n] " response
2016-05-26 09:06:38 +00:00
case $response in
[nN])
exit
;;
*)
Statistics
exit $?
;;
esac
fi
fi