summaryrefslogtreecommitdiff
path: root/localsyncthis
blob: 5960b2c9d432632cf22947f335daba7a2eed0b8c (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='-avrtz --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. --download) action=download;;
  42. --upload) action=upload;;
  43. --test|--dry-run) rsync_opts="$rsync_opts --dry-run";;
  44. --force|--whole-file) rsync_opts="$rsync_opts --whole-file";;
  45. --host) hosts="$hosts $2"; shift;;
  46. -l|--localdir) localdir="$2"; shift;;
  47. -r|--remotedir) remotedir="$2"; shift;;
  48. -p|--port) ssh_opts="$ssh_opts -p $2"; shift;;
  49. -b|--batch) ssh_opts="$ssh_opts -o BatchMode=yes";;
  50. --ssh-opts) ssh_opts="$ssh_opts $2"; shift;;
  51. -*) rsync_opts="$rsync_opts $1";;
  52. *) args="$args $1";;
  53. esac
  54. shift
  55. done
  56. set -- $args
  57. if [ -z $hosts ]; then
  58. hosts=$1
  59. shift
  60. fi
  61. if [ -z $hosts ]; then
  62. echo "$prg error: Hostname missing!"
  63. echo
  64. $0 --help
  65. exit 1
  66. fi
  67. if [ "x$1" = "x" ]; then
  68. workdirs=`pwd`
  69. localdir=''
  70. else
  71. workdirs=$@
  72. fi
  73. for workdir in $workdirs; do
  74. for host in $hosts; do
  75. here="$localdir$workdir"
  76. there="$host:$remotedir$workdir"
  77. case $action in
  78. download)
  79. rsync --rsh="ssh $ssh_opts" $rsync_opts $there/ $here
  80. [ $V ] && echo rsync --rsh="ssh $ssh_opts" $rsync_opts $there/ $here
  81. ;;
  82. upload)
  83. rsync --rsh="ssh $ssh_opts" $rsync_opts $here/ $there
  84. [ $V ] && echo rsync --rsh="ssh $ssh_opts" $rsync_opts $here/ $there
  85. ;;
  86. *) echo "$prg error: You need to specify either --upload or --download!"
  87. echo
  88. $0 --help
  89. exit 1
  90. ;;
  91. esac
  92. done
  93. done
  94. exit 0