evobackup/bkctld

617 lines
16 KiB
Plaintext
Raw Normal View History

#!/bin/sh
2016-12-20 11:04:24 +01:00
#
# bkctld is a shell script to create and manage a backup server which will
# handle the backup of many servers (clients).
#
# Author: Victor Laborie <vlaborie@evolix.fr>
# Contributor: Benoît Série <bserie@evolix.fr>, Gregory Colpart <reg@evolix.fr>, Romain Dessort <rdessort@evolix.fr>, Tristan Pilat <tpilat@evolix.fr>
# Licence: AGPLv3
#
2016-12-18 15:45:15 +01:00
2017-01-09 14:16:41 +01:00
usage(){
2017-01-09 14:10:23 +01:00
echo "Usage: $0 <subcommand> [options]"
2016-12-18 15:45:15 +01:00
echo "Subcommands:"
echo " init <jailname> Init jail <jailname>"
2017-01-09 14:16:41 +01:00
echo " update <jailname>|all Update jail <jailname> or all"
echo " remove <jailname>|all Remove jail <jailname> or all"
echo " start <jailname>|all Start jail <jailname> or all"
echo " stop <jailname>|all Stop jail <jailname> or all"
echo " reload <jailname>|all Reload jail <jailname> or all"
echo " restart <jailname>|all Restart jail <jailname> or all"
echo " sync <jailname>|all Sync jail <jailname> or all to another node"
echo " status [<jailname>] Print status of <jailname> (default all jail)"
echo " key <jailname> [<keyfile>] Set or get ssh pubic key of <jailname>"
2017-01-09 14:16:41 +01:00
echo " port <jailname> [<ssh_port>|auto] Set or get ssh port of <jailname>"
echo " ip <jailname> [<ip>|all] Set or get allowed(s) ip(s) of <jailname>"
echo " inc Make incremental inc of all jails"
echo " rm Remove old incremtal inc of all jails"
2016-12-18 15:45:15 +01:00
echo ""
}
2017-01-10 12:12:33 +01:00
## check functions
check_jail() {
jail=$1
if [ -d ${JAILDIR}/${jail} ]; then
exit 0
else
exit 1
fi
}
check_jail_on() {
jail=$1
2017-08-17 20:53:40 +02:00
return=1
if [ -f ${JAILDIR}/${jail}/${SSHD_PID} ]; then
pid=$(cat ${JAILDIR}/${jail}/${SSHD_PID})
ps -p $pid > /dev/null && return=0
fi
if [ "$return" -eq 1 ]; then
rm -f ${JAILDIR}/${jail}/${SSHD_PID}
grep -q "${JAILDIR}/${jail}/proc" /proc/mounts && umount --lazy ${JAILDIR}/${jail}/proc/
grep -q "${JAILDIR}/${jail}/dev" /proc/mounts && umount --lazy --recursive ${JAILDIR}/${jail}/dev
2017-01-10 12:12:33 +01:00
fi
2017-08-17 20:53:40 +02:00
exit "$return"
2017-01-10 12:12:33 +01:00
}
## get functions : get info on jail
get_port() {
jail=$1
port=$(grep -E "Port [0-9]+" ${JAILDIR}/${jail}/${SSHD_CONFIG}|grep -oE "[0-9]+")
echo $port
}
get_key() {
jail=$1
if [ -f ${JAILDIR}/${jail}/${AUTHORIZED_KEYS} ]; then
cat ${JAILDIR}/${jail}/${AUTHORIZED_KEYS}
fi
}
get_ip() {
jail=$1
2017-07-19 14:02:49 +02:00
grep -E "^AllowUsers" "${JAILDIR}/$jail/${SSHD_CONFIG}"|grep -Eo "root@[^ ]+"| while read allow; do
echo "$allow"|cut -d'@' -f2
2017-01-10 12:12:33 +01:00
done
}
2017-01-10 13:53:18 +01:00
get_inc() {
jail=$1
inc="0"
if [ -f ${CONFDIR}/${jail} ]; then
day=$(grep -c "day" ${CONFDIR}/${jail})
month=$(grep -c "month" ${CONFDIR}/${jail})
inc="${day}/${month}"
fi
echo $inc
}
2017-01-10 12:12:33 +01:00
## set functions : set info on jail
set_port() {
jail=$1
port=$2
if [ "$port" = "auto" ]; then
2017-07-19 14:02:49 +02:00
port=$(grep -h Port ${JAILDIR}/*/${SSHD_CONFIG} 2>/dev/null | grep -Eo "[0-9]+" | sort -n | tail -1)
2017-01-10 12:12:33 +01:00
port=$((port+1))
if [ ! $port -gt 1 ]; then
port=2222
fi
fi
sed -i "s/^Port .*/Port ${port}/" ${JAILDIR}/$jail/${SSHD_CONFIG}
set_firewall $jail
}
set_key() {
jail=$1
keyfile=$2
if [ -e $keyfile ]; then
2017-01-10 12:12:33 +01:00
cat $keyfile > ${JAILDIR}/${jail}/${AUTHORIZED_KEYS}
chmod 600 ${JAILDIR}/${jail}/${AUTHORIZED_KEYS}
else
echo "Keyfile $keyfile dosen't exist !" >&2
exit 1
fi
}
set_ip() {
jail=$1
ip=$2
2017-07-13 16:22:45 +02:00
if [ "$ip" = "all" ] || [ "$ip" = "0.0.0.0/0" ]; then
2017-01-10 12:12:33 +01:00
ips="0.0.0.0/0"
else
ips=$(get_ip $jail)
ips=$(echo $ips $ip|xargs -n1|grep -v "0.0.0.0/0"|sort|uniq)
fi
allow="AllowUsers"
for ip in $ips; do
allow="$allow root@${ip}"
done
sed -i "s~^AllowUsers .*~${allow}~" ${JAILDIR}/$jail/${SSHD_CONFIG}
set_firewall $jail
}
set_firewall() {
jail=$1
2017-01-10 13:53:18 +01:00
if [ -n "${FIREWALL_RULES}" ]; then
if [ -f $FIREWALL_RULES ]; then
sed -i "/#${jail}$/d" $FIREWALL_RULES
fi
2017-03-31 14:54:18 +02:00
if ( check_jail $jail ); then
port=$(get_port $jail)
for ip in $(get_ip $jail); do
echo "/sbin/iptables -A INPUT -p tcp --sport 1024: --dport $port -s $ip -j ACCEPT #$jail" >> $FIREWALL_RULES
done
if [ -f /etc/init.d/minifirewall ]; then
/etc/init.d/minifirewall restart >/dev/null
fi
2017-01-11 11:28:28 +01:00
fi
2017-01-10 12:12:33 +01:00
fi
}
## mk_jail function : create or update a jail
mk_jail() {
jail=$1
passwd="${TPLDIR}/passwd"
shadow="${TPLDIR}/shadow"
group="${TPLDIR}/group"
sshrc="${TPLDIR}/sshrc"
[ -f "${LOCALTPLDIR}/passwd" ] && passwd="${LOCALTPLDIR}/passwd"
[ -f "${LOCALTPLDIR}/shadow" ] && shadow="${LOCALTPLDIR}/shadow"
[ -f "${LOCALTPLDIR}/group" ] && group="${LOCALTPLDIR}/group"
[ -f "${LOCALTPLDIR}/sshrc" ] && group="${LOCALTPLDIR}/sshrc"
2017-07-21 16:15:31 +02:00
umask 077
2017-01-10 12:12:33 +01:00
echo "1 - Creating the chroot"
cd "${JAILDIR}/${jail}"
2017-08-29 17:31:40 +02:00
rm -rf bin lib lib64 run usr var/run etc/ssh/*key
2017-07-21 16:15:31 +02:00
mkdir -p dev proc
mkdir -p usr/bin usr/sbin usr/lib usr/lib/x86_64-linux-gnu usr/lib/openssh usr/lib64
2017-08-17 16:05:38 +02:00
mkdir -p etc/ssh var/log run/sshd
2017-07-21 16:15:31 +02:00
mkdir -p root/.ssh var/backup -m 0700
ln -s usr/bin bin
ln -s usr/lib lib
ln -s usr/lib64 lib64
2017-08-22 17:03:21 +02:00
ln -st var ../run
2017-08-17 16:05:38 +02:00
touch var/log/lastlog var/log/wtmp run/utmp
echo "2 - Copying essential files"
[ -f /etc/ssh/ssh_host_rsa_key ] && cp /etc/ssh/ssh_host_rsa_key etc/ssh
[ -f /etc/ssh/ssh_host_ecdsa_key ] && cp /etc/ssh/ssh_host_ecdsa_key etc/ssh
[ -f /etc/ssh/ssh_host_ed25519_key ] && cp /etc/ssh/ssh_host_ed25519_key etc/ssh
cp "$passwd" etc
cp "$shadow" etc
cp "$group" etc
cp "$sshrc" etc/ssh
echo "3 - Copying binaries"
cp -f /lib/ld-linux.so.2 lib 2>/dev/null || cp -f /lib64/ld-linux-x86-64.so.2 lib64
cp /lib/x86_64-linux-gnu/libnss* lib/x86_64-linux-gnu
2017-01-10 12:12:33 +01:00
for dbin in /bin/sh /bin/ls /bin/mkdir /bin/cat /bin/rm /bin/sed /usr/bin/rsync /usr/bin/lastlog /usr/bin/touch /usr/sbin/sshd /usr/lib/openssh/sftp-server; do
2017-01-10 12:12:33 +01:00
cp -f $dbin ${JAILDIR}/${jail}/$dbin;
for lib in $(ldd $dbin | grep -Eo "/.*so.[0-9\.]+"); do
cp -p $lib ${JAILDIR}/${jail}/$lib
done
done
}
## sub functions : functions call by subcommand
2016-12-18 15:45:15 +01:00
sub_init() {
2016-12-20 11:04:24 +01:00
jail=$1
sshd_config="${TPLDIR}/sshd_config"
inctpl="${TPLDIR}/inc.tpl"
[ -f "${LOCALTPLDIR}/sshd_config" ] && sshd_config="${LOCALTPLDIR}/sshd_config"
[ -f "${LOCALTPLDIR}/inc.tpl" ] && inctpl="${LOCALTPLDIR}/inc.tpl"
2017-01-10 12:12:33 +01:00
if ( check_jail $jail ); then
echo "Jail $jail already exist ! Use '$0 update $jail' for update it" >&2
exit 1
fi
2017-01-10 13:53:18 +01:00
2017-07-13 16:12:39 +02:00
echo "Create jail $jail :"
2017-07-19 14:02:49 +02:00
rootdir=$(dirname "$JAILDIR")
rootdir_inode=$(stat --format=%i "$rootdir")
2017-07-13 16:12:39 +02:00
jaildir_inode=$(stat --format=%i $JAILDIR)
if [ "$rootdir_inode" -eq 256 ] || [ "$jaildir_inode" -eq 256 ]; then
$BTRFS subvolume create ${JAILDIR}/${jail}
2017-01-09 12:22:52 +01:00
else
mkdir -p ${JAILDIR}/${jail}
fi
mk_jail $jail
echo "4 - Copie default sshd_config"
install -m 0640 $sshd_config ${JAILDIR}/$jail/${SSHD_CONFIG}
echo "5 - Set usable sshd port"
set_port $jail auto
echo "6 - Copie default inc configuration"
install -m 0640 $inctpl ${CONFDIR}/$jail
}
2016-12-20 11:04:24 +01:00
sub_update() {
jail=$1
2017-01-10 12:12:33 +01:00
if ! ( check_jail $jail ); then
echo "Jail $jail doesn't exist ! Use '$0 init $jail' for create it !" >&2
exit 1
fi
status=$(check_jail_on $jail)
if ( $status ); then
2017-01-10 12:12:33 +01:00
log stop $jail
2016-12-20 11:04:24 +01:00
fi
2017-01-10 13:53:18 +01:00
echo "Update jail $jail :"
mk_jail $jail
if ( $status ); then
2017-01-10 12:12:33 +01:00
log start $jail
fi
2016-12-18 15:45:15 +01:00
}
2017-01-09 14:00:49 +01:00
sub_remove() {
jail=$1
2017-01-10 12:12:33 +01:00
if ! ( check_jail $jail ); then
echo "Jail $jail doesn't exist !" >&2
exit 1
fi
if ( check_jail_on $jail ); then
log stop $jail
fi
echo "Delete jail $jail"
2017-07-13 16:12:39 +02:00
rm -f ${CONFDIR}/${jail}
jail_inode=$(stat --format=%i ${JAILDIR}/${jail})
if [ "$jail_inode" -eq 256 ]; then
$BTRFS subvolume delete ${JAILDIR}/${jail}
2017-01-09 14:00:49 +01:00
else
2017-01-10 13:53:18 +01:00
rm -rf ${JAILDIR}/${jail}
2017-07-13 16:12:39 +02:00
fi
if [ -d ${INCDIR}/${jail} ]; then
2017-07-19 14:02:49 +02:00
incs=$(ls ${INCDIR}/${jail})
for inc in $incs; do
2017-07-13 16:12:39 +02:00
inc_inode=$(stat --format=%i ${INCDIR}/${jail}/$inc)
if [ "$inc_inode" -eq 256 ]; then
$BTRFS subvolume delete ${INCDIR}/${jail}/${inc}
2017-07-13 16:12:39 +02:00
else
echo "You need to purge ${INCDIR}/${jail}/$inc manually !" >&2
fi
done
rmdir --ignore-fail-on-non-empty ${INCDIR}/${jail}
2017-01-09 14:00:49 +01:00
fi
2017-03-31 14:54:18 +02:00
set_firewall $jail
2017-01-09 14:00:49 +01:00
}
2016-12-18 15:45:15 +01:00
sub_start() {
2016-12-20 11:04:24 +01:00
jail=$1
2017-01-10 12:12:33 +01:00
if ! ( check_jail $jail ); then
echo "Jail $jail doesn't exist ! Create it with '$0 init $jail'" >&2
exit 1
fi
if ( check_jail_on $jail ); then
2016-12-20 11:04:24 +01:00
echo "Jail $jail already running !" >&2
2016-12-18 15:45:15 +01:00
exit 1
fi
2017-01-10 12:12:33 +01:00
echo "Start jail $jail"
cd "${JAILDIR}/${jail}"
2017-08-17 20:55:09 +02:00
grep -q "${JAILDIR}/${jail}/proc" /proc/mounts || mount -t proc "proc-${jail}" proc
grep -q "${JAILDIR}/${jail}/dev" /proc/mounts || mount -nt tmpfs "dev-${jail}" dev
[ -e "dev/console" ] || mknod -m 622 dev/console c 5 1
[ -e "dev/null" ] || mknod -m 666 dev/null c 1 3
[ -e "dev/zero" ] || mknod -m 666 dev/zero c 1 5
[ -e "dev/ptmx" ] || mknod -m 666 dev/ptmx c 5 2
[ -e "dev/tty" ] || mknod -m 666 dev/tty c 5 0
[ -e "dev/random" ] || mknod -m 444 dev/random c 1 8
[ -e "dev/urandom" ] || mknod -m 444 dev/urandom c 1 9
chown root:tty dev/console dev/ptmx dev/tty
2017-08-17 20:55:09 +02:00
ln -fs proc/self/fd dev/fd
ln -fs proc/self/fd/0 dev/stdin
ln -fs proc/self/fd/1 dev/stdout
ln -fs proc/self/fd/2 dev/stderr
ln -fs proc/kcore dev/core
mkdir -p dev/pts
mkdir -p dev/shm
grep -q "${JAILDIR}/${jail}/dev/pts" /proc/mounts || mount -t devpts -o gid=4,mode=620 none dev/pts
grep -q "${JAILDIR}/${jail}/dev/shm" /proc/mounts || mount -t tmpfs none dev/shm
2018-03-23 15:31:28 +01:00
chroot "${JAILDIR}/${jail}" /usr/sbin/sshd
2016-12-18 15:45:15 +01:00
}
sub_stop() {
2016-12-20 11:04:24 +01:00
jail=$1
2017-01-10 12:12:33 +01:00
if ! ( check_jail $jail ); then
echo "Jail $jail doesn't exist !" >&2
exit 1
fi
if ! ( check_jail_on $jail ); then
echo "Jail $jail doesnt't run !" >&2
2016-12-18 15:45:15 +01:00
exit 1
fi
2017-01-10 12:12:33 +01:00
echo "Stop jail $jail"
2016-12-20 11:04:24 +01:00
pid=$(cat ${JAILDIR}/${jail}/${SSHD_PID})
for conn in $(ps --ppid $pid -o pid=); do
2016-12-18 15:45:15 +01:00
kill $conn
done
kill $pid
2017-03-24 12:13:02 +01:00
umount --lazy --recursive ${JAILDIR}/${jail}/dev
umount --lazy ${JAILDIR}/${jail}/proc/
2016-12-18 15:45:15 +01:00
}
sub_reload() {
2016-12-20 11:04:24 +01:00
jail=$1
2017-01-10 12:12:33 +01:00
if ! ( check_jail $jail ); then
echo "Jail $jail doesn't exist !" >&2
exit 1
fi
if ! ( check_jail_on $jail ); then
echo "Jail $jail doesnt't run !" >&2
exit 1
2016-12-18 15:45:15 +01:00
fi
echo "Reload jail $jail"
2017-01-10 12:12:33 +01:00
pkill -HUP -F ${JAILDIR}/${jail}/${SSHD_PID}
2016-12-18 15:45:15 +01:00
}
sub_status() {
2016-12-20 11:04:24 +01:00
jail=$1
2017-01-10 12:12:33 +01:00
if ! ( check_jail $jail ); then
echo "Jail $jail doesn't exist ! Use '$0 status' for list all" >&2
exit 1
fi
2017-01-10 13:53:18 +01:00
inc=$(get_inc $jail)
if ( check_jail_on $jail ); then
status="ON "
else
status="OFF"
fi
2016-12-20 11:04:24 +01:00
port=$(get_port $jail)
ip=$(get_ip $jail|xargs|tr -s ' ' ',')
2017-01-10 18:16:15 +01:00
echo "$jail $status $port $inc $ip" | awk '{ printf("%- 30s %- 10s %- 10s %- 10s %- 40s\n", $1, $2, $3, $4, $5); }'
2016-12-18 15:45:15 +01:00
}
2017-01-10 12:12:33 +01:00
sub_params() {
jail=$1
params=$2
option=$3
if ! ( check_jail $jail ); then
echo "Jail $jail doesn't exist ! Create it with '$0 init $jail'" >&2
exit 1
fi
if [ -z "${option}" ]; then
get_${params} $jail
else
set_${params} $jail $option
2017-01-10 13:53:18 +01:00
echo "Update $jail : $params = $option"
2017-01-10 12:12:33 +01:00
fi
}
2016-12-20 14:31:20 +01:00
sub_sync() {
2017-01-10 12:12:33 +01:00
jail=$1
if ! ( check_jail $jail ); then
echo "Jail $jail doesn't exist !" >&2
exit 1
fi
2017-01-11 12:12:02 +01:00
if [ -z "${NODE}" ]; then
2017-01-11 17:10:56 +01:00
echo "You must define \$NODE in /etc/default/bkctld !" >&2
2016-12-20 14:31:20 +01:00
exit 1
fi
jail=$1
2017-02-02 10:20:05 +01:00
ssh $NODE bkctld init $jail >/dev/null
2017-08-17 16:05:38 +02:00
rsync -a ${JAILDIR}/${jail}/ ${NODE}:${JAILDIR}/${jail}/ --exclude proc/* --exclude sys/* --exclude dev/* --exclude run --exclude var/backup/*
2017-02-02 10:20:05 +01:00
rsync -a ${CONFDIR}/$jail ${NODE}:${CONFDIR}/$jail
2017-01-11 12:12:02 +01:00
if ( check_jail_on $jail ); then
2017-02-02 10:20:05 +01:00
ssh $NODE bkctld start $jail >/dev/null
2017-01-11 12:12:02 +01:00
fi
if [ -n "${FIREWALL_RULES}" ]; then
rsync -a ${FIREWALL_RULES} ${NODE}:${FIREWALL_RULES}
2017-02-01 16:31:12 +01:00
ssh $NODE /etc/init.d/minifirewall restart >/dev/null
2017-01-11 12:12:02 +01:00
fi
2016-12-20 14:31:20 +01:00
}
2016-12-18 15:45:15 +01:00
sub_inc() {
2016-12-20 11:04:24 +01:00
date=$(date +"%Y-%m-%d-%H")
2017-01-10 15:38:42 +01:00
incs_logs=""
2017-07-19 14:02:49 +02:00
jails=$(ls $JAILDIR)
for jail in $jails; do
2017-01-09 12:22:52 +01:00
inc="${INCDIR}/${jail}/${date}"
mkdir -p ${INCDIR}/${jail}
if [ ! -d "${inc}" ]; then
2017-01-12 11:23:35 +01:00
start=$(date +"%H:%M:%S")
2017-07-13 16:12:39 +02:00
jail_inode=$(stat --format=%i ${JAILDIR}/${jail})
if [ "$jail_inode" -eq 256 ]; then
$BTRFS subvolume snapshot -r ${JAILDIR}/${jail} $inc > /dev/null
2017-01-12 11:23:35 +01:00
else
cp -alx ${JAILDIR}/${jail}/ $inc
fi
end=$(date +"%H:%M:%S")
2017-07-19 14:02:49 +02:00
inc_log="Create $date inc of $jail (Start at $start / End at $end)"
2017-01-10 15:38:42 +01:00
echo "${inc_log}"
2017-07-19 14:02:49 +02:00
incs_logs="${incs_logs} ${inc_log}"
else
echo "Inc $date of $jail already exist !" >&2
2017-01-09 12:22:52 +01:00
fi
done
2017-01-18 10:33:02 +01:00
if [ -n "${NOTIF_MAIL}" ]; then
if [ -n "${incs_logs}" ]; then
echo "${incs_logs}" | mail -s "[info] EvoBackup - create incs" $NOTIF_MAIL
fi
2017-01-10 15:38:42 +01:00
fi
2016-12-18 15:45:15 +01:00
}
sub_rm() {
empty="/tmp/bkctld-${$}-$(date +%N))"
mkdir $empty
2017-01-10 15:26:58 +01:00
pidfile="/var/run/bkctld-rm.pid"
if [ -f "${pidfile}" ]; then
pid=$(cat $pidfile)
2017-01-12 12:27:16 +01:00
ps -u $pid >/dev/null
if [ $? -eq 0 ]; then
echo "$0 rm always run (PID $pid) !" >&2
kill -9 $pid
rm $pidfile
2017-01-18 10:33:02 +01:00
if [ -n "${NOTIF_MAIL}" ]; then
echo "$0 rm $pid was killed by $$ !" | mail -s "[warn] EvoBackup - purge incs interrupted" $NOTIF_MAIL
2017-01-12 12:27:16 +01:00
fi
else
rm $pidfile
2017-01-10 15:26:58 +01:00
fi
fi
echo $$ > $pidfile
2017-01-10 16:19:45 +01:00
rms_logs=""
2017-07-19 14:02:49 +02:00
jails=$(ls $JAILDIR)
for jail in $jails; do
incs=$(ls ${INCDIR}/$jail)
2017-01-09 16:17:39 +01:00
if [ -f ${CONFDIR}/$jail ]; then
keepfile="${CONFDIR}/.keep-${jail}"
while read j; do
date=$( echo "$j" | cut -d. -f1 )
before=$( echo "$j" | cut -d. -f2 )
date -d "$(date "$date") $before" "+%Y-%m-%d"
done < "${CONFDIR}/$jail" > "$keepfile"
for j in $(echo "${incs}" | grep -v -f "$keepfile"); do
start=$(date +"%H:%M:%S")
inc_inode=$(stat --format=%i "${INCDIR}/${jail}/${j}")
if [ "$inc_inode" -eq 256 ]; then
$BTRFS subvolume delete "${INCDIR}/${jail}/${j}" >/dev/null
else
cd "${INCDIR}/$jail"
rsync -a --delete "$empty/" "$j/"
rmdir "$j"
fi
end=$(date +"%H:%M:%S")
rm_log="Delete $j inc of $jail (Start at $start / End at $end)"
echo "${rm_log}"
rms_logs="${rms_logs} ${rm_log}"
done
2017-01-09 16:17:39 +01:00
fi
2017-01-10 16:19:45 +01:00
done
rmdir $empty
2017-01-10 15:26:58 +01:00
rm $pidfile
2017-01-18 10:33:02 +01:00
if [ -n "${NOTIF_MAIL}" ]; then
if [ -n "${rms_logs}" ]; then
echo "${rms_logs}" | mail -s "[info] EvoBackup - purge incs" $NOTIF_MAIL
fi
2017-01-10 16:19:45 +01:00
fi
2016-12-18 15:45:15 +01:00
}
2017-01-10 12:12:33 +01:00
## log function
2016-12-20 13:33:08 +01:00
log() {
subcommand=$1
logfile="${LOG_DIR}/bkctld.log"
shift
tty -s
if [ $? -eq 0 ]; then
2017-07-19 14:02:49 +02:00
sub_${subcommand} "$@" 2>&1 | tee -a $logfile
else
sub_${subcommand} "$@" >> $logfile 2>&1
fi
}
2017-01-10 12:12:33 +01:00
## main function : check usage and valid params
2016-12-20 11:04:24 +01:00
main() {
2017-07-19 14:02:49 +02:00
if [ "$(id -u)" -ne 0 ]; then
2017-01-09 14:16:41 +01:00
echo "Error, you need to be root to run $0 !" >&2
exit 1
fi
[ -f /etc/default/bkctld ] && . /etc/default/bkctld
[ -z "${CONFDIR}" ] && CONFDIR='/etc/evobackup'
[ -z "${JAILDIR}" ] && JAILDIR='/backup/jails'
[ -z "${INCDIR}" ] && INCDIR='/backup/incs'
2017-01-12 11:14:15 +01:00
[ -z "${TPLDIR}" ] && TPLDIR='/usr/share/bkctld'
[ -z "${LOCALTPLDIR}" ] && LOCALTPLDIR='/usr/local/share/bkctld'
2017-01-12 11:14:15 +01:00
[ -z "${LOG_DIR}" ] && LOG_DIR='/var/log'
2017-08-17 16:05:38 +02:00
[ -z "${SSHD_PID}" ] && SSHD_PID='/run/sshd.pid'
[ -z "${SSHD_CONFIG}" ] && SSHD_CONFIG='/etc/ssh/sshd_config'
[ -z "${AUTHORIZED_KEYS}" ] && AUTHORIZED_KEYS='/root/.ssh/authorized_keys'
BTRFS=$(which btrfs)
2016-12-20 11:04:24 +01:00
mkdir -p $CONFDIR $JAILDIR $INCDIR
subcommand=$1
jail=$2
option=$3
case $subcommand in
"" | "-h" | "--help")
2017-01-09 14:16:41 +01:00
usage
2016-12-20 11:04:24 +01:00
;;
"inc" | "rm")
2017-01-10 12:12:33 +01:00
log $subcommand
2016-12-20 11:04:24 +01:00
;;
"init")
2017-07-13 16:22:45 +02:00
if [ -n "${jail}" ]; then
2017-01-10 12:12:33 +01:00
log $subcommand $jail
else
usage
fi
2016-12-20 11:04:24 +01:00
;;
"key" | "port" | "ip")
2017-07-13 16:22:45 +02:00
if [ -n "${jail}" ]; then
2017-01-10 12:12:33 +01:00
log params $jail $subcommand $option
else
usage
2016-12-20 11:04:24 +01:00
fi
;;
2017-01-09 14:00:49 +01:00
"start" | "stop" | "reload" | "restart" | "sync" | "update" | "remove")
2017-07-13 16:22:45 +02:00
if [ -n "${jail}" ]; then
if [ "${jail}" = "all" ]; then
2017-07-19 14:02:49 +02:00
jails=$(ls $JAILDIR)
for jail in $jails; do
2017-01-10 12:12:33 +01:00
case $subcommand in
"start")
if ! ( check_jail_on $jail ); then
log $subcommand $jail
fi
;;
"stop" | "reload")
if ( check_jail_on $jail ); then
log $subcommand $jail
fi
;;
2017-01-12 11:32:02 +01:00
"restart")
if ( check_jail_on $jail ); then
log stop $jail
fi
log start $jail
;;
2017-01-10 12:12:33 +01:00
*)
log $subcommand $jail
;;
esac
done
else
2017-01-10 12:12:33 +01:00
if [ "${subcommand}" != "restart" ]; then
log $subcommand $jail
else
if ( check_jail_on $jail ); then
log stop $jail
fi
log start $jail
fi
fi
2017-01-10 12:12:33 +01:00
else
usage
2016-12-20 11:04:24 +01:00
fi
;;
"status")
2017-07-13 16:22:45 +02:00
if [ -z "${jail}" ]; then
2017-07-19 14:02:49 +02:00
jails=$(ls $JAILDIR)
for jail in $jails; do
2017-01-10 12:12:33 +01:00
sub_$subcommand $jail
2016-12-20 11:04:24 +01:00
done
else
2017-07-19 14:02:49 +02:00
sub_$subcommand $jail
2016-12-20 11:04:24 +01:00
fi
;;
*)
shift
2017-01-10 12:12:33 +01:00
echo "Error: '${subcommand}' is not a known subcommand." >&2
2017-01-09 14:16:41 +01:00
usage
2016-12-20 11:04:24 +01:00
exit 1
;;
esac
}
main "$@"