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.

80 lines
1.9 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. MIRROR_DIR_PATH="${MIRROR_DIR_PATH:-$PWD/resources/bin/mirror-dir}"
  12. [ -e "$MIRROR_DIR_PATH" ] || {
  13. echo "Error: you must set \$MIRROR_DIR_PATH or be the root of the charm to run this script." >&2
  14. exit 1
  15. }
  16. ## rsync
  17. type -p rsync >/dev/null 2>&1 || apt-get install -y rsync </dev/null
  18. ## creating rsync user
  19. mkdir -p /var/lib/rsync
  20. getent group rsync >/dev/null ||
  21. groupadd -r rsync
  22. getent passwd rsync >/dev/null ||
  23. useradd -r rsync -d /var/lib/rsync -g rsync
  24. chown rsync:rsync /var/lib/rsync
  25. ## rsync ssh key creation
  26. [ -e /var/lib/rsync/.ssh/id_rsa ] ||
  27. su -c 'ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa -q -C rsync@'"$DOMAIN" - rsync
  28. dest="$BACKUP_SERVER"
  29. if [[ "$dest" == *"/"* ]]; then
  30. dest="${dest%/*}"
  31. fi
  32. if [[ "$dest" == *":"* ]]; then
  33. ssh_options+=("-p" "${dest#*:}")
  34. dest="${dest%%:*}"
  35. fi
  36. ssh-keyscan "${ssh_options[@]}" -H "${dest}" > /var/lib/rsync/.ssh/known_hosts
  37. apt-get install -y kal-shlib-{common,process,cmdline,array} </dev/null
  38. case $(awk -Wversion 2>/dev/null || awk --version) in
  39. "mawk 1.3.3"*)
  40. ## Not good, it is from 1996, and we still find it on Debian 10
  41. apt-get install -y gawk </dev/null
  42. ;;
  43. esac
  44. ln -sf "$PWD/resources/bin/mirror-dir" /usr/local/sbin/mirror-dir
  45. if ! [ -e /etc/mirror-dir/config.yml ]; then
  46. mkdir -p /etc/mirror-dir
  47. cat <<EOF > /etc/mirror-dir/config.yml
  48. default:
  49. sources:
  50. - /etc
  51. EOF
  52. fi
  53. cat <<EOF > /etc/cron.d/mirror-dir
  54. SHELL=/bin/bash
  55. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  56. $((RANDOM % 60)) $((RANDOM % 4))-23/4 * * * root mirror-dir backup -q -h "$DOMAIN" -d "$BACKUP_SERVER"
  57. EOF