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

  1. #!/bin/bash
  2. set -eux
  3. [ "${DOMAIN}" ] || {
  4. echo "Error: you must set \$DOMAIN prior to running this script." >&2
  5. exit 1
  6. }
  7. [ "${BACKUP_SERVER}" ] || {
  8. echo "Error: you must set \$BACKUP_SERVER prior to running this script." >&2
  9. exit 1
  10. }
  11. ## rsync
  12. type -p rsync >/dev/null 2>&1 || apt-get install -y rsync </dev/null
  13. ## creating rsync user
  14. mkdir -p /var/lib/rsync
  15. getent group rsync >/dev/null ||
  16. groupadd -r rsync
  17. getent passwd rsync >/dev/null ||
  18. useradd -r rsync -d /var/lib/rsync -g rsync
  19. chown rsync:rsync /var/lib/rsync
  20. ## rsync ssh key creation
  21. [ -e /var/lib/rsync/.ssh/id_rsa ] ||
  22. su -c 'ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa -q -C rsync@'"$DOMAIN" - rsync
  23. dest="$BACKUP_SERVER"
  24. if [[ "$dest" == *"/"* ]]; then
  25. dest="${dest%/*}"
  26. fi
  27. if [[ "$dest" == *":"* ]]; then
  28. ssh_options+=("-p" "${dest#*:}")
  29. dest="${dest%%:*}"
  30. fi
  31. ssh-keyscan "${ssh_options[@]}" -H "${dest}" > /var/lib/rsync/.ssh/known_hosts
  32. apt-get install kal-shlib-process </dev/null
  33. ln -sf "$PWD/resources/bin/mirror-dir" /usr/local/sbin/mirror-dir
  34. if ! [ -e /etc/mirror-dir/config.yml ]; then
  35. mkdir -p /etc/mirror-dir
  36. cat <<EOF > /etc/mirror-dir/config.yml
  37. default:
  38. sources:
  39. - /etc
  40. EOF
  41. fi
  42. cat <<EOF > /etc/cron.d/mirror-dir
  43. SHELL=/bin/bash
  44. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  45. $((RANDOM % 60)) * * * * root mirror-dir -h "$DOMAIN" -d "$BACKUP_SERVER" -u rsync 2>&1 | logger -t mirror-dir
  46. EOF