2017-10-19 17:41:41 +00:00
|
|
|
#!/bin/bash
|
2019-04-04 23:19:14 +00:00
|
|
|
# Start Script for docker-borgserver
|
2017-10-19 17:41:41 +00:00
|
|
|
|
2019-12-01 16:56:05 +00:00
|
|
|
PUID=${PUID:-1000}
|
|
|
|
PGID=${PGID:-1000}
|
|
|
|
|
|
|
|
usermod -o -u "$PUID" borg &>/dev/null
|
|
|
|
groupmod -o -g "$PGID" borg &>/dev/null
|
|
|
|
|
2017-10-19 17:41:41 +00:00
|
|
|
BORG_DATA_DIR=/backup
|
|
|
|
SSH_KEY_DIR=/sshkeys
|
2019-04-04 23:19:14 +00:00
|
|
|
BORG_CMD='cd ${BORG_DATA_DIR}/${client_name}; borg serve --restrict-to-path ${BORG_DATA_DIR}/${client_name} ${BORG_SERVE_ARGS}'
|
2019-12-01 16:56:05 +00:00
|
|
|
AUTHORIZED_KEYS_PATH=/home/borg/.ssh/authorized_keys
|
2017-10-19 17:41:41 +00:00
|
|
|
|
2019-04-04 23:19:14 +00:00
|
|
|
# Append only mode?
|
|
|
|
BORG_APPEND_ONLY=${BORG_APPEND_ONLY:=no}
|
2017-11-01 16:51:51 +00:00
|
|
|
|
2022-01-21 09:33:54 +00:00
|
|
|
source /etc/os-release
|
2017-10-19 17:41:41 +00:00
|
|
|
echo "########################################################"
|
2019-04-04 23:19:14 +00:00
|
|
|
echo -n " * Docker BorgServer powered by "
|
|
|
|
borg -V
|
2022-01-21 09:33:54 +00:00
|
|
|
echo " * Based on ${PRETTY_NAME}"
|
2019-04-04 23:19:14 +00:00
|
|
|
echo "########################################################"
|
2019-12-05 15:45:31 +00:00
|
|
|
echo " * User id: $(id -u borg)"
|
|
|
|
echo " * Group id: $(id -g borg)"
|
|
|
|
echo "########################################################"
|
|
|
|
|
2019-04-04 23:19:14 +00:00
|
|
|
|
|
|
|
# Precheck if BORG_ADMIN is set
|
|
|
|
if [ "${BORG_APPEND_ONLY}" == "yes" ] && [ -z "${BORG_ADMIN}" ] ; then
|
|
|
|
echo "WARNING: BORG_APPEND_ONLY is active, but no BORG_ADMIN was specified!"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Precheck directories & client ssh-keys
|
2017-10-19 17:41:41 +00:00
|
|
|
for dir in BORG_DATA_DIR SSH_KEY_DIR ; do
|
2019-04-04 23:19:14 +00:00
|
|
|
dirpath=$(eval echo '$'${dir})
|
|
|
|
echo " * Testing Volume ${dir}: ${dirpath}"
|
|
|
|
if [ ! -d "${dirpath}" ] ; then
|
|
|
|
echo "ERROR: ${dirpath} is no directory!"
|
2017-10-19 17:41:41 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2019-07-05 11:20:24 +00:00
|
|
|
if [ "$(find ${SSH_KEY_DIR}/clients ! -regex '.*/\..*' -a -type f | wc -l)" == "0" ] ; then
|
2019-04-04 23:19:14 +00:00
|
|
|
echo "ERROR: No SSH-Pubkey file found in ${SSH_KEY_DIR}"
|
2017-10-19 17:41:41 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
done
|
2017-10-20 04:45:14 +00:00
|
|
|
|
2019-04-04 23:19:14 +00:00
|
|
|
# Create SSH-Host-Keys on persistent storage, if not exist
|
2017-10-20 04:45:14 +00:00
|
|
|
mkdir -p ${SSH_KEY_DIR}/host 2>/dev/null
|
|
|
|
echo " * Checking / Preparing SSH Host-Keys..."
|
2019-04-04 23:19:14 +00:00
|
|
|
for keytype in ed25519 rsa ; do
|
|
|
|
if [ ! -f "${SSH_KEY_DIR}/host/ssh_host_${keytype}_key" ] ; then
|
|
|
|
echo " ** Creating SSH Hostkey [${keytype}]..."
|
|
|
|
ssh-keygen -q -f "${SSH_KEY_DIR}/host/ssh_host_${keytype}_key" -N '' -t ${keytype}
|
2017-10-20 04:45:14 +00:00
|
|
|
fi
|
|
|
|
done
|
2017-10-19 17:41:41 +00:00
|
|
|
|
2019-04-04 23:19:14 +00:00
|
|
|
echo "########################################################"
|
2017-10-20 04:45:14 +00:00
|
|
|
echo " * Starting SSH-Key import..."
|
2019-04-04 23:19:14 +00:00
|
|
|
|
|
|
|
# Add every key to borg-users authorized_keys
|
2019-12-01 16:56:05 +00:00
|
|
|
rm ${AUTHORIZED_KEYS_PATH} &>/dev/null
|
2019-07-05 11:20:24 +00:00
|
|
|
for keyfile in $(find "${SSH_KEY_DIR}/clients" ! -regex '.*/\..*' -a -type f); do
|
2019-04-04 23:19:14 +00:00
|
|
|
client_name=$(basename ${keyfile})
|
2017-10-19 17:41:41 +00:00
|
|
|
mkdir ${BORG_DATA_DIR}/${client_name} 2>/dev/null
|
2019-04-04 23:19:14 +00:00
|
|
|
echo " ** Adding client ${client_name} with repo path ${BORG_DATA_DIR}/${client_name}"
|
|
|
|
|
|
|
|
# If client is $BORG_ADMIN unset $client_name, so path restriction equals $BORG_DATA_DIR
|
|
|
|
# Otherwise add --append-only, if enabled
|
|
|
|
borg_cmd=${BORG_CMD}
|
|
|
|
if [ "${client_name}" == "${BORG_ADMIN}" ] ; then
|
|
|
|
echo " ** Client '${client_name}' is BORG_ADMIN! **"
|
|
|
|
unset client_name
|
|
|
|
elif [ "${BORG_APPEND_ONLY}" == "yes" ] ; then
|
|
|
|
borg_cmd="${BORG_CMD} --append-only"
|
|
|
|
fi
|
|
|
|
|
2022-02-05 17:12:37 +00:00
|
|
|
echo -n "restrict,command=\"$(eval echo -n \"${borg_cmd}\")\" " >> ${AUTHORIZED_KEYS_PATH}
|
|
|
|
cat ${keyfile} >> ${AUTHORIZED_KEYS_PATH}
|
|
|
|
echo >> ${AUTHORIZED_KEYS_PATH}
|
2017-10-19 17:41:41 +00:00
|
|
|
done
|
2022-01-21 09:33:54 +00:00
|
|
|
chmod 0600 "${AUTHORIZED_KEYS_PATH}"
|
2017-10-19 17:41:41 +00:00
|
|
|
|
2019-12-01 16:58:29 +00:00
|
|
|
echo " * Validating structure of generated ${AUTHORIZED_KEYS_PATH}..."
|
|
|
|
ERROR=$(ssh-keygen -lf ${AUTHORIZED_KEYS_PATH} 2>&1 >/dev/null)
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
echo "ERROR: ${ERROR}"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2019-12-01 16:56:05 +00:00
|
|
|
chown -R borg:borg ${BORG_DATA_DIR}
|
|
|
|
chown borg:borg ${AUTHORIZED_KEYS_PATH}
|
|
|
|
chmod 600 ${AUTHORIZED_KEYS_PATH}
|
2017-10-19 17:41:41 +00:00
|
|
|
|
|
|
|
echo "########################################################"
|
2019-04-04 23:19:14 +00:00
|
|
|
echo " * Init done! Starting SSH-Daemon..."
|
2017-10-19 17:41:41 +00:00
|
|
|
|
|
|
|
/usr/sbin/sshd -D -e
|