1
0
mirror of https://github.com/deajan/obackup.git synced 2024-11-15 04:03:41 +01:00
obackup/dev/common_install.sh

443 lines
12 KiB
Bash
Raw Normal View History

2016-08-06 15:15:46 +02:00
#!/usr/bin/env bash
2017-03-14 22:15:13 +01:00
## Installer script suitable for osync / obackup / pmocr
2017-03-14 22:33:35 +01:00
PROGRAM=[prgname]
2017-03-14 22:15:13 +01:00
PROGRAM_VERSION=$(grep "PROGRAM_VERSION=" $PROGRAM.sh)
PROGRAM_VERSION=${PROGRAM_VERSION#*=}
2016-08-06 15:15:46 +02:00
PROGRAM_BINARY=$PROGRAM".sh"
PROGRAM_BATCH=$PROGRAM"-batch.sh"
2017-02-09 12:13:10 +01:00
SSH_FILTER="ssh_filter.sh"
2019-01-03 10:09:29 +01:00
SCRIPT_BUILD=2018100206
2018-09-12 21:14:06 +02:00
INSTANCE_ID="installer-$SCRIPT_BUILD"
2016-08-06 15:15:46 +02:00
## osync / obackup / pmocr / zsnap install script
2016-12-20 21:19:58 +01:00
## Tested on RHEL / CentOS 6 & 7, Fedora 23, Debian 7 & 8, Mint 17 and FreeBSD 8, 10 and 11
2016-08-06 15:15:46 +02:00
## Please adapt this to fit your distro needs
2018-10-02 21:21:23 +02:00
include #### OFUNCTIONS MICRO SUBSET ####
2018-09-12 21:14:06 +02:00
2016-11-30 13:28:56 +01:00
# Get current install.sh path from http://stackoverflow.com/a/246128/2635443
SCRIPT_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
2018-03-25 14:15:45 +02:00
_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
;;
*)
2018-09-12 21:14:06 +02:00
Logger "Unknown option '$i'" "SIMPLE"
2018-03-25 14:15:45 +02:00
Usage
2018-09-12 21:14:06 +02:00
exit
2018-03-25 14:15:45 +02:00
;;
esac
done
}
GetCommandlineArguments "$@"
2016-09-08 22:51:00 +02:00
CONF_DIR=$FAKEROOT/etc/$PROGRAM
BIN_DIR="$FAKEROOT/usr/local/bin"
SERVICE_DIR_INIT=$FAKEROOT/etc/init.d
2016-08-06 15:15:46 +02:00
# Should be /usr/lib/systemd/system, but /lib/systemd/system exists on debian & rhel / fedora
2016-09-08 22:51:00 +02:00
SERVICE_DIR_SYSTEMD_SYSTEM=$FAKEROOT/lib/systemd/system
SERVICE_DIR_SYSTEMD_USER=$FAKEROOT/etc/systemd/user
2018-09-12 21:14:06 +02:00
SERVICE_DIR_OPENRC=$FAKEROOT/etc/init.d
2016-08-06 15:15:46 +02:00
2017-02-09 12:13:10 +01:00
if [ "$PROGRAM" == "osync" ]; then
SERVICE_NAME="osync-srv"
elif [ "$PROGRAM" == "pmocr" ]; then
SERVICE_NAME="pmocr-srv"
fi
2016-08-06 15:15:46 +02:00
2017-02-09 12:13:10 +01:00
SERVICE_FILE_INIT="$SERVICE_NAME"
SERVICE_FILE_SYSTEMD_SYSTEM="$SERVICE_NAME@.service"
SERVICE_FILE_SYSTEMD_USER="$SERVICE_NAME@.service.user"
2018-09-12 21:14:06 +02:00
SERVICE_FILE_OPENRC="$SERVICE_NAME-openrc"
2016-08-06 15:15:46 +02:00
## Generic code
## Default log file
2017-02-09 12:13:10 +01:00
if [ -w "$FAKEROOT/var/log" ]; then
2016-12-20 21:19:58 +01:00
LOG_FILE="$FAKEROOT/var/log/$PROGRAM-install.log"
2016-08-06 15:15:46 +02:00
elif ([ "$HOME" != "" ] && [ -w "$HOME" ]); then
2016-12-20 21:19:58 +01:00
LOG_FILE="$HOME/$PROGRAM-install.log"
2016-08-06 15:15:46 +02:00
else
2016-12-20 21:19:58 +01:00
LOG_FILE="./$PROGRAM-install.log"
2016-08-06 15:15:46 +02:00
fi
2016-12-20 21:19:58 +01:00
include #### UrlEncode SUBSET ####
include #### GetLocalOS SUBSET ####
2017-02-09 12:13:10 +01:00
include #### GetConfFileValue SUBSET ####
2016-12-20 21:19:58 +01:00
function SetLocalOSSettings {
2016-11-30 13:28:56 +01:00
USER=root
2016-08-06 15:15:46 +02:00
2016-12-27 12:52:37 +01:00
# LOCAL_OS and LOCAL_OS_FULL are global variables set at GetLocalOS
2016-12-20 21:19:58 +01:00
case $LOCAL_OS in
2016-08-06 15:15:46 +02:00
*"BSD"*)
GROUP=wheel
;;
2016-12-20 21:19:58 +01:00
*"MacOSX"*)
2016-08-06 15:15:46 +02:00
GROUP=admin
;;
2016-12-20 21:19:58 +01:00
*"msys"*|*"Cygwin"*)
2016-08-06 15:15:46 +02:00
USER=""
GROUP=""
;;
2016-09-08 22:51:00 +02:00
*)
GROUP=root
;;
2016-08-06 15:15:46 +02:00
esac
2017-02-09 12:13:10 +01:00
if [ "$LOCAL_OS" == "Android" ] || [ "$LOCAL_OS" == "BusyBox" ]; then
2018-09-12 21:14:06 +02:00
Logger "Cannot be installed on [$LOCAL_OS]. Please use $PROGRAM.sh directly." "SIMPLE"
2016-12-20 21:19:58 +01:00
exit 1
fi
2016-09-08 22:51:00 +02:00
if ([ "$USER" != "" ] && [ "$(whoami)" != "$USER" ] && [ "$FAKEROOT" == "" ]); then
2018-09-12 21:14:06 +02:00
Logger "Must be run as $USER." "SIMPLE"
2016-08-06 15:15:46 +02:00
exit 1
fi
2016-12-27 12:52:37 +01:00
OS=$(UrlEncode "$LOCAL_OS_FULL")
2016-08-06 15:15:46 +02:00
}
function GetInit {
2018-09-12 21:14:06 +02:00
if [ -f /sbin/openrc-run ]; then
init="openrc"
Logger "Detected openrc." "SIMPLE"
elif [ -f /sbin/init ]; then
2016-08-06 15:15:46 +02:00
if file /sbin/init | grep systemd > /dev/null; then
init="systemd"
2018-09-12 21:14:06 +02:00
Logger "Detected systemd." "SIMPLE"
2016-08-06 15:15:46 +02:00
else
init="initV"
2018-09-12 21:14:06 +02:00
Logger "Detected initV." "SIMPLE"
2016-08-06 15:15:46 +02:00
fi
else
2018-09-12 21:14:06 +02:00
Logger "Can't detect initV, systemd or openRC. Service files won't be installed. You can still run $PROGRAM manually or via cron." "SIMPLE"
2016-08-06 15:15:46 +02:00
init="none"
fi
}
2017-02-09 12:13:10 +01:00
function CreateDir {
local dir="${1}"
2019-01-03 10:09:29 +01:00
local dirMask="${2}"
local dirUser="${3}"
local dirGroup="${4}"
2017-02-09 12:13:10 +01:00
if [ ! -d "$dir" ]; then
2019-01-03 10:09:29 +01:00
(
if [ $(IsInteger $dirMask) -eq 1 ]; then
umask $dirMask
fi
2018-03-25 14:15:45 +02:00
mkdir -p "$dir"
2019-01-03 10:09:29 +01:00
)
2016-08-06 15:15:46 +02:00
if [ $? == 0 ]; then
2018-09-12 21:14:06 +02:00
Logger "Created directory [$dir]." "SIMPLE"
2016-08-06 15:15:46 +02:00
else
2018-09-12 21:14:06 +02:00
Logger "Cannot create directory [$dir]." "SIMPLE"
2016-08-06 15:15:46 +02:00
exit 1
fi
fi
2019-01-03 10:09:29 +01:00
if [ "$dirUser" != "" ]; then
userGroup="$dirUser"
if [ "$dirGroup" != "" ]; then
userGroup="$userGroup"":$dirGroup"
fi
chown "$userGroup" "$dir"
if [ $? != 0 ]; then
Logger "Could not set directory ownership on [$dir] to [$userGroup]." "SIMPLE"
exit 1
else
Logger "Set file ownership on [$dir] to [$userGroup]." "SIMPLE"
fi
fi
2016-08-06 15:15:46 +02:00
}
2017-02-09 12:13:10 +01:00
function CopyFile {
local sourcePath="${1}"
local destPath="${2}"
2018-09-12 21:14:06 +02:00
local sourceFileName="${3}"
local destFileName="${4}"
local fileMod="${5}"
local fileUser="${6}"
local fileGroup="${7}"
local overwrite="${8:-false}"
2017-02-09 12:13:10 +01:00
local userGroup=""
local oldFileName
2018-09-12 21:14:06 +02:00
if [ "$destFileName" == "" ]; then
destFileName="$sourceFileName"
fi
if [ -f "$destPath/$destFileName" ] && [ $overwrite == false ]; then
destfileName="$sourceFileName.new"
Logger "Copying [$sourceFileName] to [$destPath/$destFilename]." "SIMPLE"
2016-08-06 15:15:46 +02:00
fi
2018-09-12 21:14:06 +02:00
cp "$sourcePath/$sourceFileName" "$destPath/$destFileName"
2016-08-06 15:15:46 +02:00
if [ $? != 0 ]; then
2018-09-12 21:14:06 +02:00
Logger "Cannot copy [$sourcePath/$sourceFileName] to [$destPath/$destFileName]. Make sure to run install script in the directory containing all other files." "SIMPLE"
Logger "Also make sure you have permissions to write to [$BIN_DIR]." "SIMPLE"
2016-08-06 15:15:46 +02:00
exit 1
else
2018-09-12 21:14:06 +02:00
Logger "Copied [$sourcePath/$sourceFileName] to [$destPath/$destFileName]." "SIMPLE"
2019-01-03 10:09:29 +01:00
if [ "$(IsInteger $fileMod)" -eq 1 ]; then
2018-09-12 21:14:06 +02:00
chmod "$fileMod" "$destPath/$destFileName"
2017-02-09 12:13:10 +01:00
if [ $? != 0 ]; then
2018-09-12 21:14:06 +02:00
Logger "Cannot set file permissions of [$destPath/$destFileName] to [$fileMod]." "SIMPLE"
2017-02-09 12:13:10 +01:00
exit 1
else
2018-09-12 21:14:06 +02:00
Logger "Set file permissions to [$fileMod] on [$destPath/$destFileName]." "SIMPLE"
2017-02-09 12:13:10 +01:00
fi
2019-01-03 10:09:29 +01:00
elif [ "$fileMod" != "" ]; then
Logger "Bogus filemod [$fileMod] for [$destPath] given." "SIMPLE"
2016-08-06 15:15:46 +02:00
fi
2017-02-09 12:13:10 +01:00
if [ "$fileUser" != "" ]; then
userGroup="$fileUser"
if [ "$fileGroup" != "" ]; then
userGroup="$userGroup"":$fileGroup"
fi
2018-09-12 21:14:06 +02:00
chown "$userGroup" "$destPath/$destFileName"
2017-02-09 12:13:10 +01:00
if [ $? != 0 ]; then
2018-09-12 21:14:06 +02:00
Logger "Could not set file ownership on [$destPath/$destFileName] to [$userGroup]." "SIMPLE"
2017-02-09 12:13:10 +01:00
exit 1
else
2018-09-12 21:14:06 +02:00
Logger "Set file ownership on [$destPath/$destFileName] to [$userGroup]." "SIMPLE"
2016-08-06 15:15:46 +02:00
fi
fi
fi
}
2017-02-09 12:13:10 +01: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-09-12 21:14:06 +02:00
CopyFile "$SCRIPT_PATH" "$CONF_DIR" "$file" "$file" "" "" "" false
2016-08-06 15:15:46 +02:00
fi
2017-02-09 12:13:10 +01:00
done
}
function CopyProgram {
binFiles=()
binFiles[0]="$PROGRAM_BINARY"
2017-03-14 22:15:13 +01:00
if [ "$PROGRAM" == "osync" ] || [ "$PROGRAM" == "obackup" ]; then
binFiles[1]="$PROGRAM_BATCH"
binFiles[2]="$SSH_FILTER"
fi
2017-02-09 12:13:10 +01:00
local user=""
local group=""
if ([ "$USER" != "" ] && [ "$FAKEROOT" == "" ]); then
user="$USER"
fi
if ([ "$GROUP" != "" ] && [ "$FAKEROOT" == "" ]); then
group="$GROUP"
2016-08-06 15:15:46 +02:00
fi
2017-02-09 12:13:10 +01:00
for file in "${binFiles[@]}"; do
2018-09-12 21:14:06 +02:00
CopyFile "$SCRIPT_PATH" "$BIN_DIR" "$file" "$file" 755 "$user" "$group" true
2017-02-09 12:13:10 +01:00
done
}
function CopyServiceFiles {
if ([ "$init" == "systemd" ] && [ -f "$SCRIPT_PATH/$SERVICE_FILE_SYSTEMD_SYSTEM" ]); then
2018-03-25 14:15:45 +02:00
CreateDir "$SERVICE_DIR_SYSTEMD_SYSTEM"
2018-09-12 21:14:06 +02:00
CopyFile "$SCRIPT_PATH" "$SERVICE_DIR_SYSTEMD_SYSTEM" "$SERVICE_FILE_SYSTEMD_SYSTEM" "$SERVICE_FILE_SYSTEMD_SYSTEM" "" "" "" true
2018-03-25 14:15:45 +02:00
if [ -f "$SCRIPT_PATH/$SERVICE_FILE_SYSTEMD_USER" ]; then
CreateDir "$SERVICE_DIR_SYSTEMD_USER"
2018-09-12 21:14:06 +02:00
CopyFile "$SCRIPT_PATH" "$SERVICE_DIR_SYSTEMD_USER" "$SERVICE_FILE_SYSTEMD_USER" "$SERVICE_FILE_SYSTEMD_USER" "" "" "" true
2017-03-14 22:42:01 +01:00
fi
2018-09-12 21:14:06 +02:00
Logger "Created [$SERVICE_NAME] service in [$SERVICE_DIR_SYSTEMD_SYSTEM] and [$SERVICE_DIR_SYSTEMD_USER]." "SIMPLE"
Logger "Can be activated with [systemctl start SERVICE_NAME@instance.conf] where instance.conf is the name of the config file in $CONF_DIR." "SIMPLE"
Logger "Can be enabled on boot with [systemctl enable $SERVICE_NAME@instance.conf]." "SIMPLE"
Logger "In userland, active with [systemctl --user start $SERVICE_NAME@instance.conf]." "SIMPLE"
2017-02-09 12:13:10 +01:00
elif ([ "$init" == "initV" ] && [ -f "$SCRIPT_PATH/$SERVICE_FILE_INIT" ] && [ -d "$SERVICE_DIR_INIT" ]); then
2018-09-12 21:14:06 +02:00
#CreateDir "$SERVICE_DIR_INIT"
CopyFile "$SCRIPT_PATH" "$SERVICE_DIR_INIT" "$SERVICE_FILE_INIT" "$SERVICE_FILE_INIT" "755" "" "" true
Logger "Created [$SERVICE_NAME] service in [$SERVICE_DIR_INIT]." "SIMPLE"
Logger "Can be activated with [service $SERVICE_FILE_INIT start]." "SIMPLE"
Logger "Can be enabled on boot with [chkconfig $SERVICE_FILE_INIT on]." "SIMPLE"
elif ([ "$init" == "openrc" ] && [ -f "$SCRIPT_PATH/$SERVICE_FILE_OPENRC" ] && [ -d "$SERVICE_DIR_OPENRC" ]); then
# Rename service to usual service file
CopyFile "$SCRIPT_PATH" "$SERVICE_DIR_OPENRC" "$SERVICE_FILE_OPENRC" "$SERVICE_FILE_INIT" "755" "" "" true
Logger "Created [$SERVICE_NAME] service in [$SERVICE_DIR_OPENRC]." "SIMPLE"
Logger "Can be activated with [rc-update add $SERVICE_NAME.instance] where instance is a configuration file found in /etc/osync." "SIMPLE"
2017-02-09 12:13:10 +01:00
else
2018-09-12 21:14:06 +02:00
Logger "Cannot properly find how to deal with init on this system. Skipping service file installation." "SIMPLE"
2016-08-06 15:15:46 +02:00
fi
}
function Statistics {
2016-12-20 21:19:58 +01:00
if type wget > /dev/null; then
wget -qO- "$STATS_LINK" > /dev/null 2>&1
if [ $? == 0 ]; then
return 0
fi
2016-08-06 15:15:46 +02:00
fi
2016-12-20 21:19:58 +01:00
if type curl > /dev/null; then
curl "$STATS_LINK" -o /dev/null > /dev/null 2>&1
if [ $? == 0 ]; then
return 0
fi
2016-08-06 15:15:46 +02:00
fi
2018-09-12 21:14:06 +02:00
Logger "Neiter wget nor curl could be used for. Cannot run statistics. Use the provided link please." "SIMPLE"
2016-12-20 21:19:58 +01:00
return 1
2016-08-06 15:15:46 +02:00
}
2017-02-09 12:13:10 +01:00
function RemoveFile {
local file="${1}"
if [ -f "$file" ]; then
rm -f "$file"
if [ $? != 0 ]; then
2018-09-12 21:14:06 +02:00
Logger "Could not remove file [$file]." "SIMPLE"
2017-02-09 12:13:10 +01:00
else
2018-09-12 21:14:06 +02:00
Logger "Removed file [$file]." "SIMPLE"
2017-02-09 12:13:10 +01:00
fi
else
2018-09-12 21:14:06 +02:00
Logger "File [$file] not found. Skipping." "SIMPLE"
2017-02-09 12:13:10 +01:00
fi
}
function RemoveAll {
RemoveFile "$BIN_DIR/$PROGRAM_BINARY"
2017-04-11 13:27:58 +02:00
if [ "$PROGRAM" == "osync" ] || [ "$PROGRAM" == "obackup" ]; then
RemoveFile "$BIN_DIR/$PROGRAM_BATCH"
fi
2017-02-10 11:42:31 +01: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
2018-09-12 21:14:06 +02:00
Logger "Skipping removal of [$BIN_DIR/$SSH_FILTER] because other programs present that need it." "SIMPLE"
2017-02-10 11:42:31 +01:00
fi
2017-02-09 12:13:10 +01:00
RemoveFile "$SERVICE_DIR_SYSTEMD_SYSTEM/$SERVICE_FILE_SYSTEMD_SYSTEM"
2018-03-25 14:15:45 +02:00
RemoveFile "$SERVICE_DIR_SYSTEMD_USER/$SERVICE_FILE_SYSTEMD_USER"
2017-02-09 12:13:10 +01:00
RemoveFile "$SERVICE_DIR_INIT/$SERVICE_FILE_INIT"
2018-09-12 21:14:06 +02:00
Logger "Skipping configuration files in [$CONF_DIR]. You may remove this directory manually." "SIMPLE"
2017-02-09 12:13:10 +01:00
}
2016-08-06 15:15:46 +02: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-30 22:18:52 +02:00
echo "--remove Remove the program."
2018-03-25 14:15:45 +02:00
echo "--prefix=/path Use prefix to install path."
2016-08-06 15:15:46 +02:00
exit 127
}
2018-10-02 21:21:23 +02: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-12 21:14:06 +02:00
############################## Script entry point
2018-10-02 21:21:23 +02:00
trap TrapQuit TERM EXIT HUP QUIT
2018-09-12 21:14:06 +02: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
2019-01-03 10:09:29 +01:00
# Set default umask
umask 0022
2018-09-12 21:14:06 +02:00
2016-12-20 21:19:58 +01:00
GetLocalOS
SetLocalOSSettings
2016-08-06 15:15:46 +02:00
GetInit
2017-02-09 12:13:10 +01:00
STATS_LINK="http://instcount.netpower.fr?program=$PROGRAM&version=$PROGRAM_VERSION&os=$OS&action=$ACTION"
if [ "$ACTION" == "uninstall" ]; then
RemoveAll
2018-09-12 21:14:06 +02:00
Logger "$PROGRAM uninstalled." "SIMPLE"
2017-02-09 12:13:10 +01:00
else
CreateDir "$CONF_DIR"
CreateDir "$BIN_DIR"
CopyExampleFiles
CopyProgram
2017-04-17 20:26:40 +02:00
if [ "$PROGRAM" == "osync" ] || [ "$PROGRAM" == "pmocr" ]; then
CopyServiceFiles
fi
2018-10-02 21:21:23 +02:00
Logger "$PROGRAM installed. Use with $BIN_DIR/$PROGRAM_BINARY" "SIMPLE"
2017-02-09 12:13:10 +01:00
if [ "$PROGRAM" == "osync" ] || [ "$PROGRAM" == "obackup" ]; then
2018-09-12 21:14:06 +02:00
echo ""
Logger "If connecting remotely, consider setup ssh filter to enhance security." "SIMPLE"
echo ""
2017-02-09 12:13:10 +01:00
fi
fi
2016-08-06 15:15:46 +02:00
if [ $_STATS -eq 1 ]; then
2016-12-20 21:19:58 +01:00
if [ $_LOGGER_SILENT == true ]; then
2016-08-06 15:15:46 +02:00
Statistics
else
2018-09-12 21:14:06 +02:00
Logger "In order to make usage statistics, the script would like to connect to $STATS_LINK" "SIMPLE"
2017-02-09 12:13:10 +01:00
read -r -p "No data except those in the url will be send. Allow [Y/n] " response
2016-08-06 15:15:46 +02:00
case $response in
[nN])
exit
;;
*)
Statistics
exit $?
;;
esac
fi
fi