lftp-mirror 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/bin/sh
  2. set -e
  3. usage() {
  4. cat <<EOH
  5. USAGE: $(basename "$0") [OPTIONS]
  6. Mirror specified remote directory to local directory using lftp
  7. Options:
  8. -l Local directory (default: '$LOCAL_DIR')
  9. -r Remote directory (default: '$REMOTE_DIR')
  10. -R Reverse mirroring (default: $REVERSE)
  11. -u FTP username (default: '$USERNAME')
  12. -p FTP password (default: '$PASSWORD')
  13. -P FTP port (default: '$PORT')
  14. -n Dry-run mode (default: $DRYRUN)
  15. -v Verbose mode (default: $VERBOSE)
  16. EOH
  17. }
  18. LOCAL_DIR=
  19. REMOTE_DIR=
  20. REVERSE=0
  21. USERNAME="anonymous"
  22. PASSWORD="${USER}@$(dnsdomainname)"
  23. PORT=21
  24. DRYRUN=0
  25. VERBOSE=0
  26. while getopts "l:r:u:p:P:nvRh" OPT ; do
  27. case "$OPT" in
  28. \?|h)
  29. usage
  30. exit 0
  31. ;;
  32. l)
  33. LOCAL_DIR="$OPTARG"
  34. ;;
  35. r)
  36. REMOTE_DIR="$OPTARG"
  37. ;;
  38. R)
  39. REVERSE=1
  40. ;;
  41. u)
  42. USERNAME="$OPTARG"
  43. ;;
  44. p)
  45. PASSWORD="$OPTARG"
  46. ;;
  47. P)
  48. PORT="$OPTARG"
  49. ;;
  50. n)
  51. DRYRUN=1
  52. ;;
  53. v)
  54. VERBOSE=1
  55. ;;
  56. esac
  57. done
  58. if [ -z "$LOCAL_DIR" ]; then
  59. printf "ERR: Local path is not defined (option -l)\n"
  60. usage
  61. exit 1
  62. fi
  63. if [ -z "$REMOTE_DIR" ]; then
  64. printf "ERR: Remote path is not defined (option -r)\n"
  65. usage
  66. exit 1
  67. fi
  68. if [ "$REVERSE" = 0 ] && [ ! -d "$LOCAL_DIR" ]; then
  69. mkdir -p "$LOCAL_DIR"
  70. fi
  71. MIRROR_OPTIONS="--delete-first --reverse --only-newer --no-perms --no-umask --ignore-time"
  72. if [ "$REVERSE" = 1 ]; then
  73. MIRROR_OPTIONS="$MIRROR_OPTIONS --reverse"
  74. fi
  75. if [ "$DRYRUN" = 1 ]; then
  76. MIRROR_OPTIONS="$MIRROR_OPTIONS --dry-run"
  77. fi
  78. if [ "$VERBOSE" = 1 ]; then
  79. MIRROR_OPTIONS="$MIRROR_OPTIONS --verbose"
  80. fi
  81. lftp -c "\
  82. set ftp:list-options -a; \
  83. lcd $LOCAL_DIR ; \
  84. open -u $USERNAME,$PASSWORD -p $PORT $REMOTE_DIR ; \
  85. mirror $MIRROR_OPTIONS \
  86. "
  87. exit 0