1
0
mirror of https://github.com/deajan/obackup.git synced 2024-11-14 19:53:42 +01:00
obackup/install.sh

394 lines
11 KiB
Bash
Raw Normal View History

2015-04-24 21:23:04 +02:00
#!/usr/bin/env bash
2017-01-04 09:00:47 +01:00
_OFUNCTIONS_BOOTSTRAP=true
2015-11-12 01:26:38 +01:00
PROGRAM=obackup
2017-01-04 09:00:47 +01:00
PROGRAM_VERSION=2.1-beta1
2015-11-12 01:26:38 +01:00
PROGRAM_BINARY=$PROGRAM".sh"
PROGRAM_BATCH=$PROGRAM"-batch.sh"
2017-01-04 09:00:47 +01:00
SCRIPT_BUILD=2016122701
2015-04-24 21:23:04 +02:00
2016-04-08 21:09:00 +02:00
## osync / obackup / pmocr / zsnap install script
2017-01-04 09:00:47 +01:00
## Tested on RHEL / CentOS 6 & 7, Fedora 23, Debian 7 & 8, Mint 17 and FreeBSD 8, 10 and 11
2015-04-24 21:23:04 +02:00
## Please adapt this to fit your distro needs
2016-11-30 14:04:41 +01:00
# Get current install.sh path from http://stackoverflow.com/a/246128/2635443
SCRIPT_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
2016-09-08 22:51:24 +02:00
CONF_DIR=$FAKEROOT/etc/$PROGRAM
BIN_DIR="$FAKEROOT/usr/local/bin"
SERVICE_DIR_INIT=$FAKEROOT/etc/init.d
2016-05-25 00:36:50 +02:00
# Should be /usr/lib/systemd/system, but /lib/systemd/system exists on debian & rhel / fedora
2016-09-08 22:51:24 +02:00
SERVICE_DIR_SYSTEMD_SYSTEM=$FAKEROOT/lib/systemd/system
SERVICE_DIR_SYSTEMD_USER=$FAKEROOT/etc/systemd/user
2016-04-08 21:09:00 +02:00
## osync specific code
OSYNC_SERVICE_FILE_INIT="osync-srv"
OSYNC_SERVICE_FILE_SYSTEMD_SYSTEM="osync-srv@.service"
OSYNC_SERVICE_FILE_SYSTEMD_USER="osync-srv@.service.user"
## pmocr specfic code
PMOCR_SERVICE_FILE_INIT="pmocr-srv"
2016-09-08 22:51:24 +02:00
PMOCR_SERVICE_FILE_SYSTEMD_SYSTEM="pmocr-srv@.service"
2016-04-08 21:09:00 +02:00
## Generic code
2015-11-12 01:26:38 +01:00
2016-05-26 11:11:56 +02:00
## Default log file
2016-09-08 22:51:24 +02:00
if [ -w $FAKEROOT/var/log ]; then
2017-01-04 09:00:47 +01:00
LOG_FILE="$FAKEROOT/var/log/$PROGRAM-install.log"
2016-05-26 11:11:56 +02:00
elif ([ "$HOME" != "" ] && [ -w "$HOME" ]); then
2017-01-04 09:00:47 +01:00
LOG_FILE="$HOME/$PROGRAM-install.log"
2016-05-26 11:11:56 +02:00
else
2017-01-04 09:00:47 +01:00
LOG_FILE="./$PROGRAM-install.log"
2016-03-22 10:56:14 +01:00
fi
2017-01-04 09:00:47 +01:00
# QuickLogger subfunction, can be called directly
2016-05-26 11:11:56 +02:00
function _QuickLogger {
2017-01-04 09:00:47 +01: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 11:11:56 +02:00
}
2017-01-04 09:00:47 +01:00
# Generic quick logging function
2016-05-26 11:11:56 +02:00
function QuickLogger {
local value="${1}"
2016-04-08 21:09:00 +02:00
2017-01-04 09:00:47 +01:00
if [ "$_LOGGER_SILENT" == true ]; then
2016-05-26 11:11:56 +02:00
_QuickLogger "$value" "log"
2015-11-12 01:26:38 +01:00
else
2016-05-26 11:11:56 +02:00
_QuickLogger "$value" "stdout"
2015-11-12 01:26:38 +01:00
fi
2016-05-26 11:11:56 +02:00
}
2017-01-04 09:00:47 +01: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 11:11:56 +02:00
}
2017-01-04 09:00:47 +01:00
function GetLocalOS {
2016-11-30 14:04:41 +01:00
local localOsVar
2016-03-20 16:49:29 +01:00
2017-01-04 09:00:47 +01: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
localOsVar="$(uname -spior 2>&1)"
if [ $? != 0 ]; then
localOsVar="$(uname -v 2>&1)"
if [ $? != 0 ]; then
localOsVar="$(uname)"
fi
fi
fi
fi
case $localOsVar in
# Android uname contains both linux and android, keep it before linux entry
*"Android"*)
LOCAL_OS="Android"
;;
*"Linux"*)
LOCAL_OS="Linux"
;;
*"BSD"*)
LOCAL_OS="BSD"
;;
*"MINGW32"*|*"MSYS"*)
LOCAL_OS="msys"
;;
*"CYGWIN"*)
LOCAL_OS="Cygwin"
;;
*"Microsoft"*)
LOCAL_OS="WinNT10"
;;
*"Darwin"*)
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
# Add a global variable for statistics in installer
LOCAL_OS_FULL="$localOsVar"
}
function SetLocalOSSettings {
2016-11-30 14:04:41 +01:00
USER=root
2016-03-20 16:49:29 +01:00
2017-01-04 09:00:47 +01:00
# LOCAL_OS and LOCAL_OS_FULL are global variables set at GetLocalOS
2016-11-30 14:04:41 +01:00
2017-01-04 09:00:47 +01:00
case $LOCAL_OS in
2016-05-26 11:11:56 +02:00
*"BSD"*)
GROUP=wheel
;;
2017-01-04 09:00:47 +01:00
*"MacOSX"*)
2016-05-26 11:11:56 +02:00
GROUP=admin
;;
2017-01-04 09:00:47 +01:00
*"msys"*|*"Cygwin"*)
2016-05-26 11:11:56 +02:00
USER=""
GROUP=""
;;
2016-09-08 22:51:24 +02:00
*)
GROUP=root
;;
2016-05-26 11:11:56 +02:00
esac
2017-01-04 09:00:47 +01: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
2016-09-08 22:51:24 +02:00
if ([ "$USER" != "" ] && [ "$(whoami)" != "$USER" ] && [ "$FAKEROOT" == "" ]); then
2017-01-04 09:00:47 +01:00
QuickLogger "Must be run as $USER."
2016-05-26 11:11:56 +02:00
exit 1
2016-03-20 16:49:29 +01:00
fi
2015-11-12 01:26:38 +01:00
2017-01-04 09:00:47 +01:00
OS=$(UrlEncode "$LOCAL_OS_FULL")
2016-05-26 11:11:56 +02:00
}
2016-05-26 11:11:56 +02: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 21:09:00 +02:00
else
2016-05-26 11:11:56 +02: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 21:09:00 +02:00
fi
2016-05-26 11:11:56 +02:00
}
function CreateConfDir {
if [ ! -d "$CONF_DIR" ]; then
mkdir "$CONF_DIR"
if [ $? == 0 ]; then
QuickLogger "Created directory [$CONF_DIR]."
else
QuickLogger "Cannot create directory [$CONF_DIR]."
exit 1
fi
2016-04-08 21:09:00 +02:00
else
2016-05-26 11:11:56 +02:00
QuickLogger "Config directory [$CONF_DIR] exists."
2016-04-08 21:09:00 +02:00
fi
2016-05-26 11:11:56 +02:00
}
2016-04-08 21:09:00 +02:00
2016-05-26 11:11:56 +02:00
function CopyExampleFiles {
2016-11-30 14:04:41 +01:00
if [ -f "$SCRIPT_PATH/sync.conf.example" ]; then
cp "$SCRIPT_PATH/sync.conf.example" "$CONF_DIR/sync.conf.example"
2016-05-26 11:11:56 +02:00
fi
2016-11-30 14:04:41 +01:00
if [ -f "$SCRIPT_PATH/host_backup.conf.example" ]; then
cp "$SCRIPT_PATH/host_backup.conf.example" "$CONF_DIR/host_backup.conf.example"
2016-05-26 11:11:56 +02:00
fi
2016-11-30 14:04:41 +01:00
if [ -f "$SCRIPT_PATH/exlude.list.example" ]; then
cp "$SCRIPT_PATH/exclude.list.example" "$CONF_DIR/exclude.list.example"
2016-05-26 11:11:56 +02:00
fi
2016-11-30 14:04:41 +01:00
if [ -f "$SCRIPT_PATH/snapshot.conf.example" ]; then
cp "$SCRIPT_PATH/snapshot.conf.example" "$CONF_DIR/snapshot.conf.example"
2016-09-08 22:51:24 +02:00
fi
2016-11-30 14:04:41 +01:00
if [ -f "$SCRIPT_PATH/default.conf" ]; then
2016-09-08 22:51:24 +02:00
if [ -f "$CONF_DIR/default.conf" ]; then
2016-11-30 14:04:41 +01:00
cp "$SCRIPT_PATH/default.conf" "$CONF_DIR/default.conf.new"
2016-09-08 22:51:24 +02:00
QuickLogger "Copied default.conf to [$CONF_DIR/default.conf.new]."
else
2016-11-30 14:04:41 +01:00
cp "$SCRIPT_PATH/default.conf" "$CONF_DIR/default.conf"
2016-09-08 22:51:24 +02:00
fi
2016-04-08 21:09:00 +02:00
fi
2016-05-26 11:11:56 +02:00
}
function CopyProgram {
2016-11-30 14:04:41 +01:00
cp "$SCRIPT_PATH/$PROGRAM_BINARY" "$BIN_DIR"
2015-11-12 01:26:38 +01:00
if [ $? != 0 ]; then
2016-05-26 11:11:56 +02:00
QuickLogger "Cannot copy $PROGRAM_BINARY to [$BIN_DIR]. Make sure to run install script in the directory containing all other files."
QuickLogger "Also make sure you have permissions to write to [$BIN_DIR]."
exit 1
2015-11-12 01:26:38 +01:00
else
2016-05-26 11:11:56 +02:00
chmod 755 "$BIN_DIR/$PROGRAM_BINARY"
QuickLogger "Copied $PROGRAM_BINARY to [$BIN_DIR]."
2015-11-12 01:26:38 +01:00
fi
2016-11-30 14:04:41 +01:00
if [ -f "$SCRIPT_PATH/$PROGRAM_BATCH" ]; then
cp "$SCRIPT_PATH/$PROGRAM_BATCH" "$BIN_DIR"
2016-05-26 11:11:56 +02:00
if [ $? != 0 ]; then
QuickLogger "Cannot copy $PROGRAM_BATCH to [$BIN_DIR]."
else
chmod 755 "$BIN_DIR/$PROGRAM_BATCH"
QuickLogger "Copied $PROGRAM_BATCH to [$BIN_DIR]."
fi
fi
2016-11-30 14:04:41 +01:00
if [ -f "$SCRIPT_PATH/ssh_filter.sh" ]; then
cp "$SCRIPT_PATH/ssh_filter.sh" "$BIN_DIR"
2016-05-26 11:11:56 +02:00
if [ $? != 0 ]; then
QuickLogger "Cannot copy ssh_filter.sh to [$BIN_DIR]."
else
chmod 755 "$BIN_DIR/ssh_filter.sh"
2016-09-08 22:51:24 +02:00
if ([ "$USER" != "" ] && [ "$GROUP" != "" ] && [ "$FAKEROOT" == "" ]); then
2016-05-26 11:11:56 +02:00
chown $USER:$GROUP "$BIN_DIR/ssh_filter.sh"
fi
QuickLogger "Copied ssh_filter.sh to [$BIN_DIR]."
fi
fi
}
function CopyServiceFiles {
# OSYNC SPECIFIC
2016-11-30 14:04:41 +01:00
if ([ "$init" == "systemd" ] && [ -f "$SCRIPT_PATH/$OSYNC_SERVICE_FILE_SYSTEMD_SYSTEM" ]); then
cp "$SCRIPT_PATH/$OSYNC_SERVICE_FILE_SYSTEMD_SYSTEM" "$SERVICE_DIR_SYSTEMD_SYSTEM" && cp "$SCRIPT_PATH/$OSYNC_SERVICE_FILE_SYSTEMD_USER" "$SERVICE_DIR_SYSTEMD_USER/$SERVICE_FILE_SYSTEMD_SYSTEM"
2016-05-26 11:11:56 +02:00
if [ $? != 0 ]; then
QuickLogger "Cannot copy the systemd file to [$SERVICE_DIR_SYSTEMD_SYSTEM] or [$SERVICE_DIR_SYSTEMD_USER]."
else
QuickLogger "Created osync-srv service in [$SERVICE_DIR_SYSTEMD_SYSTEM] and [$SERVICE_DIR_SYSTEMD_USER]."
2016-09-08 22:51:24 +02:00
QuickLogger "Can be activated with [systemctl start osync-srv@instance.conf] where instance.conf is the name of the config file in $CONF_DIR."
2016-05-26 11:11:56 +02:00
QuickLogger "Can be enabled on boot with [systemctl enable osync-srv@instance.conf]."
QuickLogger "In userland, active with [systemctl --user start osync-srv@instance.conf]."
fi
2016-11-30 14:04:41 +01:00
elif ([ "$init" == "initV" ] && [ -f "$SCRIPT_PATH/$OSYNC_SERVICE_FILE_INIT" ]); then
cp "$SCRIPT_PATH/$OSYNC_SERVICE_FILE_INIT" "$SERVICE_DIR_INIT"
2016-05-26 11:11:56 +02:00
if [ $? != 0 ]; then
QuickLogger "Cannot copy osync-srv to [$SERVICE_DIR_INIT]."
else
chmod 755 "$SERVICE_DIR_INIT/$OSYNC_SERVICE_FILE_INIT"
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]."
fi
fi
# PMOCR SPECIFIC
2016-11-30 14:04:41 +01:00
if ([ "$init" == "systemd" ] && [ -f "$SCRIPT_PATH/$PMOCR_SERVICE_FILE_SYSTEMD_SYSTEM" ]); then
cp "$SCRIPT_PATH/$PMOCR_SERVICE_FILE_SYSTEMD_SYSTEM" "$SERVICE_DIR_SYSTEMD_SYSTEM"
2016-05-26 11:11:56 +02:00
if [ $? != 0 ]; then
QuickLogger "Cannot copy the systemd file to [$SERVICE_DIR_SYSTEMD_SYSTEM] or [$SERVICE_DIR_SYSTEMD_USER]."
else
QuickLogger "Created pmocr-srv service in [$SERVICE_DIR_SYSTEMD_SYSTEM] and [$SERVICE_DIR_SYSTEMD_USER]."
2016-09-08 22:51:24 +02:00
QuickLogger "Can be activated with [systemctl start pmocr-srv@default.conf] where default.conf is the name of the config file in $CONF_DIR."
QuickLogger "Can be enabled on boot with [systemctl enable pmocr-srv@default.conf]."
2016-05-26 11:11:56 +02:00
fi
2016-11-30 14:04:41 +01:00
elif ([ "$init" == "initV" ] && [ -f "$SCRIPT_PATH/$PMOCR_SERVICE_FILE_INIT" ]); then
cp "$SCRIPT_PATH/$PMOCR_SERVICE_FILE_INIT" "$SERVICE_DIR_INIT"
2016-05-26 11:11:56 +02:00
if [ $? != 0 ]; then
QuickLogger "Cannot copy pmoct-srv to [$SERVICE_DIR_INIT]."
else
chmod 755 "$SERVICE_DIR_INIT/$PMOCR_SERVICE_FILE_INIT"
QuickLogger "Created osync-srv service in [$SERVICE_DIR_INIT]."
QuickLogger "Can be activated with [service $PMOCR_SERVICE_FILE_INIT start]."
QuickLogger "Can be enabled on boot with [chkconfig $PMOCR_SERVICE_FILE_INIT on]."
fi
fi
}
function Statistics {
2017-01-04 09:00:47 +01:00
if type wget > /dev/null; then
wget -qO- "$STATS_LINK" > /dev/null 2>&1
if [ $? == 0 ]; then
return 0
fi
fi
2016-04-08 21:09:00 +02:00
2017-01-04 09:00:47 +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-04-08 21:09:00 +02:00
fi
2017-01-04 09:00:47 +01:00
QuickLogger "Neiter wget nor curl could be used for. Cannot run statistics. Use the provided link please."
return 1
2016-05-26 11:11:56 +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."
exit 127
}
2017-01-04 09:00:47 +01:00
_LOGGER_SILENT=false
2016-05-26 11:11:56 +02:00
_STATS=1
for i in "$@"
do
case $i in
--silent)
2017-01-04 09:00:47 +01:00
_LOGGER_SILENT=true
2016-05-26 11:11:56 +02:00
;;
--no-stats)
_STATS=0
;;
--help|-h|-?)
Usage
esac
done
2016-09-08 22:51:24 +02:00
if [ "$FAKEROOT" != "" ]; then
mkdir -p "$SERVICE_DIR_SYSTEMD_SYSTEM" "$SERVICE_DIR_SYSTEMD_USER" "$BIN_DIR"
fi
2017-01-04 09:00:47 +01:00
GetLocalOS
SetLocalOSSettings
2016-05-26 11:11:56 +02:00
CreateConfDir
CopyExampleFiles
CopyProgram
GetInit
CopyServiceFiles
STATS_LINK="http://instcount.netpower.fr?program=$PROGRAM&version=$PROGRAM_VERSION&os=$OS"
QuickLogger "$PROGRAM installed. Use with $BIN_DIR/$PROGRAM"
if [ $_STATS -eq 1 ]; then
2017-01-04 09:00:47 +01:00
if [ $_LOGGER_SILENT == true ]; then
2016-05-26 11:11:56 +02:00
Statistics
else
QuickLogger "In order to make install 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
case $response in
[nN])
exit
;;
*)
Statistics
exit $?
;;
esac
fi
fi