forked from 0k/0k-charms
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
1.4 KiB
68 lines
1.4 KiB
#!/bin/bash
|
|
|
|
|
|
set -eux
|
|
|
|
|
|
[ "${DOMAIN}" ] || {
|
|
echo "Error: you must set \$DOMAIN prior to running this script." >&2
|
|
exit 1
|
|
}
|
|
|
|
[ "${BACKUP_SERVER}" ] || {
|
|
echo "Error: you must set \$BACKUP_SERVER prior to running this script." >&2
|
|
exit 1
|
|
}
|
|
|
|
|
|
## rsync
|
|
type -p rsync >/dev/null 2>&1 || apt-get install -y rsync </dev/null
|
|
|
|
## creating rsync user
|
|
mkdir -p /var/lib/rsync
|
|
|
|
getent group rsync >/dev/null ||
|
|
groupadd -r rsync
|
|
|
|
getent passwd rsync >/dev/null ||
|
|
useradd -r rsync -d /var/lib/rsync -g rsync
|
|
|
|
chown rsync:rsync /var/lib/rsync
|
|
|
|
## rsync ssh key creation
|
|
[ -e /var/lib/rsync/.ssh/id_rsa ] ||
|
|
su -c 'ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa -q -C rsync@'"$DOMAIN" - rsync
|
|
|
|
dest="$BACKUP_SERVER"
|
|
if [[ "$dest" == *"/"* ]]; then
|
|
dest="${dest%/*}"
|
|
fi
|
|
|
|
if [[ "$dest" == *":"* ]]; then
|
|
ssh_options+=("-p" "${dest#*:}")
|
|
dest="${dest%%:*}"
|
|
fi
|
|
|
|
ssh-keyscan "${ssh_options[@]}" -H "${dest}" > /var/lib/rsync/.ssh/known_hosts
|
|
|
|
apt-get install kal-shlib-process </dev/null
|
|
ln -sf "$PWD/resources/bin/mirror-dir" /usr/local/sbin/mirror-dir
|
|
|
|
|
|
|
|
if ! [ -e /etc/mirror-dir/config.yml ]; then
|
|
mkdir -p /etc/mirror-dir
|
|
cat <<EOF > /etc/mirror-dir/config.yml
|
|
default:
|
|
sources:
|
|
- /etc
|
|
EOF
|
|
fi
|
|
|
|
cat <<EOF > /etc/cron.d/mirror-dir
|
|
SHELL=/bin/bash
|
|
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
|
|
|
|
$((RANDOM % 60)) * * * * root mirror-dir -h "$DOMAIN" -d "$BACKUP_SERVER" -u rsync 2>&1 | logger -t mirror-dir
|
|
|
|
EOF
|