snapshotscli.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #!/usr/bin/env bash
  2. set -e
  3. run_solr_snapshot_tool() {
  4. JVM="java"
  5. scriptDir=$(dirname "$0")
  6. if [ -n "$LOG4J_PROPS" ]; then
  7. log4j_config="file:${LOG4J_PROPS}"
  8. else
  9. log4j_config="file:${scriptDir}/../../resources/log4j2-console.xml"
  10. fi
  11. PATH=${JAVA_HOME}/bin:${PATH} ${JVM} ${ZKCLI_JVM_FLAGS} -Dlog4j.configurationFile=${log4j_config} \
  12. -classpath "${solrLibPath}" org.apache.solr.core.snapshots.SolrSnapshotsTool "$@" 2> /dev/null
  13. }
  14. usage() {
  15. run_solr_snapshot_tool --help
  16. }
  17. distcp_warning() {
  18. echo "SOLR_USE_DISTCP environment variable is not set. \
  19. Do you want to use hadoop distcp tool for exporting Solr collection snapshot ?"
  20. }
  21. parse_options() {
  22. OPTIND=3
  23. while getopts ":c:d:s:z:p:r:i:" o ; do
  24. case "${o}" in
  25. d)
  26. destPath=${OPTARG}
  27. ;;
  28. s)
  29. sourcePath=${OPTARG}
  30. ;;
  31. c)
  32. collectionName=${OPTARG}
  33. ;;
  34. z)
  35. solrZkEnsemble=${OPTARG}
  36. ;;
  37. p)
  38. pathPrefix=${OPTARG}
  39. ;;
  40. r)
  41. backupRepoName=${OPTARG}
  42. ;;
  43. i)
  44. aysncReqId=${OPTARG}
  45. ;;
  46. *)
  47. echo "Unknown option ${OPTARG}"
  48. usage 1>&2
  49. exit 1
  50. ;;
  51. esac
  52. done
  53. }
  54. prepare_snapshot_export() {
  55. #Make sure to cleanup the temporary files.
  56. scratch=$(mktemp -d -t solrsnaps.XXXXXXXXXX)
  57. function finish {
  58. rm -rf "${scratch}"
  59. }
  60. trap finish EXIT
  61. if hdfs dfs -test -d "${destPath}" ; then
  62. run_solr_snapshot_tool --prepare-snapshot-export "$@" -t "${scratch}"
  63. hdfs dfs -mkdir -p "${copyListingDirPath}" > /dev/null
  64. find "${scratch}" -type f -printf "%f\n" | while read shardId; do
  65. echo "Copying the copy-listing for $shardId"
  66. hdfs dfs -copyFromLocal "${scratch}/${shardId}" "${copyListingDirPath}" > /dev/null
  67. done
  68. else
  69. echo "Directory ${destPath} does not exist."
  70. exit 1
  71. fi
  72. }
  73. copy_snapshot_files() {
  74. copylisting_dir_path="$1"
  75. if hdfs dfs -test -d "${copylisting_dir_path}" ; then
  76. for shardId in $(hdfs dfs -stat "%n" "${copylisting_dir_path}/*"); do
  77. oPath="${destPath}/${snapshotName}/snapshot.${shardId}"
  78. echo "Copying the index files for ${shardId} to ${oPath}"
  79. ${distCpCmd} -f "${copylisting_dir_path}/${shardId}" "${oPath}" > /dev/null
  80. done
  81. else
  82. echo "Directory ${copylisting_dir_path} does not exist."
  83. exit 1
  84. fi
  85. }
  86. collectionName=""
  87. solrZkEnsemble=""
  88. pathPrefix=""
  89. destPath=""
  90. sourcePath=""
  91. cmd="$1"
  92. snapshotName="$2"
  93. copyListingDirPath=""
  94. distCpCmd="${SOLR_DISTCP_CMD:-hadoop distcp}"
  95. scriptDir=$(dirname "$0")
  96. solrLibPath="${SOLR_LIB_PATH:-${scriptDir}/../../solr-webapp/webapp/WEB-INF/lib/*:${scriptDir}/../../lib/ext/*}"
  97. case "${cmd}" in
  98. --create)
  99. run_solr_snapshot_tool "$@"
  100. ;;
  101. --delete)
  102. run_solr_snapshot_tool "$@"
  103. ;;
  104. --list)
  105. run_solr_snapshot_tool "$@"
  106. ;;
  107. --describe)
  108. run_solr_snapshot_tool "$@"
  109. ;;
  110. --prepare-snapshot-export)
  111. : "${SOLR_USE_DISTCP:? $(distcp_warning)}"
  112. parse_options "$@"
  113. : "${destPath:? Please specify destination directory using -d option}"
  114. copyListingDirPath="${destPath}/copylistings"
  115. prepare_snapshot_export "${@:2}"
  116. echo "Done. GoodBye!"
  117. ;;
  118. --export)
  119. if [ -z "${SOLR_USE_DISTCP}" ]; then
  120. run_solr_snapshot_tool "$@"
  121. echo "Done. GoodBye!"
  122. exit 0
  123. fi
  124. parse_options "$@"
  125. : "${snapshotName:? Please specify the name of the snapshot}"
  126. : "${destPath:? Please specify destination directory using -d option}"
  127. if [ -n "${collectionName}" ] && [ -n "${sourcePath}" ]; then
  128. echo "The -c and -s options can not be specified together"
  129. exit 1
  130. fi
  131. if [ -z "${collectionName}" ] && [ -z "${sourcePath}" ]; then
  132. echo "At least one of options (-c or -s) must be specified"
  133. exit 1
  134. fi
  135. if [ -n "${collectionName}" ]; then
  136. copyListingDirPath="${destPath}/${snapshotName}/copylistings"
  137. prepare_snapshot_export "${@:2}"
  138. copy_snapshot_files "${destPath}/${snapshotName}/copylistings"
  139. hdfs dfs -rm -r -f -skipTrash "${destPath}/${snapshotName}/copylistings" > /dev/null
  140. else
  141. copy_snapshot_files "${sourcePath}/copylistings"
  142. echo "Copying the collection meta-data to ${destPath}/${snapshotName}"
  143. ${distCpCmd} "${sourcePath}/${snapshotName}/*" "${destPath}/${snapshotName}/" > /dev/null
  144. fi
  145. echo "Done. GoodBye!"
  146. ;;
  147. --help)
  148. usage 1>&2
  149. ;;
  150. *)
  151. echo "Unknown command ${cmd}"
  152. usage 1>&2
  153. exit 1
  154. esac