rsync-staged 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #! /bin/bash
  2. # Copyright (C) 2017, Ansgar Burchardt <ansgar@debian.org>
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License as
  6. # published by the Free Software Foundation; version 2.
  7. #
  8. # This program is distributed in the hope that it will be useful, but
  9. # WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. # General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software
  15. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. set -e
  17. set -u
  18. usage() {
  19. cat >&2 <<-EOF
  20. usage: $0 <stagedir> <targetdir> -- <rsync-options...>
  21. Handle incoming rsync to <stagedir>. Once all files are synced,
  22. move them to <targetdir> (with *.changes moved last).
  23. EOF
  24. exit ${1:-0}
  25. }
  26. if [ $# -lt 3 ]; then
  27. usage
  28. fi
  29. stagedir="${1}"
  30. targetdir="${2}"
  31. if [[ ! -d "${stagedir}" ]]; then
  32. echo >&2 "E: staging directory ${stagedir} is not a directory"
  33. exit 1
  34. fi
  35. if [[ ! -d "${targetdir}" ]]; then
  36. echo >&2 "E: target directory ${targetdir} is not a directory"
  37. exit 1
  38. fi
  39. if [[ "${3}" != "--" ]]; then
  40. echo >&2 "E: unexpected third parameter (${3})"
  41. exit 1
  42. fi
  43. shift 3
  44. rsync "${@}" . "${stagedir}" || exit $?
  45. find_common=(-maxdepth 1 -type f)
  46. find_exec=(-execdir mv --no-clobber -t "${targetdir}" -- "{}" +)
  47. find "${stagedir}" "${find_common[@]}" "!" -name "*.changes" "${find_exec[@]}"
  48. find "${stagedir}" "${find_common[@]}" -name "*.changes" "${find_exec[@]}"