mirror of
https://github.com/deajan/obackup.git
synced 2024-12-26 15:33:41 +01:00
Updated common scripts
This commit is contained in:
parent
25cb702d22
commit
179434e9b0
@ -4,7 +4,7 @@ PROGRAM=[prgname]
|
|||||||
PROGRAM_VERSION=[version]
|
PROGRAM_VERSION=[version]
|
||||||
PROGRAM_BINARY=$PROGRAM".sh"
|
PROGRAM_BINARY=$PROGRAM".sh"
|
||||||
PROGRAM_BATCH=$PROGRAM"-batch.sh"
|
PROGRAM_BATCH=$PROGRAM"-batch.sh"
|
||||||
SCRIPT_BUILD=2016052601
|
SCRIPT_BUILD=2016090605
|
||||||
|
|
||||||
## osync / obackup / pmocr / zsnap install script
|
## osync / obackup / pmocr / zsnap install script
|
||||||
## Tested on RHEL / CentOS 6 & 7, Fedora 23, Debian 7 & 8, Mint 17 and FreeBSD 8 & 10
|
## Tested on RHEL / CentOS 6 & 7, Fedora 23, Debian 7 & 8, Mint 17 and FreeBSD 8 & 10
|
||||||
@ -12,12 +12,12 @@ SCRIPT_BUILD=2016052601
|
|||||||
|
|
||||||
#TODO: silent mode and no stats mode
|
#TODO: silent mode and no stats mode
|
||||||
|
|
||||||
CONF_DIR=/etc/$PROGRAM
|
CONF_DIR=$FAKEROOT/etc/$PROGRAM
|
||||||
BIN_DIR=/usr/local/bin
|
BIN_DIR="$FAKEROOT/usr/local/bin"
|
||||||
SERVICE_DIR_INIT=/etc/init.d
|
SERVICE_DIR_INIT=$FAKEROOT/etc/init.d
|
||||||
# Should be /usr/lib/systemd/system, but /lib/systemd/system exists on debian & rhel / fedora
|
# Should be /usr/lib/systemd/system, but /lib/systemd/system exists on debian & rhel / fedora
|
||||||
SERVICE_DIR_SYSTEMD_SYSTEM=/lib/systemd/system
|
SERVICE_DIR_SYSTEMD_SYSTEM=$FAKEROOT/lib/systemd/system
|
||||||
SERVICE_DIR_SYSTEMD_USER=/etc/systemd/user
|
SERVICE_DIR_SYSTEMD_USER=$FAKEROOT/etc/systemd/user
|
||||||
|
|
||||||
## osync specific code
|
## osync specific code
|
||||||
OSYNC_SERVICE_FILE_INIT="osync-srv"
|
OSYNC_SERVICE_FILE_INIT="osync-srv"
|
||||||
@ -26,13 +26,13 @@ OSYNC_SERVICE_FILE_SYSTEMD_USER="osync-srv@.service.user"
|
|||||||
|
|
||||||
## pmocr specfic code
|
## pmocr specfic code
|
||||||
PMOCR_SERVICE_FILE_INIT="pmocr-srv"
|
PMOCR_SERVICE_FILE_INIT="pmocr-srv"
|
||||||
PMOCR_SERVICE_FILE_SYSTEMD_SYSTEM="pmocr-srv.service"
|
PMOCR_SERVICE_FILE_SYSTEMD_SYSTEM="pmocr-srv@.service"
|
||||||
|
|
||||||
## Generic code
|
## Generic code
|
||||||
|
|
||||||
## Default log file
|
## Default log file
|
||||||
if [ -w /var/log ]; then
|
if [ -w $FAKEROOT/var/log ]; then
|
||||||
LOG_FILE="/var/log/$PROGRAM-install.log"
|
LOG_FILE="$FAKEROOT/var/log/$PROGRAM-install.log"
|
||||||
elif ([ "$HOME" != "" ] && [ -w "$HOME" ]); then
|
elif ([ "$HOME" != "" ] && [ -w "$HOME" ]); then
|
||||||
LOG_FILE="$HOME/$PROGRAM-install.log"
|
LOG_FILE="$HOME/$PROGRAM-install.log"
|
||||||
else
|
else
|
||||||
@ -95,16 +95,16 @@ function SetOSSettings {
|
|||||||
*"Darwin"*)
|
*"Darwin"*)
|
||||||
GROUP=admin
|
GROUP=admin
|
||||||
;;
|
;;
|
||||||
*)
|
*"MINGW"*|*"CYGWIN"*)
|
||||||
GROUP=root
|
|
||||||
;;
|
|
||||||
*"MINGW32"*|*"CYGWIN"*)
|
|
||||||
USER=""
|
USER=""
|
||||||
GROUP=""
|
GROUP=""
|
||||||
;;
|
;;
|
||||||
|
*)
|
||||||
|
GROUP=root
|
||||||
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
if ([ "$USER" != "" ] && [ "$(whoami)" != "$USER" ]); then
|
if ([ "$USER" != "" ] && [ "$(whoami)" != "$USER" ] && [ "$FAKEROOT" == "" ]); then
|
||||||
QuickLogger "Must be run as $USER."
|
QuickLogger "Must be run as $USER."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
@ -141,19 +141,28 @@ function CreateConfDir {
|
|||||||
|
|
||||||
function CopyExampleFiles {
|
function CopyExampleFiles {
|
||||||
if [ -f "./sync.conf.example" ]; then
|
if [ -f "./sync.conf.example" ]; then
|
||||||
cp "./sync.conf.example" "/etc/$PROGRAM/sync.conf.example"
|
cp "./sync.conf.example" "$CONF_DIR/sync.conf.example"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -f "./host_backup.conf.example" ]; then
|
if [ -f "./host_backup.conf.example" ]; then
|
||||||
cp "./host_backup.conf.example" "/etc/$PROGRAM/host_backup.conf.example"
|
cp "./host_backup.conf.example" "$CONF_DIR/host_backup.conf.example"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -f "./exlude.list.example" ]; then
|
if [ -f "./exlude.list.example" ]; then
|
||||||
cp "./exclude.list.example" "/etc/$PROGRAM"
|
cp "./exclude.list.example" "$CONF_DIR/exclude.list.example"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -f "./snapshot.conf.example" ]; then
|
if [ -f "./snapshot.conf.example" ]; then
|
||||||
cp "./snapshot.conf.example" "/etc/$PROGRAM/snapshot.conf.example"
|
cp "./snapshot.conf.example" "$CONF_DIR/snapshot.conf.example"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -f "./default.conf" ]; then
|
||||||
|
if [ -f "$CONF_DIR/default.conf" ]; then
|
||||||
|
cp "./default.conf" "$CONF_DIR/default.conf.new"
|
||||||
|
QuickLogger "Copied default.conf to [$CONF_DIR/default.conf.new]."
|
||||||
|
else
|
||||||
|
cp "./default.conf" "$CONF_DIR/default.conf"
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -184,7 +193,7 @@ function CopyProgram {
|
|||||||
QuickLogger "Cannot copy ssh_filter.sh to [$BIN_DIR]."
|
QuickLogger "Cannot copy ssh_filter.sh to [$BIN_DIR]."
|
||||||
else
|
else
|
||||||
chmod 755 "$BIN_DIR/ssh_filter.sh"
|
chmod 755 "$BIN_DIR/ssh_filter.sh"
|
||||||
if ([ "$USER" != "" ] && [ "$GROUP" != "" ]); then
|
if ([ "$USER" != "" ] && [ "$GROUP" != "" ] && [ "$FAKEROOT" == "" ]); then
|
||||||
chown $USER:$GROUP "$BIN_DIR/ssh_filter.sh"
|
chown $USER:$GROUP "$BIN_DIR/ssh_filter.sh"
|
||||||
fi
|
fi
|
||||||
QuickLogger "Copied ssh_filter.sh to [$BIN_DIR]."
|
QuickLogger "Copied ssh_filter.sh to [$BIN_DIR]."
|
||||||
@ -200,7 +209,7 @@ function CopyServiceFiles {
|
|||||||
QuickLogger "Cannot copy the systemd file to [$SERVICE_DIR_SYSTEMD_SYSTEM] or [$SERVICE_DIR_SYSTEMD_USER]."
|
QuickLogger "Cannot copy the systemd file to [$SERVICE_DIR_SYSTEMD_SYSTEM] or [$SERVICE_DIR_SYSTEMD_USER]."
|
||||||
else
|
else
|
||||||
QuickLogger "Created osync-srv service in [$SERVICE_DIR_SYSTEMD_SYSTEM] and [$SERVICE_DIR_SYSTEMD_USER]."
|
QuickLogger "Created osync-srv service in [$SERVICE_DIR_SYSTEMD_SYSTEM] and [$SERVICE_DIR_SYSTEMD_USER]."
|
||||||
QuickLogger "Can be activated with [systemctl start osync-srv@instance.conf] where instance.conf is the name of the config file in /etc/osync."
|
QuickLogger "Can be activated with [systemctl start osync-srv@instance.conf] where instance.conf is the name of the config file in $CONF_DIR."
|
||||||
QuickLogger "Can be enabled on boot with [systemctl enable osync-srv@instance.conf]."
|
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]."
|
QuickLogger "In userland, active with [systemctl --user start osync-srv@instance.conf]."
|
||||||
fi
|
fi
|
||||||
@ -223,8 +232,8 @@ function CopyServiceFiles {
|
|||||||
QuickLogger "Cannot copy the systemd file to [$SERVICE_DIR_SYSTEMD_SYSTEM] or [$SERVICE_DIR_SYSTEMD_USER]."
|
QuickLogger "Cannot copy the systemd file to [$SERVICE_DIR_SYSTEMD_SYSTEM] or [$SERVICE_DIR_SYSTEMD_USER]."
|
||||||
else
|
else
|
||||||
QuickLogger "Created pmocr-srv service in [$SERVICE_DIR_SYSTEMD_SYSTEM] and [$SERVICE_DIR_SYSTEMD_USER]."
|
QuickLogger "Created pmocr-srv service in [$SERVICE_DIR_SYSTEMD_SYSTEM] and [$SERVICE_DIR_SYSTEMD_USER]."
|
||||||
QuickLogger "Can be activated with [systemctl start pmocr-srv] after configuring file options in [$BIN_DIR/$PROGRAM]."
|
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]."
|
QuickLogger "Can be enabled on boot with [systemctl enable pmocr-srv@default.conf]."
|
||||||
fi
|
fi
|
||||||
elif ([ "$init" == "initV" ] && [ -f "./$PMOCR_SERVICE_FILE_INIT" ]); then
|
elif ([ "$init" == "initV" ] && [ -f "./$PMOCR_SERVICE_FILE_INIT" ]); then
|
||||||
cp "./$PMOCR_SERVICE_FILE_INIT" "$SERVICE_DIR_INIT"
|
cp "./$PMOCR_SERVICE_FILE_INIT" "$SERVICE_DIR_INIT"
|
||||||
@ -282,6 +291,10 @@ do
|
|||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
|
if [ "$FAKEROOT" != "" ]; then
|
||||||
|
mkdir -p "$SERVICE_DIR_SYSTEMD_SYSTEM" "$SERVICE_DIR_SYSTEMD_USER" "$BIN_DIR"
|
||||||
|
fi
|
||||||
|
|
||||||
SetOSSettings
|
SetOSSettings
|
||||||
CreateConfDir
|
CreateConfDir
|
||||||
CopyExampleFiles
|
CopyExampleFiles
|
||||||
|
12
dev/merge.sh
12
dev/merge.sh
@ -1,6 +1,6 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
## MERGE 2016083001
|
## MERGE 2016090601
|
||||||
|
|
||||||
## Merges ofunctions.sh and n_program.sh into program.sh
|
## Merges ofunctions.sh and n_program.sh into program.sh
|
||||||
## Adds installer
|
## Adds installer
|
||||||
@ -107,17 +107,17 @@ function CopyCommons {
|
|||||||
QuickLogger "Cannot assemble batch runner." "stdout"
|
QuickLogger "Cannot assemble batch runner." "stdout"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
chmod +x ../$PROGRAM-batch.sh
|
||||||
|
if [ $? != 0 ]; then
|
||||||
|
QuickLogger "Cannot chmod $PROGRAM-batch.sh" "stdout"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
chmod +x ../install.sh
|
chmod +x ../install.sh
|
||||||
if [ $? != 0 ]; then
|
if [ $? != 0 ]; then
|
||||||
QuickLogger "Cannot chmod install.sh" "stdout"
|
QuickLogger "Cannot chmod install.sh" "stdout"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
chmod +x ../$PROGRAM-batch.sh
|
|
||||||
if [ $? != 0 ]; then
|
|
||||||
QuickLogger "Cannot chmod $PROGRAM-batch.sh" "stdout"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
rm -f ../tmp_install.sh
|
rm -f ../tmp_install.sh
|
||||||
if [ $? != 0 ]; then
|
if [ $? != 0 ]; then
|
||||||
QuickLogger "Cannot chmod $PROGRAM.sh" "stdout"
|
QuickLogger "Cannot chmod $PROGRAM.sh" "stdout"
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#### MINIMAL-FUNCTION-SET BEGIN ####
|
#### MINIMAL-FUNCTION-SET BEGIN ####
|
||||||
|
|
||||||
## FUNC_BUILD=2016090401
|
## FUNC_BUILD=2016090701
|
||||||
## BEGIN Generic bash functions written in 2013-2016 by Orsiris de Jong - http://www.netpower.fr - ozy@netpower.fr
|
## BEGIN Generic bash functions written in 2013-2016 by Orsiris de Jong - http://www.netpower.fr - ozy@netpower.fr
|
||||||
|
|
||||||
## To use in a program, define the following variables:
|
## To use in a program, define the following variables:
|
||||||
@ -542,23 +542,23 @@ function TrapError {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function LoadConfigFile {
|
function LoadConfigFile {
|
||||||
local config_file="${1}"
|
local configFile="${1}"
|
||||||
__CheckArguments 1 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG
|
__CheckArguments 1 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG
|
||||||
|
|
||||||
|
|
||||||
if [ ! -f "$config_file" ]; then
|
if [ ! -f "$configFile" ]; then
|
||||||
Logger "Cannot load configuration file [$config_file]. Cannot start." "CRITICAL"
|
Logger "Cannot load configuration file [$configFile]. Cannot start." "CRITICAL"
|
||||||
exit 1
|
exit 1
|
||||||
elif [[ "$1" != *".conf" ]]; then
|
elif [[ "$configFile" != *".conf" ]]; then
|
||||||
Logger "Wrong configuration file supplied [$config_file]. Cannot start." "CRITICAL"
|
Logger "Wrong configuration file supplied [$configFile]. Cannot start." "CRITICAL"
|
||||||
exit 1
|
exit 1
|
||||||
else
|
else
|
||||||
grep '^[^ ]*=[^;&]*' "$config_file" > "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID" # WITHOUT COMMENTS
|
# Remove everything that is not a variable assignation
|
||||||
# Shellcheck source=./sync.conf
|
grep '^[^ ]*=[^;&]*' "$configFile" > "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID"
|
||||||
source "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID"
|
source "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
CONFIG_FILE="$config_file"
|
CONFIG_FILE="$configFile"
|
||||||
}
|
}
|
||||||
|
|
||||||
function Spinner {
|
function Spinner {
|
||||||
@ -675,7 +675,6 @@ function WaitForTaskCompletion {
|
|||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
SendAlert true
|
SendAlert true
|
||||||
#errorcount=$((errorcount+1))
|
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@ -727,10 +726,16 @@ function WaitForTaskCompletion {
|
|||||||
|
|
||||||
# Take a list of commands to run, runs them sequentially with numberOfProcesses commands simultaneously runs
|
# Take a list of commands to run, runs them sequentially with numberOfProcesses commands simultaneously runs
|
||||||
# Returns the number of non zero exit codes from commands
|
# Returns the number of non zero exit codes from commands
|
||||||
|
# Use cmd1;cmd2;cmd3 syntax for small sets, use file for large command sets
|
||||||
function ParallelExec {
|
function ParallelExec {
|
||||||
local numberOfProcesses="${1}" # Number of simultaneous commands to run
|
local numberOfProcesses="${1}" # Number of simultaneous commands to run
|
||||||
local commandsArg="${2}" # Semi-colon separated list of commands
|
local commandsArg="${2}" # Semi-colon separated list of commands, or file containing one command per line
|
||||||
|
local readFromFile="${3:-false}" # Is commandsArg a file or a string ?
|
||||||
|
|
||||||
|
__CheckArguments 3 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG
|
||||||
|
|
||||||
|
local commandCount
|
||||||
|
local command
|
||||||
local pid
|
local pid
|
||||||
local counter=0
|
local counter=0
|
||||||
local commandsArray
|
local commandsArray
|
||||||
@ -743,18 +748,33 @@ function ParallelExec {
|
|||||||
|
|
||||||
local hasPids=false # Are any valable pids given to function ? #__WITH_PARANOIA_DEBUG
|
local hasPids=false # Are any valable pids given to function ? #__WITH_PARANOIA_DEBUG
|
||||||
|
|
||||||
|
if [ $readFromFile == true ];then
|
||||||
|
if [ -f "$commandsArg" ]; then
|
||||||
|
commandCount=$(wc -l < "$commandsArg")
|
||||||
|
else
|
||||||
|
commandCount=0
|
||||||
|
fi
|
||||||
|
else
|
||||||
IFS=';' read -r -a commandsArray <<< "$commandsArg"
|
IFS=';' read -r -a commandsArray <<< "$commandsArg"
|
||||||
|
commandCount=${#commandsArray[@]}
|
||||||
|
fi
|
||||||
|
|
||||||
Logger "Runnning ${#commandsArray[@]} commands in $numberOfProcesses simultaneous processes." "DEBUG"
|
Logger "Runnning $commandCount commands in $numberOfProcesses simultaneous processes." "DEBUG"
|
||||||
|
|
||||||
while [ $counter -lt "${#commandsArray[@]}" ] || [ ${#pidsArray[@]} -gt 0 ]; do
|
while [ $counter -lt "$commandCount" ] || [ ${#pidsArray[@]} -gt 0 ]; do
|
||||||
|
|
||||||
while [ $counter -lt "${#commandsArray[@]}" ] && [ ${#pidsArray[@]} -lt $numberOfProcesses ]; do
|
while [ $counter -lt "$commandCount" ] && [ ${#pidsArray[@]} -lt $numberOfProcesses ]; do
|
||||||
Logger "Running command [${commandsArray[$counter]}]." "DEBUG"
|
if [ $readFromFile == true ]; then
|
||||||
eval "${commandsArray[$counter]}" &
|
#TODO: Checked on FreeBSD 10, also check on Win
|
||||||
|
command=$(awk 'NR == num_line {print; exit}' num_line=$((counter+1)) "$commandsArg")
|
||||||
|
else
|
||||||
|
command="${commandsArray[$counter]}"
|
||||||
|
fi
|
||||||
|
Logger "Running command [$command]." "DEBUG"
|
||||||
|
eval "$command" &
|
||||||
pid=$!
|
pid=$!
|
||||||
pidsArray+=($pid)
|
pidsArray+=($pid)
|
||||||
commandsArrayPid[$pid]="${commandsArray[$counter]}"
|
commandsArrayPid[$pid]="$command"
|
||||||
counter=$((counter+1))
|
counter=$((counter+1))
|
||||||
done
|
done
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user