#!/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 || 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 /etc/mirror-dir/config.yml default: sources: - /etc EOF fi cat < /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