2
0
mirror of https://github.com/deajan/osync synced 2024-11-15 12:12:56 +00:00
osync/install.sh

463 lines
11 KiB
Bash
Raw Normal View History

2014-05-26 22:50:46 +00:00
#!/usr/bin/env bash
2016-12-13 08:19:14 +00:00
_OFUNCTIONS_BOOTSTRAP=true
2015-11-12 00:37:43 +00:00
PROGRAM=osync
2017-02-08 07:39:59 +00:00
PROGRAM_VERSION=1.2-RC2
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"
SCRIPT_BUILD=2017020801
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
2016-11-11 23:07:50 +00:00
# Get current install.sh path from http://stackoverflow.com/a/246128/2635443
SCRIPT_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
CONF_DIR=$FAKEROOT/etc/$PROGRAM
BIN_DIR="$FAKEROOT/usr/local/bin"
SERVICE_DIR_INIT=$FAKEROOT/etc/init.d
2016-05-24 22:36:00 +00:00
# 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
2016-04-06 18:14:41 +00:00
2017-02-07 15:00:44 +00:00
if [ "$PROGRAM" == "osync" ]; then
SERVICE_NAME="osync-srv"
elif [ "$PROGRAM" == "pmocr" ]; then
SERVICE_NAME="pmocr-srv"
fi
2016-04-06 18:14:41 +00:00
2017-02-07 15:00:44 +00:00
SERVICE_FILE_INIT="$SERVICE_NAME"
SERVICE_FILE_SYSTEMD_SYSTEM="$SERVICE_NAME@.service"
SERVICE_FILE_SYSTEMD_USER="$SERVICE_NAME@.service.user"
2016-04-08 19:10:54 +00:00
2016-04-06 18:14:41 +00:00
## Generic code
2015-09-28 12:43:42 +00:00
2016-05-26 09:06:38 +00:00
## Default log file
2017-02-07 15:00:44 +00:00
if [ -w "$FAKEROOT/var/log" ]; then
2016-12-13 08:19:14 +00:00
LOG_FILE="$FAKEROOT/var/log/$PROGRAM-install.log"
2016-05-26 09:06:38 +00:00
elif ([ "$HOME" != "" ] && [ -w "$HOME" ]); then
2016-12-13 08:19:14 +00:00
LOG_FILE="$HOME/$PROGRAM-install.log"
2016-05-26 09:06:38 +00:00
else
2016-12-13 08:19:14 +00:00
LOG_FILE="./$PROGRAM-install.log"
2016-03-25 22:08:48 +00:00
fi
2016-12-13 08:19:14 +00:00
# QuickLogger subfunction, can be called directly
2016-05-26 09:06:38 +00:00
function _QuickLogger {
2016-12-13 08:19:14 +00:00
local value="${1}"
local destination="${2}" # Destination: stdout, log, both
if ([ "$destination" == "log" ] || [ "$destination" == "both" ]); then
echo -e "$(date) - $value" >> "$LOG_FILE"
elif ([ "$destination" == "stdout" ] || [ "$destination" == "both" ]); then
echo -e "$value"
fi
2016-05-26 09:06:38 +00:00
}
2016-12-13 08:19:14 +00:00
# Generic quick logging function
2016-05-26 09:06:38 +00:00
function QuickLogger {
local value="${1}"
2016-12-13 08:19:14 +00:00
if [ "$_LOGGER_SILENT" == true ]; then
2016-05-26 09:06:38 +00:00
_QuickLogger "$value" "log"
2015-11-12 00:37:43 +00:00
else
2016-05-26 09:06:38 +00:00
_QuickLogger "$value" "stdout"
2015-09-28 12:43:42 +00:00
fi
2016-05-26 09:06:38 +00:00
}
2016-12-13 08:19:14 +00:00
## from https://gist.github.com/cdown/1163649
function UrlEncode {
local length="${#1}"
local LANG=C
for (( i = 0; i < length; i++ )); do
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
2016-12-13 08:19:14 +00:00
# There's no good way to tell if currently running in BusyBox shell. Using sluggish way.
if ls --help 2>&1 | grep -i "BusyBox" > /dev/null; then
localOsVar="BusyBox"
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)"
2016-12-13 08:19:14 +00:00
if [ $? != 0 ]; then
localOsVar="$(uname -v 2>&1)"
if [ $? != 0 ]; then
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"
;;
*"MINGW32"*|*"MSYS"*)
LOCAL_OS="msys"
;;
*"CYGWIN"*)
LOCAL_OS="Cygwin"
;;
*"Microsoft"*)
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"
;;
*)
if [ "$IGNORE_OS_TYPE" == "yes" ]; then
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
if [ "$_OFUNCTIONS_VERSION" != "" ]; then
Logger "Local OS: [$localOsVar]." "DEBUG"
fi
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
localOsName=$(GetConfFileValue "/etc/os-release" "NAME")
localOsVer=$(GetConfFileValue "/etc/os-release" "VERSION")
fi
2016-12-28 22:37:29 +00:00
# Add a global variable for statistics in installer
2017-02-08 07:39:59 +00:00
LOCAL_OS_FULL="$localOsVar ($localOsName $localOsVer)"
2016-12-13 08:19:14 +00:00
}
2017-02-08 07:39:59 +00:00
function GetConfFileValue () {
local file="${1}"
local name="${2}"
local value
value=$(grep "^$name=" "$file")
if [ $? == 0 ]; then
value="${value##*=}"
echo "$value"
else
Logger "Cannot get value for [$name] in config file [$file]." "ERROR"
fi
}
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
2016-12-13 08:19:14 +00:00
if [ "$LOCAL_OS" == "Android" ] || [ "$LOCAL_OS" == "MacOSX" ] || [ "$LOCAL_OS" == "BusyBox" ]; then
QuickLogger "Cannot be installed on [$LOCAL_OS]. Please use $PROGRAM.sh directly."
exit 1
fi
if ([ "$USER" != "" ] && [ "$(whoami)" != "$USER" ] && [ "$FAKEROOT" == "" ]); then
2016-12-13 08:19:14 +00:00
QuickLogger "Must be run as $USER."
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 {
if [ -f /sbin/init ]; then
if file /sbin/init | grep systemd > /dev/null; then
init="systemd"
else
init="initV"
fi
2016-04-08 19:10:54 +00:00
else
2016-05-26 09:06:38 +00:00
QuickLogger "Can't detect initV or systemd. Service files won't be installed. You can still run $PROGRAM manually or via cron."
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}"
if [ ! -d "$dir" ]; then
mkdir "$dir"
2016-05-26 09:06:38 +00:00
if [ $? == 0 ]; then
2017-02-08 07:39:59 +00:00
QuickLogger "Created directory [$dir]."
2016-05-26 09:06:38 +00:00
else
2017-02-08 07:39:59 +00:00
QuickLogger "Cannot create directory [$dir]."
2016-05-26 09:06:38 +00:00
exit 1
fi
2016-04-08 19:10:54 +00:00
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}"
local fileName="${3}"
local fileMod="${4}"
local fileUser="${5}"
local fileGroup="${6}"
2017-02-08 07:39:59 +00:00
local overwrite="${7:-false}"
2017-02-07 15:00:44 +00:00
local userGroup=""
local oldFileName
2017-02-08 07:39:59 +00:00
if [ -f "$destPath/$fileName" ] && [ $overwrite == false ]; then
2017-02-07 15:00:44 +00:00
oldFileName="$fileName"
fileName="$oldFileName.new"
cp "$sourcePath/$oldFileName" "$destPath/$fileName"
else
cp "$sourcePath/$fileName" "$destPath"
2016-09-08 20:50:13 +00:00
fi
2016-04-08 19:10:54 +00:00
if [ $? != 0 ]; then
2017-02-07 15:00:44 +00:00
QuickLogger "Cannot copy [$fileName] to [$destPath]. Make sure to run install script in the directory containing all other files."
2016-05-26 09:06:38 +00:00
QuickLogger "Also make sure you have permissions to write to [$BIN_DIR]."
exit 1
2016-04-08 19:10:54 +00:00
else
2017-02-07 15:00:44 +00:00
QuickLogger "Copied [$fileName] to [$destPath]."
if [ "$fileMod" != "" ]; then
chmod "$fileMod" "$destPath/$fileName"
if [ $? != 0 ]; then
QuickLogger "Cannot set file permissions of [$destPath/$fileName] to [$fileMod]."
exit 1
else
QuickLogger "Set file permissions to [$fileMod] on [$destPath/$fileName]."
fi
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
chown "$userGroup" "$destPath/$fileName"
if [ $? != 0 ]; then
QuickLogger "Could not set file ownership on [$destPath/$fileName] to [$userGroup]."
exit 1
else
QuickLogger "Set file ownership on [$destPath/$fileName] to [$userGroup]."
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
2017-02-08 07:39:59 +00:00
CopyFile "$SCRIPT_PATH" "$CONF_DIR" "$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"
binFiles[1]="$PROGRAM_BATCH"
binFiles[2]="$SSH_FILTER"
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
2017-02-08 07:39:59 +00:00
CopyFile "$SCRIPT_PATH" "$BIN_DIR" "$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-02-08 07:39:59 +00:00
CopyFile "$SCRIPT_PATH" "$SERVICE_DIR_SYSTEMD_SYSTEM" "$SERVICE_FILE_SYSTEMD_SYSTEM" "" "" "" true
CopyFile "$SCRIPT_PATH" "$SERVICE_DIR_SYSTEMD_USER" "$SERVICE_FILE_SYSTEMD_USER" "" "" "" true
2017-02-07 15:00:44 +00:00
QuickLogger "Created [$SERVICE_NAME] service in [$SERVICE_DIR_SYSTEMD_SYSTEM] and [$SERVICE_DIR_SYSTEMD_USER]."
QuickLogger "Can be activated with [systemctl start SERVICE_NAME@instance.conf] where instance.conf is the name of the config file in $CONF_DIR."
QuickLogger "Can be enabled on boot with [systemctl enable $SERVICE_NAME@instance.conf]."
QuickLogger "In userland, active with [systemctl --user start $SERVICE_NAME@instance.conf]."
2017-02-08 07:39:59 +00:00
elif ([ "$init" == "initV" ] && [ -f "$SCRIPT_PATH/$SERVICE_FILE_INIT" ] && [ -d "$SERVICE_DIR_INIT" ]); then
CopyFile "$SCRIPT_PATH" "$SERVICE_DIR_INIT" "$SERVICE_FILE_INIT" "755" "" "" true
2017-02-07 15:00:44 +00:00
QuickLogger "Created osync-srv service in [$SERVICE_DIR_INIT]."
QuickLogger "Can be activated with [service $OSYNC_SERVICE_FILE_INIT start]."
QuickLogger "Can be enabled on boot with [chkconfig $OSYNC_SERVICE_FILE_INIT on]."
2017-02-08 07:39:59 +00:00
else
QuickLogger "Cannot define what init style is in use on this system. Skipping service file installation."
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
2016-12-13 08:19:14 +00:00
QuickLogger "Neiter wget nor curl could be used for. Cannot run statistics. Use the provided link please."
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
QuickLogger "Could not remove file [$file]."
else
QuickLogger "Removed file [$file]."
fi
else
QuickLogger "File [$file] not found. Skipping."
fi
}
function RemoveAll {
RemoveFile "$BIN_DIR/$PROGRAM_BINARY"
RemoveFile "$BIN_DIR/$PROGRAM_BATCH"
RemoveFile "$BIN_DIR/$SSH_FILTER"
RemoveFile "$SERVICE_DIR_SYSTEMD_SYSTEM/$SERVICE_FILE_SYSTEMD_SYSTEM"
RemoveFile "$SERVICE_DIR_SYSTEMD_USER/$SERVICE_FILE_SYSTEMD_SYSTEM"
RemoveFile "$SERVICE_DIR_INIT/$SERVICE_FILE_INIT"
QuickLogger "Skipping configuration files in [$CONF_DIR]. You may remove this directory manually."
}
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."
exit 127
}
2016-12-13 08:19:14 +00:00
_LOGGER_SILENT=false
2016-05-26 09:06:38 +00:00
_STATS=1
2017-02-07 15:00:44 +00:00
ACTION="install"
2016-05-26 09:06:38 +00:00
for i in "$@"
do
case $i in
--silent)
2016-12-13 08:19:14 +00:00
_LOGGER_SILENT=true
2016-05-26 09:06:38 +00:00
;;
--no-stats)
_STATS=0
;;
2017-02-07 15:00:44 +00:00
--remove)
ACTION="uninstall"
;;
2016-05-26 09:09:56 +00:00
--help|-h|-?)
Usage
2016-05-26 09:06:38 +00:00
esac
done
if [ "$FAKEROOT" != "" ]; then
mkdir -p "$SERVICE_DIR_SYSTEMD_SYSTEM" "$SERVICE_DIR_SYSTEMD_USER" "$BIN_DIR"
fi
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
QuickLogger "$PROGRAM uninstalled."
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
CopyServiceFiles
QuickLogger "$PROGRAM installed. Use with $BIN_DIR/$PROGRAM"
if [ "$PROGRAM" == "osync" ] || [ "$PROGRAM" == "obackup" ]; then
QuickLogger ""
QuickLogger "If connecting remotely, consider setup ssh filter to enhance security."
QuickLogger ""
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
2017-02-07 15:00:44 +00:00
QuickLogger "In order to make usage statistics, the script would like to connect to $STATS_LINK"
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