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.

70 lines
1.6 KiB

  1. #!/bin/bash
  2. [ "$UID" != "0" ] && echo "You must be root." 2>&1 && exit 1
  3. ##
  4. ## code
  5. ##
  6. user=rsync
  7. src="$1"
  8. if [ -z "$src" ]; then
  9. echo "You must provide a source directory as first argument." >&2
  10. exit 1
  11. fi
  12. if ! [ -d "$src" ]; then
  13. echo "Folder '$src' not found, please provide a valid directory as first argument." >&2
  14. exit 1
  15. fi
  16. dest="$2"
  17. if [ -z "$dest" ]; then
  18. echo "You must provide a host as target." >&2
  19. exit 1
  20. fi
  21. rsync_options=(${RSYNC_OPTIONS:-})
  22. ssh_options=(${SSH_OPTIONS:--o StrictHostKeyChecking=no})
  23. if echo "$dest" | grep "/" >/dev/null 2>&1; then
  24. rsync_options=("--bwlimit" "$(echo "$dest" | cut -f 2 -d "/")" "${rsync_options[@]}")
  25. dest="$(echo "$dest" | cut -f 1 -d "/")"
  26. fi
  27. if echo "$dest" | grep ":" >/dev/null 2>&1; then
  28. ssh_options=("-p" "$(echo "$dest" | cut -f 2 -d ":")" "${ssh_options[@]}")
  29. dest="$(echo "$dest" | cut -f 1 -d ":")"
  30. fi
  31. hostname=$(hostname)
  32. hostname=${LABEL_HOSTNAME:-$hostname}
  33. dest_path="/var/mirror/$hostname"
  34. touch /etc/rsync/exclude-patterns
  35. touch /etc/rsync/include-patterns
  36. if ! [ -s "/etc/rsync/include-patterns" ]; then
  37. echo "Nothing to do as /etc/rsync/include-patterns is empty."
  38. exit 0
  39. fi
  40. cmd=(/usr/bin/rsync "${rsync_options[@]}" -azvA
  41. -e "sudo -u $user ssh ${ssh_options[*]}"
  42. --include-from /etc/rsync/include-patterns
  43. --exclude-from /etc/rsync/exclude-patterns
  44. --delete --partial --partial-dir .rsync-partial
  45. --numeric-ids "$src/" "$user@$dest":"$dest_path")
  46. rsync_uid_gid=$(stat -c "%u:%g" "/var/lib/rsync")
  47. chown "$rsync_uid_gid" "/var/lib/rsync/.ssh" -R
  48. echo "${cmd[@]}"
  49. exec "${cmd[@]}"