#!/bin/bash
# Dylan Blanqué - BR Consulting S.R.L. 2023
# Remember to use rsync as the default Xfer for Linux Clients
# Also, change the user in the sshArgs to backuppc and add sudo to the
# rsync client path
# Example:
#       - RsyncClientPath: sudo /usr/bin/rsync
#       - RsyncSshArgs: $sshPath -l backuppc
 
backuppc_server_ssh_key=""
backuppc_server_ip=""
sshd_config_path="/etc/ssh/sshd_config"
 
apt install sudo rsync -y
adduser backuppc --disabled-password
 
if [ $? -ne 0 ] ; then
    deluser backuppc
    adduser backuppc
    passwd -l backuppc # Disables password login in a different way
fi
 
touch /etc/sudoers.d/backuppc
echo -e "backuppc ALL=NOPASSWD: /usr/bin/rsync --server --sender * \n" > /etc/sudoers.d/backuppc
chmod 440 /etc/sudoers.d/backuppc
visudo -c
 
mkdir -p /home/backuppc/.ssh/
chmod 700 /home/backuppc/.ssh
chmod 600 /home/backuppc/.ssh/*
echo "$backuppc_server_ssh_key" >> /home/backuppc/.ssh/authorized_keys
ssh-keyscan "$backuppc_server_ip" >> /home/backuppc/.ssh/known_hosts
chown -R backuppc:backuppc /home/backuppc/.ssh
 
if [[ $(grep "#AuthorizedKeysFile" "$sshd_config_path") ]]; then
    echo "Enabled the AuthorizedKeysFile parameter in $sshd_config_path"
    sed -i "s/^#\(AuthorizedKeysFile.*\)$/\1/g" "$sshd_config_path"
fi
 
exit 0