summaryrefslogtreecommitdiff
path: root/localsyncthis
blob: 680f0c920321172e325369a9ca416e7d5ebb9deb (plain)
  1. #!/bin/sh -x
  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. case $action in
  77. download)
  78. rsync --rsh="ssh $ssh_opts" $rsync_opts "$host:'$thatdir/'" "'$thisdir'"
  79. [ $V ] && echo rsync --rsh="ssh $ssh_opts" $rsync_opts "$host:'$thatdir/'" "'$thisdir'"
  80. ;;
  81. upload)
  82. rsync --rsh="ssh $ssh_opts" $rsync_opts "'$thisdir/'" "$host:'$thatdir'"
  83. [ $V ] && echo rsync --rsh="ssh $ssh_opts" $rsync_opts "'$thisdir/'" "$host:'$thatdir'"
  84. ;;
  85. *) echo "$prg error: You need to specify either --upload or --download!"
  86. echo
  87. $0 --help
  88. exit 1
  89. ;;
  90. esac
  91. done
  92. $workdir="$1"
  93. [ -n "$1" ] && shift
  94. done
  95. exit 0