sync_parallel_curl.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/bin/sh
  2. #
  3. # $Id: sync_parallel_curl.sh,v 1.2 2020/11/16 00:40:06 gilles Exp gilles $
  4. # Example for imapsync massive migration on Unix systems.
  5. # See also http://imapsync.lamiral.info/FAQ.d/FAQ.Massive.txt
  6. #
  7. # Data is supposed to be in file.txt in the following format:
  8. # host001_1;user001_1;password001_1;host001_2;user001_2;password001_2;
  9. # ...
  10. # Separator is character semi-colon ";" it can be changed by any character changing IFS=';'
  11. # in the while loop below.
  12. # # Each line contains 6 columns, columns are parameter values for
  13. # --host1 --user1 --password1 --host2 --user2 --password2
  14. #
  15. # Extra columns can be used to pass extra parameters but the script reading
  16. # this file have to read them into some variables.
  17. #
  18. # Last, don't forget the last semicolon.
  19. #
  20. # You can add extra options after the last line
  21. # Use character backslash \ at the end of each supplementary line, except for the last one.
  22. # The credentials filename "file.txt" used for the loop can be renamed
  23. # by changing "file.txt" below.
  24. # Now I explain what come next, the actual stuff, which is barely
  25. # a single long command line written on several lines for the reading
  26. # convenience
  27. # The first word is the parallel command itself, it's a perl utility
  28. # written by Ole Tange, available on Linux systems, already packaged.
  29. # It is also called GNU Parallel. The GNU Parallel homepage is
  30. # https://www.gnu.org/software/parallel/
  31. # Parallel is very powerful, you could easily distribute the parallel stuff
  32. # on remote machines with it (not used here).
  33. # The parallel command is then followed by its parameters.
  34. # parallel parameters explained:
  35. #
  36. # --max-procs jobs means parallel will parallelize up to x jobs at a time,
  37. # x being the number stored in the file jobs.
  38. # Adjust this value by monitoring your system capacity and changing it with
  39. # echo 7 > jobs
  40. #
  41. # --delay 1.1 means parallel will pause 1.1 seconds after starting each job.
  42. #
  43. # --colsep ';' means the separator between values is the character semi-colon ;
  44. #
  45. # --arg-file file.txt means the actual input file is named file.txt
  46. #
  47. # --line-buffer means outputs will be of whole lines instead of a big mess
  48. # of part of them for the different processes. One line belongs to one process.
  49. #
  50. # --tagstring "job {#} slot {%} using {1} from {3} to {6} : "
  51. # means that each line will begin with the
  52. # words "job {#} slot {%} using {1} from {3} to {6} : "
  53. # where:
  54. # {1} will be replaced by the column of the file servers.txt, aka the CGI imapsync url
  55. # {3} will be replaced by the second column element, aka user1
  56. # {6} will be replaced by the fifth column element, aka user2.
  57. # Hack this part as you wish
  58. # The remaining parameters is the command to be executed by the parallel
  59. # command, ie, the command to be run several times in parallel with
  60. # different parameters each time.
  61. # Some explanations about this remaining parts.
  62. #
  63. # The part 'echo {1} | egrep "^#" > /dev/null ||' is just there to skip
  64. # commented lines in file.txt
  65. # It can be removed if there is no comment lines in file.txt
  66. # The part $DRYRUN is a variable that can be either the echo command
  67. # or nothing. It is a trick to permit you to see the command and its
  68. # parameters without running it
  69. #
  70. # {2} will be replaced by the first column in file.txt
  71. # {3} will be replaced by the second column in file.txt
  72. # {4} will be replaced by the third column in file.txt
  73. # ...
  74. # "$@" will be replaced by the parameters of this script itself,
  75. # the one you are reading now. It's useful if you want to
  76. # add temporarily a parameter for all runs without editing any file.
  77. # For example,
  78. # sync_parallel_curl.sh --justlogin
  79. # will run all imapsync with the --justlogin parameter added.
  80. check_parallel_is_here() {
  81. parallel --version > /dev/null || { echo "parallel command is not installed. Install it first."; return 1; }
  82. }
  83. # First, there is no need to go further if the parallel command is not available
  84. # one the current system.
  85. check_parallel_is_here || exit 1 ;
  86. echo Looping with parallel on account credentials found in file.txt
  87. echo
  88. DRYRUN=echo
  89. # Comment the next line if you want to see the imapsync command instead of running it
  90. # since the previous echo value will be discarded
  91. DRYRUN=
  92. parallel --max-procs jobs --delay 1.1 --colsep ';' --link --arg-file servers.txt --arg-file file.txt --line-buffer --tagstring "job {#} slot {%} using {1} from {3} to {6} : " \
  93. 'echo {1} | egrep "^#|^ *$" > /dev/null ||' \
  94. $DRYRUN "curl -k -s -d 'host1='{2}';user1='{3}';password1='{4}';host2='{5}';user2='{6}';password2='{7} {1}"