summaryrefslogtreecommitdiff
path: root/localsyncthis
blob: 99b68f391a1150621a767f92c82d3a768a841cfd (plain)
  1. #!/bin/sh
  2. set -e
  3. # Initial setup
  4. prg=$(basename $0)
  5. copyright="(C) 2000-2002 Jonas Smedegaard <dr@jones.dk>"
  6. V=''
  7. action=''
  8. hosts=''
  9. rsync_opts='-aHz --delete --force'
  10. ssh_opts=''
  11. localdir=''
  12. remotedir=''
  13. args=''
  14. while [ $# -gt 0 ]; do
  15. case $1 in
  16. -h|--help) echo "$prg, $copyright
  17. usage: $prg [<options>] host [path [path...]]
  18. or: $prg [<options>] \"host [host...]\" [path [path...]]
  19. and options are the following:
  20. --download Make local directory equal to the remote one
  21. --upload Make remote directory equal to the local one
  22. --test Simulate, don't actually change anything
  23. --dry-run (Alias for --test)
  24. --force Always copy whole files (disable rsync feature)
  25. --whole-file (Alias for --force)
  26. -v|--verbose Verbose mode
  27. --host Hostname(s) of remote host(s)
  28. -l|--localdir Local base directory (only if path specified)
  29. -r|--remotedir Remote base directory
  30. -p|--port ssh port number
  31. -b|--batch Enable BatchMode (for use in automatic scripts)
  32. --ssh-opts Arbitrary ssh options
  33. -h|--help This help text
  34. -* Arbitrary rsync options
  35. One of either --download or --upload is required. Last occurence
  36. supersedes earlier ones.
  37. If no path specified, current working directory is used."
  38. exit 0
  39. ;;
  40. # -v|--verbose) V=1; rsync_opts="$rsync_opts --progress --stats";;
  41. -v|--verbose) V=1; rsync_opts="$rsync_opts -v";;
  42. --download) action=download;;
  43. --upload) action=upload;;
  44. --test|--dry-run) rsync_opts="$rsync_opts --dry-run";;
  45. --force|--whole-file) rsync_opts="$rsync_opts --whole-file";;
  46. --host) hosts="$hosts $2"; shift;;
  47. -l|--localdir) localdir="$2"; shift;;
  48. -r|--remotedir) remotedir="$2"; shift;;
  49. -p|--port) ssh_opts="$ssh_opts -p $2"; shift;;
  50. -b|--batch) ssh_opts="$ssh_opts -o BatchMode=yes";;
  51. --ssh-opts) ssh_opts="$ssh_opts $2"; shift;;
  52. -*) rsync_opts="$rsync_opts $1";;
  53. *) args="$args $1";;
  54. esac
  55. shift
  56. done
  57. set -- $args
  58. if [ -z $hosts ]; then
  59. hosts=$1
  60. shift
  61. fi
  62. if [ -z $hosts ]; then
  63. echo "$prg error: Hostname missing!"
  64. echo
  65. $0 --help
  66. exit 1
  67. fi
  68. if [ -z "$1" ]; then
  69. workdir="`pwd`"
  70. localdir=''
  71. fi
  72. while [ -n "$workdir" ]; do
  73. for host in $hosts; do
  74. thisdir="$localdir$workdir"
  75. thatdir="$remotedir$workdir"
  76. shellmagic=""
  77. if [ -z "$localdir" ] && echo "$host" | grep -q '@'; then
  78. relativeworkdir=$(echo "$workdir" | sed "s|^$HOME/\?||")
  79. if [ "$workdir" != "$relativeworkdir" ]; then
  80. thisdir="$workdir"
  81. thatdir="$relativeworkdir"
  82. shellmagic="~/"
  83. fi
  84. fi
  85. case $action in
  86. download)
  87. [ $V ] && echo rsync --rsh="ssh $ssh_opts" $rsync_opts "$host:$shellmagic'$thatdir/'" "$thisdir"
  88. rsync --rsh="ssh $ssh_opts" $rsync_opts "$host:$shellmagic'$thatdir/'" "$thisdir"
  89. ;;
  90. upload)
  91. [ $V ] && echo rsync --rsh="ssh $ssh_opts" $rsync_opts "$thisdir/" "$host:$shellmagic'$thatdir'"
  92. rsync --rsh="ssh $ssh_opts" $rsync_opts "$thisdir/" "$host:$shellmagic'$thatdir'"
  93. ;;
  94. *) echo "$prg error: You need to specify either --upload or --download!"
  95. echo
  96. $0 --help
  97. exit 1
  98. ;;
  99. esac
  100. done
  101. workdir="$1"
  102. [ -n "$1" ] && shift
  103. done
  104. exit 0