sync_parallel_unix.sh 5.0 KB

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