PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/iRedMail/tools/create_mail_user_SQL.sh

https://bitbucket.org/dineshkummarc/iredmail
Shell | 156 lines | 70 code | 26 blank | 60 comment | 14 complexity | afd6fb43f1201fc11c67f224ef9267fd MD5 | raw file
  1. #!/usr/bin/env bash
  2. # Author: Zhang Huangbin (zhb _at_ iredmail.org)
  3. # Purpose: Import users to MySQL database from plain text file.
  4. # Project: iRedMail (http://www.iredmail.org/)
  5. # -------------------------------------------------------------------
  6. # Usage:
  7. # * Edit these variables:
  8. # STORAGE_BASE_DIRECTORY
  9. # DEFAULT_PASSWD='888888'
  10. # USE_DEFAULT_PASSWD='NO'
  11. # DEFAULT_QUOTA='100' # 100 -> 100M
  12. #
  13. # * Run this script to generate SQL files used to import to MySQL
  14. # database later.
  15. #
  16. # # sh create_mail_user_MySQL.sh domain.ltd user [user1 user2 user3 ...]
  17. #
  18. # It will generate file 'output.sql' in current directory, open
  19. # it and confirm all records are correct.
  20. #
  21. # * Import output.sql into MySQL database.
  22. #
  23. # # mysql -uroot -p
  24. # mysql> USE vmail;
  25. # mysql> SOURCE /path/to/output.sql;
  26. #
  27. # That's all.
  28. # -------------------------------------------------------------------
  29. # ChangeLog:
  30. # - 2009.05.07 Add hashed maildir style support.
  31. # - Improve file detect.
  32. # - Drop output message of 'which dos2unix'.
  33. # --------- CHANGE THESE VALUES ----------
  34. # Storage base directory used to store users' mail.
  35. STORAGE_BASE_DIRECTORY="/var/vmail/vmail1"
  36. # Password setting.
  37. # Note: password will be crypted in MD5.
  38. DEFAULT_PASSWD='88888888'
  39. USE_DEFAULT_PASSWD='NO' # If set to 'NO', password is the same as username.
  40. # Default mail quota.
  41. DEFAULT_QUOTA='100' # 100 -> 100M
  42. # -------------- You may not need to change variables below -------------------
  43. # Mailbox format: mbox, Maildir.
  44. MAILBOX_FORMAT='Maildir'
  45. # ---- Maildir settings ----
  46. # Maildir style: hashed, normal.
  47. # Hashed maildir style, so that there won't be many large directories
  48. # in your mail storage file system. Better performance in large scale
  49. # deployment.
  50. # Format: e.g. username@domain.td
  51. # hashed -> domain.ltd/u/us/use/username/
  52. # normal -> domain.ltd/username/
  53. # Default hash level is 3.
  54. MAILDIR_STYLE='hashed' # hashed, normal.
  55. # Time stamp, will be appended in maildir.
  56. DATE="$(date +%Y.%m.%d.%H.%M.%S)"
  57. STORAGE_BASE="$(dirname ${STORAGE_BASE_DIRECTORY})"
  58. STORAGE_NODE="$(basename ${STORAGE_BASE_DIRECTORY})"
  59. # Path to SQL template file.
  60. SQL="output.sql"
  61. echo '' > ${SQL}
  62. # Cyrpt the password.
  63. if [ X"${USE_DEFAULT_PASSWD}" == X"YES" ]; then
  64. export CRYPT_PASSWD="$(openssl passwd -1 ${DEFAULT_PASSWD})"
  65. else
  66. :
  67. fi
  68. generate_sql()
  69. {
  70. # Get domain name.
  71. DOMAIN="$1"
  72. shift 1
  73. for i in $@; do
  74. username="$i"
  75. mail="${username}@${DOMAIN}"
  76. if [ X"${USE_DEFAULT_PASSWD}" != X"YES" ]; then
  77. export CRYPT_PASSWD="$(openssl passwd -1 ${username})"
  78. else
  79. :
  80. fi
  81. # Different maildir style: hashed, normal.
  82. if [ X"${MAILDIR_STYLE}" == X"hashed" ]; then
  83. length="$(echo ${username} | wc -L)"
  84. str1="$(echo ${username} | cut -c1)"
  85. str2="$(echo ${username} | cut -c2)"
  86. str3="$(echo ${username} | cut -c3)"
  87. if [ X"${length}" == X"1" ]; then
  88. str2="${str1}"
  89. str3="${str1}"
  90. elif [ X"${length}" == X"2" ]; then
  91. str3="${str2}"
  92. else
  93. :
  94. fi
  95. # Use mbox, will be changed later.
  96. maildir="${DOMAIN}/${str1}/${str2}/${str3}/${username}-${DATE}"
  97. else
  98. # Use mbox, will be changed later.
  99. maildir="${DOMAIN}/${username}-${DATE}"
  100. fi
  101. # Different maildir format: maildir, mbox.
  102. if [ X"${MAILBOX_FORMAT}" == X"Maildir" ]; then
  103. # Append slash to make it 'maildir' format.
  104. maildir="${maildir}/"
  105. else
  106. # It's already mbox format.
  107. :
  108. fi
  109. cat >> ${SQL} <<EOF
  110. INSERT INTO mailbox (username, password, name, storagebasedirectory,storagenode, maildir, quota, domain, active, local_part, created)
  111. VALUES ('${mail}', '${CRYPT_PASSWD}', '${username}', '${STORAGE_BASE}','${STORAGE_NODE}', '${maildir}', '${DEFAULT_QUOTA}', '${DOMAIN}', '1','${username}', NOW());
  112. INSERT INTO alias (address, goto, domain, created, active) VALUES ('${mail}', '${mail}','${DOMAIN}', NOW(), 1);
  113. EOF
  114. done
  115. }
  116. if [ $# -lt 2 ]; then
  117. echo "Usage: $0 domain_name username [user2 user3 user4 ...]"
  118. else
  119. # Generate SQL template.
  120. generate_sql $@ && \
  121. cat <<EOF
  122. SQL template file was generated successfully, Please import it
  123. *MANUALLY* after verify the records:
  124. - ${SQL}
  125. Steps to import these users looks like below:
  126. # mysql -uroot -p
  127. mysql> USE vmail;
  128. mysql> SOURCE ${SQL};
  129. EOF
  130. fi